In: Computer Science
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.
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: