0

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

}

Michael
  • 13
  • 2
  • Don't add the shipping to the total in `getShip`, just return back the shipping cost based on the code, let the caller then decided, based on the total, if the shipping should be applied – MadProgrammer Jul 21 '18 at 21:40
  • @MadProgrammer That would also be a good change. Or change the name of the method to something a bit more descriptive, maybe "addShippingCost" and provide a quick javadoc explanation. – Kage0x3B Jul 21 '18 at 21:54
  • @Kage0x3B My general concern is about "what" the method "should" be doing. Personally, I prefer it just calculated the shipping - one job, one responsibility - a "apply discount" method might be a better choice to make the final decisions, but that's just me – MadProgrammer Jul 21 '18 at 22:18

1 Answers1

0

You can insert an if-statement into the switch case for option 3, the standard option. If the total is greater or equal to $100, you just set shipping to the total or else, you calculate add the $7.95.

case 3:
    if(total >= 100.0) {
        shipping = total;
    } else {
        shipping = total + 7.95;
    }
    break;

Or I personally would use "early returns" in this case, so I just immediately return the calculated result, improving the readability in my opinion:

public double getShip(double total, int code) {
    switch(code) {
        case 1: // Overnight Box
            return total + 16.95;
        case 2: // Express Box
            return total + 13.95;
        case 3: //Standard Box
            if(total >= 100.0) { // No shipping for orders >= $100
                return total;
            }

            return total + 7.95;
    }
}

You can find more about early returns and when/if you should use them here on Stack Overflow.

Kage0x3B
  • 93
  • 6
  • I appreciate it, that worked out well. I should have been able to figure that out most likely, but my brain is fried trying to play catch up in a 5 class summer semester. – Michael Jul 21 '18 at 22:03