2016-04-11 7 views
1

私は3つのクラスファイルを扱っています。ファイル1の私のコードの一部が、製品データベースfile3がいっぱいであるかどうかを調べるファイル2からコードを呼び出しています。私はfalseを返すdatabaseFullメソッドを期待していますが、それはデータベースに単一の項目だけがある場合でもtrueを返すようです。
なぜこのようなことが起こっているのか、助けてもらえますか?
編集:ブール値は常にtrueに戻りますか?

public class StarberksInterface 
{ 
Scanner console = new Scanner(System.in); 

public static void main(String[] args) 
{ 
    StarberksInterface intFace = new StarberksInterface(); 
    intFace.run(); 
} 

private void run() 
{ 
    int option, demandRate, choice; 
    double setupCost, unitCost, inventoryCost, sellPrice; 
    String productName, newProductName, deleteName; 
    Store store = new Store(); 

    do { 
     System.out.println("(1) Input data for one product\n(2) Show data from one product\n(3) Show product replenishment strategy\n(4) Exit"); 
     option = console.nextInt(); 
     switch(option) 
     { 
      case 1: 
       System.out.println("Enter product name (3 to 10 characters): "); 
       productName = console.next().toLowerCase(); 

       while(productName.length()>10 || productName.length()<3){ 
        System.out.println("Product name length is not valid. Try again."); 
        productName = console.next().toLowerCase(); 
       } 

       if (store.databaseFull()){ 
        System.out.println("Database is full:\n(1) Delete a product\n(2) Menu"); 
        choice = console.nextInt(); 
        if (choice == 1){ 
         System.out.println("Enter name of product to delete: "); 
         deleteName = console.next().toLowerCase(); 
         store.deleteProduct(deleteName); 
         System.out.println("Product deleted."); 
         break; 
        } 
        else{ 
         break; 
        } 
       } 

       if (store.productExists(productName)){ 
        System.out.println("Product already exists:\n(1) Change the product name\n(2) Change product data\n(3) Menu"); 
        choice = console.nextInt(); 
        if (choice == 1){ 
         System.out.println("Enter new product name: "); 
         newProductName = console.next().toLowerCase(); 
         while(newProductName.length()>10 || newProductName.length()<3){ 
          System.out.println("Product name length is not valid. Try again."); 
          newProductName = console.next().toLowerCase(); 
         } 
         store.changeName(productName, newProductName); 

        }else if (choice == 2){ 
         System.out.println("Enter new demand rate: "); 
         demandRate = console.nextInt(); 
         demandRate = checkInput(demandRate); 

         System.out.println("Enter new setup cost: "); 
         setupCost = console.nextDouble(); 
         setupCost = checkInput(setupCost); 

         System.out.println("Enter new unit cost: "); 
         unitCost = console.nextDouble(); 
         unitCost = checkInput(unitCost); 

         System.out.println("Enter new inventory cost: "); 
         inventoryCost = console.nextDouble(); 
         inventoryCost = checkInput(inventoryCost); 

         System.out.println("Enter new sell price: "); 
         sellPrice = console.nextDouble(); 
         sellPrice = checkInput(sellPrice); 

         System.out.println("Data changed."); 

         store.changeData(productName, demandRate, setupCost, unitCost, inventoryCost, sellPrice);      
        } 
        else{ 
         break; 
        } 
       } 
       else{ 
        // user inputs 
        System.out.println("Enter demand rate: "); 
        demandRate = console.nextInt(); 
        demandRate = checkInput(demandRate); 

        System.out.println("Enter setup cost: "); 
        setupCost = console.nextDouble(); 
        setupCost = checkInput(setupCost); 

        System.out.println("Enter unit cost: "); 
        unitCost = console.nextDouble(); 
        unitCost = checkInput(unitCost); 

        System.out.println("Enter inventory cost: "); 
        inventoryCost = console.nextDouble(); 
        inventoryCost = checkInput(inventoryCost); 

        System.out.println("Enter sell price: "); 
        sellPrice = console.nextDouble(); 
        sellPrice = checkInput(sellPrice); 

        store.addProduct(productName, demandRate, setupCost, unitCost, inventoryCost, sellPrice); 
       } 
       break; 

      case 2: 
       System.out.println("Enter product name (3 to 10 characters): "); 
       productName = console.next().toLowerCase(); 

       while(productName.length()>10 || productName.length()<3){ 
        System.out.println("Product name length is not valid. Try again."); 
        productName = console.next().toLowerCase(); 
       } 

       while (!store.productExists(productName)){ 
        System.out.println("This product does not exist, try again."); 
        productName = console.next().toLowerCase(); 

        while(productName.length()>10 || productName.length()<3){ 
         System.out.println("Product name length is not valid. Try again."); 
         productName = console.next().toLowerCase(); 
        } 
       } 

       //at this point, the product entered will exist in the store, so you will be able to show the product details 
       showData(store.getProduct(productName)); 

       break; 

      /**case 3: System.out.println 
      * 
      * 
      * 
       break;**/ 

      case 4: break; 

      default: System.out.println("invalid option"); 
     } 
    } 
    while(option!=4); 
} 

public void option1(){ 

} 

public void option2(){ 

} 

public void option3(){ 

} 

public void option4(){ 

} 

// This function checks integer inputs for negative numbers and returns a message and/or the input. 
public int checkInput(int input){ 

    while(input < 0){ 
     System.out.println("Cannot input negative number. Try again."); 
     input = console.nextInt(); 
    } 

    return input; 
} 

// This function checks double inputs for negative numbers and returns a message and/or the input. 
public double checkInput(double input){ 

    while(input < 0){ 
     System.out.println("Cannot input negative number. Try again."); 
     input = console.nextDouble(); 
    } 

    return input; 
} 

private static void showData(Product product) 
{ 
    System.out.println("Name = "+product.getProductName()); 
    System.out.println("Demand rate = "+product.getDemandRate()); 
    System.out.println("Setup cost = "+product.getSetupCost()); 
    System.out.println("Unit cost = "+product.getUnitCost()); 
    System.out.println("Inventory cost = "+product.getInventoryCost()); 
    System.out.println("Sell price = "+product.getSellPrice()); 
} 

}

ファイル2コード:

public class Store 
{ 
private Product product1; 
private Product product2; 
private Product product3; 

public Store(){ 
    product1 = null; 
    product2 = null; 
    product3 = null; 
} 

public void addProduct(String name, int demand, double setup, double unit, double inventory, double sell){ 
    if(product1 == null){ 
     product1 = new Product(); 
     product1.setProductName(name); 
     product1.setDemandRate(demand); 
     product1.setSetupCost(setup); 
     product1.setUnitCost(unit); 
     product1.setInventoryCost(inventory); 
     product1.setSellPrice(sell); 
    } 

    if(product2 == null){ 
     product2 = new Product(); 
     product2.setProductName(name); 
     product2.setDemandRate(demand); 
     product2.setSetupCost(setup); 
     product2.setUnitCost(unit); 
     product2.setInventoryCost(inventory); 
     product2.setSellPrice(sell); 
    } 

    if(product3 == null){ 
     product3 = new Product(); 
     product3.setProductName(name); 
     product3.setDemandRate(demand); 
     product3.setSetupCost(setup); 
     product3.setUnitCost(unit); 
     product3.setInventoryCost(inventory); 
     product3.setSellPrice(sell); 
    } 
} 

public void changeName(String oldName, String newName){ 
    if(product1 != null){ 
     if(product1.getProductName().equals(oldName)) 
      product1.setProductName(newName); 
    } 

    if(product2 != null){ 
     if(product2.getProductName().equals(oldName)) 
      product2.setProductName(newName); 
    } 

    if(product3 != null){ 
     if(product3.getProductName().equals(oldName)) 
      product3.setProductName(newName); 
    } 
} 

public void changeData(String name, int demand, double setup, double unit, double inventory, double sell){ 
    if(product1 != null){ 
     if(product1.getProductName().equals(name)) 
      product1.setDemandRate(demand); 
      product1.setSetupCost(setup); 
      product1.setUnitCost(unit); 
      product1.setInventoryCost(inventory); 
      product1.setSellPrice(sell); 
    } 

    if(product2 != null){ 
     if(product2.getProductName().equals(name)) 
      product2.setDemandRate(demand); 
      product2.setSetupCost(setup); 
      product2.setUnitCost(unit); 
      product2.setInventoryCost(inventory); 
      product2.setSellPrice(sell); 
    } 

    if(product3 != null){ 
     if(product3.getProductName().equals(name)) 
      product3.setDemandRate(demand); 
      product3.setSetupCost(setup); 
      product3.setUnitCost(unit); 
      product3.setInventoryCost(inventory); 
      product3.setSellPrice(sell); 
    } 
} 

public boolean productExists(String name){ 
    if (product1 != null){ 
     if (name.equals(product1.getProductName())) 
      return true; 
    } 

    if (product2 != null){ 
     if (name.equals(product2.getProductName())) 
      return true; 
    } 

    if (product3 != null){ 
     if (name.equals(product3.getProductName())) 
      return true; 
    } 

    return false; 
} 

public Product getProduct(String name){ 
    if (product1 != null){ 
     if (name.equals(product1.getProductName())) 
      return product1; 
    } 

    if (product2 != null){ 
     if (name.equals(product2.getProductName())) 
      return product2; 
    } 

    if (product3 != null){ 
     if (name.equals(product3.getProductName())) 
      return product3; 
    } 

    return null; 
} 

public boolean databaseFull(){ 
    return (product1 != null && product2 != null && product3 != null);  
} 

public void deleteProduct(String name){ 
    if (name.equals(product1.getProductName())){ 
     product1 = null; 
    } 

    if (name.equals(product2.getProductName())){ 
     product2 = null; 
    } 

    if (name.equals(product3.getProductName())) 
     product3 = null; 
} 

}

ファイルの要求に応じて、私は私のコード

ファイル1つのコードのすべてを貼り付けています3コード:

public class Product 
{ 
private String name; 
private int demand; 
private double setup, unit, inventory, sell; 

public Product() 
{ 
    name = "NO NAME"; 
    demand = 0; 
    setup = 0; 
    unit = 0; 
    inventory = 0; 
    sell = 0; 
} 

public void setProductName(String productName) 
{ 
    name = productName; 
} 

public void setDemandRate(int demandRate) 
{ 
    demand = demandRate; 
} 

public void setSetupCost(Double setupCost) 
{ 
    setup = setupCost; 
} 

public void setUnitCost(Double unitCost) 
{ 
    unit = unitCost; 
} 

public void setInventoryCost(Double inventoryCost) 
{ 
    inventory = inventoryCost; 
} 

public void setSellPrice(Double sellPrice) 
{ 
    sell = sellPrice; 
} 

public String getProductName() 
{ 
    return name; 
} 

public int getDemandRate() 
{ 
    return demand; 
} 

public double getSetupCost() 
{ 
    return setup; 
} 

public double getUnitCost() 
{ 
    return unit; 
} 

public double getInventoryCost() 
{ 
    return inventory; 
} 

public double getSellPrice() 
{ 
    return sell; 
} 

}

+1

デバッグはあなたを助けるかもしれません。私たちはあなたのDB側については全く知らない。 –

+1

product1-2-3とは何ですか?クラス全体を投稿してください。 –

+0

Storeクラスはコンパイルされないので、メソッドがtrueを返すかどうかは非常に疑問です – Stultuske

答えて

1

あなたがnew Product()で製品1、product2とproduct3を設定しているaddProduct()メソッドに渡す初めて。

product1 != null戻りtrue

product2 != null戻りtrue

product3 != null戻りtrue

:だから後で
public void addProduct(String name, int demand, double setup, double unit, double inventory, double sell){ 
    if(product1 == null){ 
     product1 = new Product(); 
     //... 
    } 

    if(product2 == null){ 
     product2 = new Product(); 
     //.. 
    } 

    if(product3 == null){ 
     product3 = new Product(); 
     //.. 
    } 
} 

は、これらすべての3つの属性は、有効な 人気商品のインスタンスを参照しているので、あなたは常にあなたの店で3つの製品を作成するため

その後

(product1 != null && product2 != null && product3 != null)を返します。 (ところで、あなたが同じ3回を作成している)

これを試してみてください:

public void addProduct(String name, int demand, double setup, double unit, double inventory, double sell){ 
    if(product1 == null){ 
     product1 = new Product(); 
     //... 
    }else if(product2 == null){ 
     product2 = new Product(); 
     //... 
    }else if(product3 == null){ 
     product3 = new Product(); 
     //... 
    } 
} 
+0

あなたの言っていることについて私は完全にはわかりません。 3つの製品すべてが初期化されているので、もはやnullではありませんか? 私のaddProductのif文を変更すると、他にこの問題が修正されますか? また、3人のユーザーのすべてがユーザーの入力値を使用していますか? – jeffbobmate

+0

私の更新を見て... – Nirekin

+0

優秀!すべてが今働くようです。どうもありがとう。 – jeffbobmate

関連する問題