Home > Back-end >  How to get the result of only CHECKED checkboxes and count Total
How to get the result of only CHECKED checkboxes and count Total

Time:11-12

I am doing pet project and using NetBeans JFrame. I facing with a problem , i can count only one checked chexBox Total . But how can i implement a method or smth, that can help to count all checked chexBoxes' Total. Would be very glad for answer How can i implement and count checked prices? Here is my code and Screenshot below

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         

 
// TODO add your handling code here:
                double Total = 0.0;
              
               if(cbCappuccino.isSelected()){
                    String Cappucino = chboxCappucino.getValue().toString();
                     int boom1 = Integer.parseInt(Cappucino);
                    Total =  boom1 * 1.0;
                }
                else if(cbAmericano.isSelected()){
                    String Americano = chboxAmericano.getValue().toString();
                     int boom2 = Integer.parseInt(Americano);
                    Total = Total boom2 * 1.50;
               }
               else if(cbLatte.isSelected()){
                    String Latte = chboxLatte.getValue().toString();
                     int boom3 = Integer.parseInt(Latte);
                    Total = Total   boom3 * 2.50;
               } 
                
                totalText.setText(Double.toString(Total));

    }                                        

and my screenshot here i unfortunately i can just count only Cappuccino's price

CodePudding user response:

Like @maloomeister and @vincrichaud adviced i did like this:

              
               if(cbCappuccino.isSelected()){
                    String Cappucino = chboxCappucino.getValue().toString();
                     int boom1 = Integer.parseInt(Cappucino);
                    Total = Total  boom1 * 1.0;
                }
                 if(cbAmericano.isSelected()){
                    String Americano = chboxAmericano.getValue().toString();
                     int boom2 = Integer.parseInt(Americano);
                    Total = Total boom2 * 1.50;
               }
                if(cbLatte.isSelected()){
                    String Latte = chboxLatte.getValue().toString();
                     int boom3 = Integer.parseInt(Latte);
                    Total = Total   boom3 * 2.50;
               } 
                
                totalText.setText(Double.toString(Total));

i just take out if-else statements and just put if statements, and added Total = Total etc

CodePudding user response:

For a better OOP solution consider representing each item as an object encapsulating the view and properties of a single line item:

import java.awt.*;
import javax.swing.*;

class Item extends JPanel{

    private final JCheckBox selected;
    private final JComboBox<Integer> qty;
    private final double price;
    private final String name;
    private static final Integer[] QTYS = {0,1,2,3,4,5,6,7,8,9};

    public Item(String name, double price) {
        this.price = price;
        this.name = name;
        selected = new JCheckBox(name, false);
        qty = new JComboBox<>(QTYS);
        qty.setSelectedIndex(0);
        setLayout(new GridLayout(1, 0, 15, 0));
        add(selected);
        add(qty);
        JLabel cost = new JLabel(String.format("%,.2f", price), JLabel.CENTER);
        add(cost);
    }

    public boolean isSelected() {
        return selected.isSelected();
    }

    public int getQty() {
        return (int)qty.getSelectedItem();
    }

    public double getPrice() {
        return price;
    }

    public double getTotal() {
        return isSelected() ? getQty()*getPrice() : 0;
    }

    @Override
    public String getName() {
        return name;
    }
}

Todo:

  • Add clear() method
  • improve the posted code by splitting it into two classes: a model and a view.

Side note: consider using a JListas a parent container for all Items.

  • Related