Skip to content

Commit fb650a7

Browse files
committed
progress
1 parent 57c0e9d commit fb650a7

File tree

20 files changed

+29797
-12894
lines changed

20 files changed

+29797
-12894
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
node_modules
1+
node_modules
22
__MACOSX
Binary file not shown.

Computed_Shopping_Assistant/main.c

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <stdbool.h>
5+
#include "shopping_cart.h"
6+
7+
extern struct shopping_cart shopping_cart;
8+
extern char user_input[];
9+
extern bool loaded_coupons;
10+
11+
void add_item_menu() {
12+
printf("Which item would you like to add?\n");
13+
printf("%c - Bread\n", TYPE_BREAD);
14+
printf("%c - Pasta\n", TYPE_PASTA);
15+
printf("%c - Soup\n", TYPE_SOUP);
16+
printf("%c - Drink\n", TYPE_DRINK);
17+
printf("%c - Vegetable\n", TYPE_VEGETABLE);
18+
printf("%c - Fruit\n", TYPE_FRUIT);
19+
20+
char choice_type;
21+
scanf(" %c", &choice_type);
22+
23+
if (!is_valid_food_type(choice_type)) {
24+
printf("Invalid type entered!\n");
25+
return;
26+
}
27+
28+
item* item = add_item(choice_type);
29+
30+
switch (choice_type) {
31+
case TYPE_BREAD:
32+
strcpy(item->grocery_item.description, "White bread");
33+
item->grocery_item.amount_loaves = 1;
34+
break;
35+
case TYPE_PASTA:
36+
strcpy(item->grocery_item.description, "Spaghetti");
37+
item->grocery_item.amount_kilograms = 1;
38+
break;
39+
case TYPE_SOUP:
40+
strcpy(item->grocery_item.description, "Lentil soup");
41+
item->grocery_item.amount_liters = 1;
42+
break;
43+
case TYPE_DRINK:
44+
strcpy(item->grocery_item.description, "Coca-Cola");
45+
item->grocery_item.amount_liters = 1;
46+
break;
47+
case TYPE_VEGETABLE:
48+
strcpy(item->grocery_item.description, "Potatoes");
49+
item->grocery_item.amount_kilograms = 1;
50+
break;
51+
case TYPE_FRUIT:
52+
strcpy(item->grocery_item.description, "Apples");
53+
item->grocery_item.amount_items = 1;
54+
break;
55+
}
56+
printf("1 %s of %s added!\n", food_type_to_unit(choice_type), item->grocery_item.description);
57+
}
58+
59+
void edit_item_menu() {
60+
if (shopping_cart.amount_of_items == 0) {
61+
printf("Your shopping cart is empty!\n");
62+
return;
63+
}
64+
printf("Which item index would you like to edit?\n");
65+
66+
int choice;
67+
scanf("%d", &choice);
68+
if (choice < 0 || choice >= SHOPPING_CART_SIZE) {
69+
printf("Invalid item index!\n");
70+
return;
71+
}
72+
item* item = &shopping_cart.items[choice];
73+
74+
if (!can_edit_item(item)) {
75+
printf("Can not edit this item!\n");
76+
return;
77+
}
78+
79+
printf("Which property would you like to edit?\n"
80+
"1 - Type\n"
81+
"2 - Amount of kilograms\n"
82+
"3 - Amount of items\n"
83+
"4 - Amount of loaves\n"
84+
"5 - Amount of liters\n"
85+
"6 - Description\n"
86+
"7 - Cancel\n");
87+
scanf("%d", &choice);
88+
89+
if (choice < 1 || choice > 7) {
90+
printf("Invalid choice!\n");
91+
return;
92+
}
93+
94+
char newline;
95+
scanf("%c", &newline); // clear newline from buffer
96+
char choice_type;
97+
98+
switch (choice) {
99+
case 1:
100+
printf("Enter new type: ");
101+
fflush(stdout);
102+
scanf(" %c", &choice_type);
103+
if (choice_type == TYPE_COUPON) {
104+
printf("You can not convert to coupon!\n");
105+
} else if (is_valid_food_type(choice_type)) {
106+
item->type = choice_type;
107+
} else {
108+
printf("Invalid type entered!\n");
109+
}
110+
break;
111+
case 2:
112+
printf("Enter new kilograms amount: ");
113+
fflush(stdout);
114+
scanf("%d", &choice);
115+
item->grocery_item.amount_kilograms = choice;
116+
break;
117+
case 3:
118+
printf("Enter new items amount: ");
119+
fflush(stdout);
120+
scanf("%d", &choice);
121+
item->grocery_item.amount_items = choice;
122+
break;
123+
case 4:
124+
printf("Enter new loaves amount: ");
125+
fflush(stdout);
126+
scanf("%d", &choice);
127+
item->grocery_item.amount_loaves = choice;
128+
break;
129+
case 5:
130+
printf("Enter new liters amount: ");
131+
fflush(stdout);
132+
scanf("%d", &choice);
133+
item->grocery_item.amount_liters = choice;
134+
break;
135+
case 6:
136+
printf("Enter new description: ");
137+
fflush(stdout);
138+
fgets(item->grocery_item.description, STRING_BUFFER_SIZE, stdin);
139+
item->grocery_item.description[strlen(item->grocery_item.description)-1] = '\0'; // remove newline
140+
break;
141+
case 7:
142+
return;
143+
break;
144+
}
145+
printf("Item updated!\n");
146+
}
147+
148+
void remove_item_menu() {
149+
if (shopping_cart.amount_of_items == 0) {
150+
printf("Your shopping cart is empty!\n");
151+
return;
152+
}
153+
printf("Which item index would you like to remove?\n");
154+
155+
int choice;
156+
scanf("%d", &choice);
157+
if (choice < 0 || choice >= SHOPPING_CART_SIZE) {
158+
printf("Invalid item index!\n");
159+
return;
160+
}
161+
remove_item(choice);
162+
}
163+
164+
void print_shopping_cart() {
165+
if (shopping_cart.amount_of_items == 0) {
166+
printf("\nYour shopping cart is empty!\n");
167+
return;
168+
}
169+
printf("\nYour shopping cart has %d items:\n", shopping_cart.amount_of_items);
170+
for (int i = 0; i < SHOPPING_CART_SIZE; i++) {
171+
item* item = &shopping_cart.items[i];
172+
173+
int amount = 0;
174+
switch (item->type) {
175+
case TYPE_UNDEFINED:
176+
break;
177+
case TYPE_COUPON:
178+
if (item->coupon.have_entered) {
179+
printf("(index %d) - %d%% OFF coupon - %s\n", i, item->coupon.discount_amount, item->coupon.code);
180+
}
181+
break;
182+
default:
183+
switch (item->type) {
184+
case TYPE_BREAD:
185+
amount = item->grocery_item.amount_loaves;
186+
break;
187+
case TYPE_PASTA:
188+
case TYPE_VEGETABLE:
189+
amount = item->grocery_item.amount_kilograms;
190+
break;
191+
case TYPE_SOUP:
192+
case TYPE_DRINK:
193+
amount = item->grocery_item.amount_liters;
194+
break;
195+
case TYPE_FRUIT:
196+
amount = item->grocery_item.amount_items;
197+
}
198+
printf("(index %d) - %d %s of %s\n", i, amount, food_type_to_unit(item->type), item->grocery_item.description);
199+
break;
200+
}
201+
}
202+
}
203+
204+
void apply_a_coupon() {
205+
if (!loaded_coupons) {
206+
load_coupon("coupon_10.txt", 10);
207+
load_coupon("coupon_50.txt", 50);
208+
loaded_coupons = true;
209+
}
210+
printf("Please enter your coupon:\n");
211+
char newline;
212+
scanf("%c", &newline); // clear newline from buffer
213+
fgets(user_input, STRING_BUFFER_SIZE, stdin);
214+
215+
for (int i = 0; i < SHOPPING_CART_SIZE; i++) {
216+
item* item = &shopping_cart.items[i];
217+
if (item->type == TYPE_COUPON && !item->coupon.have_entered) {
218+
if (!memcmp(item->coupon.code, user_input, item->coupon.length)) {
219+
printf("Applied coupon for %d%% OFF!\n", item->coupon.discount_amount);
220+
item->coupon.have_entered = true;
221+
return;
222+
}
223+
}
224+
}
225+
printf("Invalid coupon!\n");
226+
}
227+
228+
void checkout() {
229+
printf("\nThank you for choosing Computed Shopping Assistant!\n");
230+
printf("Your items will be delivered to you within 24 hours.\n");
231+
printf("Goodbye!\n");
232+
exit(0);
233+
}
234+
235+
void main_menu() {
236+
printf("\n---> Welcome to Computed Shopping Assistant <---\n");
237+
238+
while (true) {
239+
printf("\nWhat would you like to do?\n"
240+
"1 - Add item to shopping cart\n"
241+
"2 - Edit item in shopping cart\n"
242+
"3 - Remove item from shopping cart\n"
243+
"4 - View shopping cart\n"
244+
"5 - Apply a coupon\n"
245+
"6 - Checkout\n");
246+
247+
int choice = 0;
248+
scanf("%d", &choice);
249+
250+
switch (choice) {
251+
case 1:
252+
add_item_menu();
253+
break;
254+
case 2:
255+
edit_item_menu();
256+
break;
257+
case 3:
258+
remove_item_menu();
259+
break;
260+
case 4:
261+
print_shopping_cart();
262+
break;
263+
case 5:
264+
apply_a_coupon();
265+
break;
266+
case 6:
267+
checkout();
268+
break;
269+
default:
270+
printf("Invalid choice!\n");
271+
exit(0);
272+
break;
273+
}
274+
}
275+
}
276+
277+
int main() {
278+
memset(&shopping_cart, 0, sizeof(shopping_cart));
279+
main_menu();
280+
}

0 commit comments

Comments
 (0)