In: Computer Science
For this homework we are going to walk you through creating a class. Lets look at a pizza restaurant and create a class for this. We will call our class pizza. The goal for every class is to include everything that has to happen for that class in 1 place. So whenever we create a class we need to think of what the nouns or variables will be for that class and then what the actions or methods will be for that class.For the pizza class lets say we will need to know pizza size (1 for small, 2 for medium, and 3 for large), how many toppings, how many drinks, what size drinks (again lets assume 1 for small, 2 for medium, and 3 for large – lets assume if people order more than 1 drink it’s going to be the same size for simplicity). We will also need to know the total cost (make up numbers for this one) and whether or no this will be for dine in, pick up, or delivery (assume 1, 2, or 3). So these will all become our member variables. In general we want these all to be private.Our actions will need to be ordering the pizza and calculating the cost so these will become our methods. Methods are usually public.So go ahead and write or type out your class.Finally, we will need a main to run it. Main acts like the conductor of an orchestra. We don’t want main to do the actual work, rather it will direct others to do the work. So we will need to have an instance of our pizza class and then call the ordering method and the compute cost method. Write main.
Solution for given question in two possible ways:
1. Pizza Class ( that includes Main)
============================================================
import java.util.Scanner;
public class pizza
{
public static void main(String[] args) {
Scanner keyboard = new Scanner( System.in );
int pizzaLength = 0;
int pizzaWidth = 0;
int pizzaShape;
double pizzaDiameter = 0;
double pizzaToppingPrice = 0.025;
int numberOfPizzaToppings;
double pizzaSauceAndCheese = 0.036;
double pizzaDoughPrice = 0.019;
char doughType;
String dough = null;
int numberOfPizzas;
double pizzasBeforeTax;
int cheesyCrustInput;
int deliveryWantedInput;
int deliveryCharge = 0;
double doughVolume = 0;
double area = 0;
double crustCost;
int basePrice;
int costOfPizzaWithTax;
//Shape of pizza
System.out.println ("What is the shape of pizza ?");
System.out.println("Enter (1) for Rectange or (2) Round?");
pizzaShape = keyboard.nextLine().charAt(0);
if(pizzaShape == 1){
System.out.println("What Length?");
pizzaLength = keyboard.nextInt();
System.out.println("What Width?");
pizzaWidth = keyboard.nextInt();
area = pizzaLength * pizzaWidth;
}
else if(pizzaShape == 2){
System.out.println("What is the Diameter?");
pizzaDiameter = keyboard.nextInt();
area = Math.PI * (pizzaDiameter / 2);
}
//Volume
System.out.print("type of dough you want? (1)Classic Hand-Tossed, (2)Thin and Crispy, (3)Texas Toast, or (4)Pan. (enter 1,2,3, or 4.");
doughType = keyboard.nextLine().charAt(0);
String Crust = "";
String deliveryOutput="";
if (doughType == 1) {
dough = "Classic Hand-Tossed";
doughVolume = area * .25;
}
else if (doughType == 2) {
dough = "Thin and Crispy";
doughVolume = area * .1;
}
else if (doughType == 3) {
dough = "Texas Toast";
doughVolume = area * .9;
}
else if (doughType == 4) {
dough = "Pan";
doughVolume = area * .5;
}
//Cheesey crust
if(doughType == 2){
}
else{
System.out.println("Would you like cheesy crust? (1) for yes (2) for no");
cheesyCrustInput = keyboard.nextInt();
if(cheesyCrustInput == 1){
crustCost = area * .02;
Crust = "";
}
else{
Crust = "NO";
}
}
//Toppings
System.out.print("Enter the number of toppings:");
numberOfPizzaToppings = keyboard.nextInt();
int toppings;
if(numberOfPizzaToppings == 0){
toppings = 0;
}
else{
toppings = numberOfPizzaToppings;
}
//Base price
basePrice = area (pizzaSauceAndCheese + (pizzaToppingPrice * numberOfPizzaToppings) + (pizzaDoughPrice * doughVolume));
//how many pizzas
System.out.print("Enter the number of pizzas being ordered:");
numberOfPizzas = keyboard.nextInt();
pizzasBeforeTax = basePrice * numberOfPizzas;
//tax
costOfPizzaWithTax = pizzasBeforeTax ( 1 + 0.07);
//delivery fee
System.out.print("Would you like delivery? (1) for yes, (2) for no.");
deliveryWantedInput = keyboard.nextInt();
if(deliveryWantedInput == 1){
deliveryOutput = numberOfPizzas + "deliverd";
if(costOfPizzaWithTax < 10){
deliveryCharge = 3;
}
if( 10 < costOfPizzaWithTax && costOfPizzaWithTax < 20 ){
deliveryCharge = 2;
}
if(20 < costOfPizzaWithTax && costOfPizzaWithTax < 30){
deliveryCharge = 1;
}
if(costOfPizzaWithTax > 30){
deliveryCharge = 0;
}
}
else{
deliveryOutput = "no delivery";
}
//display total cost
int total = costOfPizzaWithTax + deliveryCharge;
//System.out.println("Total Due: $" + df.format(total));
if(pizzaShape == 1){
System.out.print("User requested: Rectangular, " + pizzaWidth + "X"
+ pizzaLength + ", " + dough + toppings + "topping(s)"
+ Crust + "cheesy crust," + deliveryOutput + " - Program Output:");
System.out.println("Details for - Rectangular Pizza (" + pizzaWidth + "\" X " + pizzaLength + "\"):");
System.out.println( "Area: " + area );
System.out.println( "Volume:" + doughVolume);
System.out.println( "Base price: $" + basePrice);
System.out.println( "With Cheesy: $");
System.out.println( "Multi Total: $" + pizzasBeforeTax);
System.out.println( "With Tax: $" + costOfPizzaWithTax);
System.out.println( "And Delivery: $" + total);
}
else{
System.out.print("User requested: Circular, " + pizzaDiameter + "\", " + dough + toppings + "topping(s)"
+ Crust + "cheesy crust," + deliveryOutput + " - Program Output:");
System.out.println( "Area: " + area );
System.out.println( "Volume:" + doughVolume);
System.out.println( "Base price: $" + basePrice);
System.out.println( "With Cheesy: $");
System.out.println( "Multi Total: $" + pizzasBeforeTax);
System.out.println( "With Tax: $" + costOfPizzaWithTax);
System.out.println( "And Delivery: $" + total);
}
}
2. Pizza Class and separate Main class ( pizza.java and MainClass.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";
}
}
===============================================
// MainClass.java
================================================
public class MainClass
{
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