Question

I have been trying to figure out this problem for the past day and yet every tweak I make I seem to mess it up even more. I should add that I am relatively new to java and programming in general.

My problem is that in my program, I have the user input data for one of three product objects, however when I read the data for all three objects, the same data is stored in all of them. The program compiles and runs fine so I'm at a complete loss to what I've done wrong.

This is the method from the Interface class:

private void readInput() // the only method in the program that accepts product data from the user
{
    Store matesStore = new Store();
    String name;
    int demandRate, productChoice;
    double setupCost, unitCost, inventoryCost, sellingPrice;

    Scanner console = new Scanner(System.in); 
    System.out.println("Please choose a product: (1), (2), (3)"); //this is where the user chooses which product they wish to enter data for.
    productChoice = console.nextInt();
    System.out.println("Please enter the product's name: ");
    name = console.next();   
    Store.check(name);    
    //checks to make sure that the user doesn't enter the same product name more than once
    System.out.println("Please enter the product's demand rate: ");
    demandRate = console.nextInt();                         
    System.out.println("Please enter the product's setup cost: ");
    setupCost = console.nextDouble();
    System.out.println("Please enter the product's unit cost: ");
    unitCost = console.nextDouble();
    System.out.println("Please enter the product's inventory cost: ");
    inventoryCost = console.nextDouble();
    System.out.println("Please enter the product's selling price: ");
    sellingPrice = console.nextDouble();
    matesStore.addData(productChoice, name, demandRate, setupCost, unitCost, inventoryCost, sellingPrice); 
    // uses the addData() method in the Store class to set the data in the created Store object matesStore
    //continueOption();  //is meant to ask the user whether they are ready to continue but I still have some problems with it
    showInterface();
    chooseOption();
}

And here is the method from the Store class:

public static void addData(int option, String newName, int newDemand, double newSetup, double newUnit, double newInventory, double newPrice) 
//is meant to set the product data for a particular product
{
    if (option==1) setData(product1, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    else if (option==2) setData(product2, newName, newDemand, newSetup, newUnit, newInventory, newPrice);
    else /*(option==3)*/ setData(product3, newName, newDemand, newSetup, newUnit, newInventory, newPrice);      
}
private static void setData(Product product, String name, int demandRate, double setupCost, double unitCost, double inventoryCost, double sellingPrice)
//uses the Product class' mutator methods to set the data
{        
    Product.setName(name);
    Product.setDemand(demandRate);
    Product.setSetup(setupCost);
    Product.setUnit(unitCost);
    Product.setInventory(inventoryCost);
    Product.setPrice(sellingPrice);        
}

If there's any additional info you need to help, I'm happy to provide it. Any help would be greatly appreciated.

Cheers

Était-ce utile?

La solution

"Why is my code storing the same data in all three product objects?"

Looks like all your Product fields are static, since you're calling the set methods in a static way. That would cause the values to be the same in all Product objects. Do some research on the difference between static and instance

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top