Java applet shipping calculator, unsure how to add free shipping if under $100
我在课堂上做一个编码作业。它是一个虚构的蜡烛网站的基本运费计算器。客户会输入他们的销售总额,然后选择他们想要的运输水平,它会说成本。选项包括隔夜、优先或标准。一夜之间,优先权是固定成本,所以这很容易。但如果订单低于100美元,则标准价格为7.95美元;如果订单高于100美元,则标准价格为0美元。
我已经为订单总额创建了一个文本字段,并为过夜、优先级和标准创建了复选框。不出一个小奇迹(我在这门课上很差劲),我这里实际上有一个功能性的小程序,只是我不知道如何计算订单总额大于100美元=免费送货。
我觉得我需要创建一个case 4,其中shipping=total+0,但我不确定如何创建代码4,或者在哪里添加了一个true/false或if..else语句来实现这一点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 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)); } |
}
可以将if语句插入选项3的switch case中,即标准选项。如果总额大于或等于100美元,您只需将Shipping设置为总额,否则,您将计算加上7.95美元。
1 2 3 4 5 6 7 | case 3: if(total >= 100.0) { shipping = total; } else { shipping = total + 7.95; } break; |
或者在这种情况下,我个人会使用"早期返回",因此我只需立即返回计算结果,以提高可读性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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; } } |
您可以找到更多关于早期返回以及何时/如果您应该在堆栈溢出时使用它们的信息。