added purchase function
This commit is contained in:
parent
e6e218f3c4
commit
2656180e25
BIN
vendorInfo
BIN
vendorInfo
Binary file not shown.
37
vendorInfo.c
37
vendorInfo.c
|
@ -88,6 +88,14 @@ int main()
|
|||
char user_query[100];
|
||||
read_input(user_query, 100);
|
||||
|
||||
while (1)
|
||||
{
|
||||
if ((strcmp(user_query, "DONE") == 0))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
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++)
|
||||
|
@ -96,6 +104,7 @@ int main()
|
|||
};
|
||||
|
||||
search(user_query, vendors, total_vendors);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
free(file_buffer);
|
||||
|
@ -132,6 +141,10 @@ int read_input(char str[], int n)
|
|||
- query: search query
|
||||
- vendors: the vendor table
|
||||
- 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)
|
||||
{
|
||||
|
@ -145,27 +158,37 @@ int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vend
|
|||
vendors[i].vendor_name,
|
||||
vendors[i].merchandise
|
||||
);
|
||||
return i;
|
||||
break;
|
||||
};
|
||||
};
|
||||
|
||||
if (i == total_vendors)
|
||||
{
|
||||
printf("No results found - %d\n", i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Purchase function
|
||||
|
||||
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
|
||||
- vendors: vendor table
|
||||
*/
|
||||
int purchase(char item[], int amount, struct vendor_item vendors[MAX_VENDORS])
|
||||
{
|
||||
|
||||
Return values:
|
||||
-
|
||||
*/
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue