Create a class named Pizza that stores information about a single pizza. It should contain the following:
Private instance variables to store the size of the pizza (either small, medium, or large), the number of cheese toppings, the number of pepperoni toppings, and the number of ham toppings.
Constructor(s) that set all of the instance variables.
Public methods to get and set the instance variables.
A public method named calcCost( ) that returns a double that is the cost of the pizza.
Pizza cost is determined by:
Small: $10 + $2 per topping
Medium: $12 + $2 per topping
Large: $14 + $2 per topping
A public method named getDescription( ) that returns a String contain- ing the pizza size, quantity of each topping, and the pizza cost as calculated by calcCost( ).
Write test code to create several pizzas and output their descriptions. For example, a large pizza with one cheese, one pepperoni and two ham toppings should cost a total of $22.
Program:
// Pizza.java
public class Pizza
{
private String pizzaSize;
private int cheeseCount;
private int pepperoniCount;
private int hamCount;
public Pizza()
{
this.pizzaSize = "";
this.cheeseCount = 0;
this.pepperoniCount = 0;
this.hamCount = 0;
}
public Pizza(String pizzaSize, int cheeseCount,
int pepperoniCount, int hamCount)
{
this.pizzaSize = pizzaSize;
this.cheeseCount = cheeseCount;
this.pepperoniCount = pepperoniCount;
this.hamCount = hamCount;
}
public String getPizzaSize()
{
return pizzaSize;
}
public void setPizzaSize(String pizzaSize)
{
this.pizzaSize = pizzaSize;
}
public int getNumCheeseToppings()
{
return cheeseCount;
}
public void setNumCheeseToppings(int cheeseCount)
{
this.cheeseCount = cheeseCount;
}
public int getNumPepperoniToppings()
{
return pepperoniCount;
}
public void setNumPepperoniToppings(int pepperoniCount)
{
this.pepperoniCount = pepperoniCount;
}
public int getNumHmaToppings()
{
return hamCount;
}
public void setNumHmaToppings(int hamCount)
{
this.hamCount = hamCount;
}
public double calcCost()
{
if(pizzaSize.equalsIgnoreCase("small"))
{
return 10 + (cheeseCount + pepperoniCount + hamCount) * 2;
}
else if(pizzaSize.equalsIgnoreCase("medium"))
{
return 12 + (cheeseCount + pepperoniCount + hamCount) * 2;
}
else if(pizzaSize.equalsIgnoreCase("large"))
{
return 14 + (cheeseCount + pepperoniCount + hamCount) * 2;
}
else
{
return 0.0;
}
}
public String getDescription()
{
return "Pizza size: " + pizzaSize + "\n Cheese toppings: "
+ cheeseCount + "\n Pepperoni toppings: "
+ pepperoniCount + "\n Ham toppings: " + hamCount
+ "\n Pizza cost: $" + calcCost() + "\n";
}
}
// PizzaTest.java
public class PizzaTest
{
public static void main(String[] args)
{
Pizza p1 = new Pizza("large", 1, 1, 2);
Pizza p2 = new Pizza("small", 2, 1, 1);
Pizza p3 = new Pizza("medium", 1, 2, 1);
System.out.println(p1.getDescription());
System.out.println(p2.getDescription());
System.out.println(p3.getDescription());
}
}
Output:
Pizza size: large
Cheese toppings: 1
Pepperoni toppings: 1
Ham toppings: 2
Pizza cost: $22.0
Pizza size: small
Cheese toppings: 2
Pepperoni toppings: 1
Ham toppings: 1
Pizza cost: $18.0
Pizza size: medium
Cheese toppings: 1
Pepperoni toppings: 2
Ham toppings: 1
Pizza cost: $20.0