modified purchase function

This commit is contained in:
Win 2024-09-05 10:13:53 +07:00
parent b6b164f434
commit 76c22938c1
2 changed files with 60 additions and 22 deletions

Binary file not shown.

View File

@ -1,8 +1,11 @@
/* /*
* Win (Thanawin Pattanaphol)
* vendorInfo.c * vendorInfo.c
* 8th August 2024 *
*/ * Program that
*
*
* Created by Win (Thanawin Pattanaphol), 28th August 2024
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -20,8 +23,22 @@ struct vendor_item
int item_price_baht; int item_price_baht;
} vendors[MAX_VENDORS]; } vendors[MAX_VENDORS];
struct purchase_res
{
int status;
char vendor_name[20];
int price_owe_baht;
};
int read_input(char str[], int n); 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() int main()
{ {
@ -52,7 +69,7 @@ int main()
strncpy(vendors[index].vendor_name, token, strlen(token) + 1); strncpy(vendors[index].vendor_name, token, strlen(token) + 1);
token = strtok(NULL, " "); 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); strncpy(vendors[index].merchandise, token, strlen(token) + 1);
token = strtok(NULL, " "); token = strtok(NULL, " ");
@ -84,19 +101,18 @@ int main()
printf("%-50s %s\n", vendor_info, vendor_stock); printf("%-50s %s\n", vendor_info, vendor_stock);
} }
char user_query[100];
while (1) 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)) if ((strcmp(user_query, "DONE") == 0))
{ {
break; 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 // convert string to lowercase - each character one by one
int user_query_length = strlen(user_query); int user_query_length = strlen(user_query);
for (int i = 0; i < user_query_length; i++) for (int i = 0; i < user_query_length; i++)
@ -104,18 +120,26 @@ int main()
user_query[i] = tolower(user_query[i]); user_query[i] = tolower(user_query[i]);
}; };
int result = search(user_query, vendors, total_vendors); int item_id = search(user_query, vendors, total_vendors);
if(result > 0) printf("THING: %d\n", item_id);
if(item_id > 0)
{ {
int item_amount;
char input[10]; char input[10];
read_input(input, 10); read_input(input, 10);
if(atoi(input) < 0) 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 else
{ {
printf("You must enter a positive whole number for the number of items"); 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 str = string itself
n = max amount of characters that the function is able to read into buffer 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 - i: Item Number
- 0: No results found - 0: No results found
*/ */
/* TO-DO IMPLEMENT RESPONSE STRUCT */
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)
{ {
int i; int i;
for (i = 0; i < total_vendors; i++) for (i = 0; i < total_vendors; i++)
{ {
printf("Step: %d\n", i);
if (strcmp(query, vendors[i].merchandise) == 0) if (strcmp(query, vendors[i].merchandise) == 0)
{ {
printf("Query: %s | Result: %s %s\n", printf("\nQuery: %s | Result: %s %s\n",
query, query,
vendors[i].vendor_name, vendors[i].vendor_name,
vendors[i].merchandise vendors[i].merchandise
); );
return i; return i;
break; break;
}; };
}; };
if (i == total_vendors) if (i == total_vendors)
{ {
printf("THING: %d %d", i, total_vendors);
return 0; return 0;
} }
}; };
@ -202,16 +230,26 @@ int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vend
Return values: 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) if(amount <= vendors[item_id].inventory_count)
{ {
// Decrease vendor inventory
vendors[item_id].inventory_count = vendors[item_id].inventory_count - amount; 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 else
{ {
return 0; struct purchase_res response;
response.status = 1;
return response;
} }
}; };