added purchase function

This commit is contained in:
Win 2024-09-03 09:20:56 +07:00
parent e6e218f3c4
commit 2656180e25
2 changed files with 37 additions and 14 deletions

Binary file not shown.

View File

@ -84,18 +84,27 @@ int main()
printf("%-50s %s\n", vendor_info, vendor_stock); printf("%-50s %s\n", vendor_info, vendor_stock);
} }
char user_query[100]; char user_query[100];
read_input(user_query, 100); read_input(user_query, 100);
// convert string to lowercase - each character one by one while (1)
int user_query_length = strlen(user_query);
for (int i = 0; i < user_query_length; i++)
{ {
user_query[i] = tolower(user_query[i]); if ((strcmp(user_query, "DONE") == 0))
}; {
break;
}
search(user_query, vendors, total_vendors); 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++)
{
user_query[i] = tolower(user_query[i]);
};
search(user_query, vendors, total_vendors);
}
fclose(fp); fclose(fp);
free(file_buffer); free(file_buffer);
@ -132,6 +141,10 @@ int read_input(char str[], int n)
- query: search query - query: search query
- vendors: the vendor table - vendors: the vendor table
- total_vendors: the amount of vendors - total_vendors: the amount of vendors
Return values:
- i: Item Number
- 0: No results found
*/ */
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)
{ {
@ -145,27 +158,37 @@ int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vend
vendors[i].vendor_name, vendors[i].vendor_name,
vendors[i].merchandise vendors[i].merchandise
); );
return i;
break; break;
}; };
}; };
if (i == total_vendors) if (i == total_vendors)
{ {
printf("No results found - %d\n", i); return 0;
} }
return 0;
}; };
/* /*
Purchase function Purchase function
Parameters: Parameters:
- item: The item that the user is purchasing - item_id: The item id that the user is purchasing
- amount: How many items that the user is purchasing - amount: How many items that the user is purchasing
- vendors: vendor table - vendors: vendor table
Return values:
-
*/ */
int purchase(char item[], int amount, struct vendor_item vendors[MAX_VENDORS]) int purchase(int item_id, int amount, struct vendor_item vendors[MAX_VENDORS])
{ {
if(amount <= vendors[item_id].inventory_count)
{
vendors[item_id].inventory_count = vendors[item_id].inventory_count - amount;
return vendors[item_id].inventory_count;
}
else
{
return 0;
}
}; };