这是我目前的代码。我需要帮助让设置列表在比萨饼和测试代码中工作。
package pizza;
import java.util.EnumSet;
import java.util.Set;
import pizza.Pizza.Crust;
import pizza.Pizza.Size;
import pizza.Pizza.Topping;
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 Topping{
MUSHROOMS,
GREEN_PEPPERS,
HAM,
PEPPERONI,
SAUGSAGE
}
// declare variables
private Size pizzaSize;
private Crust crustType;
private Set<Topping> setOfToppings = EnumSet.noneOf(Topping.class);
public Pizza(){
}
public void addTopping(Topping topping) {
setOfToppings.add(topping);
}
public Set<Topping> getToppings() {
return setOfToppings;
}
public Pizza(Size pizzaSize, Crust
crustType, Topping greenPeppers, Topping saugsage, Topping
pepperoni) {
this.pizzaSize = pizzaSize;
this.crustType = crustType;
}
public Pizza(Size large, Crust cheesy, Topping greenPeppers) {
// TODO Auto-generated constructor stub
}
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 String toString(){
return "A "+pizzaSize+" Pizza with "+ crustType +" crust" + "with " +
setOfToppings + "Toppings";
}
}
这是我的TestCode
package pizza;
import pizza.Pizza.Crust;
import pizza.Pizza.Size;
import pizza.Pizza.Topping;
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);
vegieCrunch.addTopping(Topping.MUSHROOMS);
// constructor 2
Pizza doubleCheese = new Pizza(Size.LARGE, Crust.CHEESY, Topping.GREEN_PEPPERS);
Pizza PartyPizza = new Pizza(Size.JUMBO, Crust.DEEP_PAN, Topping.HAM,
Topping.SAUGSAGE, Topping.PEPPERONI);
// use of getters
System.out.println("Pizza Vegie Crunch::");
System.out.println("Size: "+vegieCrunch.getPizzaSize());
System.out.println("Crust Type: "+vegieCrunch.getCrustType());
System.out.println("Toppings Chosen: " + vegieCrunch.getToppings());
// use of toString
System.out.println("\nPizza Double Cheese:");
System.out.println(doubleCheese.toString());
System.out.println("\nPizza Party Pizza:");
System.out.println(PartyPizza.toString());
}
}
到目前为止,它编译并运行良好,但我没有为用户添加或删除浇头选项。我真的需要帮助,谢谢大家!
答案 0 :(得分:0)
所以这里有你的顶级方法:
public void addTopping(Topping topping) {
setOfToppings.add(topping);
}
public void removeTopping(Topping topping) {
setOfToppings.remove(topping);
}
public void addToppings(Topping... toppings) {
setOfToppings.addAll(Arrays.asList(toppings));
}
public Set<Topping> getToppings() {
return setOfToppings;
}
以下是您在测试代码中使用它们的方法:
// use constructor 1
Pizza vegieCrunch = new Pizza();
vegieCrunch.setCrustType(Crust.THIN_AND_CRISPY);
vegieCrunch.setPizzaSize(Size.MEDIUM);
//////////////////////////////////////////////////////////////
vegieCrunch.addTopping(Topping.MUSHROOMS);
vegieCrunch.addTopping(Topping.HAM);
vegieCrunch.addToppings(Topping.GREEN_PEPPERS, Topping.PEPPERONI);
vegieCrunch.removeTopping(Topping.HAM);
//////////////////////////////////////////////////////////////