2017-05-08 9 views
0

私は別のリンクリストに追加しようとしているこれらすべての成分を含むこの現在のクラスを持っていますが、私は苦労しています。あるクラスから別のクラスへのLinkedListの特定の要素

import java.util.*; 
public class Kitchen { 
    public static final Category CRUST = new Category("crust", 1, 1); 
    public static final Category SAUCE = new Category("sauce", 1, 1); 
    public static final Category TOPPING = new Category("topping", 2, 3); 
    private Category[] categories = { CRUST, SAUCE, TOPPING }; 
    private LinkedList<Ingredient> ingredients = new LinkedList<Ingredient>(); 

    public Kitchen() { 
     ingredients.add (new Ingredient("Thin", 3.00, CRUST)); 
     ingredients.add (new Ingredient("Thick", 3.50, CRUST)); 
     ingredients.add (new Ingredient("Tomato", 1.00, SAUCE)); 
     ingredients.add (new Ingredient("Barbeque", 1.00, SAUCE)); 
     ingredients.add (new Ingredient("Capsicum", 0.50, TOPPING)); 
     ingredients.add (new Ingredient("Olives", 1.50, TOPPING)); 
     ingredients.add (new Ingredient("Jalapenos", 1.00, TOPPING)); 
     ingredients.add (new Ingredient("Beef", 2.75, TOPPING)); 
     ingredients.add (new Ingredient("Pepperoni", 2.50, TOPPING)); 
    } 

これはすべての成分を含むクラスであり、これはいくつかの成分をコピーしようとしているクラスです。

import java.util.*; 
import java.text.*; 

public class Pizza { 
    private LinkedList<Ingredient> ingredients = new LinkedList<Ingredient>(); 
    private int sold; 

    public void add(){ 
     ... 
     ... 
      if (...); 
       ingredients.add(...); 
     ... 
    } 

キッチンの特定の成分をピザの成分に加えるにはどうすればいいですか?)私は材料自体を追加しようとしましたが、それはうまくいかないようです。 (inredients.add(成分))

答えて

0

LinkedListではなくenumを使用して、すべての成分を保存することができます。キッチンクラスの成分の列挙型を追加します。

public enum Ingredient { 
    THIN("Thin", 3.00, CRUST), 
    THICK("Thick", 3.50, CRUST); 
    // add other ingredient 
    private String name; 
    private double cost; 
    private Category category; 

    private Ingredient(String name, double cost, Category category) { 
     this.name = name; 
     this.cost = cost; 
     this.category = category; 
    } 
} 

次にクラスピザで。

public void add() { 
    ingredients.add(Ingredient.THICK); 
    // add other ingredient needed 
} 
0

あなたはキッチンが提供する成分を返しKitchenクラスにgetIngredients()メソッドを追加することができます。これにより、Pizzaクラス(およびそれ以上のクラス)からKitchenの成分にアクセスすることができます。

関連する問題