2017-07-18 72 views
-1

我正在嘗試實施鞋的庫存計劃。我想要做的是給每隻鞋一個id(productId)和一定數量的鞋子(ammountToPick)。我希望能夠讓運行該程序的用戶輸入鞋號來獲取庫存中剩餘的那種鞋子的數量(ammountToPick)。我目前與我的程序有關的問題是它沒有返回任何內容,只是不斷打印「無效的產品ID」。在Java中實施鞋庫存程序時遇到困難

我提供我下面的代碼:

public class Product { 
    private String productId = ""; 
    private int ammountToPick = 0; 
    private int ammountToRestock = 0; 

    public Product (String productId, int ammountToPick){ 

    this.productId = productId; 
    this.ammountToPick = ammountToPick; 

    } 

    public String getProductId() { 
    return productId; 
    } 

    public int getAmmountToPick() { 
    return ammountToPick; 
    } 

} 

public class Shoes extends Product{ 
    public Shoes(String productId, int ammountToPick){ 
    super(productId, ammountToPick); 

    } 


} 

import java.util.Scanner; 
public class Inventory 
{ 
    private static String productId = ""; 
    private int ammountToPick = 0; 
    private int ammountToRestock = 0; 
    public static final int MAX_ITEMS = 999999; 
    private static Product product [] = new Shoes[MAX_ITEMS]; 

    public static void main (String args[]){ 

    buildInventory(); 
    getInventory(); 
} 

public static void buildInventory(){ 

product[1] = new Shoes("shoe101", 19); 
product[2] = new Shoes("shoe200", 1); 
product[3] = new Shoes("shoe420", 9); 
} 

public static void getInventory() { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter the product id of the product you would like to pick: "); 
    String userinput = input.nextLine(); 
    if(userinput.equals(productId)) { 
    System.out.println("there are" + product[1].getAmmountToPick() +"left"); 
    } 
    else { 

    System.out.println("invalid product id "); 
    } 
} 
} 
+1

您絕不會將productId初始化爲除空字符串外的任何內容,因此它始終是空字符串。 – Malphrush

+0

我已經更新了我的回答 – ControlAltDel

回答

1
if(userinput.equals(productId)) { 
    System.out.println("there are" + product[1].getAmmountToPick() +"left"); 
    } 
else { 

在第一行,你的問題是,ProductID等於在類的頂部設置爲空字符串。它應該實際上工作,如果你只是在沒有輸入實際的productId和userinput爲「」的情況下回車。但是,爲了使這項工作,你所希望的方式,擺脫你的productId參數變量,並檢查是否userinput您的數組中的項目之一,包含一個productId匹配

你需要做的

userinput = ...; //this part is fine 
for (Product p : products) { 
    if (userinput.equals(p.getProductId())) { 
    System.out.println('there are ' + p.getAmmountToPick() + " left"); 
    } 
} 
+0

真棒謝謝你! – chris2656

0

無論是ProductInventory類有productId成員變量。您將userInputInventory類中的private staticproductId進行比較,該類爲空字符串。

我認爲你應該遍歷你的庫存數組,尋找匹配的productId

下面是與增強環和沒有新的方法來做到這一點的一種方法:

boolean found = false; 
for (final Product aProduct: product) { 
    if(userInput.equals(aProduct.getProductId())) { 
     System.out.println("there are" + aProduct.getAmmountToPick() +"left"); 
     found = true; 
     break; 
    } 
} 
if(!found) { 
    System.out.println("invalid product id "); 
} 

我的product成員變量重命名爲products所以它的意圖是清晰的。它會使代碼更清晰。

Inventory,您使用它的方式,不需要productId。它應該被刪除。

+0

我將如何去遍歷庫存數組? – chris2656

+0

是這樣的嗎? public static void getInventory(){ Scanner input = new Scanner(System.in);對於(int i = 0; i chris2656

+0

您的解決方案不考慮負面結果,如果找不到匹配結果。 – user6629913