I am expanding my pizza class, I need some help with this!! Here is my original
ID: 3566352 • Letter: I
Question
I am expanding my pizza class, I need some help with this!! Here is my original code, please help me off of this!!
package pizza;
import java.util.HashSet;
import java.util.Set;
public class Pizza {
// Declare enums
public enum Size{
SMALL,
MEDIUM,
LARGE,
JUMBO
}
public enum Crust{
CHEESY,
HAND_TOSSEDS,
THIN_AND_CRISPY,
DEEP_PAN
}
public enum Toppings{
PEPPERONI,
CHEESE,
ANCHOVIES,
SAUSAGE,
PINEAPPLE,
HAM,
PEPPERS
}
// declare variables
private Size pizzaSize;
private Crust crustType;
private Set pizzaToppings = new HashSet();
public Pizza(){
}
public Pizza(Size pizzaSize, Crust crustType) {
this.pizzaSize = pizzaSize;
this.crustType = crustType;
}
public Size getPizzaSize() {
return pizzaSize;
}
public void setPizzaSize(Size pizzaSize) {
this.pizzaSize = pizzaSize;
}
public Crust getCrustType() {
return crustType;
}
public void setCrustType(Crust crustType) {
this.crustType = crustType;
}
public void addTopping(Toppings topping){
this.pizzaToppings.add(topping);
}
public Set getToppings(){
return pizzaToppings;
}
public void removeTopping(Toppings topping){
this.pizzaToppings.remove(topping);
}
public String toString(){
return "A "+pizzaSize+" Pizza with "+crustType+" crust and" + " toppings: " + pizzaToppings ;
}
}
package pizza;
import pizza.Pizza.Crust;
import pizza.Pizza.Size;
import pizza.Pizza.Toppings;
public class PizzaTest {
public static void main(String[] args) {
// use constructor 1
Pizza vegieCrunch = new Pizza();
vegieCrunch.setCrustType(Crust.THIN_AND_CRISPY);
vegieCrunch.setPizzaSize(Size.MEDIUM);
// constructor 2
Pizza doubleCheese = new Pizza(Size.LARGE, Crust.CHEESY);
Pizza PartyPizza = new Pizza(Size.JUMBO, Crust.DEEP_PAN);
PartyPizza.addTopping(Toppings.PEPPERONI);
PartyPizza.addTopping(Toppings.HAM);
// use of getters
System.out.println("Pizza Vegie Crunch:");
System.out.println("Size: "+vegieCrunch.getPizzaSize());
System.out.println("Crust Type: "+vegieCrunch.getCrustType());
// use of toString
System.out.println(" Pizza Double Cheese:");
System.out.println(doubleCheese.toString());
System.out.println(" Pizza Party Pizza:");
System.out.println(PartyPizza.toString());
}
}
Explanation / Answer
Please see below for the changes: The two maps can be placed in Pizza.java and can change according to you.
The required methods:
public Pizza(Size pizzaSize, Crust crustType, String speciality) {
this.pizzaSize = pizzaSize;
this.crustType = crustType;
this.speciality = speciality;
}
public Set<Toppings> getToppingsFromSpeciality(String speciality) {
return order.get(speciality);
}
public String getSpecialityFromToppings(Set<Toppings> toppings) {
return order1.get(toppings);
}
public String getSpeciality() {
return speciality;
}
public void setSpeciality(String speciality) {
this.speciality = speciality;
}
Changes to toString Method:
@Override
public String toString() {
if (pizzaToppings.size() == 0) {
return "A " + pizzaSize + " Pizza with " + crustType + " crust";
}
String sp = getSpecialityFromToppings(pizzaToppings);
if (sp != null) {
return "A " + pizzaSize + " Pizza with " + crustType + " crust and" + " toppings: " + pizzaToppings
+ ". Speciality = " + sp;
}
return "A " + pizzaSize + " Pizza with " + crustType + " crust and" + " toppings: " + pizzaToppings;
}
Changes to TEST Class Method:
public static void main(String[] args) {
// use constructor 1
Pizza vegieCrunch = new Pizza();
vegieCrunch.setCrustType(Crust.THIN_AND_CRISPY);
vegieCrunch.setPizzaSize(Size.MEDIUM);
// constructor 2
Pizza doubleCheese = new Pizza(Size.LARGE, Crust.CHEESY);
// constructor 3
Pizza vegCheese = new Pizza(Size.LARGE, Crust.CHEESY, "Vegetarian");
Pizza PartyPizza = new Pizza(Size.JUMBO, Crust.DEEP_PAN);
PartyPizza.addTopping(Toppings.PEPPERONI);
PartyPizza.addTopping(Toppings.HAM);
// use of getters
System.out.println("Pizza Vegie Crunch-");
System.out.println("Size: " + vegieCrunch.getPizzaSize());
System.out.println("Crust Type: " + vegieCrunch.getCrustType());
// use of getters
System.out.println("Pizza Veg Cheese-");
System.out.println("Size: " + vegCheese.getPizzaSize());
System.out.println("Crust Type: " + vegCheese.getCrustType());
System.out.println("Speciality: " + vegCheese.getSpeciality());
Set<Toppings> toppings = vegCheese.getToppingsFromSpeciality(vegCheese.getSpeciality());
System.out.print("Toppings with Speciality are: ");
for (Toppings s : toppings) {
System.out.print(s + " ");
}
System.out.println();
// use of toString
System.out.println(" Pizza Double Cheese:");
System.out.println(doubleCheese.toString());
System.out.println(" Pizza Party Pizza:");
System.out.println(PartyPizza.toString());
System.out.println(" Veg Pizza:");
System.out.println(vegCheese.toString());
}
private static final Map<String, Set<Toppings>> order = new HashMap<String, Set<Toppings>>() {
{
put("Meatlovers", new HashSet(Arrays.asList(Toppings.SAUSAGE, Toppings.CHEESE)));
put("Vegetarian", new HashSet(Arrays.asList(Toppings.PEPPERONI, Toppings.CHEESE)));
put("Hawaiian", new HashSet(Arrays.asList(Toppings.PEPPERONI, Toppings.HAM)));
put("Supreme", new HashSet(Arrays.asList(Toppings.PINEAPPLE, Toppings.CHEESE)));
}
};
private static final Map<Set<Toppings>, String> order1 = new HashMap<Set<Toppings>, String>() {
{
put(new HashSet(Arrays.asList(Toppings.SAUSAGE, Toppings.CHEESE)), "Meatlovers");
put(new HashSet(Arrays.asList(Toppings.PEPPERONI, Toppings.CHEESE)), "Vegetarian");
put(new HashSet(Arrays.asList(Toppings.PEPPERONI, Toppings.HAM)), "Hawaiian");
put(new HashSet(Arrays.asList(Toppings.PINEAPPLE, Toppings.CHEESE)), "Supreme");
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.