Results 1 to 1 of 1
  1. #1
    newbie_vs is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Post calculate multiple checkboxes?

    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


    Java Code:
    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));
            }
    Last edited by newbie_vs; 12-06-2010 at 03:55 PM.

Similar Threads

  1. Java help...Checkboxes
    By university123 in forum New To Java
    Replies: 32
    Last Post: 10-25-2010, 12:39 PM
  2. ComboBox with CheckBoxes
    By heba.farouk in forum AWT / Swing
    Replies: 14
    Last Post: 06-09-2010, 11:03 AM
  3. CheckBoxes and JTables
    By lakshayghai in forum AWT / Swing
    Replies: 1
    Last Post: 03-16-2010, 08:01 PM
  4. Replies: 1
    Last Post: 08-02-2009, 12:12 PM
  5. Problem with checkboxes
    By carl in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 08:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •