2013-03-17 2 views
0

내 프로그램은 항목 목록입니다. 각 항목은 사용자가 10에 도달 할 때까지 사용자가 입력 한 다음 각 항목의 총 비용 (비용 * 수량)이 끝에 표시됩니다.ArrayList의 값 업데이트

는하지만 특정 항목의 수량을 업데이트 할 수 있어야합니다. 그래서 어떻게 든 사용자에게 "어떤 항목을 업데이트 하시겠습니까?"라고 물어볼 것입니다. 그리고 "얼마를 뺄 것인가?"하지만 특정 수량에 항목을 연결하는 방법을 모르겠습니다. 그 후, 나는 아이템의 업데이트 된 목록과 업데이트 된 총 비용을 보여주고 싶다.

그것은 ArrayList로 할 수 있습니까? 아니면 다른 구조를 사용해야합니까?

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/)를 게시하십시오. –

답변

0

당신이 List의 요소를 업데이트 할 수있는 약 다만 염려되는 경우, 그래, 당신이 캘리포니아 그럴 수있어. 당신은거야, 당신은 목록을 인쇄하는 경우 지금

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

: 100의 stockItemQuantityAvailable 10 개 StockItem의, 모두와 함께 list를 작성하고, 경우 예를 들어, 사용자는 첫 번째 항목의 5를 사려고 첫 번째 항목 95 개와 나머지 항목 100 개를 찾습니다.

0

당신이 HashMap 오히려 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; 
} 

및 방법 :

그래서 여기 클래스입니다. "업데이트"에 의해 당신이 를 교체 의미하는 경우, 다음이 작동합니다 :

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이 stockItemId를보기 위해'equals()'와'hashCode()'를 구현하는 경우에만 작동한다는 것을 명심하십시오. – Blake

0

HashMap은 검색을보다 효율적으로 만듭니다.

당신의 HashMap을 사용하지 않은 경우

후 나는이 접근 할 방법은 StockItem과 shoppingCart가 클래스에 대한 별도의 수업을하는 것입니다. ShoppingCart에서는 다음과 같은 utlity 메소드를 작성할 수 있습니다. 또는 Comparator를 구현하고 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); 
      } 
     } 
    }