2013-03-17 300 views
0

我的程序是一個項目清單。每個項目由用戶輸入,直至達到10,然後每個項目的總成本(成本*數量)顯示在最後。更新ArrayList中的值

但我需要能夠更新特定項目的數量。所以我想以某種方式詢問用戶「你想更新哪個項目?」和「你想要減去多少」,但我不知道如何將項目與其具體數量聯繫起來。之後,我想再次顯示更新的項目列表和更新的總成本。

是否可以做爲ArrayList?或者我應該使用不同的結構?

所以這裏的類:

public class StockItem { 
String stockItemID; 
String stockItemDescription; 
double stockItemCostPrice; 
double stockItemSellingPrice; 
int stockItemQuantityAvailable; 
String toString; 
int updateItemQuantityAvailable; 

StockItem(String stockItemID, String stockItemDescription, double stockItemCostPrice, 
double stockItemSellingPrice, int stockItemQuantityAvailable, int 
updateItemQuantityAvailable) { 
    this.stockItemID = stockItemID; 
    this.stockItemDescription = stockItemDescription; 
    this.stockItemCostPrice = stockItemCostPrice; 
    this.stockItemSellingPrice = stockItemSellingPrice; 
    this.stockItemQuantityAvailable = stockItemQuantityAvailable; 
    this.updateItemQuantityAvailable = updateItemQuantityAvailable; 
} 

public void setStockItemID(String stockItemID) { 
    this.stockItemID = stockItemID; 
} 

public String getStockItemID() { 
    return stockItemID; 
} 

public void setStockItemDescription(String stockItemDescription) { 
    this.stockItemDescription = stockItemDescription; 
} 

public String getStockItemDescription() { 
    return stockItemDescription; 
} 

public void setStockItemCostPrice(double stockItemCostPrice) { 
    this.stockItemCostPrice = stockItemCostPrice; 
} 

public double getStockItemCostPrice() { 
    return stockItemCostPrice; 
} 

public void setStockItemSellingPrice(double stockItemSellingPrice) { 
    this.stockItemSellingPrice = stockItemSellingPrice; 
} 

public double getStockItemSellingPrice() { 
    return stockItemSellingPrice; 
} 

public void setStockItemQuantityAvailable(int stockItemQuantityAvailable) { 
    this.stockItemQuantityAvailable = stockItemQuantityAvailable; 
} 

public int getStockItemQuantityAvailable() { 
    return stockItemQuantityAvailable; 
} 

public String toString() { 
    return (stockItemID + "\t" + stockItemDescription + "\t" + " Cost Price: $" + stockItemCostPrice + "\t" + "Selling Price: $" + stockItemSellingPrice + "\t" + "Quantity: " + stockItemQuantityAvailable + "\n"); 
} 

public void setUpdateItemQuantityAvailable(int updateItemQuantityAvailable){ 
    this.updateItemQuantityAvailable = updateItemQuantityAvailable; 
} 
public int getUpdateItemQuantityAvailable() { 
    return updateItemQuantityAvailable; 
} 

,然後方法:

public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    ArrayList<StockItem> list = new ArrayList<StockItem>(); 
    for (int i = 0; i < 2; i++) { 

     System.out.print(" Enter ID: "); 
     String stockItemID = input.next(); 

     System.out.print(" Enter Item Description: "); 
     String stockItemDescription = input.next(); 

     System.out.print(" Enter Item Cost Price: "); 
     double stockItemCostPrice = input.nextDouble(); 

     System.out.print(" Enter Item Selling Price: "); 
     double stockItemSellingPrice = input.nextDouble(); 

     System.out.print(" Enter Item Quantity Available: "); 
     int stockItemQuantityAvailable = input.nextInt(); 

     int updateItemQuantityAvailable = (0); 

     list.add(new StockItem(stockItemID, stockItemDescription, stockItemCostPrice, stockItemSellingPrice, stockItemQuantityAvailable, updateItemQuantityAvailable)); 
     list. 
    } 

    System.out.println(list.toString().replace("]", "").replace("[", "").replace(",", "").replace(" ","")); 

    for (StockItem data : list) { 
     double totalStockCost = (data.getStockItemQuantityAvailable()- data.getUpdateItemQuantityAvailable()) * (data.getStockItemCostPrice()); 
     System.out.println(("Total Cost for ") + data.getStockItemDescription() + ": $" + totalStockCost);    
    } 
+1

爲了更好地幫助您,請發佈[SSCCE](http://sscce.org/)。 – 2013-03-17 03:43:50

回答

0

如果您只是在擔心能夠更新List的元素,那麼您可以ñ那樣做。例如,如果您填寫list 10個StockItem S,所有的100 stockItemQuantityAvailable,而用戶想要買5的第一個項目:

StockItem chosen = list.get(0); 
int currentStock = chosen.getStockItemQuantityAvailable(); 
chosen.setStockItemQuantityAvailable(currentStock - 5); 

現在如果你打印出清單,你會發現第一件物品有95件,其餘100件。

0

你應該使用HashMap而非ArrayList。如果「更新」你的意思是更換,那麼這應該工作:

HashMap<String, StockItem> map = new HashMap<String, StockItem>(); 
for (int i = 0; i < 2; i++) { 

    System.out.print(" Enter ID: "); 
    String stockItemID = input.next(); 
    ... 

    StockItem item = new StockItem(stockItemID, stockItemDescription, stockItemCostPrice, stockItemSellingPrice, stockItemQuantityAvailable, updateItemQuantityAvailable); 
    map.put(stockItemID, item); // this line will replace any earlier item with same stockItemID 
} 

來顯示地圖的內容:

for(StockItem item : map.values()) { 
    System.out.println(item); 
} 

顯示每個項目的總成本:

for(StockItem data : map.values()) { 
    double totalStockCost = ... 
    ... 
} 
+0

請記住,只有在StockItem實現'equals()'和'hashCode()'來查看stockItemId時,它纔會起作用。 – Blake 2013-03-17 04:41:58

0

HashMap將使搜索更有效率。

如果你不想使用HashMap,那麼我會採取的辦法是爲StockItem和ShoppingCart類創建一個單獨的類。在ShoppingCart中,您可以編寫如下所示的有用方法。或者,您也可以實現比較器並使用List.contains()。

ShoppingCart 
    private List<StockItem> items = new ArrayList<StockItem>(); 

    public void updateQuantity(String itemId, int qty) { 
     if(null == itemId) { return; } 

     for(StockItem item : items) { 
      if (itemId.equals(item.getStockItemID())){ 
       item.setStockItemQuantityAvailable(qty); 
      } 
     } 
    }