import java.util.Scanner;
//TASK #5 add an import statement to use the DecimalFormat class
import java.text.DecimalFormat;
public class PizzaOrder
{
private double cost; //cost of the pizza
private String crust; //type of crust
private String toppingList; //list of the toppings
private int size; //diameter in inches
private int numToppings = 0; //number of toppings
public PizzaOrder() //Constructor creates a 12" hand-tossed pizza
{
cost = 12.99;
crust = "Hand-tossed";
size = 12;
numToppings = 0;
toppingList = null;
}
public void setCost (double amount) //adds the parameter amount to the cost
{
cost += amount;
}
public void setCrust (String type) //sets crust type
{
crust = type;
}
public void setSize (int diameter) //changes the size of the pizza to the parameter diameter
{
size = diameter;
}
public void setNumToppings (int number) //sets the number of toppings to the parameter number
{
numToppings = number;
}
public void setToppingList (String newTopping) //sets the list of toppings
{
toppingList = newTopping;
}
public double getCost() //returns the cost of the pizza
{
return cost;
}
public String getCrust() //returns crust type
{
return crust;
}
public int getSize() //returns the size of the pizza
{
return size;
}
public int getNumToppings() //returns the number of toppings
{
return numToppings;
}
public String getToppingList() //returns the list of toppings
{
return toppingList;
}
public static void main (String [] args)
{
//TASK #5 Create a DecimalFormat object with 2 decimal places
DecimalFormat formatter = new DecimalFormat("#0.00");
//create a Scanner object to read input
Scanner keyboard = new Scanner (System.in);
//create an instance ofa pizza
Pizza order = new Pizza();
String firstName; //user's first name
boolean discount = false; //flag, true if user is eligible for discount
int inches; //sizes of the pizza
char crustType; //type of crust
double cost; //cost of the pizza
final double TAX_RATE = .08; //sales tax rate
double tax; //amount of tax
char choice; //user's choice
String input; //user input
String toppings = "Cheese "; // list of toppings
int numberOfToppings = 0; //number of toppings
String maleOwner = "Mike"; //compare with user's first name for discount
String femaleOwner = "Diane"; //compare with user's first name for discount
//prompt user and get first name
System.out.println("Welcome to Mike and Diane's Pizza");
System.out.println("Enter your first name: ");
firstName = keyboard.nextLine();
//determine if user is eligible for discount by having the same first name as one of the owners
//add lines here for task 1
firstName = firstName.toUpperCase();
if ( (firstName.equals(maleOwner)) || (firstName.equals(femaleOwner)) )
{
discount = true; //<------------------------------------------------------------------
}
//prompt user and get pizza size choice;
System.out.println("Pizza Size (inches) Cost");
System.out.println(" 10 $10.99");
System.out.println(" 12 $12.99");
System.out.println(" 14 $14.99");
System.out.println(" 16 $16.99");
System.out.println("What size pizza would you like?");
System.out.println("10, 12, 14, or 16 (enter the number only): ");
inches = keyboard.nextInt();
//set price and size of pizza ordered
//add lines her for task 2
if ( inches == 10 )
{
order.setSize(inches);
order.setCost(10.99);
}
else if ( inches == 12 )
{
order.setSize(inches);
order.setCost(12.99);
}
else if ( inches == 14 )
{
order.setSize(inches);
order.setCost(14.99);
}
else if ( inches == 16 )
{
order.setSize(inches);
order.setCost(16.99);
}
else
{
System.out.println("The input was not one of the choices so a 12 inch pizza will be made.");
order.setSize(12);
order.setCost(12.99);
}
//consume the remaining newline character
keyboard.nextLine();
//prompt user and get crust choice
System.out.println("What type of crust do you want? ");
System.out.println(" (H) Hand-tossed, (T) Thin-crust, or " + " (D) Deep-dish (enter H, T, or D): ");
input = keyboard.nextLine();
crustType = input.charAt(0);
//set user's crust choice on pizza ordered
// add lines for task 3
switch (crustType)
{
case 'h':
case 'H':
order.setCrust("Hand-tossed");
break;
case 't':
case 'T':
order.setCrust("Thin-crust");
break;
case 'd':
case 'D':
order.setCrust("Deep-dish");
default:
System.out.println("The input was not one of the choices so a Hand-tossed crust will be made.");
order.setCrust("Hand-tossed");
}
//prompt user and get topping choices one at a time
System.out.println("All pizzas come with cheese. ");
System.out.println("Additional toppings are $1.25 each, " + " choose from");
System.out.println("Pepperoni, Sausage, Onion, Mushroom");
//if topping is desired
//add to topping list and number of toppings
System.out.println("Do you want Pepperoni? (Y/N); ");
input = keyboard.nextLine();
choice = input.charAt(0);
if (choice == 'Y' || choice == 'y')
{
numberOfToppings += 1;
toppings = toppings + "Pepperoni ";
}
System.out.println("Do you want Sausage? (Y/N): ");
input = keyboard.nextLine();
choice = input.charAt(0);
if (choice == 'Y' || choice == 'y');
{
numberOfToppings += 1;
toppings = toppings + "Sausage ";
}
System.out.println("Do you want onion? (Y/N): ");
input = keyboard.nextLine();
choice = input.charAt(0);
if (choice == 'Y' || choice == 'y');
{
numberOfToppings += 1;
toppings = toppings + "Onion ";
}
System.out.println("Do you want mushroom? (Y/N): ");
input = keyboard.nextLine();
choice = input.charAt(0);
if (choice == 'Y' || choice == 'y');
{
numberOfToppings += 1;
toppings = toppings + "Mushroom ";
}
//set number of toppings and toppings list on pizza ordered
order.setNumToppings (numberOfToppings);
order.setToppingList (toppings);
//add additional toppings cost to cost of pizza ordered
order.setCost(1.25 * numberOfToppings);
//display order confirmation
System.out.println();
System.out.println("Your order is as follows: ");
System.out.println(order.getSize() + " inch pizza ");
System.out.println("The type of crust is " + crustType); //("The type of crust is " + crustType) (order.getCrust() + " crust ")
System.out.println(order.getToppingList());
//display cost of pizza
cost = order.getCost();
//apply discount if user is elibible
//add lines for task 4 here
if (discount)
{
System.out.println("You are eligible for a $2.00 discount."); //<---------------------------------------------
cost -= 2;
}
//edit program for task 5
//so all money output appears with 2 deciamal places
System.out.println("The cost of the order is: $" + formatter.format(cost));
//calculate and display tax and total cost
tax = cost * TAX_RATE;
System.out.println("The tax is: $" +formatter.format(tax));
System.out.println("The total due is: $" + formatter.format((tax + cost)));
System.out.println("Your order will be ready" + " for pickup in 30 minutes.");
}
}