Prime b

Prime b

  • NA
  • 810
  • 346k

Work with classes

Mar 20 2012 10:55 AM
Vulpes, why do I always get the same price for any size of pizza?
This is like pizza store program.


============Pizza class========================== that's where i have problems i always get the price of the small pizza..


import java.text.NumberFormat;
import java.util.ArrayList;

/**
 * This class represents Pizzas. It uses two public enums, one to represent
 * the size of the pizza, the other to represent the crust for the pizza.
 * It keeps the toppings in a list, each topping is represented as a string.
 *
 * @author Mr. Bryan
 */
public class Pizza
{

  //these are enumerations, programers use them to create data types that
  //are not usually part of a language, like size, and crust!
  public enum Size { SMALL, MEDIUM, LARGE, XTRA_LARGE };
  public enum Crust { THIN, HAND_TOSSED, DEEP_DISH };

  //used to hold all of the toppings - We learn about ArrayList in chapter 8
  private ArrayList<String> toppings;

  private Size size;
  private Crust crust;

  /**
  * Creates new Pizza objects with initial values for the size and crust
  *
  * @param size The initial size of this pizza object
  * @param crust The initial crust of this pizza object
  */
  public Pizza(Size size, Crust crust)
  {
  toppings = new ArrayList<String>();
  this.size = size;
  this.crust = crust;
  }

  /**
  * This method allows the pizza size to be changed after the pizza object
  * has been created.
  * @param size The new size type for the pizza
  */
  public void changeSize(Size size)
  {
  this.size = size;
  }

  /**
  * This method allows the pizza crust type to be changed after the pizza
  * object has been created.
  * @param crust The new crust type for the pizza
  */
  public void changeCrust(Crust crust)
  {
  this.crust = crust;
  }

  /**
  * This method adds new toppings to the pizza. Toppings are represented
  * by Strings, duplicates are ignored.
  * @param topping The new topping as a string
  */
  public void addTopping(String topping)
  {
  toppings.add(topping);
  }

  /**
  * This method will remove toppings from the pizza. If the topping is not
  * found (possible if topping is  miss-spelled), nothing happens.
  * @param topping The topping to be removed from the pizza
  */
  public void removeTopping(String topping)
  {
  toppings.remove(topping);
  }

  /**
  * This method adds up the total cost of this pizza object and
  * returns the value according to the following chart:
  * Small : 7.99
  * Medium : 8.99
  * Large : 9.99
  * XTRA_LARGE : 10.99
  * Deep Dish : add 1.00
  * Each topping : add .50
  *
  * @return The current price of this pizza object
  */
  public double getPrice()
  {
  Crust deepDish = Crust.DEEP_DISH;
  Size small = Size.SMALL;
  Size medium = Size.MEDIUM;
  Size large = Size.LARGE;
  Size xlarge = Size.XTRA_LARGE;
 
  if (small == Size.SMALL)
  {
  return 7.99;
  }
  if(medium == Size.MEDIUM)
  {
  return 8.99;
  }
  if(large == Size.LARGE)
  {
  return  9.99;
  }
  if(xlarge == Size.XTRA_LARGE)
  {
  return 10.99;
  }
  return 0;
 
  }


  @Override
  public String toString()
  {
  NumberFormat fmt = NumberFormat.getCurrencyInstance();
  String pizza = size + " " + crust + " pizza:\n";
  for (String topping : toppings)
  {
  pizza += "\t" + topping + "\n";
  }
  pizza += fmt.format(getPrice());
  return pizza;
  }
}



==================My main method===== where i add toppings and different pizzas

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author trowella8418
 */
public class PizzaShop
{
 
  /**
  * @param args the command line arguments
  */
  public static void main(String[] args)
  {
  Pizza firstPizza = new Pizza(Pizza.Size.SMALL,Pizza.Crust.HAND_TOSSED);
  Pizza secondPizza = new Pizza(Pizza.Size.XTRA_LARGE,Pizza.Crust.DEEP_DISH);
 
  firstPizza.addTopping("Onions");
  firstPizza.addTopping("Green Peppers");
  firstPizza.addTopping("Mushrooms");
  System.out.println(firstPizza);
 
  secondPizza.addTopping("pepperoni");
  secondPizza.addTopping("sausage");
  secondPizza.addTopping("bacon");
  secondPizza.addTopping("chicken");
  secondPizza.addTopping("ham");
  System.out.println(secondPizza);
  }
}





Answers (4)