【发布时间】:2017-07-18 17:35:20
【问题描述】:
我正在尝试为鞋子实施库存计划。我想要做的是给每只鞋子一个 id (productId) 和库存鞋子的数量 (ammountToPick)。我希望能够要求运行程序的用户输入鞋子 id 以获取库存中剩余的那种鞋子的数量(ammountToPick)。我目前的程序问题是它没有返回任何内容,只是不断打印“无效的产品 ID”。
我在下面提供了我的代码:
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 初始化为任何内容,因此它始终是空字符串。
-
我已经更新了答案