vendor-info/vendorInfo.c

256 lines
5.1 KiB
C
Raw Normal View History

2024-08-28 19:00:25 +07:00
/*
* vendorInfo.c
2024-09-05 10:13:53 +07:00
*
* Program that
*
*
* Created by Win (Thanawin Pattanaphol), 28th August 2024
*/
2024-08-28 19:00:25 +07:00
#include <stdio.h>
#include <stdlib.h>
2024-08-29 11:31:21 +07:00
#include <string.h>
#include <ctype.h>
2024-08-28 19:00:25 +07:00
#define MAX_VENDORS 20
#define MAX_FILENAME_LENGTH 4096
2024-08-29 10:57:19 +07:00
struct vendor_item
{
2024-08-28 19:00:25 +07:00
char vendor_name[20];
char merchandise[15];
int inventory_count;
int item_price_baht;
} vendors[MAX_VENDORS];
2024-09-05 10:13:53 +07:00
struct purchase_res
{
int status;
char vendor_name[20];
int price_owe_baht;
};
int read_input(char str[], int n);
2024-09-05 10:13:53 +07:00
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]);
2024-08-29 10:57:19 +07:00
int main()
{
2024-08-28 19:00:25 +07:00
char file_name[MAX_FILENAME_LENGTH];
2024-08-29 15:30:23 +07:00
printf("Enter name of input file: ");
2024-08-28 19:00:25 +07:00
read_input(file_name, MAX_FILENAME_LENGTH);
FILE *fp;
2024-08-29 10:57:19 +07:00
if ((fp = fopen(file_name, "r")) == NULL)
{
2024-08-28 19:00:25 +07:00
printf("%s cannot be opened.\n", file_name);
exit(EXIT_FAILURE);
}
2024-08-29 10:57:19 +07:00
// calculate file size
fseek(fp, 0, SEEK_END);
unsigned long file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
2024-08-29 11:31:21 +07:00
// read file to buffer
2024-08-29 10:57:19 +07:00
char *file_buffer = (char *) malloc(file_size);
2024-08-29 11:31:21 +07:00
int index = 0;
2024-08-29 10:57:19 +07:00
2024-08-29 15:30:23 +07:00
while ((fgets(file_buffer, 50, fp) != NULL) && (index <= MAX_VENDORS))
2024-08-29 11:31:21 +07:00
{
2024-09-01 18:37:50 +07:00
/* TO-DO: VALIDATE FILE DATA */
2024-08-29 11:31:21 +07:00
char *token = strtok(file_buffer, " ");
2024-08-29 15:30:23 +07:00
strncpy(vendors[index].vendor_name, token, strlen(token) + 1);
2024-08-29 11:31:21 +07:00
token = strtok(NULL, " ");
2024-09-05 10:13:53 +07:00
/* TO-DO: VALIDATE MERCHANDISE BY PUTTING EVERYTHING AS LOWERCASE */
2024-08-29 15:30:23 +07:00
strncpy(vendors[index].merchandise, token, strlen(token) + 1);
2024-08-29 11:31:21 +07:00
token = strtok(NULL, " ");
vendors[index].inventory_count = atoi(token);
token = strtok(NULL, " ");
vendors[index].item_price_baht = atoi(token);
2024-08-29 15:30:23 +07:00
index++;
2024-08-29 11:31:21 +07:00
}
2024-08-29 15:30:23 +07:00
int total_vendors = index - 1;
printf("\n>>> Read %d vendors from file %s\n", total_vendors, file_name);
2024-08-29 20:04:02 +07:00
// print out each vendor's info
2024-08-29 15:30:23 +07:00
for (int i = 0; i <= total_vendors; i++)
2024-08-29 11:31:46 +07:00
{
2024-08-29 15:30:23 +07:00
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
);
2024-08-28 19:00:25 +07:00
2024-08-29 15:30:23 +07:00
printf("%-50s %s\n", vendor_info, vendor_stock);
}
2024-09-03 09:20:56 +07:00
while (1)
2024-09-01 18:25:50 +07:00
{
2024-09-05 10:13:53 +07:00
char user_query[100];
printf("What do you want to buy? (DONE to end) ");
read_input(user_query, 100);
2024-09-03 09:20:56 +07:00
if ((strcmp(user_query, "DONE") == 0))
{
break;
}
// 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]);
};
2024-09-01 18:25:50 +07:00
2024-09-05 10:13:53 +07:00
int item_id = search(user_query, vendors, total_vendors);
printf("THING: %d\n", item_id);
if(item_id > 0)
2024-09-03 13:00:50 +07:00
{
char input[10];
read_input(input, 10);
if(atoi(input) < 0)
{
2024-09-05 10:13:53 +07:00
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
);
}
}
2024-09-03 13:00:50 +07:00
else
{
printf("You must enter a positive whole number for the number of items");
}
}
else
{
printf("No vendor in this market sells %s\n", user_query);
}
2024-09-03 09:20:56 +07:00
}
2024-08-29 15:30:23 +07:00
2024-08-28 19:00:25 +07:00
fclose(fp);
2024-08-29 10:57:19 +07:00
free(file_buffer);
2024-08-28 19:00:25 +07:00
return 0;
};
2024-09-01 18:37:50 +07:00
/*
2024-09-05 10:13:53 +07:00
read_input: Custom read input function
2024-09-01 18:37:50 +07:00
str = string itself
n = max amount of characters that the function is able to read into buffer
reads one character at a time into buffer and adds a null terminator when finish reading
*/
2024-08-29 10:57:19 +07:00
int read_input(char str[], int n)
{
2024-08-28 19:00:25 +07:00
int ch, i = 0;
2024-08-29 10:57:19 +07:00
while ((ch = getchar()) != '\n')
{
if (i < n)
{
2024-08-28 19:00:25 +07:00
str[i++] = ch;
}
}
str[i] = '\0';
return i;
2024-08-29 15:30:23 +07:00
};
2024-08-29 20:04:02 +07:00
2024-09-01 18:37:50 +07:00
/*
Search function
Parameters:
- query: search query
- vendors: the vendor table
- total_vendors: the amount of vendors
2024-09-03 09:20:56 +07:00
Return values:
- i: Item Number
- 0: No results found
2024-09-01 18:37:50 +07:00
*/
2024-09-05 10:13:53 +07:00
/* TO-DO IMPLEMENT RESPONSE STRUCT */
2024-09-01 18:25:50 +07:00
int search(char query[], struct vendor_item vendors[MAX_VENDORS], int total_vendors)
{
2024-09-01 18:25:50 +07:00
int i;
for (i = 0; i < total_vendors; i++)
{
2024-09-05 10:13:53 +07:00
printf("Step: %d\n", i);
2024-09-01 18:25:50 +07:00
if (strcmp(query, vendors[i].merchandise) == 0)
{
2024-09-05 10:13:53 +07:00
printf("\nQuery: %s | Result: %s %s\n",
2024-09-01 18:25:50 +07:00
query,
vendors[i].vendor_name,
vendors[i].merchandise
);
2024-09-03 09:20:56 +07:00
return i;
2024-09-05 10:13:53 +07:00
break;
2024-09-01 18:25:50 +07:00
};
};
if (i == total_vendors)
{
2024-09-05 10:13:53 +07:00
printf("THING: %d %d", i, total_vendors);
2024-09-03 09:20:56 +07:00
return 0;
2024-09-01 18:25:50 +07:00
}
};
2024-09-01 18:37:50 +07:00
/*
Purchase function
Parameters:
2024-09-03 09:20:56 +07:00
- item_id: The item id that the user is purchasing
2024-09-01 18:37:50 +07:00
- amount: How many items that the user is purchasing
- vendors: vendor table
2024-09-03 09:20:56 +07:00
Return values:
-
2024-09-01 18:37:50 +07:00
*/
2024-09-05 10:13:53 +07:00
struct purchase_res purchase(int item_id, int amount, struct vendor_item vendors[MAX_VENDORS])
{
2024-09-03 09:20:56 +07:00
if(amount <= vendors[item_id].inventory_count)
{
2024-09-05 10:13:53 +07:00
// Decrease vendor inventory
2024-09-03 09:20:56 +07:00
vendors[item_id].inventory_count = vendors[item_id].inventory_count - amount;
2024-09-05 10:13:53 +07:00
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;
2024-09-03 13:00:50 +07:00
2024-09-05 10:13:53 +07:00
return (response);
2024-09-03 09:20:56 +07:00
}
else
{
2024-09-05 10:13:53 +07:00
struct purchase_res response;
response.status = 1;
return response;
2024-09-03 09:20:56 +07:00
}
};