Question

In: Computer Science

Trying to complete this in Java. Application called 'Coffee Shop' that uses a while loop to...

Trying to complete this in Java. Application called 'Coffee Shop' that uses a while loop to build a customer order. The Coffee Shops sells: Coffee ($3.25), Espresso ($4.25), and Tea ($2.75).

The coffee selection presents the customer with the choices of iced (no charge), cream (50 cents), and sugar (50 cents). The espresso selection presents the customer with choice of caramel (no charge) and chocolate (no charge) with one shot (no charge) or two shots ($1.25) of espresso. Once the customer is done, he or she will receive a bill of the total price. After each selection, the customer will have the choice of returning to the main menu for additional purchases. Use nested loops to handle customers' submenu selections.

Solutions

Expert Solution

Code:

import java.util.*;
class CoffeeShop
{
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);
       // Total Price
       double totalPrice = 0;
       // Types of products
       int totalCoffee = 0;
       int totalEspresso =0;
       int totalTea = 0;
       // Types of coffee
       int totalIced = 0;
       int totalCream = 0;
       int totalSugar = 0;
       // Types of Espresso
       int totalCaramel = 0;
       int totalChocolate = 0;
       int totalOneShot = 0;
       int totalTwoShots = 0;
       // This variable is used to know whether the user completed their order or not
       boolean orderCompleted = false;
       // Until user order is not completed, this loop is iterated
       while(!orderCompleted)
       {
           // Printing the available products
           System.out.println("Enter 1 to order Coffee");
           System.out.println("Enter 2 to order Espresso");
           System.out.println("Enter 3 to order Coffee");
           System.out.println("Enter 4 to exit");
           System.out.print("Enter your choice:");
           // Reading the user choice
           int orderChoice = input.nextInt();
           // Switch case on user choice
           switch(orderChoice)
           {
               // If user chose coffee
               case 1:
                   // Incrementing the coffee count
                   totalCoffee += 1;
                   // Adding the price of coffee to the totalPrice
                   totalPrice += 3.25;
                   // Printing the sub choices in coffee
                   System.out.println("Enter 1 to select iced");
                   System.out.println("Enter 2 to select cream");
                   System.out.println("Enter 3 to select sugar");
                   System.out.print("Enter your choice:");
                   // Reading the sub choice
                   int coffeeChoice = input.nextInt();
                   // If user entered 1
                   if(coffeeChoice == 1)
                   {
                       totalIced += 1;
                   }
                   // If user entered 2
                   else if(coffeeChoice == 2)
                   {
                       // Adding price of cream to totalPrice
                       totalPrice += 0.5;
                       totalCream += 1;
                   }
                   // If user entered 3
                   else if(coffeeChoice == 3)
                   {
                       // Adding price of sugar to totalPrice
                       totalPrice += 0.5;
                       totalSugar += 1;
                   }                  
                   break;
               // If user chose Espresso
               case 2:
                   // Incrementing the Espresso count
                   totalEspresso += 1;
                   // Adding price of Espresso to the totalPrice
                   totalPrice += 4.25;
                   // Printing the sub choices in Espresso
                   System.out.println("Enter 1 to select caramel");
                   System.out.println("Enter 2 to select chocolate");
                   System.out.println("Enter 3 to select one shot");
                   System.out.println("Enter 4 to select two shots");
                   System.out.print("Enter your choice:");
                   // Reading the sub choice
                   int espressoChoice = input.nextInt();
                   // If user entered 1
                   if(espressoChoice == 1)
                   {
                       // Incrementing the caramel count
                       totalCaramel += 1;
                   }
                   // If user entered 2
                   else if(espressoChoice == 2)
                   {
                       // Incrementing the chocoalte count
                       totalChocolate += 1;
                   }
                   // If user entered 3
                   else if(espressoChoice == 3)
                   {
                       // Incrementing the oneShot count
                       totalOneShot += 1;
                   }
                   // If user entered 4
                   else if(espressoChoice == 4)
                   {
                       // Incrementing the twoShot count
                       totalTwoShots += 1;
                       // Adding price of twoShot to totalPrice
                       totalPrice += 1.25;
                   }                  
                   break;
               // If user chose tea
               case 3:
                   // Incrementing the tea count
                   totalTea += 1;
                   // Adding price of tea to totalPrice
                   totalPrice += 2.75;
                   break;
               // If user wants to complete the order
               case 4:
                   // Setting the orderCompleted to true
                   orderCompleted = true;
                   break;
               // Invalid choice
               default:
                   System.out.println("Invalid choice");
                   break;
           }
       }
      
       // Printing the Order
       // If coffee count is greater than 0
       if(totalCoffee > 0)
       {
           // Printing number of coffee's ordered with their price
           System.out.printf("Coffee x%d\t %.2f\n",totalCoffee,totalCoffee*3.25);\
           // If any sub choices were ordered, their Quantity with price(if exists) is printed
           if(totalIced > 0)
           {
               System.out.printf("\t Iced x%d\n",totalIced);
           }
          
           if(totalCream > 0)
           {
               System.out.printf("\t Cream x%d\t %.2f\n",totalCream,totalCream*0.5);
           }
          
           if(totalSugar > 0)
           {
               System.out.printf("\t Sugar x%d\t %.2f\n",totalSugar,totalSugar*0.5);
           }
       }
       // If espresso count is greater than 0
       if(totalEspresso > 0)
       {
           // Printing number of Espresso's ordered with their price
           System.out.printf("Espresso x%d\t %.2f\n",totalEspresso,totalEspresso*4.25);
           // If any sub choices were ordered, their Quantity with price(if exists) is printed          
           if(totalCaramel > 0)
           {
               System.out.printf("\t Caramel x%d\n",totalCaramel);
           }
          
           if(totalChocolate > 0)
           {
               System.out.printf("\t Chocolate x%d\n",totalChocolate);
           }
          
           if(totalOneShot > 0)
           {
               System.out.printf("\t OneShot x%d\n",totalOneShot);
           }
           // If one or more teaare ordered
           if(totalTwoShots > 0)
           {
               System.out.printf("\t TwoShots x%d\t %.2f\n",totalTwoShots,totalTwoShots*1.25);
           }
       }
       // If tea count is greater than 0
       if(totalTea > 0)
       {
           // Printing number of tea's ordered with their price
           System.out.printf("Tea x%d\t %.2f\n",totalTea,totalTea*2.75);
       }

       // Printing the totalPrice
       System.out.printf("Total Price: %.2f\n",totalPrice);
   }
}

Sample Output:


Related Solutions

(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
Write a Java program called Decision that includes a while loop to prompt the user to...
Write a Java program called Decision that includes a while loop to prompt the user to enter 5 marks using the JOptionPane statement and a System. Out statement to output a message inside the loop highlighting a pass mark >= 50 and <= 100. Any other mark will output a messaging stating it’s a fail.
I have to use a sentinel while loop to complete the following task in a java...
I have to use a sentinel while loop to complete the following task in a java program, I want to see how this is executed so I can better understand how the sentinel while loop works. Thank you! Convert Lab 10 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Java Input, Output and for-loop function. Practice01) Write-an average SCORE application called TestScores01 This project reads...
Java Input, Output and for-loop function. Practice01) Write-an average SCORE application called TestScores01 This project reads in a list of integers as SCOREs, one per line, until a sentinel value of -1. After user type in -1, the application should print out how many SCOREs are typed in, what is the max SCORE, the 2nd max SCORE, and the min SCORE, the average SCORE after removing the max and min SCORE. When SCORE >= 90, the school will give this...
Write a program that uses loops (both the for-loop and the while loop). This assignment also...
Write a program that uses loops (both the for-loop and the while loop). This assignment also uses a simple array as a collection data structure (give you some exposure to the concept of data structure and the knowledge of array in Java). In this assignment, you are asked to construct an application that will store and retrieve data. The sequence of data retrieval relative to the input is Last In First Out (LIFO). Obviously, the best data structure that can...
Using JAVA The Jumpin' Jive coffee shop charges $2 for a cup of coffee and offers...
Using JAVA The Jumpin' Jive coffee shop charges $2 for a cup of coffee and offers the add-ins shown in Table 6-2. Design the logic for an application that allows a user to enter ordered add-ins continuously until a sentinel value is entered. After each item, display its price or the message Sorry, we do not carry //that as output. After all items have been entered, display the total price for the order. whipper cream 0.89 cinnamon 0.25 chocolate sauce...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT