如何计算购物车 JList 中商品价格的总和?

huangapple 未分类评论45阅读模式
英文:

How to calculate the sum of prices in a cart JList?

问题

我一直在创建这个库存程序,它从一个txt文件中获取产品ID、名称、物品价格和库存。它将其导入到我的产品列表(JList),然后当我按下“添加到购物车”按钮时,它会将所选产品移动到购物车(另一个JList)。

我的主要问题是,如何计算购物车中所有价格的总和?如何将产品项的价格与特定价格绑定?假设为了显示总价格,我正在使用一个JLabel

以下是一些图片和代码:

这是当前“移动”按钮的代码:

  1. public void actionPerformed(ActionEvent e) {
  2. if (e.getSource() == movebutton) {
  3. model2.addElement(list.getSelectedValue());
  4. }
  5. }

这是显示图像、物品名称、价格和库存的代码:

  1. public void valueChanged(ListSelectionEvent e) {
  2. try {
  3. String gadgets = (String) c1.getSelectedItem(); //当选择下拉框时
  4. switch (gadgets) {
  5. case "Consoles": //选择第一个产品类别
  6. image.setIcon(graphicsconsole[list.getSelectedIndex()]); //设置图像
  7. File file = new File("stock\\consoles\\consoles.txt"); //读取txt文件
  8. int ctr = 0;
  9. try {
  10. Scanner s1 = new Scanner(new File(String.valueOf(file)));
  11. while (s1.hasNextLine()) {
  12. ctr = ctr + 1;
  13. s1.next();
  14. s1.next();
  15. s1.next();
  16. s1.nextLine();
  17. }
  18. String[] ID = new String[ctr];
  19. String[] PRICE = new String[ctr];
  20. String[] STOCK = new String[ctr];
  21. String[] NAME = new String[ctr];
  22. Scanner s2 = new Scanner(new File(String.valueOf(file))); //创建字符串
  23. for (int i = 0; i < ctr; i = i + 1) {
  24. ID[i] = s2.next();
  25. PRICE[i] = s2.next();
  26. STOCK[i] = s2.next();
  27. NAME[i] = s2.nextLine();
  28. iteminfo.setText(NAME[list.getSelectedIndex()]); //显示物品名称
  29. itemdescription.setText(" P " + PRICE[list.getSelectedIndex()]); //显示价格
  30. itemstock.setText(" Stock: "+ STOCK[list.getSelectedIndex()]); //显示库存
  31. }
  32. } catch (Exception a) {
  33. a.printStackTrace();
  34. }
  35. break;
  36. }
  37. } catch (Exception ex) {
  38. ex.printStackTrace();
  39. }
  40. }

我是否需要一个空数组并在其中添加值?如何在购物车中不存在时删除值?如果我的帖子/问题听起来令人困惑,我很抱歉,我希望它是可以理解的。

英文:

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:

My inventory program

This is the code for the move button as of now:

  1. public void actionPerformed(ActionEvent e) {
  2. if (e.getSource() == movebutton) {
  3. model2.addElement(list.getSelectedValue());
  4. }

This is the code to show the images, item name, price and stock of an item:

  1. public void valueChanged(ListSelectionEvent e) {
  2. try {
  3. String gadgets = (String) c1.getSelectedItem(); //when the combobox is selected
  4. switch (gadgets) {
  5. case &quot;Consoles&quot;: //selects first product category
  6. image.setIcon(graphicsconsole[list.getSelectedIndex()]); //sets the image
  7. File file = new File(&quot;stock\\consoles\\consoles.txt&quot;); //reads the txt file
  8. int ctr = 0;
  9. try {
  10. Scanner s1 = new Scanner(new File(String.valueOf(file)));
  11. while (s1.hasNextLine()) {
  12. ctr = ctr + 1;
  13. s1.next();
  14. s1.next();
  15. s1.next();
  16. s1.nextLine();
  17. }
  18. String[] ID = new String[ctr];
  19. String[] PRICE = new String[ctr];
  20. String[] STOCK = new String[ctr];
  21. String[] NAME = new String[ctr];
  22. Scanner s2 = new Scanner(new File(String.valueOf(file))); //creates strings
  23. for (int i = 0; i &lt; ctr; i = i + 1) {
  24. ID[i] = s2.next();
  25. PRICE[i] = s2.next();
  26. STOCK[i] = s2.next();
  27. NAME[i] = s2.nextLine();
  28. iteminfo.setText(NAME[list.getSelectedIndex()]); //shows the item name
  29. itemdescription.setText(&quot; P &quot; + PRICE[list.getSelectedIndex()]); //shows the price
  30. itemstock.setText(&quot; Stock: &quot;+ STOCK[list.getSelectedIndex()]); //shows the stock
  31. }
  32. } catch (Exception a) {
  33. a.printStackTrace();
  34. }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.

huangapple
  • 本文由 发表于 2020年4月8日 14:28:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/61094537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定