Code:
#include <stdio.h>
int isItemExists(char c, char itemPrefixes[]);
void displayMenu(char itemPrefixes[], int prices[]);
int getPrices(char c, char itemPrefixes[], int prices[]);
void getBudget();
int purchaseItem(int budget, char itemPurchased, char itemPrefixes[], int prices[]);
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];
int marked = 0;
while (marked < size) {
printf("\n(%d) enter Fruit prefix:", marked + 1);
char prefix = ' ';
scanf_s(" %c", &prefix, 1);
if (isItemExists(prefix, itemPrefixes) == 1) {
printf("Error: Fruit already chosen!");
continue;
}
else {
itemPrefixes[marked] = prefix;
printf("enter Price for (%c):", prefix);
scanf_s("%d", &prices[marked]);
marked++;
}
}
displayMenu(itemPrefixes, prices);
getBudget();
purchaseItem(itemPrefixes, prices);
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");
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:");
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:");
scanf_s(" %c", &cSearch, 1);
int m = getPrices(cSearch, itemPrefixes, prices);
if (m > 0) {
printf("The price of %c is %d:", 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 comapre your budget with: \n");
scanf_s(" %c", &cSearch, 1);
int i = getPrices(cSearch, itemPrefixes, prices);
if (i <= Budget) {
printf("You have sufficiant funds 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') {
displayMenu;
printf("Please enter what item you would like to purchase from the Menu: ");
scanf_s("%d", &cSearch);
int p = purchaseItem(cSearch, itemPrefixes);
RemainingBudget = Budget - p;
if (p > Budget) {
printf("You have insuffciant funds for this item\n");
}
else (p <= Budget); {
purchaseItem;
}
}
else {
printf("Please enter a valid choice!!!");
}
printf("\nDo you want to keep shopping: (y/n)?");
scanf_s(" %c", &run, 1);
}
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) {
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]);
}
printf("\n\n"); // Display menu
}
int purchaseItem(int budget, char itemPurchased, char itemPrefixes[], int prices[]) {
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", itemPrefixes);
printf("item price : %d\n", prices);
printf("Remaining budget : %d\n", RemainingBudget);
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
}
}
return -1;
}