2013-05-03 53 views
0

我已經在java中創建了股票控制程序,但是卡在用戶輸入部分。 用戶需要能夠從界面的主菜單中選擇「添加項目」,然後從中輸入項目的ID,說明,價格,數量和重新排序級別,然後選擇他們是否需要添加另一個項目,或返回到主菜單。卡住如何將項目添加到股票列表java

我已經編寫了一套掃描儀來讀取項目屬性的輸入,但我不確定如何將它們添加到我的股票列表,也不確定如果用戶使用什麼類型的循環想要添加另一個項目或導航回主菜單。

任何人有任何想法如何最好地解決這些問題?任何建議/解決方案將不勝感激。 感謝

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

僅發佈相關代碼。 – 2013-05-03 09:44:23

+0

StockListApp和StockListInterface看起來相同的複製粘貼,只考慮粘貼相關的代碼。 – 2013-05-03 09:50:56

+0

企業模式是遞增的 – Val 2013-05-03 09:51:57

回答

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作爲PARAM不是一個字符串。 然後做一個實現(在我看來,這似乎有點矯枉過正,但是這可能會引起爭論),如果是我,簡單的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類分開。

您可以將此作爲成熟的,只要你喜歡,但因爲我猜你是一個初學者一個基本的例子是:

例如

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

+0

非常感謝,這解決了我遇到的所有問題! – 2013-05-03 10:55:56