2017-05-09 114 views
-2

我有一个小的库存程序。下面列出了三个类。我认为,当我添加一个项目它正常工作,但是当我选择视图选项,它不打印出我想要的。程序没有正确打印

InventoryApp.java

package cockeb; 

import java.io.IOException; 
import java.util.Scanner; 
import java.math.BigDecimal; 

public class InventoryApp { 

public static void main(String[] args) throws IOException, ClassNotFoundException { 

    while (true) { 
     System.out.println(""); 
     System.out.println("\"add\" - to add a product"); 
     System.out.println("\"remove\" - to remove a product"); 
     System.out.println("\"update\" - to update a product"); 
     System.out.println("\"view\" - to view a product"); 
     System.out.println("\"quit\" - to quit the menu"); 
     System.out.println(""); 

     System.out.print("Please Enter Your Choice: "); 
     Scanner userInput = new Scanner(System.in); 

     String stringInput; 
     stringInput = userInput.nextLine(); 

     if (stringInput.equalsIgnoreCase("quit")) { 
      break; 
     } else if (stringInput.equalsIgnoreCase("add") || stringInput.equalsIgnoreCase("update")) { 

      Product newProduct = new Product(); 
      InventoryManager inventory = new InventoryManager(); 

      System.out.print("Please enter UPC for the product: "); 
      String upc = userInput.nextLine(); 
      newProduct.setUpc(upc); 

      System.out.print("Please enter the short detail for the product: "); 
      String sDetail = userInput.nextLine(); 
      newProduct.setShortDetails(sDetail); 

      System.out.print("Please enter the long detail for the product: "); 
      String lDetail = userInput.nextLine(); 
      newProduct.setLongDetails(lDetail); 

      System.out.print("Please enter the price for the product: "); 
      BigDecimal price = userInput.nextBigDecimal(); 
      newProduct.setPrice(price); 

      System.out.print("Please enter the stock for the product: "); 
      int stock = userInput.nextInt(); 
      newProduct.setStock(stock); 

      if (stringInput.equalsIgnoreCase("add")) { 
       inventory.addProduct(newProduct); 
      } else if (stringInput.equalsIgnoreCase("update")) { 
       inventory.updateProduct(newProduct); 
      } 
     } else if (stringInput.equalsIgnoreCase("view") || stringInput.equalsIgnoreCase("remove")) { 
      System.out.print("Please enter the UPC for the product: "); 
      String upc = userInput.nextLine(); 

      InventoryManager inventory = new InventoryManager(); 

      if (stringInput.equalsIgnoreCase("view")) { 
       inventory.getProduct(upc); 
       System.out.println(inventory.toString()); 
      } else if (stringInput.equalsIgnoreCase("remove")) { 
       inventory.removeProduct(upc); 
       System.out.println("UPC Has Been Removed"); 
      } 
     } else { 
      System.out.println("Invalid Selection, Please Try Again"); 
     } 
    } 
} 
} 

InventoryManager.java

package cockeb; 

import edu.lcc.citp.utility.CollectionFileStorageUtility; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

/** 
* This class has lets you view, update, add, and delete items from the list 
* 
* @author brandon.c 
*/ 
public class InventoryManager { 

/** 
* List of all products stored 
* 
* @return - List of Products stored 
* @throws IOException 
* @throws ClassNotFoundException 
*/ 
public List<Product> getProductList() throws IOException, ClassNotFoundException { 

    return new ArrayList<>(CollectionFileStorageUtility.load(Product.class)); 

} 

/** 
* Finds a product using the UPC 
* 
* @param upc - UPC of the product you are searching for 
* @return - The product 
* @throws IOException 
* @throws ClassNotFoundException 
*/ 
public Product getProduct(String upc) throws IOException, ClassNotFoundException { 

    Product matchP = null; 
    for (Product p : getProductList()) { 
     if (p.getUpc().equals(upc)) { 
      matchP = p; 
     } 
    } 

    return matchP; 

} 

/** 
* Lets user add a new product 
* 
* @param p - The new Product 
* @throws IOException 
* @throws ClassNotFoundException 
*/ 
public void addProduct(Product p) throws IOException, ClassNotFoundException { 

    List<Product> pList = getProductList(); 

    if (getProduct(p.getUpc()) != null) { 
     System.out.println("Product UPC Already Exists, Please Try Again"); 
    } else { 
     pList.add(p); 
     Collections.sort(pList); 
     CollectionFileStorageUtility.save(pList, Product.class); 
    } 

} 

/** 
* Lets the user change all fields except the UPC field. 
* 
* @param p - the Product with the same UPC 
* @throws IOException 
* @throws ClassNotFoundException 
*/ 
public void updateProduct(Product p) throws IOException, ClassNotFoundException { 

    List<Product> pList = getProductList(); 

    Product matchP = null; 
    for (Product pElement : pList) { 
     if (pElement.getUpc().equals(p.getUpc())) { 
      matchP = pElement; 
     } 
    } 

    if (matchP == null) { 
     System.out.println("Product UPC Does Not Exist"); 
    } else { 
     if (p.getLongDetails() != null) { 
      matchP.setLongDetails(p.getLongDetails()); 
     } 
     if (p.getPrice() != null) { 
      matchP.setPrice(p.getPrice()); 
     } 
     if (p.getShortDetails() != null) { 
      matchP.setShortDetails(p.getShortDetails()); 
     } 
     if (p.getStock() != null) { 
      matchP.setStock(p.getStock()); 
     } 

     CollectionFileStorageUtility.save(pList, Product.class); 
    } 

} 

/** 
* Lets the user to delete an item using the UPC 
* 
* @param upc - UPC of the Item 
* @throws IOException 
* @throws ClassNotFoundException 
*/ 
public void removeProduct(String upc) throws IOException, ClassNotFoundException { 

    List<Product> pList = getProductList(); 

    Product matchP = null; 
    for (Product p : pList) { 
     if (p.getUpc().equals(upc)) { 
      matchP = p; 
      break; 
     } 
    } 

    if (matchP == null) { 
     System.out.println("Product UPC Does Not Exist"); 
    } else { 
     pList.remove(matchP); 
     CollectionFileStorageUtility.save(pList, Product.class); 
    } 

} 
} 

Product.java

package cockeb; 

import java.io.Serializable; 
import java.math.BigDecimal; 

public class Product implements Serializable, Comparable<Product> { 
private static final long serialVersionUID = 1L; 

private String upc; 
private String shortDetails; 
private String longDetails; 
private BigDecimal price; 
private Integer stock; 

public String getUpc() { 
    return upc; 
} 

public void setUpc(String upc) { 
    this.upc = upc; 
} 

public String getShortDetails() { 
    return shortDetails; 
} 

public void setShortDetails(String shortDetails) { 
    this.shortDetails = shortDetails; 
} 

public String getLongDetails() { 
    return longDetails; 
} 

public void setLongDetails(String longDetails) { 
    this.longDetails = longDetails; 
} 

public BigDecimal getPrice() { 
    return price; 
} 

public void setPrice(BigDecimal price) { 
    this.price = price; 
} 

public Integer getStock() { 
    return stock; 
} 

public void setStock(Integer stock) { 
    this.stock = stock; 
} 

@Override 
public int compareTo(Product t) { 
    return this.getUpc().compareTo(t.getUpc()); 
} 

} 

我没有得到任何错误,但输出这一程序如下:

"add" - to add a product 
"remove" - to remove a product 
"update" - to update a product 
"view" - to view a product 
"quit" - to quit the menu 

Please Enter Your Choice: add 
Please enter UPC for the product: 123 
Please enter the short detail for the product: car 
Please enter the long detail for the product: big car 
Please enter the price for the product: 200 
Please enter the stock for the product: 2 


"add" - to add a product 
"remove" - to remove a product 
"update" - to update a product 
"view" - to view a product 
"quit" - to quit the menu 

Please Enter Your Choice: view 
Please enter the UPC for the product: 123 
[email protected] 

我希望它打印出我输入的任何产品的列表。感谢您的帮助。

+2

你需要重写里面'toString'方法为您'InventoryManager',还要确保你打印正确的结果'的System.out.println( inventory.getProduct(UPC)的ToString());'。 –

回答

0

第一个问题是您试图打印inventory变量而不是product。为了解决这个问题更改以下行:

if (stringInput.equalsIgnoreCase("view")) { 
    inventory.getProduct(upc); 
    System.out.println(inventory.toString()); 
} 

要这样:

if (stringInput.equalsIgnoreCase("view")) {   
    System.out.println(inventory.getProduct(upc).toString()); 
} 

但因为你在没有覆盖了toString()方法你Product.java你会得到一个内存中的表示来代替。因此,您需要将以下内容添加到Product.java以解决此问题。

public String toString() { 
    return upc + " - " + price; // or whatever you want. 
} 

输出示例:

Please Enter Your Choice: view 
Please enter the UPC for the product: 123 
123 - 999 
+0

谢谢你的帮助,完美的工作。 – Spectre6

+0

@ Spectre6当然!如果你完成了,请将答案标记为正确。 – lxcky

0

当您致电getProduct()时,您的InventoryManager inventory为空。是的,你的toString()需要在正确的元素上调用,并被覆盖。

+0

那么我需要做些什么来解决它?我没有得到任何错误,所以我很难指出问题。 – Spectre6

+0

在打印之前添加的产品之前,您正在执行'InventoryManager inventory = new InventoryManager();并且您在新对象上调用了打印件,而新对象实际上是空的。你只需要在'while'之前初始化'InventoryManager'对象,然后处理这个对象。您不必每次都重新初始化它。在这里,你只能在你的'InventoryManager'中有一个'Product'。 你可以按照你的方式来做,但你的'InventoryManager'类应该遵循Singleton模式。 – Ecterion

+0

但是,它的工作,因为CollectionFileStorageUtility ..我的坏!但你仍然可以简化它。 – Ecterion