I have a problem with my java applet assignment. For some reason that I cannot figure out, whenever I check a checkbox all of my if statements fire at once. The program is supposed to update the Total Price and notify the user each time a box is pressed. We must use checkbox and not radio buttons or JCheckBox and the program must be written as an applet. Here is a link to my code.
// Classes
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class ReasonableComputersChristiansen extends Applet implements ItemListener
{
// Variables
Image logo;
double basePrice;
Label companyLabel = new Label("Reasonable Computers");
Label instructionLabel = new Label("Please select your desired peripherals from the list below");
Label outputLabel = new Label("TOTAL PRICE: ");
Checkbox printerBox = new Checkbox("Ink Waster 9000");
Checkbox monitorBox = new Checkbox("EvilCorp Monitor");
Checkbox keyboardBox = new Checkbox("Clickity-Clackity Keyboard");
Checkbox mouseBox = new Checkbox("Worst-Aim Laser Gaming Mouse");
Checkbox joystickBox = new Checkbox("Crash and Burn X-2 Joystick");
Checkbox webcamBox = new Checkbox("Not-So-Anonymous Webcam");
// Components
public void init()
{
add(companyLabel);
add(instructionLabel);
add(printerBox);
printerBox.addItemListener(this);
add(monitorBox);
monitorBox.addItemListener(this);
add(keyboardBox);
keyboardBox.addItemListener(this);
add(mouseBox);
mouseBox.addItemListener(this);
add(joystickBox);
joystickBox.addItemListener(this);
add(webcamBox);
webcamBox.addItemListener(this);
add(outputLabel);
logo = getImage(getDocumentBase(), "logo.gif");
}
// Actions
public void itemStateChanged(ItemEvent e)
{
basePrice = 575.00;
if (printerBox.getState());
{
basePrice += 69.99;
System.out.println("Printer added!");
}
if (monitorBox.getState());
{
basePrice += 125.49;
System.out.println("Monitor added!");
}
if (keyboardBox.getState());
{
basePrice += 55.59;
System.out.println("Keyboard added!");
}
if (mouseBox.getState());
{
basePrice += 19.99;
System.out.println("Mouse added!");
}
if (joystickBox.getState());
{
basePrice += 224.97;
System.out.println("Joystick added!");
}
if (webcamBox.getState());
{
basePrice += 99.19;
System.out.println("Webcam added!");
}
}
// Output
public void output()
{
outputLabel.setText("TOTAL PRICE: $" + (basePrice));
}
}