我正在尝试实施鞋的库存计划。我想要做的是给每只鞋一个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 ");
}
}
}
您绝不会将productId初始化为除空字符串外的任何内容,因此它始终是空字符串。 – Malphrush
我已经更新了我的回答 – ControlAltDel