我已經在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() {}
}
僅發佈相關代碼。 – 2013-05-03 09:44:23
StockListApp和StockListInterface看起來相同的複製粘貼,只考慮粘貼相關的代碼。 – 2013-05-03 09:50:56
企業模式是遞增的 – Val 2013-05-03 09:51:57