Checbox help!!!
I am trying to write a program that calculates the price when the checkboxes
are selected and the calculate button is pressed.
Now I am not sure how to code the part where you click the box and the price
of that item appears. Any suggestions?
Here is what I have now:
import java.awt.*; //Abstract Window Toolkit (AWT) for creating
//painting graphics, interfaces and images
import java.applet.*; //gives the classes for an applet to create and
//communicate in applet context
import java.awt.event.*; //gives classes and interface for different
//types of events from the AWT
import java.text.DecimalFormat; //this class formats decimal numbers
public class PetsApplet extends Applet implements ActionListener
{
//labels declared
Label welcomeLabel = new Label ("Welcome to the Pet Clinic");
Label price = new Label("The total price is $0");
Label current = new Label("The current price is $0");
Label serLabel = new Label("Select the service:");
//string text declared
String currentString = new String();
String priceString = new String();
//checkboxes identified
Checkbox officeBox = new Checkbox("Office Visits $10");
Checkbox vacBox = new Checkbox("Vaccination $25");
Checkbox xrayBox = new Checkbox("X-Rays $40");
Checkbox hosBox = new Checkbox("Hospitalization $100");
Checkbox denBox = new Checkbox("Dentistry $50");
Checkbox labBox = new Checkbox("Laboratory Work $30");
Checkbox heartBox = new Checkbox("Heartworm Prevention $35");
Checkbox boardBox = new Checkbox("Boarding $5");
Checkbox preBox = new Checkbox("Prescription $55");
Button calcButton = new Button ("Calculate");
Label outputLabel = new Label("Click the calculate button for total price");
//price variables of the services declared along with the totalPrice
int totalPrice = 0;
int officeBoxPrice = 10, vacBoxPrice = 25, xrayBoxPrice = 40, hosBoxPrice =
100;
int denBoxPrice = 50, labBoxPrice = 30, heartBoxPrice = 35, boardBoxPrice =
5, preBoxPrice = 55;
public void init()
{
//adds all the components to the applet interface
setBackground (Color.blue);
add(welcomeLabel);
add(serLabel);
add(officeBox);
add(vacBox);
add(xrayBox);
add(hosBox);
add(denBox);
add(labBox);
add(heartBox);
add(boardBox);
add(preBox);
add(current);
add(calcButton);
calcButton.addActionListener(this);
add(outputLabel);
add(price);
}
public void actionPerformed(ActionEvent e)
{
//the current price statement is identified
if (officeBox.getState())
currentString = ("The current price is $"+ Integer.toString
(officeBoxPrice));
if (vacBox.getState())
currentString = ("The current price is $"+ Integer.toString (vacBoxPrice))
;
if (xrayBox.getState())
currentString = ("The current price is $"+ Integer.toString (xrayBoxPrice))
;
if (hosBox.getState())
currentString = ("The current price is $"+ Integer.toString (hosBoxPrice))
;
if (denBox.getState())
currentString = ("The current price is $"+ Integer.toString (denBoxPrice))
;
if (labBox.getState())
currentString = ("The current price is $"+ Integer.toString (labBoxPrice))
;
if (heartBox.getState())
currentString = ("The current price is $"+ Integer.toString (heartBoxPrice)
);
if (boardBox.getState())
currentString = ("The current price is $"+ Integer.toString (boardBoxPrice)
);
if (preBox.getState())
currentString = ("The current price is $"+ Integer.toString (preBoxPrice))
;
//the text is set equal to currentString
current.setText (currentString);
//total price is calculated
totalPrice = 0;
if (officeBox.getState())
totalPrice += officeBoxPrice;
if (vacBox.getState())
totalPrice += vacBoxPrice;
if (xrayBox.getState())
totalPrice += xrayBoxPrice;
if (hosBox.getState())
totalPrice += hosBoxPrice;
if (denBox.getState())
totalPrice += denBoxPrice;
if (labBox.getState())
totalPrice += labBoxPrice;
if (heartBox.getState())
totalPrice += heartBoxPrice;
if (boardBox.getState())
totalPrice += boardBoxPrice;
if (preBox.getState())
totalPrice += preBoxPrice;
//output statement for total price is identified
priceString = ("The total price is $" + Integer.toString (totalPrice));
price.setText (priceString);
//statements to clear the fields after the calculate button is clicked
officeBox.setState(false);
vacBox.setState(false);
xrayBox.setState(false);
hosBox.setState(false);
denBox.setState(false);
labBox.setState(false);
heartBox.setState(false);
boardBox.setState(false);
preBox.setState(false);
}
}