-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shopping_Cart.c
524 lines (444 loc) · 15 KB
/
Shopping_Cart.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct structura
{
int id_produs;
char name_produs[20];
int cant_produs;
float pret_produs;
struct structura *next;
} STR; // p q cap_lista
typedef struct cumparaturi
{
int id_produs;
char nume_produs[10];
int cantitate;
struct cumparaturi *next;
} CUMP; // cump1 cump2 cmp_lista
int name_compare(char n[], char m[])
{
char name[20], name2[20];
strcpy(name, n);
strcpy(name2, m);
// exluding the input mistake by uppering both strings
for (int i = 0; i < strlen(name); i++)
{
if (name[i] >= 'a' && name[i] <= 'z')
name[i] = name[i] - 32;
if (name2[i] >= 'a' && name2[i] <= 'z')
name2[i] = name2[i] - 32;
}
// comparing the strings
for (int i = 0; i < strlen(name2); i++)
{
if (name[i] != name2[i])
{
// printf("Product not found!\nPlease try again.");
return 0;
}
}
return 1;
}
void Verify_file(FILE *file)
{
if (file == NULL)
{
printf("Stock file could not open properly!\nPlease check the file and try again.");
exit(-1);
}
}
void deleteNode_ID(CUMP **head_ref, int key)
{
// Store head node
CUMP *temp = *head_ref, *prev;
// If head node itself holds the key to be deleted
if (temp != NULL && temp->id_produs == key)
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && temp->id_produs != key)
{
prev = temp;
temp = temp->next;
}
// If key was not present in linked list
if (temp == NULL)
return;
// Unlink the node from linked list
prev->next = temp->next;
free(temp); // Free memory
}
void deleteNode_NAME(CUMP **head_ref, char *key)
{
// Store head node
CUMP *temp = *head_ref, *prev;
// If head node itself holds the key to be deleted
if (temp != NULL && name_compare(temp->nume_produs, key))
{
*head_ref = temp->next; // Changed head
free(temp); // free old head
return;
}
// Search for the key to be deleted, keep track of the
// previous node as we need to change 'prev->next'
while (temp != NULL && !name_compare(temp->nume_produs, key))
{
prev = temp;
temp = temp->next;
}
// If key was not present in linked list
if (temp == NULL)
return;
// Unlink the node from linked list
prev->next = temp->next;
free(temp); // Free memory
}
void append(CUMP **head_ref, int new_id, int new_cantitate, char nume[])
{
/* 1. allocate node */
CUMP *new_node = (CUMP *)malloc(sizeof(CUMP));
CUMP *last = *head_ref; /* used in step 5*/
/* 2. put in the data */
new_node->id_produs = new_id;
new_node->cantitate = new_cantitate;
strcpy(new_node->nume_produs, nume);
/* 3. This new node is going to be the last node, so make next
of it as NULL*/
new_node->next = NULL;
/* 4. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 5. Change the next of last node */
last->next = new_node;
return;
}
int name_or_id(char input[]) // if the user input is ID or name
{
if (input[0] >= '0' && input[0] <= '9')
{
return 1;
}
return 0;
// 1 -> ID, 0 -> Product_name
}
void main()
{
FILE *stock_input;
stock_input = fopen("stock.txt", "rt");
Verify_file(stock_input);
// Stock list
STR *cap_lista, *p, *q;
int no_stock_products = 0, nr;
int i;
p = (STR *)malloc(sizeof(STR));
if (p == NULL)
{
printf("Memory allocation failed!");
exit(-1);
}
fscanf(stock_input, "%d", &p->id_produs);
fscanf(stock_input, "%s", &p->name_produs);
fscanf(stock_input, "%d", &p->cant_produs);
fscanf(stock_input, "%f", &p->pret_produs); // (*p).valoare
p->next = NULL;
cap_lista = p;
while (!feof(stock_input))
{
q = (STR *)malloc(sizeof(STR));
if (q == NULL)
{
printf("Memory allocation failed!");
exit(-1);
}
fscanf(stock_input, "%d", &(q->id_produs));
fscanf(stock_input, "%s", (q->name_produs));
fscanf(stock_input, "%d", &(q->cant_produs));
fscanf(stock_input, "%f", &(q->pret_produs));
q->next = NULL;
p->next = q;
p = q;
no_stock_products++;
}
no_stock_products++;
for (p = cap_lista, i = 1; p != NULL; p = p->next, i++)
{
printf("Element with id: %d called: %s is in stock with quantity: %d at %.2f EUR\n", p->id_produs, p->name_produs, p->cant_produs, p->pret_produs);
}
// Cart list
CUMP *cmp_lista, *cump1, *cump2;
int numar_prod_cump = 0, j;
// First element of Cart
cump1 = (CUMP *)malloc(sizeof(CUMP));
if (cump1 == NULL)
{
printf("Memory allocation failed");
exit(1);
}
char mask[10];
int id2; // knows if the user works with ID or name. If 1, user works with ID
printf("Please enter the name or ID of the product you want: ");
buy:
id2 = 1;
fflush(stdin);
scanf("%s", &mask);
if (name_or_id(mask))
{
cump1->id_produs = mask[0] - '0';
}
else
{
strcpy(cump1->nume_produs, mask);
id2 = 0;
}
fflush(stdin);
printf("Enter the quantity: ");
scanf("%d", &cump1->cantitate);
numar_prod_cump = 1;
int ok = 0;
if (id2 == 1)
{
for (p = cap_lista; p != NULL; p = p->next)
{
if (cump1->id_produs == p->id_produs)
{
while (cump1->cantitate > p->cant_produs)
{
printf("Quantity not available in stock. Please re-enter a smaller than %d quantity: ", p->cant_produs + 1);
scanf("%d", &cump1->cantitate);
//break;
}
strcpy(cump1->nume_produs, p->name_produs);
break;
}
}
}
else
{
for (p = cap_lista; p != NULL; p = p->next)
{
if (name_compare(cump1->nume_produs, p->name_produs))
{
while (cump1->cantitate > p->cant_produs)
{
printf("Quantity not available in stock. Please re-enter a smaller than %d quantity: ", p->cant_produs + 1);
scanf("%d", &cump1->cantitate);
cump1->id_produs = p->id_produs;
ok = 1;
//break;
}
ok = 1; // if product found, set ok to 1
cump1->id_produs = p->id_produs;
break;
}
}
// checks if product was found or not
if (ok == 0)
{
printf("Product not found in list.\nPlease re-enter the name or ID: ");
goto buy;
}
}
cump1->next = NULL;
cmp_lista = cump1;
printf("\nProduct with ID: %d added in cart in %d quantity\n", cump1->id_produs, cump1->cantitate);
char r; // User answer
int id, cantitate;
// Modify Cart
printf("If you want to add or modify products in your cart, press a.\nIf you want to delete products from cart, press s.\nOtherwise, press n: ");
input:
id2 = 1;
int del = 0; // check for delete
fflush(stdin);
scanf("%c", &r);
fflush(stdin);
switch (r)
{
// adding
case 'a':
printf("Please enter the name or ID of the product you want: ");
scanf("%s", &mask);
cump2 = (CUMP *)malloc(sizeof(CUMP));
id2 = 1;
if (name_or_id(mask))
{
cump2->id_produs = mask[0] - '0';
}
else
{
strcpy(cump2->nume_produs, mask);
id2 = 0;
}
if (id2 == 1)
{
// checking if the product is already in the basket
for (cump1 = cmp_lista, i = 1; cump1 != NULL || i < numar_prod_cump; cump1 = cump1->next, i++)
{
if (cump1->id_produs == cump2->id_produs)
{
printf("Product already in cart. Enter the new quantity: ");
scanf("%d", &cump1->cantitate);
for (p = cap_lista, i = 1; p != NULL; p = p->next, i++)
{
if (cump1->id_produs == p->id_produs)
{
while (cump1->cantitate > p->cant_produs)
{
printf("Quantity not available in stock. Please re-enter a smaller than %d quantity: ", p->cant_produs + 1);
scanf("%d", &cump1->cantitate);
strcpy(cump1->nume_produs, p->name_produs);
}
strcpy(cump1->nume_produs, p->name_produs);
}
}
break;
}
}
if (cump1 == NULL)
{
for (p = cap_lista, i = 1; p != NULL; p = p->next, i++)
{
if (p->id_produs == cump2->id_produs)
{
printf("Enter the quantity: ");
scanf("%d", &cantitate);
while (cantitate > p->cant_produs)
{
printf("Quantity not available in stock. Please re-enter a smaller than %d quantity: ", p->cant_produs + 1);
scanf("%d", &cantitate);
}
append(&cmp_lista, cump2->id_produs, cantitate, p->name_produs);
numar_prod_cump++;
printf("Do you want to make other changes to the cart? a->add/modify, s->delete, n->no: ");
goto input;
}
}
printf("Wrong input. Cart has not been modified!\n");
printf("If you want to add or modify products in your cart, press a.\nIf you want to delete products from cart, press s.\nOtherwise, press n: ");
goto input;
}
printf("Do you want to make other changes to the cart? a->add/modify, s->delete, n->no: ");
goto input;
}
else
{
for (cump1 = cmp_lista, i = 1; cump1 != NULL || i < numar_prod_cump; cump1 = cump1->next, i++)
{
if (name_compare(cump1->nume_produs, cump2->nume_produs))
{
printf("Product already in cart. Enter the new quantity: ");
scanf("%d", &cump1->cantitate);
for (p = cap_lista, i = 1; p != NULL; p = p->next, i++)
{
if (cump1->id_produs == p->id_produs)
while (cump1->cantitate > p->cant_produs)
{
printf("Quantity not available in stock. Please re-enter a smaller than %d quantity: ", p->cant_produs + 1);
scanf("%d", &cump1->cantitate);
cump1->id_produs = p->id_produs;
}
}
break;
}
}
if (cump1 == NULL)
{
for (p = cap_lista, i = 1; p != NULL; p = p->next, i++)
{
if (name_compare(p->name_produs, cump2->nume_produs))
{
printf("Enter the quantity: ");
scanf("%d", &cantitate);
while (cantitate > p->cant_produs)
{
printf("Quantity not available in stock. Please re-enter a smaller than %d quantity: ", p->cant_produs + 1);
scanf("%d", &cantitate);
}
append(&cmp_lista, p->id_produs, cantitate, p->name_produs);
printf("Do you want to make other changes to the cart? a->add/modify, s->delete, n->no: ");
goto input;
}
}
printf("Wrong input. Cart has not been modified!\n");
printf("If you want to add or modify products in your cart, press a.\nIf you want to delete products from cart, press s.\nOtherwise, press n: ");
goto input;
}
printf("Do you want to make other changes to the cart? a->add/modify, s->delete, n->no: ");
goto input;
}
// Remove from cart
case 's':
printf("Enter the ID or name of the product you want to remove from cart: ");
scanf("%s", &mask);
id2 = 1;
if (name_or_id(mask))
{
id = atoi(mask);
}
else
{
id2 = 0;
}
if (id2 == 1)
{
for (cump1 = cmp_lista; cump1 != NULL; cump1 = cump1->next)
{
if (cump1->id_produs == atoi(mask))
{
deleteNode_ID(&cmp_lista, atoi(mask));
printf("Product with ID %s has been removed from cart!\n", mask);
printf("Do you want to make other changes to the cart? a->add/modify, s->delete, n->no: ");
del = 1;
goto input;
}
}
}
else
{
for (cump1 = cmp_lista; cump1 != NULL; cump1 = cump1->next)
{
if (name_compare(cump1->nume_produs, mask))
{
deleteNode_NAME(&cmp_lista, mask);
printf("Product with ID %s has been removed from cart!\n", mask);
printf("Do you want to make other changes to the cart? a->add/modify, s->delete, n->no: ");
del = 1;
goto input;
}
}
}
if (del == 0)
{
printf("Product not in cart!");
goto input;
}
case 'n':
break;
// Wrong input
default:
printf("Wrong input.\nPlease try again a/s/n: ");
goto input;
}
printf("Current cart: \n");
// Total sum
float suma = 0;
for (cump1 = cmp_lista, i = 1; cump1 != NULL; cump1 = cump1->next, i++)
{
printf("Product with ID: %d name: %s quantity: %d\n", cump1->id_produs, cump1->nume_produs, cump1->cantitate);
for (p = cap_lista, j = 1; p != NULL; p = p->next, j++)
{
if (cump1->id_produs == p->id_produs)
{
suma = suma + (cump1->cantitate) * (p->pret_produs);
}
}
}
printf("Checkout Total: %.2f EUR. Have a nice day!\n\n", suma);
char wait_for_input;
printf("Press enter to exit");
scanf("%c", &wait_for_input);
}