2014-10-18 49 views
0

這是我目前的代碼。我需要獲得幫助才能使集合列表在比薩和測試代碼中工作。需要幫助寫一套我的披薩代碼

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()); 

} 

} 

到目前爲止,編譯和運行良好但我沒有添加或刪除澆頭選項的用戶。我真的需要幫助,謝謝大家!

+2

你的問題還是有點廣闊。什麼讓你特別困惑?正如你所寫的,你的問題有點瑣碎,因爲它基本上是「這裏是我的代碼,這是我的要求」,沒有具體的可回答的問題。你可以做得更好。 – 2014-10-18 23:50:03

+0

對不起,我在前一個關於相同代碼的問題上與另一個人交談,我應該更具體。我一直在試圖爲我的澆頭添加一套,我需要爲用戶編寫一個添加和刪除選項,這就是我對如何做的困惑。感謝您注意到此問題 – InNeedOfHelp 2014-10-19 00:07:59

+0

您是否解決了該問題? – aioobe 2014-10-29 16:39:38

回答

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); 
//////////////////////////////////////////////////////////////