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 = new JCheckBox("Use points to pay!");
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
CheckboxGroup cbg1 = new CheckboxGroup(); //flower types (checkboxes),quantity (dropdownboxes) ()
CheckboxGroup cbg2 = new CheckboxGroup(); //extras,include (checkboxes): card, vase, balloonm bear
JCheckBox rose = new JCheckBox ("Rose");
JCheckBox daisy= new JCheckBox ("Daisy");
JCheckBox tulip= new JCheckBox ("Tulip");
JCheckBox lily= new JCheckBox ("Lily");
JCheckBox mixed= new JCheckBox ("Mixed"); // must be bought by the dozen quantity
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);
//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.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,3));
cMiddle.add(new JLabel ("TYPE"));
cMiddle.add(new JLabel ("QUANTITY (by single)"));
cMiddle.add(new JLabel ("QUANTITY (by Dozen)"));
cMiddle.add(rose);
cMiddle.add(rSingle);
cMiddle.add(rDozen);
cMiddle.add(daisy);
cMiddle.add(dSingle);
cMiddle.add(dDozen);
cMiddle.add(tulip);
cMiddle.add(tSingle);
cMiddle.add(tDozen);
cMiddle.add(lily);
cMiddle.add(lSingle);
cMiddle.add(lDozen);
cMiddle.add(mixed);
cMiddle.add(mSingle);
cMiddle.add(mDozen);
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.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, item_numD;
double result;
if (e.getSource()==rose){
item_numS = Integer.parseInt(rSingle.getText());
item_numD = Integer.parseInt(rDozen.getText());
if (usePts.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));
}
if (e.getSource() == daisy){
}
}
}