Question

In: Computer Science

JAVA CODE_ Use menu based functionality and validation loops to create a new customer bonus fruit...

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!

Solutions

Expert Solution

// 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:


Related Solutions

Use menu based functionality and validation loops to create a new customer bonus fruit package application...
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...
Create a menu in Java. A menu is a presentation of options for you to select....
Create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Buy Stock. B. Sell Stock X. Exit Enter your Selection: 3. Prompt for the value menuChoice....
Class, Let's create a menu in Java. A menu is a presentation of options for you...
Class, Let's create a menu in Java. A menu is a presentation of options for you to select. You can do this in the main() method. 1. Create a String variable named menuChoice. 2. Display 3 options for your program. I recommend using 3 System.out.println statements and a 4th System.out.print to show "Enter your Selection:". This needs to be inside the do -- while loop. A. Deposit Cash. B. Withdraw Cash X. Exit Enter your Selection: 3. Prompt for the...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Use C++ to create an application that provide the menu with two options TemperatureConverter_Smith.cpp MENU CONVERTER...
Use C++ to create an application that provide the menu with two options TemperatureConverter_Smith.cpp MENU CONVERTER TEMPERATURE – JAMES SMITH Convert Fahrenheit temperature to Celsius Convert Celsius temperature to Fahrenheit Exit CASE 1: Convert Fahrenheit temperature to Celsius -Display the message to ask to enter Fahrenheit degree from the keyboard -Use the following formula to convert to Celsius degree         Celsius Temperature = (Fahrenheit Temperature – 32) * 5/9 ; -Display the output as below: TemperatureConverter_Smith.cpp TEMPERATURE CONVERTER – JAMES...
Using Ruby Your extended Text Based Music Application must add the following functionality: Display a menu...
Using Ruby Your extended Text Based Music Application must add the following functionality: Display a menu that offers the user the following options: 1. Read in Albums 2. Display Albums 3. Select an Album to play 4. Update an existing Album 5. Exit the application Menu option 1 should prompt the user to enter a filename of a file that contains the following information: ·The number of albums ·The first album name ·The first artist name ·The genre of the...
Java Create a method to display a menu. When this method is called it should receive...
Java Create a method to display a menu. When this method is called it should receive the user's name, great the user, and ask them to make a selection. 1 - Change your name, 2 - Test your IQ, 3 - Display a table, 4 - Play a game, 5 - Exit.
JAVA program to simulate a customer’s online grocery shopping. The customer will shop fruit and vegetable...
JAVA program to simulate a customer’s online grocery shopping. The customer will shop fruit and vegetable each week online. The customer will select one vegetable and one fruit from the Table 1 and 2 respectively. The program that is needed for the online ordering system will allow the customer to place a single order, calculating subtotals, additional fee, and outputting a shopping summary. When completing an order, an itemized summary is to be displayed. This bill should include a 3.5%...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
Create a menu-based program that will allow the user to calculate the area for a few...
Create a menu-based program that will allow the user to calculate the area for a few different shapes: square, rectangle, parallelogram, and circle. Refer to the Sample Output in this document to see what menu options you should use. Create PI as a global constant variable. Main Function use do-while to repeat program until user chooses option 5 call the displayMenu function get user choice & validate with while loop depending on user’s choice, get the data required to calculate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT