Hello,
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.
Anyway 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); 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) //sets 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
Edit: The problem I had was with static variables and methods. Any one having similar problems should read about the difference between static and instance variables and methods.