Java设计一个快递运费计算程序

Java设计一个快递运费计算程序

一、引言

随着电子商务的快速发展,快递行业逐渐成为了人们生活中不可或缺的一部分。在购物过程中,运费是消费者非常关心的一个方面。本文将介绍如何使用Java编写一个简单的运费计算器,帮助消费者快速计算快递运费。

项目场景:

我们寄快递是时,快递公司会根据所寄货物的重量和目的地来计算运费,收费标准如下:
快递到达地首重1KG元续重1KG/10元
广西、广东105
安微、湖南、江苏、浙江、上海、江西、贵州、湖北、山西、福建、河南、云南、重庆、四川128
北京、天津、陕西、河北、山东、海南1510
青海、宁夏、甘肃、辽宁、吉林、黑龙江、内蒙古1812
西藏、新疆3025

二、程序设计目标

我们的程序需要满足以下几个目标:

1.能够对操作人员的身份进行验证(最多输入3次密码)。
2.能计算若干个快递货物的费用,直到操作人员让它停止为止。
3.能够方便地录入货物信息。
4.能够自动识别出货物所在的收费段,计算出运费。
5.能够显示货物信息及其运费。

程序设计步骤

Created with Raphaël 2.3.0 操作员登陆 显示程序菜单 用户输入选择操作的项目 1.录入数据 2.计算运费 3.输出结果 0退出程序

我们将按照以下步骤来设计我们的程序:

1.显示程序菜单:在屏幕上显示程序的功能菜单,让操作员选择要执行的操作。
2.登录验证:要求操作员输入密码,如果密码正确,允许操作员使用程序;如果密码错误,提示错误信息,并允许操作员再次尝试输入,最多尝试3次。
3.货物数据录入:让操作员输入货物的重量和目的地,然后将这些信息存储起来。
4.计算运费:根据货物的重量和目的地,计算出运费。
5.显示货物信息:在屏幕上显示货物的重量、目的地和运费。

三、 代码

定义常量:密码和最大尝试次数

    private static final int PASSWORD = 1234;
    private static final int MAX_ATTEMPTS = 3;

创建一个货物列表来存储所有的货物

private static List<Goods> goodsList = new ArrayList<>();

首先进行登录验证,如果登录失败则退出程序

        if (!login()) {
            System.out.println("登录失败,程序退出。");
            return;
        }

显示菜单并处理用户的选择,直到用户选择退出程序

while (true) {
            showMenu();
            int choice = new Scanner(System.in).nextInt();
            switch (choice) {
                case 1:
                    // 选择1,货物数据录入
                    goodsList.add(inputGoods());
                    break;
                case 2:
                    // 选择2,运费计算
                    calculateFee(goodsList);
                    break;
                case 3:
                    // 选择3,显示货物信息
                    displayGoods(goodsList);
                    break;
                case 0:
                    // 选择0,退出程序
                    System.out.println("退出程序");
                    return;
                default:
                    // 选择不在0-3之间,显示错误提示
                    System.out.println("输入错误,请重新输入。");
            }
        }

登录验证函数

private static boolean login() {
        Scanner scanner = new Scanner(System.in);
        // 允许尝试输入密码MAX_ATTEMPTS次
        for (int i = 0; i < MAX_ATTEMPTS; i++) {
            System.out.println("请输入密码:");
            int password = scanner.nextInt();
            // 如果密码正确,返回true
            if (password == PASSWORD) {
                System.out.println("密码正确");
                return true;
            } else {
                // 如果密码错误,显示错误提示
                System.out.println("密码错误");
            }
        }
        // 如果尝试输入密码MAX_ATTEMPTS次后仍未成功,返回false
        return false;
    }

显示菜单函数

private static void showMenu() {
        System.out.println("---快递运费用计算程序---");
        System.out.println("1.货物数据录入");
        System.out.println("2.计算运费");
        System.out.println("3.货物信息显示");
        System.out.println("0.退出程序");
        System.out.println("请输入你的选择(0-3):");
    }

货物数据录入函数

private static Goods inputGoods() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入货物的重量(KG):");
        double weight = scanner.nextDouble();
        System.out.println("请输入货物的目的地:");
        String destination = scanner.next();
        // 创建一个新的货物对象并返回
        return new Goods(weight, destination);
    }

计算运费函数

 private static void calculateFee(List<Goods> goodsList) {
        // 遍历货物列表,根据每个货物的重量和目的地来计算运费
        for (Goods goods : goodsList) {
            double fee = 0;
            // 根据货物的目的地来确定运费的计算方式
            if (goods.getDestination().equals("广西") || goods.getDestination().equals("广东")) {
                fee = 10 + (goods.getWeight() - 1) * 5;
            } else if (goods.getDestination().equals("安微") || goods.getDestination().equals("湖南") || goods.getDestination().equals("江苏") || goods.getDestination().equals("浙江") || goods.getDestination().equals("上海") || goods.getDestination().equals("江西") || goods.getDestination().equals("贵州") || goods.getDestination().equals("湖北") || goods.getDestination().equals("山西") || goods.getDestination().equals("福建") || goods.getDestination().equals("河南") || goods.getDestination().equals("云南") || goods.getDestination().equals("重庆") || goods.getDestination().equals("四川")) {
                fee = 12 + (goods.getWeight() - 1) * 8;
            } else if (goods.getDestination().equals("北京") || goods.getDestination().equals("天津") || goods.getDestination().equals("陕西") || goods.getDestination().equals("河北") || goods.getDestination().equals("山东") || goods.getDestination().equals("海南")) {
                fee = 15 + (goods.getWeight() - 1) * 10;
            } else if (goods.getDestination().equals("青海") || goods.getDestination().equals("宁夏") || goods.getDestination().equals("甘肃") || goods.getDestination().equals("辽宁") || goods.getDestination().equals("吉林") || goods.getDestination().equals("黑龙江") || goods.getDestination().equals("内蒙古")) {
                fee = 18 + (goods.getWeight() - 1) * 12;
            } else if (goods.getDestination().equals("西藏") || goods.getDestination().equals("新疆")) {
                fee = 30 + (goods.getWeight() - 1) * 25;
            }
            // 将计算出的运费设置到货物对象中
            goods.setFee(fee);
        }
    }

显示货物信息函数

private static void displayGoods(List<Goods> goodsList) {
        // 遍历货物列表,显示每个货物的重量、目的地和运费
        for (Goods goods : goodsList) {
            System.out.println("货物重量:" + goods.getWeight() + "KG,目的地:" + goods.getDestination() + ",运费:" + goods.getFee() + "元");
        }
    }

创造Goods类,设置货物类,包含重量、目的地和运费三个属性

class Goods {
    private double weight;
    private String destination;
    private double fee;
    
    // 构造函数,用于创建新的货物对象
    public Goods(double weight, String destination) {
        this.weight = weight;
        this.destination = destination;
    }

    // 获取货物重量
    public double getWeight() {
        return weight;
    }

    // 获取货物目的地
    public String getDestination() {
        return destination;
    }

    // 获取货物运费
    public double getFee() {
        return fee;
    }

    // 设置货物运费
    public void setFee(double fee) {
        this.fee = fee;
    }
}

运行结果

在这里插入图片描述

四、总结:

这个程序首先会要求用户登录,如果密码输入错误超过3次,程序会退出。登录成功后,程序会显示一个菜单,用户可以通过输入数字选择要执行的操作。在录入货物数据时,用户需要输入货物的重量和目的地。在计算运费时,程序会根据货物的重量和目的地来计算运费。在显示货物信息时,程序会显示每个货物的重量、目的地和运费。

最后给大家分享学习网站,w3school这可以学习到很多有关编程的知识。
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值