我有一个小库存计划。这三个类列在下面。我认为当我添加一个项目时它正常工作但是当我选择视图选项时,它不会打印出我想要的内容。
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
cockeb.InventoryManager@7ef20235
我希望它打印出我输入的任何产品的列表。谢谢你的帮助。
答案 0 :(得分:0)
致电InventoryManager inventory
时,getProduct()
为空。是的,您的toString()
需要调用正确的元素并被覆盖。
答案 1 :(得分: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