Code:
#include <stdio.h>
#include <stdlib.h>
char ItemChosen = 0;
int Orange = 0, Apple = 0, Pear = 0;
int Budget = 0, RemainingBudget = 0;
void checkChoice();
void mainMenu();
void setPrices();
void getBudget();
void thankYou();
void mainMenu() {
printf("**************************************\n");
printf("Shop Menu : \n");
printf("Item: Price:\n");
printf("O: %d\n", Orange);
printf("A: %d\n", Apple);
printf("P: %d\n", Pear);
printf("**************************************\n");
printf("Please type what item of fruit you would like to purchase : ");
scanf(" %c", &ItemChosen);
checkChoice();
}
void thankYou() {
printf("Thanks for shopping with us!\n");
exit(0);
}
int main() {
setPrices();
getBudget();
mainMenu();
return 0;
}
void checkChoice() {
int success = 0;
if (ItemChosen == 'A') {
if (Apple <= Budget) {
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", ItemChosen);
printf("item price : %d\n", Apple);
RemainingBudget = Budget - Apple;
if (RemainingBudget < 0) RemainingBudget = 0;
printf("Remaining budget : %d\n", RemainingBudget);
//printf("Thank you for Shopping with us!");
success = 1;
}
else { success = 0; }
}
else if (ItemChosen == 'P') {
if (Pear <= Budget)
{
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", ItemChosen);
printf("item price : %d\n", Pear);
RemainingBudget = Budget - Pear;
printf("Remaining budget : %d\n", RemainingBudget);
//printf("Thank you for Shopping with us!");
success = 1;
} else { success = 0; }
}
else if (ItemChosen == 'O') {
if (Orange <= Budget) {
printf("Your purchase has been successful!\n");
printf("Purchase details\n");
printf("---------------------------------\n");
printf("Item selected : %c\n", ItemChosen);
printf("item price : %d\n", Orange);
RemainingBudget = Budget - Orange;
printf("Remaining budget : %d\n", RemainingBudget);
//printf("Thank you for Shopping with us!");
success = 1;
} else { success = 0; }
} else {
printf("Incorrect item [%c] selected.\n", ItemChosen);
thankYou();
}
if (success) {
Budget = RemainingBudget;
mainMenu();
} else {
printf("Error your budget has insufficant funds or Missing item\n");
}
}
void setPrices() {
printf("Please enter the price of Oranges :");
scanf(" %d", &Orange);
printf("Please enter the price of Apples :");
scanf(" %d", &Apple);
printf("Please enter the price of Pears :");
scanf(" %d", &Pear);
}
void getBudget() {
printf("**************************************\n");
printf("Hello and welcome to my fruit shop\n");
printf("Please enter your budget : ");
scanf(" %d", &Budget);
}