import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
// click on the checkbox
// the total price will show up
public class CheckBoxTest2 extends JFrame implements ActionListener {
    // the label the display the price
    private JLabel display = new JLabel("The total price is $0");
    // a list of JCheckBox, with the the electronic on it 
    // with the price
    private JCheckBox computer = new JCheckBox("Computer: $900");    
    private JCheckBox dvd = new JCheckBox("DVD: $250");
    private JCheckBox printer = new JCheckBox("Printer: $70");  
    private JCheckBox monitor = new JCheckBox("Monitor: $299");   
    private JPanel pricePanel = new JPanel(new GridLayout(1,0));
    private int price = 0;
    
    // constructor
    public CheckBoxTest2() {
	getContentPane().add(display);
	// add the components to pricePanel
	pricePanel.add(computer);
	pricePanel.add(dvd);
	pricePanel.add(printer);
	pricePanel.add(monitor);
	
	// add actionListener to all the check boxes
	computer.addActionListener(this);
	dvd.addActionListener(this);
	printer.addActionListener(this);
	monitor.addActionListener(this);
	
	getContentPane().add(pricePanel, BorderLayout.SOUTH);
    }
    
    public static void main(String[] args) {
        CheckBoxTest2 f = new CheckBoxTest2();
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	f.pack();
	f.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
	// check the source first
	// if the source is selected, than add to the price
	// else, it is unselected, reduce from the price.
	if(e.getSource() == computer) {
	    if(computer.isSelected()) {
		price+=900;
	    } else {
		price-=900;
	    }
	} else if (e.getSource() == dvd) {
	    if(dvd.isSelected()) {
		price+=250;
	    } else {
		price-=250;
	    }
	} else if(e.getSource() == printer) {
	    if(printer.isSelected()){
		price+=70;
	    } else {
		price-=70;
	    }
	} else if(e.getSource() == monitor) {
	    if(monitor.isSelected()) {
		price+=299;
	    } else {
		price-=299;
	    }
	}
	// update the price
	display.setText("The total price is $" + price);
    }
	    
}
	
	
    
