I am doing a coding assignment from class. It's a basic shipping calculator for a fictional candle website. Customer would enter their sales total, then pick the level of shipping they want, and it would say the cost. The options are overnight, priority, or standard. Overnight and priority is fixed cost, so that's pretty easy. But standard is $7.95 if the order is < $100 or $0 if order is > $100.
I've created a text field for the order total, as well as check boxes for overnight, priority, and standard. And by no small miracle (I suck at this class) I actually have a functional applet here, except I don't know how to account for the order total being > $100 == free shipping.
I feel like I need to create a case #4 where shipping = total + 0 , but I'm unsure how to make a code 4 or where I add a true/false or if..else statement to get this to work.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class ShippingApplet extends Applet implements ItemListener
{
int shipSpeed;
double dollars, answer;
Label headLabel = new Label ("CandleLine--Candles Online");
Label promptLabel = new Label("Please enter the total dollar amount of your order:");
TextField totalField = new TextField(20);
Label hiddenLabel = new Label("");
Label codeLabel = new Label ("Please choose your method of shipping:");
CheckboxGroup codeGroup = new CheckboxGroup();
Checkbox overnightBox = new Checkbox("Priority(Overnight)",false,codeGroup);
Checkbox expressBox = new Checkbox("Express (2 business days)",false,codeGroup);
Checkbox standardBox = new Checkbox("Standard (3 to 7 business days)",false,codeGroup);
Checkbox hiddenBox = new Checkbox("",true,codeGroup);
Label outputLabel=new Label("We guarantee on time delivery, or your money back.");
public void init()
{
setBackground(Color.cyan);
setForeground(Color.black);
add(headLabel);
add(promptLabel);
add(totalField);
totalField.requestFocus();
totalField.setForeground(Color.black);
add(hiddenLabel);
add(codeLabel);
add(overnightBox);
overnightBox.addItemListener(this);
add(expressBox);
expressBox.addItemListener(this);
add(standardBox);
standardBox.addItemListener(this);
add(outputLabel);
}
public void itemStateChanged(ItemEvent choice)
{
try
{
dollars = getTotal();
shipSpeed = getCode();
answer = getShip(dollars,shipSpeed);
output(answer, dollars);
}
catch(NumberFormatException e)
{
outputLabel.setText("You must enter a dollar amount greater than zero.");
hiddenBox.setState(true);
totalField.setText("");
totalField.requestFocus();
}
}
public double getTotal()
{
double total = Double.parseDouble(totalField.getText());
if (total <= 0) throw new NumberFormatException();
return total;
}
public int getCode()
{
int code = 0;
if (overnightBox.getState()) code = 1;
else
if (expressBox.getState()) code = 2;
else
if (standardBox.getState()) code = 3;
return code;
}
public double getShip(double total, int code)
{
double shipping = 0.0;
switch(code)
{
case 1:
shipping = total + 16.95;
break;
case 2:
shipping = total + 13.95;
break;
case 3:
shipping = total + 7.95;
break;
}
return shipping;
}
public void output(double shipping, double total)
{
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
outputLabel.setText("Your order of " + twoDigits.format(total) + " plus shipping totals to: " + twoDigits.format(shipping));
}
}