【问题标题】:Java applet shipping calculator, unsure how to add free shipping if under $100Java 小程序运费计算器,如果低于 100 美元,不确定如何添加免费送货
【发布时间】:2018-12-29 19:33:09
【问题描述】:

我正在课堂上做一个编码作业。这是一个虚构蜡烛网站的基本运费计算器。客户会输入他们的销售总额,然后选择他们想要的运输水平,它会说成本。选项是隔夜、优先或标准。一夜之间,优先级是固定成本,所以这很容易。但如果订单 100 美元,标准是 0 美元。

我为订单总额创建了一个文本字段,并为隔夜、优先级和标准创建了复选框。而且奇迹不小(我在这门课上很烂)我实际上在这里有一个功能性小程序,除了我不知道如何计算订单总额> 100美元==免费送货。

我觉得我需要创建一个 case #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));
 }

}

【问题讨论】:

  • getShip中的运费不要加到总计中,只根据代码返回运费,让调用者根据总计决定是否应用运费
  • @MadProgrammer 这也是一个很好的改变。或者将方法的名称更改为更具描述性的名称,例如“addShippingCost”并提供快速的 javadoc 解释。
  • @Kage0x3B 我一般关心的是方法“应该”做什么。就个人而言,我更喜欢它只是计算运费——一份工作,一项责任——“应用折扣”方法可能是做出最终决定的更好选择,但这只是我

标签: java


【解决方案1】:

您可以在选项 3(标准选项)的 switch case 中插入 if 语句。如果总额大于或等于 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找到更多关于提前退货以及何时/是否应该使用它们的信息。

【讨论】:

  • 我很感激,效果很好。我很可能应该能够弄清楚这一点,但是我的大脑在试图赶上 5 节课的夏季学期。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
  • 2020-11-05
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多