Click to See Complete Forum and Search --> : How to use checkboxes and and buttons to do simple calculations?


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));
}

}

agent_x91
09-26-2006, 05:57 AM
Please use PHP tags to display your code next time it makes it much easier to read:)

I'm trying to work out what your problem is from your code hang on and I'll get back to you :)

abc4616
09-26-2006, 11:04 PM
Ok!! I'll wait for you!!

agent_x91
09-28-2006, 08:21 AM
I can't see exactly why you're having the problem frm the code, but I was curious about this method:


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;
}



Why have you included the argument dollars if it's never used in the method? :confused: