-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateAndDelete.h
353 lines (235 loc) · 9.33 KB
/
UpdateAndDelete.h
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "create.h"
#define MAX_LINE_SIZE 256
#define MAX_NAME_SIZE 100
struct product {
char Name[MAX_NAME_SIZE];
char Brand[MAX_NAME_SIZE];
char ProducType[MAX_NAME_SIZE];
int AmountInStorage;
float Price;
char ExpireDate[MAX_LINE_SIZE];
};
//The delete function by Pasin Shinwasusin 66070503439
int Delete(const char *filename, int i, int Status) {
int Validity = 0, DeleteOrNot;
while (!Validity) {
printf("Press 0 to exit delete mode and press 1 to delete: ");
if (scanf("%d", &DeleteOrNot) == 1 && DeleteOrNot == 0 || DeleteOrNot == 1) {
Validity = 1;
} else {
while (getchar() != '\n');
printf("You can only input either 0 or 1. Try again.\n");
}
}
if (DeleteOrNot == 0) {
return 1;
} else {
// Delete a row from a CSV file
FILE *inputFile = fopen(filename, "r");
FILE *outputFile = fopen("temp.csv", "w");
if (inputFile == NULL || outputFile == NULL) {
perror("Error opening files");
exit(EXIT_FAILURE);
}
char line[MAX_LINE_SIZE];
int currentRow = 0;
while (fgets(line, sizeof(line), inputFile) != NULL) {
if (currentRow != i + 1) {
fputs(line, outputFile);
}
currentRow++;
}
fclose(inputFile);
fclose(outputFile);
// Rename the temporary file to the original file
remove(filename);
rename("temp.csv", filename);
printf("Deleted.\n");
}
return 0;
}
//A part of the update function by Karun Tancharoen 66070503407
void UpdateFile(int Num, struct product Product[]) {
FILE *File = fopen("priceyCosmetics.csv", "w");
int i;
if (File == NULL) {
perror("Error opening file.");
exit(EXIT_FAILURE);
}
fprintf(File, "Name,Brand,ProductType,AmountInStorage,Price,ExpireDate\n");
for (i = 0; i < Num; i++) {
fprintf(File, "%s,%s,%s,%d,%.2f,%s", Product[i].Name, Product[i].Brand, Product[i].ProducType, Product[i].AmountInStorage, Product[i].Price, Product[i].ExpireDate);
}
fclose(File);
}
//A part of the update function by Karun Tancharoen 66070503407
void UpdateStruct(struct product Product[], int Status, int i) {
int Validity = 0, WhatToEdit, EditOrExit;
if (Status == 1) {
while (!Validity) {
printf("Input the number corresponding to data you want to edit: ");
if (scanf("%d", &WhatToEdit) != 1) {
while (getchar() != '\n');
printf("Invalid input. Input again.\n");
continue;
}
if (WhatToEdit == 1) {
while (getchar() != '\n');
Validity = 1;
printf("New name: ");
scanf("%[^\n]", Product[i].Name);
} else if (WhatToEdit == 2) {
while (getchar() != '\n');
Validity = 1;
printf("New brand: ");
scanf("%[^\n]", Product[i].Brand);
} else if (WhatToEdit == 3) {
while (getchar() != '\n');
Validity = 1;
printf("New type: ");
scanf("%[^\n]", Product[i].ProducType);
} else if (WhatToEdit == 4) {
while (!Validity) {
printf("New quantity: ");
if (scanf("%d", &Product[i].AmountInStorage) == 1) {
Validity = 1;
} else {
while (getchar() != '\n');
printf("You can only input an integer. Input again.\n");
}
}
} else if (WhatToEdit == 5) {
while (!Validity) {
printf("New price: ");
if (scanf("%f", &Product[i].Price) == 1) {
Validity = 1;
} else {
while (getchar() != '\n');
printf("You can only input a float number. Input again.\n");
}
}
} else if (WhatToEdit == 6) {
while (getchar() != '\n');
char Temp[MAX_NAME_SIZE];
int Length;
printf("New expire: ");
scanf("%[^\n]", Product[i].ExpireDate);
strcpy(Temp, Product[i].ExpireDate);
while (isExpireDateFormatValid(Temp) == -1 || isExpireDateExists(Temp) == -1) {
while (getchar() != '\n');
printf("New expire: ");
scanf("%[^\n]", Product[i].ExpireDate);
strcpy(Temp, Product[i].ExpireDate);
}
Length = strlen(Product[i].ExpireDate);
Product[i].ExpireDate[Length] = '\n';
Validity = 1;
} else {
while (getchar() != '\n');
printf("Invalid input. Input again.\n");
}
}
Validity = 0;
while (!Validity) {
printf("Data was processed successfully. Press 0 to exit the editing mode and 1 to continue editing: ");
if (scanf("%d", &EditOrExit) == 1 && EditOrExit == 0 || EditOrExit == 1) {
Validity = 1;
} else {
while (getchar() != '\n');
printf("You can only input either 0 or 1. Try again.\n");
}
}
if (EditOrExit == 1) {
UpdateStruct(Product, Status, i);
}
} else {
while (!Validity) {
printf("New quantity: ");
if (scanf("%d", &Product[i].AmountInStorage) == 1) {
Validity = 1;
} else {
while (getchar() != '\n');
printf("You can only input an integer. Input again.\n");
}
}
printf("Data was processed successfully.\n");
}
}
//The update function by Karun Tancharoen 66070503407
int Update(int Status, int Num, struct product Product[], const char *filename) {
char SearchedProduct[MAX_NAME_SIZE];
int NextOrEdit, FoundAResult, i;
while (getchar() != '\n');
printf("Which product do you wish to edit its data: ");
scanf("%[^\n]", SearchedProduct);
for (i = 0; i < Num; i++) {
int Validity = 0;
if (strstr(Product[i].Name, SearchedProduct) != NULL) {
FoundAResult = 1;
printf("Found a result.\n");
printf("1: Name: %s\n", Product[i].Name);
printf("2: Brand: %s\n", Product[i].Brand);
printf("3: Type: %s\n", Product[i].ProducType);
printf("4: Quantity: %d\n", Product[i].AmountInStorage);
printf("5: Price: %.2f\n", Product[i].Price);
printf("6: Expire: %s", Product[i].ExpireDate);
while (!Validity) {
int BackFromDelete = 0;
if (i == 99) {
printf("\nPress 0 to view the next result, 1 to edit this result, and 2 to delete this result: ");
} else {
printf("Press 0 to view the next result, 1 to edit this result, and 2 to delete this result: ");
}
if (scanf("%d", &NextOrEdit) == 1 && NextOrEdit == 0 || NextOrEdit == 1 || NextOrEdit == 2) {
if (NextOrEdit == 2) {
if (Status == 1) {
BackFromDelete = Delete(filename, i, Status);
if (BackFromDelete == 0) {
return 0;
}
} else {
printf("Only the admin can delete.\n");
}
} else {
Validity = 1;
}
} else {
while (getchar() != '\n');
printf("You can only input 0, 1, or 2. Try again.\n");
}
}
if (NextOrEdit == 0) {
while (getchar() != '\n');
continue;
} else {
UpdateStruct(Product, Status, i);
UpdateFile(Num, Product);
Validity = 0;
NextOrEdit = 2;
while (!Validity) {
printf("Data updated successfully. Press 0 to exit and 1 to view the next result: ");
if (scanf("%d", &NextOrEdit) == 1 && NextOrEdit == 0 || NextOrEdit == 1) {
Validity = 1;
} else {
while (getchar() != '\n');
printf("You can only input either 0 or 1. Try again.\n");
}
}
if (NextOrEdit == 0) {
return 0;
} else {
while (getchar() != '\n');
continue;
}
}
}
}
if (FoundAResult == 0) {
printf("No more results found.\n");
Update(Status, Num, Product, filename);
}
return 0;
}