Java Applet运费计算器,如果价格低于$ 100,则不确定如何增加免运费

时间:2018-07-21 21:35:54

标签: java

我正在上课时进行编码作业。这是一个虚构的蜡烛网站的基本运输计算器。客户将输入他们的销售总额,然后选择他们想要的运输水平,然后说成本。选项为通宵,优先或标准。过夜和优先是固定成本,所以这很容易。但是,如果订单<$ 100,则标准价格为$ 7.95;如果订单> $ 100,则标准价格为$ 0。

我为订单总额创建了一个文本字段,并为隔夜,优先和标准创建了复选框。奇迹般的(我在这堂课上很烂)我实际上在这里有一个实用的applet,只是我不知道如何计算订单总额> $ 100 ==免运费。

我觉得我需要创建#4案例,其中shipping = total + 0,但是我不确定如何编写代码4或在其中添加true / false或if..else语句来获取此代码。工作。

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

}

1 个答案:

答案 0 :(得分:0)

您可以将if语句插入选项3(标准选项)的开关盒中。如果总金额大于或等于100美元,则只需将运费设置为总金额,否则,您需要计算得出7.95美元。

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

或者在这种情况下,我个人会使用“早期回报”,因此我立即返回计算结果,以提高可读性:

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

您可以找到有关提早退还以及何时/是否应使用here on Stack Overflow的更多信息。