Search code examples
javaparameter-passinguser-inputgetter

Multiple inputs with just one Setter


I was hoping if you could help me with a school project. I am writing a code where the user inputs what groceries the user has in the Cupboard. It also specifies what kind of product it is, the name, store where it was bought, label, price, quantity - it's like an inventory. I have a class name Dairy and I can create a getter for every input I need separately. That works just fine I set it up, have a passing parameter and when I call the getter in the InputDairy method it works just fine.

But since I am asking the user a lot of question about the product I don't want to have 10 getters is there a way of having just one and then passing the parameters? The code down here is the non working one, where I tried setting all the parameters in one getter and then passing the parameters just one by one. And that doesn't work. Is this possible or do I have to set a getter for every question I ask?

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;

    }

}

Solution

  • First take all the answers from user, and then pass these to your setter function in one line.

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