0

我的程序是物品清单。每个项目由用户输入,直到达到 10 个,然后在最后显示每个项目的总成本(成本 * 数量)。

但我需要能够更新特定项目的数量。所以我想以某种方式询问用户“你想更新哪个项目?” 和“你想减去多少”,但我不知道如何将项目链接到它的具体数量。之后,我想再次显示更新的项目列表和更新的总成本。

可以作为一个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);            
    }
4

3 回答 3

0

你应该使用 aHashMap而不是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 = ...
    ...
}
于 2013-03-17T03:39:19.773 回答
0

如果您只是关心能够更新 的元素List,是的,您可以这样做。比如你已经填list了 10 StockItems,都填了stockItemQuantityAvailable100,用户想买第 5 件:

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

现在,如果您打印出列表,您会发现第一项有 95 个,其余的有 100 个。

于 2013-03-17T03:48:14.870 回答
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);
            }
        }
    }
于 2013-03-17T03:55:51.037 回答