0

我在 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() {}
}
4

1 回答 1

1

先说几条评论:

StockItem 中的实例变量应该是私有的。

StockList 有一个公共构造函数,但使用一个静态列表——这意味着 StockList 的所有实例共享同一个列表——除非这是你想要的(我猜它不是),你应该使该列表非静态。因此,只需将其设为私有成员变量。

如果这是您的意图, StockListInterface 也应该是一个实际的接口。

public interface StockList {
    public void addItem(StockItem item); 
    public void deleteItemById(String itemID); 
    // Updates price of existing item
    public void updateItemPrice(String itemID, double price);
    /// etc.
}

请注意,我建议使用 deleteItemById,因为您可以轻松地提供一个名为 deleteItem 的方法,该方法采用 StockItem 和 addItem 将 StockItem 作为参数而不是字符串。然后进行一个实现(在我看来,这在这里有点矫枉过正,但无论哪种方式都可以争论),如果是我,一个简单的 StockList 类就足够了。

public class StockListImpl implements StockList {

   private List<StockItem> stockList = new LinkedList<StockItem>();

   public void addItem(StockItem item) {
         stockList.add(item);
   }

  //etc...

请注意,您应该使用泛型来避免类型转换:

private List<StockItem> = new LinkedList<StockItem>();

这样,您将获得一个返回 StockItems 的迭代器,而您不必强制转换。所以你可以像这样实现 removeItemById :

public void removeItemById(String itemId) {
   Iterator<StockItem> itr = stockList.listIterator();
   while(itr.hasNext()) {
       StockItem item = itr.next();
   if(item.getitemID().equals(itemId)) {
     itr.remove();
     break;
    }
   }
}

所以对于你的问题。

我猜您可能想使用扫描仪-请参阅此答案

如何在 Java 中使用 readline() 方法?

我认为您需要一种方法来创建每个子菜单,而另一种方法需要用户输入并决定要显示哪个菜单。这应该与您的 StockList 类分开。

您可以根据自己的喜好使其复杂,但是由于我猜您是初学者,因此一个基本示例是:

例如

class StockListApp {

private static Scanner scanner = new Scanner(System.in);

private static final int ADD_ITEM = 1;
private static final int DELETE_ITEM = 2;

private StockListApp() {}

public static void displayRootMenu() {
     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] :>");
     int choice = scanner.nextInt();
     displaySubMenu(choice);
}

private static void displayAddItemMenu() {
    // display menu and read next char from scanner.
}

private static void displayDeleteMenu() {
    // display menu and read next char from scanner.
}


private static void displaySubMenu(int option) {

    switch (option) {
      case StockListApp.ADD_ITEM: {
          displayAddItemMenu();
          break;
      }
      case StockListApp.DELETE_ITEM: {
          displayDeleteMenu();
          break;
      }
    }

}

}

记得在用户选择退出时调用scanner.close()。

于 2013-05-03T10:34:33.263 回答