In: Computer Science
JAVA CODE_
Use menu based functionality and validation loops to create a new customer bonus fruit package application for the Dirt to Dish discount produce company.
The program you create will prompt the user to enter a number to select one of three bonus pack options:
1 for Apple pack
2 for Orange pack
3 for Banana pack
**Use input validation to ensure a valid option has been
selected before proceeding.
Use variables to keep track of how many of each fruit a customer will have depending on which bonus pack has been chosen. The amount of fruit per bonus pack is given as follows:
Banana pack: 5 bananas, 2 apples, 2 oranges
Orange pack: 5 oranges, 2 apples, 2 bananas
Apple pack: 5 apples, 2 bananas, 2 oranges
Use menu based functionality and input validation and prompt the user to choose from one of the following recipes:
1. Fruit medley (2 of each fruit)
2. Mixed Apple Pie (3 apples, 1 orange, 1 banana)
3. Banana tower (4 bananas, 2 oranges)
The amount of fruit required for each recipe is shown in
parenthesis.
After a valid recipe choice has been made, subtract the amount of fruit needed for the recipe if the customer has enough fruit. If the customer does not have enough fruit then show a message telling the customer to buy more fruits.
Finally display the amount of fruit remaining from the bonus pack after the recipe selection has been made.
First example run:
This is Dirt to Dish's new customer bonus service! Please select your free fruit package: 1. Apple pack 2. Orange pack 3. Banana pack Please enter 1, 2, or 3: 2 You have chosen: Orange pack You have a total of: 2 apples 5 oranges 2 bananas Which dish would you like to make first? 1. Fruit medley (2 of each fruit) 2. Mixed Apple Pie (3apples, 1 orange, 1 banana 3. Banana tower (4 bananas, 2 oranges Please enter 1, 2, or 3: 2 Mixed Apple Pie chosen Sorry, you need more fruits! The fruit you have left is: 2 apples 5 oranges 2 bananas Thank you!
// Java program to create a new customer bonus fruit package application for the Dirt to Dish discount produce company.
import java.util.Scanner;
public class DirtToDishCompany {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int numApples=0, numOranges = 0, numBananas = 0;
int packChoice, dishChoice;
boolean enoughFruits;
System.out.println("This is Dirt to Dish's new customer bonus service!");
// input of pack choice
System.out.println("Please select your free fruit package:");
System.out.println("1. Apple pack");
System.out.println("2. Orange pack");
System.out.println("3. Banana pack");
System.out.print("Please enter 1, 2, or 3: ");
packChoice = scan.nextInt();
// validate pack choice and re-prompt until valid
while(packChoice < 1 || packChoice > 3)
{
System.out.println("Invalid choice.");
System.out.print("Please enter 1, 2, or 3: ");
packChoice = scan.nextInt();
}
// set the number of apples, oranges and bananas based on the pack choice
switch(packChoice)
{
case 1:
numApples = 5;
numBananas = 2;
numOranges = 2;
System.out.println("You have chosen: Apple pack");
break;
case 2:
numApples = 2;
numOranges = 5;
numBananas = 2;
System.out.println("You have chosen: Orange pack");
break;
case 3:
numApples = 2;
numOranges = 2;
numBananas = 5;
System.out.println("You have chosen: Banana pack");
break;
}
System.out.println("\nYou have a total of:");
System.out.printf("%d apples %d oranges %d bananas",numApples, numOranges,numBananas);
System.out.println("\nWhich dish would you like to make first? ");
System.out.println("1. Fruit medley (2 of each fruit)");
System.out.println("2. Mixed Apple Pie (3 apples, 1 orange, 1 banana)");
System.out.println("3. Banana tower (4 bananas, 2 oranges)");
// input of dish choice
System.out.print("Please enter 1, 2, or 3: ");
dishChoice = scan.nextInt();
// validate dish choice and re-prompt until valid
while(dishChoice < 1 || dishChoice > 3)
{
System.out.println("Invalid choice.");
System.out.print("Please enter 1, 2, or 3: ");
dishChoice = scan.nextInt();
}
enoughFruits = true;
// check if enough fruits are available to create the dish and calculate the number of remaining fruits after creating the dish
switch(dishChoice)
{
case 1:
System.out.println("Fruit medley chosen");
enoughFruits = (numApples >= 2 && numOranges >=2 && numBananas >=2);
if(enoughFruits)
{
numApples -= 2;
numBananas -= 2;
numOranges -= 2;
}
break;
case 2:
System.out.println("Mixed Apple Pie chosen");
enoughFruits = (numApples >= 3 && numOranges >=1 && numBananas >=1);
if(enoughFruits)
{
numApples -= 3;
numBananas -= 1;
numOranges -= 1;
}
break;
case 3:
System.out.println("Banana tower chosen");
enoughFruits = (numBananas >=4 && numOranges >=2);
if(enoughFruits)
{
numBananas -= 4;
numOranges -= 2;
}
break;
}
// check if enough fruits are present for the dish, else print an error message
if(!enoughFruits)
System.out.println("Sorry, you need more fruits!");
System.out.println("The fruit you have left is:");
// print the number of fruits that are left
if(numApples > 0)
System.out.println(numApples+" apples");
if(numOranges > 0)
System.out.println(numOranges+" oranges");
if(numBananas > 0)
System.out.println(numBananas+" bananas");
System.out.println("\nThank you!");
scan.close();
}
}
//end of program
Output: