Solution
Arun Shankar answered on
Nov 16 2021
customer.csv
James,1000
Coke Can,10
Bread,3
Spagetti,5
Tomato Sauce,1
Bin Bags,2
main.c
#include #include #include char chunk[128];
struct Product
{
int pid;
Product id - unique to the product.
char* name;
Name of the product
double price;
Price of one unit of the product.
int quantity;
How many units of this pdt. in the shop ?
};
struct Shop
{
double cash;
Cash in the shop
struct Product stock[20];
Stock info
int count;
Number of items in the inventory.
};
struct CartEntry
{
int pid;
int qty;
};
struct Customer
{
char* name;
Customer name
double budget;
Customer's budget
struct CartEntry shoppingList[10];
Shopping cart.
int cart_size;
Number of items in the cart
double cart_value;
Value of the cart
};
void printProduct(struct Product p)
{
printf("PRODUCT ID: %d\tNAME: %s\tPRICE: %.2f\n", p.pid, p.name, p.price);
}
struct Shop createAndStockShop()
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("stock.csv", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
ead in first line from csv
fgets(chunk, sizeof(chunk), fp);
double cashInShop = atof(chunk);
struct Shop shop = { cashInShop };
shop.count = 0;
while loop reads in subsequent lines
int id = 0;
while((fgets(chunk, sizeof(chunk), fp)) != NULL)
{
char *n = strtok(chunk, ",");
char *p = strtok(NULL, ",");
char *q = strtok(NULL, ",");
int quantity = atoi(q);
double price = atof(p);
char *name = malloc(sizeof(char) * 50);
strcpy(name, n);
struct Product product = { id++, name, price, quantity };
shop.stock[shop.count++] = product;
}
return shop;
}
double GetStock(struct Shop* sptr, int pid)
{
for(int i=0;icount;i++)
if(sptr->stock[i].pid==pid)
return sptr->stock[i].quantity;
return 0;
}
void DecreaseStock(struct Shop* sptr, int pid, int qty)
{
...