2017-03-01 2 views
0

学校のプロジェクトで私を助けることができたら嬉しいです。私はユーザが食卓に食べ物の種類を入力するコードを書いています。また、商品の種類、名前、購入店、ラベル、価格、数量なども指定します。在庫のようなものです。私はクラス名Dairyを持っています。私は別々に必要なすべての入力に対してゲッターを作成できます。それはちょうどうまく動作し、私はそれを設定し、渡すパラメータを持っていると私はInputDairyメソッドでゲッターを呼び出すときはうまく動作します。複数の入力が1つのセッターで

しかし、私は10人のゲッタを持ちたくないという製品に関する多くの質問をしているので、ただ1つのパラメータを渡す方法がありますか?ここでのコードは、私が1つのゲッターですべてのパラメーターを設定し、次にパラメーターを1つずつ渡すことを試みた非機能のものです。そして、それは動作しません。これは可能ですか、私が尋ねるすべての質問に対してゲッターを設定する必要がありますか?

public static void InputDairy (Dairy DairyProducts[], Fruit FruitProducts[], Scanner input){ // START OF INPUT DAIRY 

    System.out.println("Let's put in some DAIRY products"); 

    for (int i = 0; i < DairyProducts.length; i++){ 

     System.out.println("Enter name of DIARY product"); 
     String a = input.next(); 
     DairyProducts[i].setDairyData(a); 

     System.out.println("Enter Store where you bought DIARY product"); 
     String a = input.next(); 
     DairyProducts[i].setDairyData(b); 

     System.out.println("Enter how much you paid for DIARY product"); 
     String a = input.next(); 
     DairyProducts[i].setDairyData(c); 

     System.out.println("Enter when did you buy DIARY product"); 
     String a = input.next(); 
     DairyProducts[i].setDairyData(d); 
    } 

} // END OF INPUT DAIRY enter code here 


public class Dairy { 
    private String D_NameOfDairy; 
    private String D_NameOfStore; 
    private int D_PricePaid; 
    private int D_DayPurchased; 
    private int D_BestBefore; 
    private int D_AmountInGrams; 
    private int D_Pieces; 

    public Dairy() { 

     D_NameOfDairy = "Default name of dairy"; 
     D_NameOfStore = "Default name of store for dairy"; 
     D_PricePaid = 0; 
     D_DayPurchased = 01/01/2000; 
     D_BestBefore = 10/10/2010; 
     D_AmountInGrams = 0; 
     D_Pieces = 0; 
    } 

    public void setDairyData (String a, String b, int c, int d){ 

     D_NameOfDairy = a; 
     D_NameOfStore = b; 
     D_PricePaid = c; 
     D_DayPurchased = d; 

    } 

} 
+0

メソッドが4つのパラメータをとっている場合は、これらの4つのパラメータを渡すだけです。 –

答えて

3

最初にすべての回答をユーザーから受け取り、これをセッター機能に1行で渡します。

public static void InputDairy (Dairy DairyProducts[], Fruit FruitProducts[], Scanner input){ 

    System.out.println("Let's put in some DAIRY products"); 

    for (int i = 0; i < DairyProducts.length; i++){ 

     System.out.println("Enter name of DIARY product"); 
     String a = input.next(); 

     System.out.println("Enter Store where you bought DIARY product"); 
     String b = input.next(); 

     System.out.println("Enter how much you paid for DIARY product"); 
     String c = input.next(); 

     System.out.println("Enter when did you buy DIARY product"); 
     String d= input.next(); 

     DairyProducts[i].setDairyData(a,b,c,d); 
    } 

} 
+0

OMGのような簡単な解決策です!ありがとう、私は今私の間違いを見ることができます。 – Ingrid

+0

多くのことが、見た目よりも簡単です。方法を見つけなければならない。 :p –

関連する問題