计算快递费系统(java版)

编写一个计算快递费的程序。

上海市的某快递公司根据投送目的地距离公司的远近,将全国划分成5个区域:

 快递费按邮件重量计算,由起重费用、续重费用两部分构成,具体计算方法如下:

(1)起重(首重)1公斤按起重资费计算(不足1公斤,按1公斤计算),超过首重的重量,按公斤(不足1公斤,按1公斤计算)收取续重费;

(2)同城起重资费10元,续重3元/公斤;

(3)寄往1区(江浙两省)的邮件,起重资费10元,续重4元;

(4)寄往其他地区的邮件,起重资费统一为15元。而续重部分,不同区域价格不同:2区的续重5元/公斤,3区的续重6.5元/公斤,4区的续重10元/公斤。

编写程序,从键盘输入邮件的目的区域编码和重量,计算并输出运费,计算结果保留2位小数。

提示:

续重部分不足一公斤,按1公斤计算。因此,如包裹重量2.3公斤:1公斤算起重,剩余的1.3公斤算续重,不足1公斤按1公斤计算,1.3公斤折合续重为2公斤。如果重量应大于0、区域编号不能超出0-4的范围。

小结:因为题目简单,使用的是面向过程的方法,个人觉得没太大必要使用面向对象的写法,不过也很简单,这里写的,仅供参考。

import java.util.Scanner;

/**
 * @author abner
 * @version 1.0
 */
public class Calculate_Express_Fee {
    public static void main(String[] args) {
        int a, d;
        float b, c = 0;
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt();
        b = sc.nextFloat();
        d = (int) b;
        switch (a) {
            case 0:
                if (b <= 1) c = 10;
                else {
                    if (d == b) c = 3 * (d - 1) + 10;
                    else c = 3 * (d) + 10;
                }
                ;
                break;
            case 1:
                if (b <= 1) c = 10;
                else {
                    if (d == b) c = 3 * (d - 1) + 10;
                    else c = 3 * (d) + 10;
                }
                ;
                break;
            case 2:
                if (b <= 1) c = 15;
                else {
                    if (d == b) c = (float) (5.0 * (d - 1) + 15);
                    else c = (float) (5.0 * (d) + 15);
                }
                ;
                break;
            case 3:
                if (b <= 1) c = 15;
                else {
                    if (d == b) c = (float) (6.5 * (d - 1) + 10);
                    else c = (float) (6.5 * (d) + 15);
                }
                ;
                break;
            case 4:
                if (b <= 1) c = 15;
                else {
                    if (d == b) c = (float) (10.0 * (d - 1) + 15);
                    else c = (float) (10.0 * (d) + 15);
                }
                ;
                break;
        }
        System.out.println(String.format("%.2f",c));
    }
}

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
受用户输入的快递重量和快递型(顺路快递或地地快递),然后根据输入的型创建对应的对象,并调用对象的getTotal()方法计算快递运费,最后输出结果。以下是可能的实Express.java: ``` public abstract class Express { protected int weight; public Express(int weight) { this.weight = weight; } public int getWeight() { return weight; } public abstract double getTotal(); } ``` SLExpress.java: ``` public class SLExpress extends Express { private static final double BASE_PRICE = 12.0; private static final double EXTRA_PRICE = 2.0; public SLExpress(int weight) { super(weight); } @Override public double getTotal() { double total = BASE_PRICE; if (weight > 1) { total += EXTRA_PRICE * (weight - 1); } return total; } } ``` DDExpress.java: ``` public class DDExpress extends Express { private static final double BASE_PRICE = 5.0; private static final double EXTRA_PRICE = 1.0; public DDExpress(int weight) { super(weight); } @Override public double getTotal() { double total = BASE_PRICE; if (weight > 1) { total += EXTRA_PRICE * (weight - 1); } return total; } } ``` Main.java: ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter weight: "); int weight = scanner.nextInt(); System.out.print("Enter type (SL or DD): "); String type = scanner.next(); Express express; if (type.equals("SL")) { express = new SLExpress(weight); } else if (type.equals("DD")) { express = new DDExpress(weight); } else { System.out.println("Invalid type"); return; } double total = express.getTotal(); System.out.printf("Total: %.2f\n", total); } } ``` 在这个程序中,我们使用了抽象抽象方法来实快递的通用结构和不同计价规则的区别实。在Main中,我们使用Scanner来接收用户的输入,然后根据输入的型创建对应的对象并调用getTotal()方法计算运费,最后输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值