-
Notifications
You must be signed in to change notification settings - Fork 0
/
FP.c
581 lines (530 loc) · 17.4 KB
/
FP.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <conio.h>
#define max(a, b) a > b ? a : b
#define min(a, b) a < b ? a : b
#define black "\033[0;30m"
#define red "\033[0;31m"
#define green "\033[0;32m"
#define yellow "\033[0;33m"
#define blue "\033[0;34m"
#define purple "\033[0;35m"
#define cyan "\033[0;36m"
#define white "\033[0;37m"
#define clear system("cls")
#define printError printf(red"%s\n"white, errorMsg);
typedef long long ll;
typedef struct{
char nama[1001];
int stok;
} item;
typedef struct{
char nama[1001];
item itm[1001];
} gudang;
gudang Gudang[1001];
bool updated = true;
int maxlenGudang = 0;
int maxlenItem = 0;
int maxlenStok = 0;
int tableLen = 0;
int totalData = 0;
int totalDataGroup = 0;
char errorMsg[1001] = "";
void updateTotalData(){
totalData = 0;
totalDataGroup = 0;
for(int i=0; i<=1000; i++){
if(Gudang[i].nama[0]=='\0') continue;
totalDataGroup++;
for(int j=0; j<=1000; j++){
if(Gudang[i].itm[j].nama[0]=='\0') continue;
totalData++;
}
}
}
void updatelenGudang(){
maxlenGudang = 0;
for(int i=0; i<=1000; i++){
if(Gudang[i].nama[0]=='\0') continue;
maxlenGudang = max(maxlenGudang, strlen(Gudang[i].nama));
}
}
int checkDigit(int n){
int res = 1;
while(n >= 10){
n /= 10;
res++;
}
return res;
}
void updatelenItemStok(){
maxlenItem = 0;
maxlenStok = 0;
for(int i=0; i<=1000; i++){
if(Gudang[i].nama[0]=='\0') continue;
for(int j=0; j<=1000; j++){
if(Gudang[i].itm[j].nama[0]=='\0') continue;
maxlenItem = max(maxlenItem, strlen(Gudang[i].itm[j].nama));
maxlenStok = max(maxlenStok, checkDigit(Gudang[i].itm[j].stok));
}
}
}
void printRoof(int tableLen){
printf(cyan"/");
for(int i=0; i<tableLen; i++) printf("-");
printf("\\\n"white);
}
void printFloor(int tableLen){
printf(cyan"\\");
for(int i=0; i<tableLen; i++) printf("-");
printf("/\n"white);
}
void printOut(int *pointer, int *selectedGroup, int *selectedData){
clear;
updatelenGudang();
updatelenItemStok();
printRoof(4+2+1+(maxlenGudang+2)+1+4+2+1+(maxlenItem+2)+1+(maxlenStok+2));
// Print the data with table format
const char colors[4][10] = {red, purple, green, white};
int coloridx = 0;
int curridx = 0;
for(int i=0; i<=1000; i++){
if(Gudang[i].nama[0]=='\0') continue;
int temp = coloridx;
for(int j=0; j<=1000; j++){
if(Gudang[i].itm[j].nama[0]=='\0') continue;
if(curridx==*pointer) coloridx = 3;
printf(cyan"|%s %04d " cyan"|", colors[coloridx], i);
printf("%s %s"white, colors[coloridx] , Gudang[i].nama);
for(int k=strlen(Gudang[i].nama); k<=maxlenGudang; k++) printf(" ");
printf(cyan"|%s %04d " cyan"|", colors[coloridx], j);
printf("%s %s"white, colors[coloridx], Gudang[i].itm[j].nama);
for(int k=strlen(Gudang[i].itm[j].nama); k<=maxlenItem; k++) printf(" ");
printf(cyan"|%s %d"cyan, colors[coloridx], Gudang[i].itm[j].stok);
for(int k=checkDigit(Gudang[i].itm[j].stok); k<=maxlenStok; k++) printf(" ");
printf("|"white);
if(curridx==*pointer){
printf(" <--");
*selectedGroup = i;
*selectedData = j;
}
printf("\n");
curridx++;
coloridx = temp;
}
coloridx++;
if(coloridx==3) coloridx = 0;
}
printFloor(4+2+1+(maxlenGudang+2)+1+4+2+1+(maxlenItem+2)+1+(maxlenStok+2));
}
void printDataGroup(int *pointer, int *selectedGroup){
clear;
int curridx = 0;
const char colors[4][10] = {red, purple, green, white};
int coloridx = 0;
int temp = 0;
printRoof(4+2+1+(maxlenGudang+2));
for(int i=0; i<=1000; i++){
if(Gudang[i].nama[0]=='\0') continue;
temp = coloridx;
if(curridx==*pointer) coloridx = 3;
printf(blue"|%s %04d "blue"|", colors[coloridx], i);
printf("%s %s", colors[coloridx], Gudang[i].nama);
for(int k=strlen(Gudang[i].nama); k<=maxlenGudang; k++) printf(" ");
printf(blue"|"white);
if(curridx==*pointer){
printf(" <--");
*selectedGroup = i;
}
printf("\n");
curridx++;
coloridx = temp;
coloridx++;
if(coloridx==3) coloridx = 0;
}
printFloor(4+2+1+(maxlenGudang+2));
}
void printSpecificGroup(int *selectedGroup, int *selectedData){
clear;
int curridx = 0;
const char colors[4][10] = {red, purple, green, white};
int coloridx = 0;
int temp = 0;
printRoof(4+2+1+(maxlenItem+2)+1+(maxlenStok+2));
for(int i=0; i<=1000; i++){
if(Gudang[*selectedGroup].itm[i].nama[0]=='\0') continue;
temp = coloridx;
if(curridx==*selectedData) coloridx = 3;
printf(cyan"|%s %04d " cyan"|", colors[coloridx], i);
printf("%s %s"white, colors[coloridx], Gudang[*selectedGroup].itm[i].nama);
for(int k=strlen(Gudang[*selectedGroup].itm[i].nama); k<=maxlenItem; k++) printf(" ");
printf(cyan"|%s %d"cyan, colors[coloridx], Gudang[*selectedGroup].itm[i].stok);
for(int k=checkDigit(Gudang[*selectedGroup].itm[i].stok); k<=maxlenStok; k++) printf(" ");
printf("|"white);
if(curridx==*selectedData) printf(" <--");
printf("\n");
curridx++;
coloridx = temp;
coloridx++;
if(coloridx==3) coloridx = 0;
}
printFloor(4+2+1+(maxlenItem+2)+1+(maxlenStok+2));
}
bool inputNum(int *num){
if(scanf("%d", num)!=1){
strcpy(errorMsg, "Invalid input!");
fflush(stdin);
return false;
}
fflush(stdin);
if(*num>1000){
strcpy(errorMsg, "Too big of an input! Please keep it within 0 to 1000 inclusive.");
return false;
}
if(*num<0){
strcpy(errorMsg, "Too small of an input! Please keep it within 0 to 1000 inclusive.");
return false;
}
return true;
}
void addData(int *group){
int trash = -1;
bool flag = false;
if(*group==-1){
flag = true;
printDataGroup(&trash, group);
printf("Prompt data group ID.\n");
while(!inputNum(group) || Gudang[*group].nama[0]=='\0'){
if(Gudang[*group].nama[0]=='\0') strcpy(errorMsg, "That ID doesn\'t exist!");
printError;
printf("Prompt data group ID.\n");
}
}
int idx;
if(flag) printSpecificGroup(group, &trash);
printf("Prompt data ID.\n");
while(!inputNum(&idx) || Gudang[*group].itm[idx].nama[0]!='\0'){
if(Gudang[*group].itm[idx].nama[0]!='\0') strcpy(errorMsg, "That ID already exists!");
printError;
printf("Prompt data ID.\n");
}
printf("Prompt data name.\n");
scanf("%s", Gudang[*group].itm[idx].nama);
fflush(stdin);
printf("Prompt data stock.\n");
while(scanf("%d", &Gudang[*group].itm[idx].stok)!=1){
strcpy(errorMsg, "Invalid input!");
fflush(stdin);
printError;
printf("Prompt data stock.\n");
}
fflush(stdin);
Gudang[*group].itm[idx].stok = max(Gudang[*group].itm[idx].stok, 0);
printOut(&trash, &trash, &trash);
updatelenItemStok();
updateTotalData();
updated = false;
strcpy(errorMsg, "The new data has successfully been added.");
}
void addDataGroup(){
int trash = -1;
printDataGroup(&trash, &trash);
printf("Prompt data group ID.\n");
int group;
while(!inputNum(&group) || Gudang[group].nama[0]!='\0'){
if(Gudang[group].nama[0]!='\0') strcpy(errorMsg, "That ID already exists!");
printError;
printf("Prompt data group ID.\n");
}
updateTotalData();
printf("Prompt data group name.\n");
scanf("%s", Gudang[group].nama);
fflush(stdin);
updatelenGudang();
printf("Please add data to newly added data group.\n");
addData(&group);
updated = false;
strcpy(errorMsg, "The new data group has successfully been added.");
}
void deleteData(int *group, int *data){
strcpy(Gudang[*group].itm[*data].nama, "\0");
Gudang[*group].itm[*data].stok = 0;
updatelenItemStok();
updateTotalData();
updated = false;
strcpy(errorMsg, "The selected data has successfully been deleted.");
}
void deleteDataGroup(int *group){
strcpy(Gudang[*group].nama, "\0");
for(int data=0; data<=1000; data++) deleteData(group, &data);
updatelenGudang();
updateTotalData();
strcpy(errorMsg, "The selected data group has successfully been deleted.");
}
void modifyData(int *group, int *data){
printSpecificGroup(group, data);
printf("Prompt new ID. (-1 to leave unchanged.)\n");
int ID;
while((!inputNum(&ID) || Gudang[*group].itm[ID].nama[0]!='\0') && ID != -1){
if(Gudang[*group].itm[ID].nama[0]!='\0') strcpy(errorMsg, "That ID already exists!");
printError;
printf("Prompt new ID.\n");
}
if(ID==-1 || ID==*data) ID = *data;
else{
strcpy(Gudang[*group].itm[ID].nama, Gudang[*group].itm[*data].nama);
Gudang[*group].itm[ID].stok = Gudang[*group].itm[*data].stok;
deleteData(group, data);
}
printf("Prompt new name. (-1 to leave unchanged.)\n");
char name[1001]; scanf("%s", name);
fflush(stdin);
if(strcmp(name, "-1")!=0) strcpy(Gudang[*group].itm[ID].nama, name);
printf("Prompt updated stock. (-1 to leave unchanged.)\n");
int stock;
while(scanf("%d", &stock)!=1){
strcpy(errorMsg, "Invalid input!");
fflush(stdin);
printError;
printf("Prompt data stock.\n");
}
if(stock!=-1) Gudang[*group].itm[ID].stok = stock;
Gudang[*group].itm[ID].stok = max(Gudang[*group].itm[ID].stok, 0);
strcpy(errorMsg, "The selected data has successfully been modified.");
updated = false;
}
void modifyDataGroup(int *pointer, int *group){
printDataGroup(pointer, group);
printf("Prompt new ID. (-1 to leave unchanged.)\n");
int ID;
while((!inputNum(&ID) || Gudang[ID].nama[0]!='\0') && ID != -1 && ID != *group){
if(Gudang[ID].nama[0]!='\0') strcpy(errorMsg, "That ID already exists!");
printError;
printf("Prompt new ID.\n");
fflush(stdin);
}
if(ID==-1 || ID == *group) ID = *group;
else{
strcpy(Gudang[ID].nama, Gudang[*group].nama);
for(int i=0; i<=1000; i++){
if(Gudang[*group].itm[i].nama[0]=='\0') continue;
strcpy(Gudang[ID].itm[i].nama, Gudang[*group].itm[i].nama);
Gudang[ID].itm[i].stok = Gudang[*group].itm[i].stok;
}
deleteDataGroup(group);
}
printf("Prompt new name. (-1 to leave unchanged.)\n");
char name[1001]; scanf("%s", name);
fflush(stdin);
if(strcmp(name, "-1")!=0) strcpy(Gudang[*group].itm[ID].nama, name);
strcpy(errorMsg, "The selected data group has successfully been modified.");
updated = false;
}
void wrongFormat(){
strcpy(errorMsg, "gudang.txt is not on correct format!");
}
void writeBackup(){
FILE *fptr1, *fptr2;
fptr1 = fopen("gudang.txt", "r");
fptr2 = fopen("backup.txt", "w");
char c;
while (fscanf(fptr1, "%c", &c) != EOF){
fprintf(fptr2, "%c", c);
}
fclose(fptr1);
fclose(fptr2);
}
void getBackup(){
FILE *fptr1, *fptr2;
fptr1 = fopen("gudang.txt", "w");
fptr2 = fopen("backup.txt", "r");
char c;
while (fscanf(fptr2, "%c", &c) != EOF){
fprintf(fptr1, "%c", c);
}
fclose(fptr1);
fclose(fptr2);
}
void readFile(){
int i=0, j=0;
for(i=0; i<=1000; i++){
strcpy(Gudang[i].nama, "");
for(j=0; j<=1000; j++){
strcpy(Gudang[i].itm[j].nama, "");
Gudang[i].itm[j].stok = 0;
}
}
FILE *fptr;
fptr = fopen("gudang.txt", "r");
int idx = 0;
while(1){
int output = fscanf(fptr, "%d", &idx);
if(output!=1 || output==EOF){
wrongFormat();
return;
}
if(idx>1000){
wrongFormat();
return;
}
if(idx==-1) break;
if(Gudang[idx].nama[0]!='\0'){
wrongFormat();
return;
}
fscanf(fptr, "%s", &Gudang[idx].nama);
}
while(1){
int output = fscanf(fptr, "%d:", &idx);
if(output!=1 || output==EOF){
wrongFormat();
return;
}
if(idx>1000){
wrongFormat();
return;
}
if(idx==-1) break;
if(Gudang[idx].nama[0]=='\0'){
wrongFormat();
return;
}
while(1){
int i;
int output = fscanf(fptr, "%d", &i);
if(output!=1 || output==EOF){
wrongFormat();
return;
}
if(i>1000){
wrongFormat();
return;
}
if(i==-1) break;
if(Gudang[idx].itm[i].nama[0]!='\0'){
wrongFormat();
return;
}
fscanf(fptr, "%s %d", Gudang[idx].itm[i].nama, &Gudang[idx].itm[i].stok);
}
}
fclose(fptr);
updated = true;
updatelenGudang();
updatelenItemStok();
updateTotalData();
strcpy(errorMsg, "File has successfully been read.");
}
void writeFile(){
FILE *fptr;
fptr = fopen("gudang.txt", "w");
for(int i=0; i<=1000; i++){
if(Gudang[i].nama[0]=='\0') continue;
fprintf(fptr, "%d %s\n", i, Gudang[i].nama);
}
fprintf(fptr, "-1\n");
for(int i=0; i<=1000; i++){
if(Gudang[i].nama[0]=='\0') continue;
fprintf(fptr, "%d:\n", i);
for(int j=0; j<=1000; j++){
if(Gudang[i].itm[j].nama[0]=='\0') continue;
fprintf(fptr, "%d %s %d\n", j, Gudang[i].itm[j].nama, Gudang[i].itm[j].stok);
}
fprintf(fptr, "-1\n");
}
fprintf(fptr, "-1\n");
// fprintf(fptr, "test\n");
fclose(fptr);
updated = true;
strcpy(errorMsg, "Data has successfully been written.");
writeBackup();
}
int main()
{
int pointer = 0;
int selectedGroup = 0;
int selectedID = 0;
bool groupToggle = false;
readFile();
if(strcmp(errorMsg, "gudang.txt is not on correct format!")==0){
clear;
printError;
printf("Do you wish to use a backup? ("green"Y"white"/"red"N"white")\n");
int query = getch();
if(query=='Y' || query=='y') getBackup();
else return 0;
readFile();
strcpy(errorMsg, "Data successfully restored.");
}
updateTotalData();
while(1){
if(!groupToggle) printOut(&pointer, &selectedGroup, &selectedID);
else printDataGroup(&pointer, &selectedGroup);
printf(blue"The data are%sup to date with the file.\n\n"white, (updated ? " " : red" NOT "blue));
printf("1. Modify selected data%s\n", groupToggle ? " group." : ".");
printf("2. Delete selected data%s\n", groupToggle ? " group." : ".");
printf("3. Add new data%s\n", groupToggle ? " group." : ".");
printf("T. Toggle to %s\n", groupToggle ? "singular." : "group.");
printf("R. Reset changes.\n");
printf("C. Commit changes to file.\n");
printf("B. Get backup. "red"(ONLY USE WHEN NECESSARY!)\n"white);
printf("Esc. Exit the program.\n\n");
printError;
strcpy(errorMsg, "");
int query = getch();
if(query==0 || query==224){
int dir = getch();
if(dir==72) pointer--;
else if(dir==80) pointer++;
else strcpy(errorMsg, "Invalid input!");
} else{
if(query=='1'){
if(groupToggle) modifyDataGroup(&pointer, &selectedGroup);
else modifyData(&selectedGroup, &selectedID);
}
else if(query=='2'){
if(groupToggle) deleteDataGroup(&selectedGroup);
else deleteData(&selectedGroup, &selectedID);
}
else if(query=='3'){
int trash = -1;
if(groupToggle) addDataGroup();
else addData(&trash);
}
else if(query=='T' || query=='t'){
groupToggle = !groupToggle;
pointer = 0;
}
else if(query=='C' || query=='c') writeFile();
else if(query=='R' || query=='r'){
readFile();
if(strcmp(errorMsg, "gudang.txt is not on correct format!")==0){\
getBackup();
readFile();
strcpy(errorMsg, "gudang.txt is tampered manually and not on correct format! backup.txt has been used to fix the issue.\n");
}
}
else if(query=='B' || query=='b'){
printf(red"Are you sure? "white"("green"Y"white"/"red"N"white")\n");
int confirm = getch();
if(confirm=='Y' || confirm=='y'){
getBackup();
readFile();
strcpy(errorMsg, "Restoration executed.");
} else strcpy(errorMsg, "Restoration aborted.");
}
else if(query==27) break;
else strcpy(errorMsg, "Invalid input!");
}
pointer = max(0, pointer);
pointer = min(pointer, (groupToggle ? totalDataGroup-1 : totalData-1));
updatelenGudang();
updatelenItemStok();
updateTotalData();
}
printf(red"Program has successfully been exited.\n");
return 0;
}