我希望您能帮助我完成一个学校项目。我正在编写一个代码,用户输入用户在橱柜中的食品杂货。它还指定了哪种产品,名称,购买的商店,标签,价格,数量 - 就像库存一样。我有一个类名称乳制品,可以为我需要的每个输入创建一个getter。这很好,我设置了它,有一个传递参数,当我在InputAry方法中调用Getter时,它可以正常工作。
但是,由于我问用户关于产品的很多问题,所以我不想拥有10个getters,只有一个方法可以拥有一个,然后传递参数?这里的代码是不工作的代码,我尝试将所有参数设置为一个getter,然后将参数仅一个一个一个。而且那行不通。这是可能的还是我必须为我提出的每个问题设置一个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;
}
}
首先从用户那里获取所有答案,然后将它们传递给您的二线功能。
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);
}
}