坚持如何将项目添加到股票列表java

时间:2013-05-03 09:42:19

标签: java interface linked-list

我在java中创建了一个股票控制程序,但我被困在用户输入部分。 用户需要能够从界面的主菜单中选择“添加项目”,然后从中输入项目的ID,描述,价格,数量和重新订购级别,然后选择是否要添加另一个项目,或退回到主菜单。

我已在一组扫描程序中编码以读取项目属性的输入,但我不确定如何将它们添加到我的库存清单中,还不确定如果用户想要我应该使用什么类型的循环添加其他项目或导航回主菜单。

任何人都知道如何最好地解决这两个问题?任何建议/解决方案将不胜感激。 感谢

STOCKITEM CODE:

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





}

StockList:

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");
}



}

StockListInterface:

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

1 个答案:

答案 0 :(得分:1)

首先提出几点意见:

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

StockList有一个公共构造函数,但使用静态List - 这意味着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;
    }
   }
}

那么你的问题。

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

How to use readline() method in Java?

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

你可以根据自己的喜好使它变得复杂,但是因为我猜你是一个初学者,一个基本的例子就是:

e.g。

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()。