如何计算 JList 元素的总和? [英] How to calculate total sum of JList elements?

查看:40
本文介绍了如何计算 JList 元素的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个购物车模拟,其中包含一个产品列表作为我的库存,一个列表用作用户可以将项目扫描到的购物车.理论上,这是在将总成本添加到购物篮时将总成本显示为列表下方 JTextField 中的项目.

I'm creating a shopping cart simulation with a list of products as my inventory and a list used as the shopping cart which the user can scan items into. This, in theory, is to display the total cost as items in a JTextField below the list as they are being added to the basket.

要扫描项目,我有以下方法:

To scan the items I have the following method :

public void actionPerformed(ActionEvent evt) {
  //Get the newly added list values.
  JList list = productList.getSelectedValuesList();
  double totalAddedValue = 0.0;
  double oldCartValue    = 0.0;

  //Iterate to get the price of the new items.
  for (int i = 0; i < list.getModel().getSize(); i++) {
    CartItem item = (CartItem) list.getModel().getElementAt(i);
    totalAddedValue += Double.ParseDouble(item.getPrice());
  }

  //Set total price value as an addition to cart total field.

  //cartTotalField must be accessible here.
  string cartFieldText = cartTotalField.getText();

  //Check that cartTextField already contains a value.
  if(cartTextField != null && !cartTextField.isEmpty())
  {
    oldCartValue = Double.parseDouble(cartFieldText);
  }

  cartTotalField.setText(String.valueOf(oldCartValue  + totalAddedValue));
  checkoutBasket.addElement(list);
}

目前我的主要问题是将一件商品扫描到购物车中将显示库存列表中所有商品的总和,而不仅仅是我要扫描的一件商品.它还将在项目名称下打印一行,例如 javax.swing.JList[,0,0,344x326,layout=java.awt.BorderLa... .我怎样才能解决这个问题?

Currently my main issue is that scanning one item into the cart will display the sum total of all items in the inventory list rather than just the one item I'm trying to scan. It will also print a line under the item name as such javax.swing.JList[,0,0,344x326,layout=java.awt.BorderLa... . How could I get around this issue ?

ItemList 类

public class StockList extends DefaultListModel {
    public StockList(){
        super();
}

public void addItem(String barcodeNo, String itemName, String price){
    super.addElement(new Item(barcodeNo, itemName, price));
}

public Item findItemByName(String name){
    Item temp;
    int indexLocation = -1;
    for (int i = 0; i < super.size(); i++) {
        temp = (Item)super.elementAt(i);
        if (temp.getItemName().equals(name)){
            indexLocation = i;
            break;
        }
    }

    if (indexLocation == -1) {
        return null;
    } else {
        return (Item)super.elementAt(indexLocation);
    }
}

public Item findItemByBarcode(String id){
    Item temp;
    int indexLocation = -1;
    for (int i = 0; i < super.size(); i++) {
        temp = (CheckoutItem)super.elementAt(i);
        if (temp.getBarcodeNo().equals(id)){
            indexLocation = i;
            break;
        }
    }

    if (indexLocation == -1) {
        return null;
    } else {
        return (Item)super.elementAt(indexLocation);
    }        
}

public void Item(String id){
    Item empToGo = this.findItemByBarcode(id);
    super.removeElement(empToGo);
}

}

推荐答案

javax.swing.JList[,0,0,344x326,layout=java.awt.BorderLa... .

那是 JList 的 toString() 表示.JList 默认渲染器只调用添加到 ListModel 的任何对象的 toString() 方法.

That is the toString() representation of a JList. The JList default renderer just invokes the toString() method of any object added to the ListModel.

checkoutBasket.addElement(list);

不要将 JList 添加到结帐篮中.您必须将列表中的每个项目分别添加到结帐篮中.

Don't add the JList to the checkout basket. You have to add each item in the list to the checkout basket separately.

这篇关于如何计算 JList 元素的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆