我在 java 中创建了一个库存控制程序,但卡在用户输入部分。用户需要能够从界面的主菜单中选择“添加商品”,然后从那里输入商品的 ID、描述、价格、数量和重新订购级别,然后选择是否要添加添加另一个项目,或返回主菜单。
我已经编写了一组扫描仪来读取项目属性的输入,但我不确定如何将它们添加到我的库存列表中,并且不确定如果用户想要添加另一个我应该使用哪种类型的循环项目或导航回主菜单。
有人知道如何最好地解决这些问题吗?任何建议/解决方案将不胜感激。谢谢
库存代码:
public class StockItem {
String itemID;
String itemDesc;
Double price;
int quantity;
int reOrderLevel;
//declaring my instance variables
public StockItem (String itemID, String itemDesc, Double price, int quantity, int reOrderLevel)
{
this.itemID = itemID;
this.itemDesc = itemDesc;
this.price = price;
this.quantity = quantity;
this.reOrderLevel = reOrderLevel;
}
//declaring a contructor with 5 parameters for my instance variables
public String getitemID() {
return itemID;
}
public String getitemDesc() {
return itemDesc;
}
public Double getprice() {
return price;
}
public int getquantity() {
return quantity;
}
public int getreOrderLevel() {
return reOrderLevel;
}
//creating my getters for all instance variables
public void setprice(Double price) {
this.price = price;
}
public void setquantity(int quantity) {
this.quantity = quantity;
}
public void setreOrderLevel(int reOrderLevel) {
this.reOrderLevel = reOrderLevel;
}
//creating my setters for 3 instance variables
public String toString()
{
return getClass().getName() + "itemID: " + itemID + "itemDesc: " + itemDesc + "price: "
+ price + "quantity: " + quantity + "reOrderLevel: " + reOrderLevel;
}
//public String format() {}
}
库存清单:
import java.util.*;
public class StockList
{
public static LinkedList<StockItem> stock
= new LinkedList<StockItem>();
public StockList() {};
// Adds item to end of stock list
public void addItem(StockItem item)
{
StockList.stock.addLast(item);
}
// Removes item identified by productID from stock list
public void deleteItem(String itemID)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
itr.remove();
break;
}
}
}
// Updates price of existing item
public void updateItemPrice(String itemID, double price)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setprice(price);
break;
}
}
}
// Updates quantity of existing item
public void updateItemQuantity(String itemID, int quantity)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setquantity(quantity);
break;
}
}
}
// Updates re-order level of existing item
public void updateReOrderLevel(String itemID,
int reOrderLevel)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setreOrderLevel(reOrderLevel);
break;
}
}
}
@Override public String toString()
{
return "" + stock;
}
// Returns formatted representation of the stock list
// public String formatStockList()
// {…}
// Returns formatted representation of re-order list
// Items are on this list if quantity < reOrderLevel
// public String formatReOrderList()
// {…}
ListIterator listIterator() {
throw new UnsupportedOperationException("Not yet implemented");
}
}
库存清单接口:
import java.util.*;
public class StockListInterface
{
private StockList stock = null;
public StockListInterface(StockList stock){}
// Displays main menu and gets valid option from user
public void doMenu()
{
System.out.println("StockList Main Menu");
System.out.println("*******************");
System.out.println("1. Add an Item");
System.out.println("2. Delete an Item");
System.out.println("3. Update Item Price");
System.out.println("4. Update Item Quantity");
System.out.println("5. Update ReOrder Level");
System.out.println("6. Print Stock List");
System.out.println("7. Print ReOrder List");
System.out.println("8. Exit");
System.out.println("Select option [1-8] :>");
}
// Obtain input for stock list operation
// and invoke operation
private void doAddItem(StockItem item)
{
StockList.stock.addLast(item);
}
private void doDeleteItem(String itemID)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
itr.remove();
break;
}
}
private void doUpdateItemPrice(String itemID, double price)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setprice(price);
break;
}
}
}
private void doUpdateItemQuantity(String itemID, int quantity)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setquantity(quantity);
break;
}
}
}
private void doUpdateReOrderLevel(String itemID, int reOrderLevel)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setreOrderLevel(reOrderLevel);
break;
}
}
}
// Display contents of stock list
private void doPrintStockList() {}
// Display contents of re-order list
private void doPrintReorderLIst() {}
}
StockListApp:
import java.util.*;
public class StockListInterface
{
private StockList stock = null;
public StockListInterface(StockList stock){}
// Displays main menu and gets valid option from user
public void doMenu()
{
System.out.println("StockList Main Menu");
System.out.println("*******************");
System.out.println("1. Add an Item");
System.out.println("2. Delete an Item");
System.out.println("3. Update Item Price");
System.out.println("4. Update Item Quantity");
System.out.println("5. Update ReOrder Level");
System.out.println("6. Print Stock List");
System.out.println("7. Print ReOrder List");
System.out.println("8. Exit");
System.out.println("Select option [1-8] :>");
}
// Obtain input for stock list operation
// and invoke operation
private void doAddItem(StockItem item)
{
StockList.stock.addLast(item);
}
private void doDeleteItem(String itemID)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
itr.remove();
break;
}
}
private void doUpdateItemPrice(String itemID, double price)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setprice(price);
break;
}
}
}
private void doUpdateItemQuantity(String itemID, int quantity)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setquantity(quantity);
break;
}
}
}
private void doUpdateReOrderLevel(String itemID, int reOrderLevel)
{
ListIterator itr = stock.listIterator();
while(itr.hasNext())
{
StockItem item = (StockItem)itr.next();
if(item.getitemID().equals(itemID))
{
item.setreOrderLevel(reOrderLevel);
break;
}
}
}
// Display contents of stock list
private void doPrintStockList() {}
// Display contents of re-order list
private void doPrintReorderLIst() {}
}