英文:
How to calculate the sum of prices in a cart JList?
问题
我一直在创建这个库存程序,它从一个txt文件中获取产品ID、名称、物品价格和库存。它将其导入到我的产品列表(JList
),然后当我按下“添加到购物车”按钮时,它会将所选产品移动到购物车(另一个JList
)。
我的主要问题是,如何计算购物车中所有价格的总和?如何将产品项的价格与特定价格绑定?假设为了显示总价格,我正在使用一个JLabel
。
以下是一些图片和代码:
这是当前“移动”按钮的代码:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == movebutton) {
model2.addElement(list.getSelectedValue());
}
}
这是显示图像、物品名称、价格和库存的代码:
public void valueChanged(ListSelectionEvent e) {
try {
String gadgets = (String) c1.getSelectedItem(); //当选择下拉框时
switch (gadgets) {
case "Consoles": //选择第一个产品类别
image.setIcon(graphicsconsole[list.getSelectedIndex()]); //设置图像
File file = new File("stock\\consoles\\consoles.txt"); //读取txt文件
int ctr = 0;
try {
Scanner s1 = new Scanner(new File(String.valueOf(file)));
while (s1.hasNextLine()) {
ctr = ctr + 1;
s1.next();
s1.next();
s1.next();
s1.nextLine();
}
String[] ID = new String[ctr];
String[] PRICE = new String[ctr];
String[] STOCK = new String[ctr];
String[] NAME = new String[ctr];
Scanner s2 = new Scanner(new File(String.valueOf(file))); //创建字符串
for (int i = 0; i < ctr; i = i + 1) {
ID[i] = s2.next();
PRICE[i] = s2.next();
STOCK[i] = s2.next();
NAME[i] = s2.nextLine();
iteminfo.setText(NAME[list.getSelectedIndex()]); //显示物品名称
itemdescription.setText(" P " + PRICE[list.getSelectedIndex()]); //显示价格
itemstock.setText(" Stock: "+ STOCK[list.getSelectedIndex()]); //显示库存
}
} catch (Exception a) {
a.printStackTrace();
}
break;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
我是否需要一个空数组并在其中添加值?如何在购物车中不存在时删除值?如果我的帖子/问题听起来令人困惑,我很抱歉,我希望它是可以理解的。
英文:
I have been creating this inventory program that gets the product ID, name, item price and stocks from a txt file. It imports it to my product list (JList
), then when I press the "add to cart" button, it will then move the selected product to the Cart (which is another JList
).
My main question is, how can I calculate the sum of all of the prices in the Cart? How do I bind the prices of the product items with a specific price? Assuming that to show the total price, I'm using a JLabel
.
Here are some pictures and code:
This is the code for the move button as of now:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == movebutton) {
model2.addElement(list.getSelectedValue());
}
This is the code to show the images, item name, price and stock of an item:
public void valueChanged(ListSelectionEvent e) {
try {
String gadgets = (String) c1.getSelectedItem(); //when the combobox is selected
switch (gadgets) {
case "Consoles": //selects first product category
image.setIcon(graphicsconsole[list.getSelectedIndex()]); //sets the image
File file = new File("stock\\consoles\\consoles.txt"); //reads the txt file
int ctr = 0;
try {
Scanner s1 = new Scanner(new File(String.valueOf(file)));
while (s1.hasNextLine()) {
ctr = ctr + 1;
s1.next();
s1.next();
s1.next();
s1.nextLine();
}
String[] ID = new String[ctr];
String[] PRICE = new String[ctr];
String[] STOCK = new String[ctr];
String[] NAME = new String[ctr];
Scanner s2 = new Scanner(new File(String.valueOf(file))); //creates strings
for (int i = 0; i < ctr; i = i + 1) {
ID[i] = s2.next();
PRICE[i] = s2.next();
STOCK[i] = s2.next();
NAME[i] = s2.nextLine();
iteminfo.setText(NAME[list.getSelectedIndex()]); //shows the item name
itemdescription.setText(" P " + PRICE[list.getSelectedIndex()]); //shows the price
itemstock.setText(" Stock: "+ STOCK[list.getSelectedIndex()]); //shows the stock
}
} catch (Exception a) {
a.printStackTrace();
}break;
Do I need to have an empty array and add the values there? How do I also remove values when it is not in the cart? Sorry if my post/question sounds confusing, I hope that it is understandable.
专注分享java语言的经验与见解,让所有开发者获益!
评论