import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class CandleApplet extends Applet implements ItemListener
{
//Declares variables
double dollars, answer,sales, amount;
int empCode;
//Create components for applet
Label candleLabel = new Label("CandleLine -- Candles Online");
Label orderLabel = new Label("Please enter the total dollar amount of your order:");
TextField amountField = new TextField(20);
Label shippingLabel = new Label("Please choose your method of shipping:");
CheckboxGroup deliveryGroup = new CheckboxGroup();
Checkbox priorityBox = new Checkbox("Priority (Overnight)",false,deliveryGroup);
Checkbox expressBox = new Checkbox("Express (2 business days)",false,deliveryGroup);
Checkbox standardBox = new Checkbox("Standard (3 to 7 business days)",false,deliveryGroup);
Checkbox hiddenBox = new Checkbox("", true,deliveryGroup);
Label outputLabel = new Label("We guarantee on time delivery, or your money back.");
public void init()
{
setBackground(Color.cyan);
setForeground(Color.black);
add(candleLabel);
add(orderLabel);
add(amountField);
amountField.requestFocus();
amountField.setForeground(Color.black);
add(shippingLabel);
add(priorityBox);
priorityBox.addItemListener(this);
add(expressBox);
expressBox.addItemListener(this);
add(standardBox);
standardBox.addItemListener(this);
add(outputLabel);
}
//This method is triggered by the user clicking an option button.
public void itemStateChanged(ItemEvent choice)
{
try
{
dollars = getShipping();
empCode = getCode();
answer = getAmount(dollars,empCode);
output(answer, dollars);
}
catch (NumberFormatException e)
{
outputLabel.setText("If your order is more than $100.00 we will ship it for free.");
hiddenBox.setState(true);
amountField.setText("");
amountField.requestFocus();
}
}
public double getShipping()
{
double dollars = Double.parseDouble(amountField.getText());
if (dollars >=100.01) throw new NumberFormatException();
return dollars;
}
public int getCode()
{
int code = 0;
if (priorityBox.getState()) code = 1;
else
if (expressBox.getState()) code = 2;
else
if (standardBox.getState()) code = 3;
return code;
}
public double getAmount(double dollars, int code)
{
double [] shippingFees = {16.95, 13.95, 7.95};
return dollars + shippingFees[code-1];
}
public void output(double amount, double sales)
{
DecimalFormat twoDigits = new DecimalFormat("$#,00.00");
outputLabel.setText("The total of your order is" + twoDigits.format(sales));
}
}
*edit: Forgot the forward slash in the closing tag. Thanks for using the code tags.
This post has been edited by Martyr2: 01 May 2008 - 12:49 PM

New Topic/Question
Reply




MultiQuote





|