【问题标题】:Trouble implementing Shoe Inventory program in Java在 Java 中实现 Shoe Inventory 程序时遇到问题
【发布时间】: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 初始化为任何内容,因此它始终是空字符串。
  • 我已经更新了答案

标签: java inventory


【解决方案1】:
if(userinput.equals(productId)) {
    System.out.println("there are" + product[1].getAmmountToPick() +"left");
  }
else {

在第一行,您的问题是 productId 在类的顶部设置为空字符串。如果您只是按 Enter 键而不输入实际的 productId 并且用户输入是“”,它实际上应该可以工作。但是要使这项工作按照您想要的方式进行,请摆脱您的 productId 变量并检查用户输入是否与您的数组中某个项目的 productId 匹配

你需要做的

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

【讨论】:

  • 太棒了,非常感谢!
【解决方案2】:

ProductInventory 类都有 productId 成员变量。您将userInputInventory 类中的private static productId 进行比较,这是一个空字符串。

我认为您应该遍历您的库存数组,以寻找与productIdProduct 匹配。

这是一种使用增强循环且没有新方法的方法:

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。它应该被删除。

【讨论】:

  • 我将如何遍历库存数组?
  • 这样的? public static void getInventory() { Scanner input = new Scanner(System.in); for(int i = 0; i
  • 您的解决方案没有考虑负面结果,即找不到匹配项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 1970-01-01
相关资源
最近更新 更多