I have problem how to calculate mutiple checkboxes on the panel. ex:
24 roses with a bear: This costs $72 or 1000 points and $12.
14 Lilies: $24 or 360 points

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.io.*;
import java.text.NumberFormat;

public class customerInfoGUI extends JFrame implements ActionListener{
    
    //data fields
    String memberType;
    String memberPoints; 
    double result =0.0;
    
    //03 main panels 
    JPanel topPanel       = new JPanel ();
    JPanel centerPanel    = new JPanel ();
    JPanel bottomPanel    = new JPanel ();
    

    //topPanel: include Customer's memberType, currentPts
    JLabel custType_Label = new JLabel("Member Type:");    
    JTextField custType_tField = new JTextField();
    JLabel custInfo_Label = new JLabel("Current points:");
    JTextField custInfo_tField = new JTextField();
    JCheckBox usePts_r = new JCheckBox("Use points!");
    JCheckBox usePts_d = new JCheckBox("Use points!");
    JCheckBox usePts_t = new JCheckBox("Use points!");
    JCheckBox usePts_l = new JCheckBox("Use points!");
    JCheckBox usePts_m = new JCheckBox("Use points!");    
    JLabel space = new JLabel();
    
    //centerPanel: include Price Table (center_LEFT), selection (center_RIGHT)
                                                    //selecting the checkBox groups (flower types, quanlity, extras)            
        //center_LEFT: display price table                                              
        //JTextArea tableArea = new JTextArea(200,100);
        JLabel roseLabel = new JLabel ("Rose");
        JLabel daisyLabel = new JLabel ("Daisy");        
        JLabel tulipLabel = new JLabel ("Tulip");
        JLabel lilyLabel = new JLabel ("Lily");        
        JLabel mixedLabel = new JLabel ("Mixed");
        
        //center_MIDDLE
        ButtonGroup rbg = new ButtonGroup(); //flower types (checkboxes),quantity (dropdownboxes) ()
        //CheckboxGroup cbg2 = new CheckboxGroup(); //extras,include (checkboxes): card, vase, balloonm bear
        JRadioButton rose = new JRadioButton ("Rose");
        JRadioButton daisy= new JRadioButton ("Daisy");    
        JRadioButton tulip= new JRadioButton ("Tulip");
        JRadioButton lily= new JRadioButton ("Lily");
        JRadioButton mixed= new JRadioButton ("Mixed"); // must be bought by the dozen quantity
        
//      rbg.add(rose);
//      rbg.add(daisy);
//      rbg.add(tulip);
//      rbg.add(lily);
//      rbg.add(mixed);
        
        JTextField rSingle = new JTextField(2);
        JTextField dSingle = new JTextField(2);
        JTextField tSingle = new JTextField(2);
        JTextField lSingle = new JTextField(2);
        JTextField mSingle = new JTextField(2);     //this textField should be BLACKOUT if the checkbox of Mixed isSelected()   
        JTextField rDozen = new JTextField(2);
        JTextField dDozen = new JTextField(2);
        JTextField tDozen = new JTextField(2);
        JTextField lDozen = new JTextField(2);
        JTextField mDozen = new JTextField(2);        
        
        
        //center_RIGHT
        JCheckBox card = new JCheckBox ("Card ($2)");
        JCheckBox vase = new JCheckBox ("Vase ($15)");    
        JCheckBox balloons = new JCheckBox ("Balloons ($10)");
        JCheckBox bear = new JCheckBox ("Bear ($12)");
        
        
    //bottomPanel: display current order items, quantity, and total
    JTextArea currOrderArea = new JTextArea(); //list current order
    JButton addBtn = new JButton("Add");
    JButton clearBtn = new JButton("Clear");
    JButton exitBtn = new JButton("Exit");
    
    //data fields
    private final double CARD = 2.0;
    private final double VASE = 15.0;
    private final double BALLOONS = 10.0;
    private final double BEAR = 12.0;
    
    //constructor
    public customerInfoGUI(){
        
        super ("Customer Information");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(900,450);
        
     //to determine if the customer is a reward member
        //memberType = JOptionPane.showInputDialog("Are you reward member? (answer yes/no) ");
        //custType_tField.setText(memberType);
        do{
            memberType = JOptionPane.showInputDialog("Are you reward member? (answer yes/no) ");
            custType_tField.setText(memberType);        
                if (memberType.equalsIgnoreCase("yes")){ //but it has ERROR if the user click "Cancel" on the InputDialogbox
                    memberPoints = JOptionPane.showInputDialog("How many points you have? ");                   
                    custInfo_tField.setText(memberPoints);
                    break;
                }else if (memberType.equalsIgnoreCase("no")){
                    custInfo_tField.setText(null);
                    break;
                }else
                    JOptionPane.showMessageDialog(null,"Please enter the correct answer as indicated!!!");
        }while(!memberType.equalsIgnoreCase("yes") || !memberType.equalsIgnoreCase("no"));     

       
       
        //Menu
        JMenu fileMenu = new JMenu ("File");
        JMenuItem newOrderItem = new JMenuItem ("New order");
        JMenuItem clearOrderItem = new JMenuItem ("Clear order");
        JMenuItem exitItem = new JMenuItem ("Exit", KeyEvent.VK_X);        //Mnemonic for the Exit
        

        //setAccelerator for key stroke
        newOrderItem.setAccelerator (KeyStroke.getKeyStroke('N',Event.CTRL_MASK));
        clearOrderItem.setAccelerator (KeyStroke.getKeyStroke('C',Event.CTRL_MASK));
        
        fileMenu.add(newOrderItem);
        fileMenu.add(clearOrderItem);
        fileMenu.add(exitItem);        
        
        //create the menu bar to hold the menu
        JMenuBar menuBar = new JMenuBar();
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);
        
        //add to the frame
        getContentPane().add(topPanel,BorderLayout.NORTH);            
        getContentPane().add(centerPanel,BorderLayout.CENTER);            
        getContentPane().add(bottomPanel,BorderLayout.SOUTH);            

        /*****************************************************************
         * topPanel
         *****************************************************************/
        //centerPanel.setLayout(new FlowLayout());
        topPanel.setLayout(new GridLayout(1,3));        
        topPanel.add(custType_Label);
        topPanel.add(custType_tField);        
        topPanel.add(custInfo_Label);
        topPanel.add(custInfo_tField);
        topPanel.add(space);
        //topPanel.add(usePts);
        
        /*****************************************************************
         * centerPanel
         *****************************************************************/
         //centerPanel.setLayout(new FlowLayout());
         centerPanel.setLayout(new GridLayout(1,3));
        
        JPanel cLeft = new JPanel();//Panel has FlowLayout be default
        cLeft.setLayout(new GridLayout(6,3));
        cLeft.setBorder(new TitledBorder("Price Table"));;
        centerPanel.add(cLeft, BorderLayout.WEST);        
        cLeft.add(new JLabel("TYPE"));        
        cLeft.add(new JLabel ("PRICE (1 flower)"));                 
        cLeft.add(new JLabel ("PRICE (1 dozen)"));                        
        cLeft.add(roseLabel);
        cLeft.add(new JLabel ("$3 or 50 points"));                
        cLeft.add(new JLabel ("$30 or 500 points"));                        
        cLeft.add(daisyLabel);
        cLeft.add(new JLabel ("$1 or 20 points"));                
        cLeft.add(new JLabel ("$10 or 200 points"));                
        cLeft.add(lilyLabel);
        cLeft.add(new JLabel ("$2 or 30 points"));                        
        cLeft.add(new JLabel ("$20 or 300 points"));                
        cLeft.add(tulipLabel);
        cLeft.add(new JLabel ("$2 or 30 points"));                        
        cLeft.add(new JLabel ("$20 or 300 points"));                        
        cLeft.add(mixedLabel);        
        cLeft.add(new JLabel ("--------------"));
        cLeft.add(new JLabel ("$25 or 400 points"));                

        
        JPanel cMiddle = new JPanel();//Panel has FlowLayout be default
        cMiddle.setBorder(new TitledBorder("Selection:"));;
        centerPanel.add(cMiddle, BorderLayout.CENTER);                
        cMiddle.setLayout(new GridLayout(6,4));
        cMiddle.add(new JLabel ("TYPE"));
        cMiddle.add(new JLabel ("Single"));        
        cMiddle.add(new JLabel ("Dozen"));        
        cMiddle.add(space);
        cMiddle.add(rose);
        cMiddle.add(rSingle);
        cMiddle.add(rDozen);        
        cMiddle.add(usePts_r);
        cMiddle.add(daisy);
        cMiddle.add(dSingle);
        cMiddle.add(dDozen);
        cMiddle.add(usePts_d);
        cMiddle.add(tulip);
        cMiddle.add(tSingle);
        cMiddle.add(tDozen);
        cMiddle.add(usePts_t);
        cMiddle.add(lily);
        cMiddle.add(lSingle);
        cMiddle.add(lDozen);
        cMiddle.add(usePts_l);
        cMiddle.add(mixed);
        cMiddle.add(mSingle);        
        cMiddle.add(mDozen);
        cMiddle.add(usePts_m);
        //radiobutton group
        rbg.add(rose);
        rbg.add(daisy);
        rbg.add(tulip);
        rbg.add(lily);
        rbg.add(mixed);
        
        
        JPanel cRight = new JPanel();//Panel has FlowLayout be default
        cRight.setBorder(new TitledBorder("Extras:"));;        
        cRight.setLayout(new GridLayout(4,1));
        centerPanel.add(cRight, BorderLayout.EAST);                        
        cRight.add(card);
        cRight.add(vase);
        cRight.add(balloons);
        cRight.add(bear);        
        
        
        /*****************************************************************
         * bottomPanel : display order while customer still checking...
         *****************************************************************/
        bottomPanel.setLayout(new GridLayout(1,3)); 
        
        JPanel btnPanel = new JPanel();
        btnPanel.add(addBtn);
        btnPanel.add(clearBtn);
        btnPanel.add(exitBtn);
        bottomPanel.setLayout(new BorderLayout());
        bottomPanel.add(currOrderArea,BorderLayout.CENTER);
        bottomPanel.add(btnPanel,BorderLayout.EAST);
        currOrderArea.setPreferredSize(new Dimension(300,100));
        currOrderArea.setEditable(false);
        
/*****************************************************************
* Add action listener to components, which have actions
*****************************************************************/
//        inFTextField.setName("inF");

        rose.addActionListener(this);
        daisy.addActionListener(this);    
        tulip.addActionListener(this);
        lily.addActionListener(this);
        mixed.addActionListener(this);
        

    }
    
    
    
    //main method
    public static void main (String []args){
        double result =0.0;
        JFrame mainForm = new customerInfoGUI();
        mainForm.pack();
        mainForm.setVisible (true);
        
        //System.out.print(result);
        
    }
    
    
    public void actionPerformed (ActionEvent e){  
        double item_subtotal;
        //String input_Single, input_Dozen;
        int item_numS = 0, item_numD = 0;
        double result;
        
        //Button Add
        if(e.getSource()==addBtn){
            //add new item into textArea => append String
            
            if (rose.isSelected() && usePts_r.isSelected() ){
                
                item_numS = Integer.parseInt(rSingle.getText());
                item_numD = Integer.parseInt(rDozen.getText());
                //if (rose.isSelected()&& usePts_r.isSelected()){
                    result = (item_numS*50) + (item_numD*500);
                    //currOrderArea.setText(result);
            }else{
                    result = (item_numS*3.0) + (item_numD*30.0);
                    //currOrderArea.setText(result);
            }
                //result = Integer.parseInt(input_Single)*
                //calculate (item_numS, item_numD);
                NumberFormat nf = NumberFormat.getCurrencyInstance();
                currOrderArea.setText(nf.format(result));
        }

Recommended Answers

All 4 Replies

Write first CustomerInfo program without GUI
with the input data as in your example
24 roses
TOTAL=
1 bear
TOTAL=
14 Lilies
TOTAL=

should I do that in a separate class, then extends it to?


Write first CustomerInfo program without GUI
with the input data as in your example
24 roses
TOTAL=
1 bear
TOTAL=
14 Lilies
TOTAL=

Yes, do it in separate class (classes).
Define first three arrays in new class Product
with NamesOfProducts
with Prices
with Points
-in the correct order and type

write constructor Product(int id)
where id identifies product from array
introduce field id in this class
...
write methods to get name,price,point using id and three arrays
...
write second constructor Product(int id,int items)
write methods to get totalPrice,totalPoint using id and three arrays and items
In the main(...)write
Product rose=new Product(0,24); // 24 roses
int totalPriceRose=rose.getTotalPrice();

...

write third constructor

Product(int id, int items, boolean usePoints) {
        this.id = id;
        this.items = items;
        this.usePoints = usePoints;
    }

run main

public static void main(String[] args) {
        Product rose = new Product(0, 24, false); // 24 roses price
        System.out.println(rose.getName());
        System.out.println(rose.getPrice());
        System.out.println(rose.getPoints());
        System.out.println(rose.getItems());
        System.out.println(rose.getTotalPrice());
        System.out.println(rose.getTotalPoints());
        //
        System.out.println();
        //
        Product rose2 = new Product(0, 24, true); // 24 roses points
        System.out.println(rose2.getName());
        System.out.println(rose2.getPrice());
        System.out.println(rose2.getPoints());
        System.out.println(rose2.getItems());
        System.out.println(rose2.getTotalPrice());
        System.out.println(rose2.getTotalPoints());
    }

in my case results are
Rose
3
50
24
72
0

Rose
3
50
24
0
1200

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.