In: Computer Science
Write a Java Program to place a pizza ordering program. It creates a pizza ordered to the specifications that the user desires. It walks the user through ordering, giving the user choices, which the program then uses to decide how to make the pizza and how much the cost of the pizza will be. Note: Use dialog boxes for communicating with the user. Remember to use the JOptionPane class which is the graphical user interface (GUI) and Comments that are not read by the computer, they are for use by the programmer. They are to help a programmer document what the program does and how it accomplishes it. This is important when a programmer needs to modify code that is written by another person.
Task 1. Name the program, PizzaOrder.java ( Ask the customer the following Question using the message box then the summary of the order should show up in the console )
2. Write a welcome message that said, “Welcome to ___________________ Pizza.”
3. Ask the user to input his or her first name. “Enter your first name:”
4. A menu should display to the user: Pizza sizes (inches) Cost 10” $10.99 12” $12.99 14” $14.99 16” $16.99 What size pizza would you like? 10, 12, 14, or 16 (enter the number only):
5. After the user has inputted the number another print statement should ask “What type of crust do you want? (H)Hand-tossed, (T) Thin-crust, or (D) Deepdish (enter H, T, or D):”
6. After the user has inputted the type of crust another print statement asking All pizzas come with cheese. Additional toppings are $1.25 each, choose from: Pepperoni, Sausage, Onion, Mushroom Do you want Pepperoni? (Y/N):
7. After you inputted Yes, or No another question should pop up Do you want Sausage? (Y/N): then Do you want Onion? (Y/N): then Do you want Mushroom? (Y/N):
8. At the end, it should show a receipt depending on the input: Note: The sales tax is 8.875% (below is an example) Your order is as follows:
10-inch Pizza
Hand-tossed crust $ 10.99
Pepperoni $ 1.25
The subtotal cost of your order is: $ 12.24
The tax is: $ 1.09 ----------------
total is: $ 13.33
*Your order will be ready for pick up in 30 minutes*
Here is the Code for PizzaOrder.java with all requried comments:
package pizzaorder;
import javax.swing.*;
public class PizzaOrder {
public static void main(String[] args) {
JFrame frame=new JFrame();
//Show welcome message
JOptionPane.showMessageDialog(frame, "Welcome to _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Pizza");
//Shown input for name
String input = JOptionPane.showInputDialog("Enter your first name:");
System.out.println(input);
//If name is not entered then exit
if(input==null||input.isEmpty() || input.equalsIgnoreCase(""))
return;
//Show options for Pizza sizes
String option = JOptionPane.showInputDialog("Pizza sizes (inches) Cost:\n" +
" 1. 10”: $10.99 \n" +
" 2. 12”: $12.99 \n" +
" 3. 14”: $14.99 \n" +
" 4. 16”: $16.99.\n"
+ "What size pizza would you like?\n"
+ "10, 12, 14, or 16 (enter the number only):");
System.out.println(option);
//if no option entered, exit then
if(option==null|| option.isEmpty() || option.equalsIgnoreCase(""))
return;
//Show type of crust options
String typeOfCrust = JOptionPane.showInputDialog("What type of crust do you want?\n" +
"(H)Hand-tossed\n" +
"(T)Thin-crust\n" +
"(D)Deepdish\n" +
"(enter H, T, or D):");
System.out.println(typeOfCrust);
if(typeOfCrust==null ||typeOfCrust.isEmpty() || typeOfCrust.equalsIgnoreCase(""))
return;
//Show message for toppings
JOptionPane.showMessageDialog(frame," All pizzas come with cheese. Additional toppings are $1.25 each, choose from:\n"
+ " Pepperoni, Sausage, Onion, Mushroom");
//option for peperoni
String peperoni=JOptionPane.showInputDialog( "Do you want Pepperoni? (Y/N):");
System.out.println(peperoni);
if(peperoni==null ||peperoni.isEmpty() || peperoni.equalsIgnoreCase(""))
return;
//option for sousage
String sousage=JOptionPane.showInputDialog("Do you want Sausage? (Y/N):");
System.out.println(sousage);
if(sousage.isEmpty() || sousage.equalsIgnoreCase(""))
return;
//option for onion
String onion=JOptionPane.showInputDialog("Do you want Onion? (Y/N):");
System.out.println(onion);
if(onion==null ||onion.isEmpty() || onion.equalsIgnoreCase(""))
return;
//option for mushroom
String mushroom=JOptionPane.showInputDialog(" Do you want Mushroom? (Y/N):");
System.out.println(mushroom);
if(mushroom==null ||mushroom.isEmpty() || mushroom.equalsIgnoreCase(""))
return;
/*
* Your order is as follows:
*
* 10-inch Pizza
*
* Hand-tossed crust $ 10.99
*
* Pepperoni $ 1.25
*
* The subtotal cost of your order is: $ 12.24
*
* The tax is: $ 1.09 ----------------
*
* total is: $ 13.33
*/
String order="Your order is as follows:\n";
order+=option+"-intch Pizza\n";
switch (typeOfCrust) {
case "H":
order+="Hand-tossed crust";
break;
case "T":
order+="Thin-crust";
break;
case "D":
order+="Deepdish crust";
break;
default:
break;
}
order+=" $ ";
double cost=0.0;
switch (option) {
case "10":cost=10.99; break;
case "12": cost=12.99;break;
case "14": cost=14.99;break;
case "16": cost=16.99;break;
default:
break;
}
order+= Double.toString(cost)+"\n";
double costtopping=0.0;
if(peperoni.equalsIgnoreCase("Y") || peperoni.equalsIgnoreCase("y")) {
costtopping+=1.25;
order+="\nPeperoni $ "+"1.25";
}
if(sousage.equalsIgnoreCase("Y") || sousage.equalsIgnoreCase("y")) {
costtopping+=1.25;
order+="\nSousage $ "+"1.25";
}
if(onion.equalsIgnoreCase("Y") || onion.equalsIgnoreCase("y")) {
costtopping+=1.25;
order+="\nOnion $ "+"1.25";
}
if(mushroom.equalsIgnoreCase("Y") || mushroom.equalsIgnoreCase("y")) {
costtopping+=1.25;
order+="\nOnion $ "+"1.25";
}
//sub total
Double totalcost=cost+costtopping;
order+="\nThe subtotal cost of your order is: $ "+Double.toString(totalcost);
//tax calculation
Double tax=(Double)((totalcost/100)*8.875);
order+="\nThe tax is: $ "+ tax +" ----------------\n";
//final amount calculation
Double finalAmount=totalcost+tax;
order+="total is: $ "+finalAmount; //13.33
//reciept display
JOptionPane.showMessageDialog(frame, order);
}
}
Output:
UI Screens for all inputs and messages: