Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更新ArrayList中的值_Java_Arraylist - Fatal编程技术网

Java 更新ArrayList中的值

Java 更新ArrayList中的值,java,arraylist,Java,Arraylist,我的程序是一个物品清单。每个项目由用户输入,直到达到10,然后在末尾显示每个项目的总成本(成本*数量) 但我需要能够更新特定项目的数量。所以我想问用户“你想更新哪个项目?”和“你想减去多少”,但我不知道如何将项目与其具体数量联系起来。之后,我想再次显示更新后的项目列表和更新后的总成本 是否可以作为阵列列表执行操作?或者我应该使用不同的结构 下面是课程: public class StockItem { String stockItemID; String stockItemDescription

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

但我需要能够更新特定项目的数量。所以我想问用户“你想更新哪个项目?”和“你想减去多少”,但我不知道如何将项目与其具体数量联系起来。之后,我想再次显示更新后的项目列表和更新后的总成本

是否可以作为
阵列列表执行操作?或者我应该使用不同的结构

下面是课程:

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);            
    }
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
ArrayList=新建ArrayList();
对于(int i=0;i<2;i++){
系统输出打印(“输入ID:”);
字符串stockItemID=input.next();
系统输出打印(“输入项目说明:”);
字符串stockItemDescription=input.next();
系统输出打印(“输入物料成本价:”);
double stockItemCostPrice=input.nextDouble();
系统输出打印(“输入项目销售价格:”);
double stockItemSellingPrice=input.nextDouble();
系统输出打印(“输入可用物料数量:”);
int stockItemQuantityAvailable=input.nextInt();
int updateItemQuantityAvailable=(0);
添加(新的StockItem(stockItemID、stockItemDescription、stockItemCostPrice、stockItemSellingPrice、stockItemQuantityAvailable、updateItemQuantityAvailable));
列表
}
System.out.println(list.toString().replace(“]”,”).replace(“[”,”).replace(“,”,”).replace(“,”);
用于(库存项目数据:列表){
double totalStockCost=(data.getStockItemQuantityAvailable()-data.getUpdateItemQuantityAvailable())*(data.getStockItemCostPrice());
System.out.println((“总成本”)+data.getStockItemDescription()+“:$”+totalStockCost);
}

您应该使用
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 data : map.values()) {
    double totalStockCost = ...
    ...
}

如果您只关心更新
列表
的元素,是的,您可以这样做。例如,如果您在
列表
中填充了10个
StockItem
s,所有这些都是
stockItemQuantityAvailable
为100的,并且用户想要购买第一个项目中的5个:

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

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

HashMap将使搜索更加高效

如果您不想使用HashMap,那么我的方法是为StockItem和ShoppingCart类创建一个单独的类。在ShoppingCart中,您可以编写一个UTILITY方法,如下面所示。或者,您也可以实现一个Comparator并使用List.contains()

购物车
私有列表项=新的ArrayList();
public void updatequality(字符串itemId,int-qty){
如果(null==itemId){return;}
用于(库存项目:项目){
if(itemId.equals(item.getStockItemID())){
item.SetStockItemQuantity可用(数量);
}
}
}

为了更快地获得更好的帮助,请发布一个。请记住,只有StockItem实现了
equals()
hashCode()
来查看stockItemId时,这才有效。
 StockItem chosen = list.get(0);
 int currentStock = chosen.getStockItemQuantityAvailable();
 chosen.setStockItemQuantityAvailable(currentStock - 5);
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);
            }
        }
    }