为什么我的代码在所有三个产品对象中存储相同的数据?

时间:2014-05-08 02:57:49

标签: java class object methods getter

过去一天我一直试图弄清楚这个问题但是我做的每一次调整我似乎都搞砸了。我应该补充一点,我对java和编程相对较新。

我的问题是,在我的程序中,我有三个产品对象之一的用户输入数据,但是当我读取所有三个对象的数据时,相同的数据存储在所有这三个对象中。该程序编译并运行正常,所以我完全不知道我做错了什么。

这是Interface类的方法:

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

以下是Store类的方法:

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

如果您需要提供任何其他信息,我很乐意提供。任何帮助将不胜感激。

干杯

1 个答案:

答案 0 :(得分:0)

  

"为什么我的代码在所有三个产品对象中存储相同的数据?"

您的所有Product字段看起来都是static,因为您以静态方式调用set方法。这将导致所有Product个对象中的值相同。对difference between static and instance

进行一些研究