abc4616
09-25-2006, 08:56 PM
I am having trouble with the checkboxes. I cannot get price to show up everytime I check the box and update the total. Then after the boxes are checked, the calculate button doesn't seem to work. How can I approach this?
Here is the program:
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 ItemListener,
ActionListener
{
//declare variables
double dollars, answer;
int serCode, cost;
Label serLabel = new Label("Select the service:");
//Check box
Checkbox officeBox = new Checkbox("Office Visits");
Checkbox vacBox = new Checkbox("Vaccination");
Checkbox xrayBox = new Checkbox("X-Rays");
Checkbox hiddenBox = new Checkbox("");
Button calcButton = new Button ("Calculate");
Label outputLabel = new Label("Click the calcuale button for total amount");
public void init()
{
add(serLabel);
add(officeBox);
officeBox.addItemListener(this);
add(vacBox);
vacBox.addItemListener(this);
add(xrayBox);
xrayBox.addItemListener(this);
add(calcButton);
calcButton.addActionListener(this);
add(outputLabel);
}
//This method is triggered by the user clicking an option button
public void itemStateChanged(ItemEvent Choice)
{
try
{
serCode = getCode();
answer = getCost (dollars,serCode);
output(answer,dollars);
}
catch (NumberFormatException e)
{
outputLabel.setText("You must select a service.");
hiddenBox.setState(true);
}
}
public int getCode()
{
int code = 0;
if (officeBox.getState()) code = 1;
else
if (vacBox.getState()) code = 2;
else
if (xrayBox.getState()) code = 3;
else
if (code == 4)
{
officeBox.getState();
vacBox.getState();
}
else
if (code == 5)
{
officeBox.getState();
xrayBox.getState();
}
else
if (code == 6)
{
vacBox.getState();
xrayBox.getState();
}
else
if (code == 7)
{
officeBox.getState();
vacBox.getState();
xrayBox.getState();
}
return code;
}
public static double getCost(double dollars, int serCode)
{
double cost = 0.0;
switch(serCode)
{
case 1:
cost = 10;
break;
case 2:
cost = 25;
break;
case 3:
cost = 40;
break;
}
return cost;
}
public void actionPerformed(ActionEvent e)
{
//calculations
answer=0;
if (officeBox.getState())
answer=answer+10;
if (vacBox.getState())
answer=answer+20;
if (xrayBox.getState())
answer=answer+30;
}
public void output(double cost, double dollars)
{
DecimalFormat twoDigits = new DecimalFormat("$#,#00.00");
outputLabel.setText("Your total amount is " +
twoDigits.format(answer));
}
}
Here is the program:
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 ItemListener,
ActionListener
{
//declare variables
double dollars, answer;
int serCode, cost;
Label serLabel = new Label("Select the service:");
//Check box
Checkbox officeBox = new Checkbox("Office Visits");
Checkbox vacBox = new Checkbox("Vaccination");
Checkbox xrayBox = new Checkbox("X-Rays");
Checkbox hiddenBox = new Checkbox("");
Button calcButton = new Button ("Calculate");
Label outputLabel = new Label("Click the calcuale button for total amount");
public void init()
{
add(serLabel);
add(officeBox);
officeBox.addItemListener(this);
add(vacBox);
vacBox.addItemListener(this);
add(xrayBox);
xrayBox.addItemListener(this);
add(calcButton);
calcButton.addActionListener(this);
add(outputLabel);
}
//This method is triggered by the user clicking an option button
public void itemStateChanged(ItemEvent Choice)
{
try
{
serCode = getCode();
answer = getCost (dollars,serCode);
output(answer,dollars);
}
catch (NumberFormatException e)
{
outputLabel.setText("You must select a service.");
hiddenBox.setState(true);
}
}
public int getCode()
{
int code = 0;
if (officeBox.getState()) code = 1;
else
if (vacBox.getState()) code = 2;
else
if (xrayBox.getState()) code = 3;
else
if (code == 4)
{
officeBox.getState();
vacBox.getState();
}
else
if (code == 5)
{
officeBox.getState();
xrayBox.getState();
}
else
if (code == 6)
{
vacBox.getState();
xrayBox.getState();
}
else
if (code == 7)
{
officeBox.getState();
vacBox.getState();
xrayBox.getState();
}
return code;
}
public static double getCost(double dollars, int serCode)
{
double cost = 0.0;
switch(serCode)
{
case 1:
cost = 10;
break;
case 2:
cost = 25;
break;
case 3:
cost = 40;
break;
}
return cost;
}
public void actionPerformed(ActionEvent e)
{
//calculations
answer=0;
if (officeBox.getState())
answer=answer+10;
if (vacBox.getState())
answer=answer+20;
if (xrayBox.getState())
answer=answer+30;
}
public void output(double cost, double dollars)
{
DecimalFormat twoDigits = new DecimalFormat("$#,#00.00");
outputLabel.setText("Your total amount is " +
twoDigits.format(answer));
}
}