From a119eac1688e5b3bd5e87d8c3999f0703b51e2b9 Mon Sep 17 00:00:00 2001 From: winsdominoes Date: Tue, 17 Sep 2024 11:44:01 +0700 Subject: [PATCH] Added duplication checks, printf formats and footer --- vendorInfo | Bin 17728 -> 17848 bytes vendorInfo.c | 226 +++++++++++++++++++++++++++++++++++------------ vendorsmall.data | 4 + 3 files changed, 173 insertions(+), 57 deletions(-) create mode 100644 vendorsmall.data diff --git a/vendorInfo b/vendorInfo index 95d346833ae3abdfe245a16ffc09daccaba3f0f7..a1d12393ae7188e9ac63ebfbf1142eab2099e32b 100755 GIT binary patch delta 4628 zcmb7I4OCOt9l!UH0AWA~pb+ffC4%Bt0*I((LL$7HsdL!Pk9K}Ek|<0e2*K5^Hd8D+OB7)xP{>qD+;x`xBtEGy%dD5?w*tT z@&Et+pZ9<7doRJ4aGMV|r|N2S7DSuWBh=P1={K5L8b(92S}vJmTH>WeOFT)$rkGNd zv@BJT`Pig6%1O&XvJDRy4jQG^L8FCDlyQYbMk~n6+|A^R-9`97$_ta9uiUujlM_GP z6@T-+J>+1-3bJ*Miu_HTjI)SNm4cVBK8aXWW@1sL;w^0bIO}W3A(a_7vbmM@Kcc<* zO)$ih<6IJ6$Eb@~?_+&F>!*=iwHdqUoFu4HNuN4iRa5V-BM;Qa$?K7Nawal0s|C~~ z$UeW-vM&;$2y{TE)u&ZLdN}lJf}^J3c>lDh42{{D=dhM5BZH5K4GyJw2N600e0Use>=*A`O1rw+~X^G8yE%clyif7dg zWK!ZhPK+Y8iRUNapbcNJo$TV2VdwTPYSbrOuz5eTdC%Ise-3?2%9C=Yjes@;6KraR z-f2?}m)JsrNv7lx5^9x|aLMLT7|~WDBroy!ID~w{U_%glh5nuen6CP5<>1Q&+X!vL z?1R+T;~Ii0oA-U2w%rh(%>IAC=bs%u!e|hm}C@HPOtqnIu6X}t? ziyPIu;rRd8xdx#hLoy46PVJ7@{v(Q``QnjlfyLxQO+jr_H;eu&bE}Sq9QkraQzq+r z)Lq)y++|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fo.c @@ -21,7 +21,9 @@ struct vendor char vendor_name[20]; char merchandise[15]; int inventory_count; - int item_price_baht; + int item_price_baht; + int revenue; + int duplicate; } vendors[MAX_VENDORS]; struct search_r @@ -38,6 +40,7 @@ struct purchase_r }; /* FUNCTION DECLARATION */ +void reset_output(); int read_input(char str[], size_t n); char *strnlower(char *s, size_t n); @@ -55,6 +58,8 @@ struct purchase_r purchase struct vendor vendors[MAX_VENDORS] ); +int check_vendors(struct vendor vendors[MAX_VENDORS], int total_vendors); + int main() { char file_name[MAX_FILENAME_LENGTH]; @@ -79,12 +84,10 @@ int main() while ((fgets(file_buffer, 50, fp) != NULL) && (index <= MAX_VENDORS - 1)) { - /* TO-DO: VALIDATE FILE DATA */ char *token = strtok(file_buffer, " "); strncpy(vendors[index].vendor_name, token, strlen(token) + 1); token = strtok(NULL, " "); - /* TO-DO: VALIDATE MERCHANDISE BY PUTTING EVERYTHING AS LOWERCASE */ strncpy(vendors[index].merchandise, token, strlen(token) + 1); token = strtok(NULL, " "); @@ -104,74 +107,139 @@ int main() { for (int j = i + 1; j < total_vendors; j++) { + if ((strcmp(vendors[i].merchandise, vendors[j].merchandise)) == 0) { - break; + vendors[i].duplicate = 1; + vendors[j].duplicate = 1; + } + + if ((strcmp(vendors[i].vendor_name, vendors[j].vendor_name)) == 0) + { + vendors[i].duplicate = 1; + vendors[j].duplicate = 1; } } - char vendor_info[200]; - sprintf(vendor_info, "%s sells %s for %d each", - vendors[i].vendor_name, - vendors[i].merchandise, - vendors[i].item_price_baht - ); - - char vendor_stock[50]; - sprintf(vendor_stock, "Inventory %d items", - vendors[i].inventory_count - ); - - printf("%-50s %s\n", vendor_info, vendor_stock); + if (vendors[i].duplicate == 0) + { + char vendor_info[200]; + sprintf(vendor_info, "%s sells %s for %d each", + vendors[i].vendor_name, + vendors[i].merchandise, + vendors[i].item_price_baht + ); + + char vendor_stock[50]; + sprintf(vendor_stock, "Inventory %d items", + vendors[i].inventory_count + ); + + printf("%-50s %s\n", vendor_info, vendor_stock); + } + else + { + printf("\033[0;31mDuplicate record detected!\n"); + reset_output(); + } } printf("\n"); while (1) { + // CHECK IF ALL MERCHANDISE ARE EMPTY + if(check_vendors(vendors, total_vendors) == 0) + { + break; + }; + 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)) + if (strcmp(user_query, "DONE") == 0) { break; } - // convert string to lowercase - each character one by one - int user_query_length = strlen(user_query); - strnlower(user_query, user_query_length); - - struct search_r item = search(user_query, vendors, total_vendors); - if(item.status == 0) + if (strcmp(user_query, "") == 0) { - char input[10]; - - printf("How many do you want to buy? "); - read_input(input, 10); - - if(atoi(input) > 0) - { - int item_amount = atoi(input); - struct purchase_r response = purchase(item.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"); - } - + printf("Please enter an item name\n"); } else { - printf("No vendor in this market sells %s\n", user_query); + // convert string to lowercase - each character one by one + int user_query_length = strlen(user_query); + strnlower(user_query, user_query_length); + + struct search_r item = search(user_query, vendors, total_vendors); + if(item.status == 0) + { + char input[10]; + + printf("How many do you want to buy? "); + read_input(input, 10); + + if(atoi(input) > 0) + { + int item_amount = atoi(input); + struct purchase_r response = purchase(item.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("\033[0;31mSorry -- %s does not have enough items available\n", + response.vendor_name + ); + reset_output(); + } + } + else + { + printf("\033[0;31mYou must enter a positive whole number for the number of items\n"); + reset_output(); + } + + } + else if (item.status == 2) + { + printf("\033[0;31mThis item has duplicate records, please choose another item\n"); + reset_output(); + } + else + { + printf("\033[0;31mNo vendor in this market sells %s\n", user_query); + reset_output(); + } + } + } + + printf("\n>>> End of day details\n"); + + // Print out remaining inventory + for (int i = 0; i <= (total_vendors - 1); i++) + { + if (vendors[i].duplicate == 0) + { + char vendor_info[200]; + sprintf(vendor_info, "%s made %d baht today", + vendors[i].vendor_name, + vendors[i].revenue + ); + + char vendor_stock[50]; + sprintf(vendor_stock, "Remaining inventory %d items", + vendors[i].inventory_count + ); + + printf("%-50s %s\n", vendor_info, vendor_stock); } } @@ -180,6 +248,14 @@ int main() return 0; }; +/* + Output color reset +*/ +void reset_output() +{ + printf("\033[0m"); +}; + /* read_input: Custom read input function str = string itself @@ -237,21 +313,30 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot { int i; for (i = 0; i < total_vendors; i++) - { + { + // First, check if duplicate if (strcmp(query, vendors[i].merchandise) == 0) { - struct search_r response; - response.status = 0; - response.item_id = i; - - return (response); - break; - }; + if (vendors[i].duplicate == 0) + { + struct search_r response; + response.status = 0; + response.item_id = i; + + return (response); + break; + } + else + { + struct search_r response; + response.status = 2; + return (response); + } + } }; if (i == total_vendors) { - printf("\n\n%d\n%d\n", i, total_vendors); struct search_r response; response.status = 1; response.item_id = 0; @@ -282,6 +367,8 @@ struct purchase_r purchase(int item_id, int amount, struct vendor vendors[MAX_VE response.status = 0; strcpy(response.vendor_name, vendors[item_id].vendor_name); response.price_owe_baht = vendors[item_id].item_price_baht * amount; + + vendors[item_id].revenue = vendors[item_id].item_price_baht * amount; return (response); } @@ -289,7 +376,32 @@ struct purchase_r purchase(int item_id, int amount, struct vendor vendors[MAX_VE { struct purchase_r response; response.status = 1; + strcpy(response.vendor_name, vendors[item_id].vendor_name); return response; } }; + +/* + Check function + + Parameters: +*/ +int check_vendors(struct vendor vendors[MAX_VENDORS], int total_vendors) +{ + int i; + for (i = 0; i < total_vendors; i++) + { + // If there are any items left + if (vendors[i].inventory_count != 0 && vendors[i].duplicate == 0) + { + return 1; + } + } + + // There aren't any + if (i == total_vendors) + { + return 0; + } +}; diff --git a/vendorsmall.data b/vendorsmall.data new file mode 100644 index 0000000..c89a4b9 --- /dev/null +++ b/vendorsmall.data @@ -0,0 +1,4 @@ +Win phone 1 50 +Win books 1 70 +Win books 5 100 +Deez computers 5 80