2017-03-01 1 views
0

학교 프로젝트에 저를 도울 수 있다면 좋겠다고 생각했습니다. 나는 사용자가 찬장에 어떤 식료품 점을 입력하는지 코드를 작성 중이다. 또한 제품 종류, 이름, 구입처, 레이블, 가격, 수량 등을 지정합니다. 이는 인벤토리와 같습니다. 나는 Dairy라는 클래스 이름을 가지고 있으며, 나는 별도로 필요한 모든 입력에 대해 getter를 만들 수 있습니다. 그게 잘 작동, 그냥 통과 매개 변수를 가지고 InputDairy 메서드에서 getter를 호출하면 잘 작동합니다.하나의 Setter로 여러 개의 입력이 있습니다.

하지만 사용자에게 제품에 대한 많은 질문을하므로 10 명의 게터를 갖고 싶지 않으므로 하나만 가지고 매개 변수를 전달하는 방법이 있습니까? 여기 아래의 코드는 작동하지 않는 코드입니다. 하나의 getter에서 모든 매개 변수를 설정 한 다음 하나씩 매개 변수를 전달하려고했습니다. 그리고 그것은 작동하지 않습니다. 이것이 가능합니까 아니면 내가 묻는 모든 질문에 게터를 설정해야합니까?

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 개의 매개 변수를 사용하는 경우이 네 개의 매개 변수를 전달해야합니다. –

답변

3

먼저 사용자의 모든 대답을 한 다음 세터 기능에 한 줄로 전달하십시오.

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

여러번 것들이 보이는 것보다 더 간단합니다. 그냥 길을 찾아야 해. :피 –

관련 문제