Code:
#include <stdio.h>
int isItemExists(char c, char itemPrefixes[]);
void displayMenu(char itemPrefixes[], int prices[]); //function call including the arrays within the functions
int getPrices(char c, char itemPrefixes[], int prices[]);
void getBudget();
int purchaseItem(char itemPrefixes[], int prices[]);
void thankYou();
int size;
int Budget = 0;
int RemainingBudget = 0;
int main()
{
printf("--- ALL AVAILABLE FRUITS ---\n");
printf("P: Pears\n");
printf("M: Mango\n");
printf("A: Apple\n");
printf("O: Orange\n");
printf("C: Cherry\n");
printf("W: Watermelon\n");
printf("-------------------------------------\n");
printf("How many Fruits have you got in stock: ");
scanf_s("%d", &size);
int prices[8];
char itemPrefixes[8]; //Declares the size of the arrays
int Choice = 0;
while (Choice < size) {
printf("\n(%d) Enter Fruit prefix:", Choice + 1);
char prefix = ' ';
scanf_s(" %c", &prefix, 1);
if (isItemExists(prefix, itemPrefixes) == 1) { //Calls for the fucntion that Checks for duplication choice of items
printf("Error: Fruit already chosen!");
continue;
}
else {
itemPrefixes[Choice] = prefix;
printf("Enter Price for (%c):", prefix);
scanf_s("%d", &prices[Choice]);
Choice++;
}
}
displayMenu(itemPrefixes, prices);
getBudget();
char run = 'y';
while (run == 'y') {
char choice;
printf("\nSHOP OPERATIONS\n");
printf("1. Enter 'c' to check if Fruit is in stock\n");
printf("2. Enter 'd' to display Menu\n");
printf("3. Enter 'm' to display item prices\n"); //Displays the options of the shops to the customers
printf("4. Enter 'i' to compare budget with item prices\n");
printf("5. Enter 'p' to purchase an item\n");
printf("Enter your choice: ");
scanf_s(" %c", &choice, 1);
char cSearch;
if (choice == 'c') {
printf("Please enter Fruit prefix to search: \n");
scanf_s(" %c", &cSearch, 1);
int found = isItemExists(cSearch, itemPrefixes);
if (found) {
printf("Fruit FOUND!");
}
else {
printf("Fruit NOT FOUND!");
}
}
else if (choice == 'd') {
displayMenu(itemPrefixes, prices);
}
else if (choice == 'm') {
printf("Please enter fruit prefix: \n");
scanf_s(" %c", &cSearch, 1);
int m = getPrices(cSearch, itemPrefixes, prices);
if (m > 0) {
printf("The price of %c is %d\n", cSearch, m);
}
else {
printf("%c has not been selected!", cSearch);
}
}
else if (choice == 'i') {
printf("Your avalible budget is %d\n", Budget);
printf("Please enter an item you would like to compare your budget with: \n");
scanf_s(" %c", &cSearch, 1);
int i = getPrices(cSearch, itemPrefixes, prices);
if (i <= Budget) {
printf("You have sufficiant budget to purchase this fruit\n");
RemainingBudget = Budget - i;
printf("Remaining budget is: %d\n", RemainingBudget);
}
else {
printf("You do not have enough funds to purchase this item, Please add funds");
}
}
else if (choice == 'p') {
printf("Please enter what item you would like to purchase from the Menu: ");
scanf_s(" %c", &cSearch, 1);
displayMenu(itemPrefixes, prices);
int p = purchaseItem(cSearch, itemPrefixes, prices); // Option P is how the customers makes the purchase and receievs recipt
if (p > Budget) {
printf("You have insuffciant funds for this item\n");
}
else if (p <= Budget); {
int o = getPrices(cSearch, itemPrefixes, prices);
RemainingBudget = Budget - o;
printf("Item selected: %c\n", cSearch);
printf("Item price : %d\n", o);
printf("Remaining budget:%d\n", RemainingBudget);
}
}
else {
printf("Please enter a valid choice!");
}
printf("\nDo you want to keep shopping: (y/n)?");
scanf_s(" %c", &run, 1);
}
thankYou(); // function exits program when n is selected
return 0;
}
void getBudget() {
printf("**************************************\n");
printf("Hello and welcome to my fruit shop\n"); // function Initlization for the customer to enter their budget
printf("Please enter your budget : ");
scanf_s(" %d", &Budget);
}
int isItemExists(char prefix, char itemPrefixes[]) {
for (int i = 0; i < size; i++) {
if (itemPrefixes[i] == prefix) { //function uses arrays to see whether item has been chosen twice
return 1;
}
}
return 0;
}
void displayMenu(char itemPrefixes[], int prices[]) {
printf("\n***** SHOP MENU *****\n");
printf("Fruit:\tPrices");
for (int i = 0; i < size; i++) {
printf("\n%c:\t%d", itemPrefixes[i], prices[i]); // fucntion displays menu
}
printf("\n\n");
}
int purchaseItem(char itemPrefixes[], int prices[]) {
printf("Your purchase has been successful!\n"); // function gives the recipt to the customer
printf("Purchase details\n");
printf("---------------------------------\n");
return 0;
}
int getPrices(char c, char itemPrefixes[], int prices[]) {
for (int i = 0; i < size; i++) {
if (c == itemPrefixes[i]) {
return prices[i]; //get prices of the items
}
}
return -1;
}
void thankYou() {
printf("Thanks for shopping with us!\n"); // Function intlization for thank you message and to exit program
exit(0);
}