0

Say, my Store class has this information...

public class Store {

    private Product product1, product2, product3;
    private static int productCount = 0;

    public Store() {
        product1 = new Product();
        product2 = new Product();
        product3 = new Product();
    }

public void setData(String name, int demand, double setup, double unit,
            double inventory, double selling) {
        if (productCount == 0) {

            product1.setName(name);
            product1.setDemand(demand);
            product1.setSetUpCost(setup);
            product1.setUnitCost(unit);
            product1.setInvCost(inventory);
            product1.setSellPrice(selling);

            productCount = 1;
        } else if (productCount == 1) {

            product2.setName(name);
            product2.setDemand(demand);
            product2.setSetUpCost(setup);
            product2.setUnitCost(unit);
            product2.setInvCost(inventory);
            product2.setSellPrice(selling);

            productCount = 2;
        } else if (productCount == 3) {

            product3.setName(name);
            product3.setDemand(demand);
            product3.setSetUpCost(setup);
            product3.setUnitCost(unit);
            product3.setInvCost(inventory);
            product3.setSellPrice(selling);

            productCount = 3;

        }
    }

However, in my Interface class...

private void enterData() {
// Assume the user put in the required inputs here

Store.setData(name, demandRate, setupCost, unitCost, inventoryCost, sellingPrice );
}

When I try to use this code, the productCount variable never changes from zero. Also, when I use another method to show product data, it never shows any information like name & so on (as though nothing was ever input). Something is wrong with how I'm trying to store this information.

I want to set data for only 3 products, but my program won't store any information. One guess of mine is that I can't statically try to address a non-static method to store product information. If so, I'm confused how I could store information for ONLY three products.

On top of that (assuming you can now store product information for 3 products), I want to ask the user if they would like to return to the main menu (a .run() method) or replace product information for one of the three products (which are now full) if they try to add information more than three times. I'm not sure how I could do this?

Thanks.

  • 2
    In this case, you are accessing a static data member from an instance method, which is fine. However, your client code appears to be accessing setData() as a static method, which should throw a compiler exception. Is this the actual code you are compiling and running? – Warren Dew May 10 '14 at 07:26
  • Did your try to create a Store object and then apply setData to it? – Kostas Kryptos May 10 '14 at 07:27

1 Answers1

0

The code that you provided should not even compile.

You need something like:

public class YourInterfaceClass {

    private Store store = new Store();

    ...

    private void enterData() {
         // Assume the user put in the required inputs here
         store.setData(name, demandRate, setupCost, unitCost, inventoryCost, sellingPrice );
    }

}

Steve C
  • 18,876
  • 5
  • 34
  • 37