-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckDatabase.c
106 lines (82 loc) · 2.51 KB
/
checkDatabase.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ITEMS 20
struct Item {
char code[6];
char name[12];
int stock;
int lowLimit;
double costPrice;
double sellingPrice;
};
void displayKioskDatabase() {
struct Item item;
FILE* file = fopen("MyKdata.ori", "rb");
if (file == NULL) {
printf("Error opening kiosk database file!\n");
return;
}
printf("Stock Low:\n");
printf("----------------------------------------\n");
printf("Code Name Stock Low Limit Cost Price(RM) Selling Price(RM)\n");
printf("----------------------------------------\n");
while (fread(&item, sizeof(struct Item), 1, file) == 1) {
if (strcmp(item.code, "") != 0 && item.stock <= item.lowLimit) {
printf("%-6s %-12s %5d %5d %10.2lf %10.2lf\n", item.code, item.name, item.stock, item.lowLimit, item.costPrice, item.sellingPrice);
}
}
printf("----------------------------------------\n");
fclose(file);
}
void updateKioskStock() {
system("cls");
displayKioskDatabase();
char code[6];
int quantity;
printf("Update Kiosk Stock:\n");
printf("Enter item code: ");
scanf("%s", code);
FILE* file = fopen("MyKdata.ori", "rb+");
if (file == NULL) {
printf("Error opening kiosk stock database!\n");
return;
}
struct Item currentItem;
int found = 0;
while (fread(¤tItem, sizeof(struct Item), 1, file)) {
if (strcmp(currentItem.code, code) == 0) {
found = 1;
printf("Enter quantity: ");
scanf("%d", &quantity);
currentItem.stock += quantity;
fseek(file, -(long int)sizeof(struct Item), SEEK_CUR);
fwrite(¤tItem, sizeof(struct Item), 1, file);
break;
}
}
fclose(file);
if (!found) {
printf("Item not found!\n");
getchar();
return;
}
printf("Kiosk stock updated successfully!\n");
getchar();
}
int main() {
system("cls");
displayKioskDatabase();
char choice;
do {
printf("Do you want to update the kiosk stock? (Y/N): ");
scanf(" %c", &choice);
if (choice == 'Y' || choice == 'y') {
updateKioskStock();
// printf("Do you want to update more items? (Y/N): ");
// scanf(" %c", &choice);
}
} while (choice == 'Y' || choice == 'y');
getchar();
return 0;
}