Added duplication checks, printf formats and footer
This commit is contained in:
parent
0a0d94ab13
commit
a119eac168
BIN
vendorInfo
BIN
vendorInfo
Binary file not shown.
128
vendorInfo.c
128
vendorInfo.c
|
@ -22,6 +22,8 @@ struct vendor
|
|||
char merchandise[15];
|
||||
int inventory_count;
|
||||
int item_price_baht;
|
||||
int revenue;
|
||||
int duplicate;
|
||||
} vendors[MAX_VENDORS];
|
||||
|
||||
struct search_r
|
||||
|
@ -38,6 +40,7 @@ struct purchase_r
|
|||
};
|
||||
|
||||
/* FUNCTION DECLARATION */
|
||||
void reset_output();
|
||||
int read_input(char str[], size_t n);
|
||||
char *strnlower(char *s, size_t n);
|
||||
|
||||
|
@ -55,6 +58,8 @@ struct purchase_r purchase
|
|||
struct vendor vendors[MAX_VENDORS]
|
||||
);
|
||||
|
||||
int check_vendors(struct vendor vendors[MAX_VENDORS], int total_vendors);
|
||||
|
||||
int main()
|
||||
{
|
||||
char file_name[MAX_FILENAME_LENGTH];
|
||||
|
@ -79,12 +84,10 @@ int main()
|
|||
|
||||
while ((fgets(file_buffer, 50, fp) != NULL) && (index <= MAX_VENDORS - 1))
|
||||
{
|
||||
/* TO-DO: VALIDATE FILE DATA */
|
||||
char *token = strtok(file_buffer, " ");
|
||||
strncpy(vendors[index].vendor_name, token, strlen(token) + 1);
|
||||
|
||||
token = strtok(NULL, " ");
|
||||
/* TO-DO: VALIDATE MERCHANDISE BY PUTTING EVERYTHING AS LOWERCASE */
|
||||
strncpy(vendors[index].merchandise, token, strlen(token) + 1);
|
||||
|
||||
token = strtok(NULL, " ");
|
||||
|
@ -104,12 +107,22 @@ int main()
|
|||
{
|
||||
for (int j = i + 1; j < total_vendors; j++)
|
||||
{
|
||||
|
||||
if ((strcmp(vendors[i].merchandise, vendors[j].merchandise)) == 0)
|
||||
{
|
||||
break;
|
||||
vendors[i].duplicate = 1;
|
||||
vendors[j].duplicate = 1;
|
||||
}
|
||||
|
||||
if ((strcmp(vendors[i].vendor_name, vendors[j].vendor_name)) == 0)
|
||||
{
|
||||
vendors[i].duplicate = 1;
|
||||
vendors[j].duplicate = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (vendors[i].duplicate == 0)
|
||||
{
|
||||
char vendor_info[200];
|
||||
sprintf(vendor_info, "%s sells %s for %d each",
|
||||
vendors[i].vendor_name,
|
||||
|
@ -124,20 +137,38 @@ int main()
|
|||
|
||||
printf("%-50s %s\n", vendor_info, vendor_stock);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\033[0;31mDuplicate record detected!\n");
|
||||
reset_output();
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
while (1)
|
||||
{
|
||||
// CHECK IF ALL MERCHANDISE ARE EMPTY
|
||||
if(check_vendors(vendors, total_vendors) == 0)
|
||||
{
|
||||
break;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (strcmp(user_query, "") == 0)
|
||||
{
|
||||
printf("Please enter an item name\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
// convert string to lowercase - each character one by one
|
||||
int user_query_length = strlen(user_query);
|
||||
strnlower(user_query, user_query_length);
|
||||
|
@ -162,16 +193,53 @@ int main()
|
|||
response.price_owe_baht
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("\033[0;31mSorry -- %s does not have enough items available\n",
|
||||
response.vendor_name
|
||||
);
|
||||
reset_output();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("You must enter a positive whole number for the number of items");
|
||||
printf("\033[0;31mYou must enter a positive whole number for the number of items\n");
|
||||
reset_output();
|
||||
}
|
||||
|
||||
}
|
||||
else if (item.status == 2)
|
||||
{
|
||||
printf("\033[0;31mThis item has duplicate records, please choose another item\n");
|
||||
reset_output();
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("No vendor in this market sells %s\n", user_query);
|
||||
printf("\033[0;31mNo vendor in this market sells %s\n", user_query);
|
||||
reset_output();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n>>> End of day details\n");
|
||||
|
||||
// Print out remaining inventory
|
||||
for (int i = 0; i <= (total_vendors - 1); i++)
|
||||
{
|
||||
if (vendors[i].duplicate == 0)
|
||||
{
|
||||
char vendor_info[200];
|
||||
sprintf(vendor_info, "%s made %d baht today",
|
||||
vendors[i].vendor_name,
|
||||
vendors[i].revenue
|
||||
);
|
||||
|
||||
char vendor_stock[50];
|
||||
sprintf(vendor_stock, "Remaining inventory %d items",
|
||||
vendors[i].inventory_count
|
||||
);
|
||||
|
||||
printf("%-50s %s\n", vendor_info, vendor_stock);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -180,6 +248,14 @@ int main()
|
|||
return 0;
|
||||
};
|
||||
|
||||
/*
|
||||
Output color reset
|
||||
*/
|
||||
void reset_output()
|
||||
{
|
||||
printf("\033[0m");
|
||||
};
|
||||
|
||||
/*
|
||||
read_input: Custom read input function
|
||||
str = string itself
|
||||
|
@ -238,7 +314,10 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot
|
|||
int i;
|
||||
for (i = 0; i < total_vendors; i++)
|
||||
{
|
||||
// First, check if duplicate
|
||||
if (strcmp(query, vendors[i].merchandise) == 0)
|
||||
{
|
||||
if (vendors[i].duplicate == 0)
|
||||
{
|
||||
struct search_r response;
|
||||
response.status = 0;
|
||||
|
@ -246,12 +325,18 @@ struct search_r search(char query[], struct vendor vendors[MAX_VENDORS], int tot
|
|||
|
||||
return (response);
|
||||
break;
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
struct search_r response;
|
||||
response.status = 2;
|
||||
return (response);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (i == total_vendors)
|
||||
{
|
||||
printf("\n\n%d\n%d\n", i, total_vendors);
|
||||
struct search_r response;
|
||||
response.status = 1;
|
||||
response.item_id = 0;
|
||||
|
@ -283,13 +368,40 @@ struct purchase_r purchase(int item_id, int amount, struct vendor vendors[MAX_VE
|
|||
strcpy(response.vendor_name, vendors[item_id].vendor_name);
|
||||
response.price_owe_baht = vendors[item_id].item_price_baht * amount;
|
||||
|
||||
vendors[item_id].revenue = vendors[item_id].item_price_baht * amount;
|
||||
|
||||
return (response);
|
||||
}
|
||||
else
|
||||
{
|
||||
struct purchase_r response;
|
||||
response.status = 1;
|
||||
strcpy(response.vendor_name, vendors[item_id].vendor_name);
|
||||
|
||||
return response;
|
||||
}
|
||||
};
|
||||
|
||||
/*
|
||||
Check function
|
||||
|
||||
Parameters:
|
||||
*/
|
||||
int check_vendors(struct vendor vendors[MAX_VENDORS], int total_vendors)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < total_vendors; i++)
|
||||
{
|
||||
// If there are any items left
|
||||
if (vendors[i].inventory_count != 0 && vendors[i].duplicate == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// There aren't any
|
||||
if (i == total_vendors)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Win phone 1 50
|
||||
Win books 1 70
|
||||
Win books 5 100
|
||||
Deez computers 5 80
|
Loading…
Reference in New Issue