我正在创建一个存储到arraylist(项目键,数量和项目的价格)的篮子.如何仅更新数量而不是使用相同的Item键创建另一行.以下是该程序的结果.
Java Book 1 £48.90
Samsung Galaxy S7 1 £639.50
Speakers 3 £59.80
Java Book 2 £48.90
Samsung Galaxy S7 2 £639.50
Run Code Online (Sandbox Code Playgroud)
//////////////////////////////////////////////////
private void ItemsBasket(String name, int qty, String key) throws HeadlessException {
if (name == null) {
JOptionPane.showMessageDialog(null, "Please Selecet a key");
String imageFileName = "./images/" + key + ".png";
File imageFile = new File(imageFileName);
if (!imageFile.exists()) {
imageFileName = "./images/empty.png";
}
} else if (qty <= StockData.getQuantity(key)) {
arList.add(StockData.getName(key) + "\t\t " + qty + " " + pounds.format(StockData.getPrice(key)));
bagtotal += StockData.getPrice(key) * qty;
JOptionPane.showMessageDialog(null, "Sucessfully added to the basket");
} else {
JOptionPane.showMessageDialog(null, "Insuffcient stock");
JOptionPane.showMessageDialog(null, "Available items qty : " + "**" + StockData.getQuantity(key) + "**");
}
}
Run Code Online (Sandbox Code Playgroud)
创建一个StockData对象并将其直接添加到ArrayList<StockData>
public class StockData{
String name;
int qty;
double price;
public StockData(String n, int q, double p){
// Initialization
}
}
Run Code Online (Sandbox Code Playgroud)
然后,使用setter,您可以修改您的对象中的对象的数据 List
arList.get(index).setName("NewName");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
776 次 |
| 最近记录: |