From 76c22938c12306ae0a8dc726292d43b55080c180 Mon Sep 17 00:00:00 2001 From: Win Date: Thu, 5 Sep 2024 10:13:53 +0700 Subject: [PATCH] modified purchase function --- vendorInfo | Bin 17640 -> 17648 bytes vendorInfo.c | 82 +++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/vendorInfo b/vendorInfo index 3155726758641c5bcfc9ce0eac38fa246ccb3843..e14050e5ada8d31bef14ad07c71db81ba6e2e98b 100755 GIT binary patch delta 2131 zcmZ8idrTBZ7@t{=*P$E_VefqIQBIh#(h-^};>{l3S{ z?A`iRJf`B76g<|N`m1#^60tEEN#bx?WIt!P{k{!29mlme4Y$Q9vPFxN9OtHHy&^u& z-ca1Vq|N!+J%9RSYir(5XUT^lJ8>e3weVPGHkw)@TI{%-`AeDI%g}Z}E1AEN*|(Ve zgy2vfZ)fXVW{)ttm|20@7nm(%b`H55IzP;#RO~IUB6mW6NX3Xl_0VXH(KYI7Jr#yf z2x>Ml@9YxTE{9J0ZinrR!Zl<~ToTu7@fF5>#9{qJ!fFa7*duC20|B|IK_|Nt7FPGR z3nJ>K8U3UG7&y>9MF7a^fd4Rt8MG7|1SQ4pg04aLCdr!Qr~WA#r%6L}y&cvp+x^_9 zk(8(fLX`#wq;XnnI8~FzJ>iSTc+t#ClriW_!7<23-!|SAR41SI@`1Um||LSxn zx@dHg12t5sUz7UPZ+pKBZe$VUsXN?{Y|UrrHkukP{sHY<%`KgOipgsAfd4e88}zCo zQ%9RU%9_;yX@N*BU)Q|-UrrbO1&V5nXwYp9JyRfPI0rJh>3yn}n_jl4eX{y}?@+Lz zQT|Z z;k60S1bBf=B*t^y6J#d8tH>2((}9JE;mkVe37q7U9OwZWLj zf17tWjIMl>FRQ=Hbt8LZ^>DZ9?NakQWOd^?P3j^S_<8Z?WPDu{le47`+k5v|a}2jK zTuwG+iQx9BgG;0ib#3y1bPI&A4W^t&l`@=HlXOe2(=D?4Gra)+BibQT6dE;BY!@J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;AWyS9@SQ`d}?G-o84D>D-G3x8)G92_Wo zWcas3i=A)MopbSvJvXwC!P|xgnh8#$NNYIS8G>x|&=zhNa=W$K2${xAh@(Hk{iPiD zaeNP`Nu-Z)eLKevj(Hq6aNNf6DUM#4Fl{ilM%vq8&h&L5A(RND8%K2{=O$-jN74u} zkb#shvOa7j$azDXO>0P}<52C&5ta?w)2?BGb`80!s0`I^k464P*F%T73PsUdB4kWHOVPC*Ydf)5#ba=f=^V z#T0cisw{?%zV^C_`FoT_igu-PzT?tb;um9Qu)!$SB8f&!iPm3O9cNrlj?;y)tBS7c zgQ2LBV7=UIoT&0G)4%&e*LnKX=k+1T6DtJwGBk>n#*>%^BVw5)hK#=P$a83H1ea&? zjIoqTXx|A~)cT${+LNQY_5Sy;NuL%YFQlr1!PptR^!`Z0_YjnRJXrQ%wHt-&L+ zT+-x$#?tlhQ)#YkcgnS!yZ&cFU0H5=Q;HGx$S@-Kw0K!Qr52dF`#u4`eph)59fkwt z#k3Yi%0>Dp>?#+%VM>Tg$RMi?c~^uNS(Kgyr(g7TGY(ltKFN}Km_IgZDzESsMjgaM z2Juecygo`*V0A7r-873#2`9UiF2H5KxcM)%o$PzZOUo8}=WMY*8SD3sD07>?M%3JF z8d|_-x(7K9;w2@)B!?3b0bL#!$rN&*hsO~3K`1AeRIOJ+-& S8ZaB^3dDjH^b|}5H~j;1l+wTe diff --git a/vendorInfo.c b/vendorInfo.c index 96fd0ce..42cb584 100644 --- a/vendorInfo.c +++ b/vendorInfo.c @@ -1,8 +1,11 @@ /* - * Win (Thanawin Pattanaphol) * vendorInfo.c - * 8th August 2024 -*/ + * + * Program that + * + * + * Created by Win (Thanawin Pattanaphol), 28th August 2024 + */ #include #include @@ -20,8 +23,22 @@ struct vendor_item int item_price_baht; } vendors[MAX_VENDORS]; +struct purchase_res +{ + int status; + char vendor_name[20]; + int price_owe_baht; +}; + int read_input(char str[], int n); -int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vendors); + +int search(char query[], + struct vendor_item vendors[MAX_VENDORS], + int total_vendors); + +struct purchase_res purchase(int item_id, + int amount, + struct vendor_item vendors[MAX_VENDORS]); int main() { @@ -52,7 +69,7 @@ int main() strncpy(vendors[index].vendor_name, token, strlen(token) + 1); token = strtok(NULL, " "); - /* TO-DO: VALIDATE MERCHANDISE BY PUTTING EVERYTHING AS LOWERCASE*/ + /* TO-DO: VALIDATE MERCHANDISE BY PUTTING EVERYTHING AS LOWERCASE */ strncpy(vendors[index].merchandise, token, strlen(token) + 1); token = strtok(NULL, " "); @@ -84,19 +101,18 @@ int main() printf("%-50s %s\n", vendor_info, vendor_stock); } - - char user_query[100]; while (1) { + char user_query[100]; + printf("What do you want to buy? (DONE to end) "); + read_input(user_query, 100); + if ((strcmp(user_query, "DONE") == 0)) { break; } - printf("What do you want to buy? (DONE to end) "); - read_input(user_query, 100); - // convert string to lowercase - each character one by one int user_query_length = strlen(user_query); for (int i = 0; i < user_query_length; i++) @@ -104,18 +120,26 @@ int main() user_query[i] = tolower(user_query[i]); }; - int result = search(user_query, vendors, total_vendors); - if(result > 0) + int item_id = search(user_query, vendors, total_vendors); + printf("THING: %d\n", item_id); + if(item_id > 0) { - int item_amount; - char input[10]; read_input(input, 10); if(atoi(input) < 0) { - item_amount = atoi(input); - } + int item_amount = atoi(input); + struct purchase_res response = purchase(item_id, item_amount, vendors); + + if (response.status == 0) + { + printf("You owe %s a total of %d baht.\n", + response.vendor_name, + response.price_owe_baht + ); + } + } else { printf("You must enter a positive whole number for the number of items"); @@ -134,7 +158,7 @@ int main() }; /* - custom read input function + read_input: Custom read input function str = string itself n = max amount of characters that the function is able to read into buffer @@ -168,25 +192,29 @@ int read_input(char str[], int n) - i: Item Number - 0: No results found */ + +/* TO-DO IMPLEMENT RESPONSE STRUCT */ int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vendors) { int i; for (i = 0; i < total_vendors; i++) { + printf("Step: %d\n", i); if (strcmp(query, vendors[i].merchandise) == 0) { - printf("Query: %s | Result: %s %s\n", + printf("\nQuery: %s | Result: %s %s\n", query, vendors[i].vendor_name, vendors[i].merchandise ); return i; - break; + break; }; }; if (i == total_vendors) { + printf("THING: %d %d", i, total_vendors); return 0; } }; @@ -202,16 +230,26 @@ int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vend Return values: - */ -int purchase(int item_id, int amount, struct vendor_item vendors[MAX_VENDORS]) +struct purchase_res purchase(int item_id, int amount, struct vendor_item vendors[MAX_VENDORS]) { if(amount <= vendors[item_id].inventory_count) { + // Decrease vendor inventory vendors[item_id].inventory_count = vendors[item_id].inventory_count - amount; + + struct purchase_res response; + + response.status = 0; + strcpy(response.vendor_name, vendors[item_id].vendor_name); + response.price_owe_baht = vendors[item_id].item_price_baht * amount; - return vendors[item_id].inventory_count; + return (response); } else { - return 0; + struct purchase_res response; + response.status = 1; + + return response; } };