第二次Blog作业

前言
这次的两次题目集主要包含了合成复用,继承与多态,接口等知识点,题目难度中等,包含了基础和重点,题目难度是逐渐提升的,题目量也相对不大。
接下来我就重点分析题目集8~9的航空货运管理系统问题。

设计与分析
客户填写货运订单并进行支付,需要提供如下信息:
 客户信息(姓名,电话号码等)
 货物信息(货物名称,货物包装长、宽、高尺寸,货物重量等)
 运送信息(发件人姓名、电话、地址,收件人姓名、电话、地址,所选
航班号,订单日期)
 支付方式(支付宝支付、微信支付)
因为想着题目需求需要这些信息,我就定义了以下一些类:
Customer类:用来接收客户信息;
Goods类:用来接收货物信息;
Flight类:用来接收航班信息;
Order类:用来接收订单所需的全部信息并可输出订单信息;
然后我想着Order类需要通过聚合,调用Customer类和Flight类来获取订单所需的信息,而Customer类中又需要加入Goods,所以用了List;
在想清楚这些基本的框架之后,我就思考着主函数该怎么写来输入这些信息,并初始化对象,最终也是成功解决了。

题目需求
航空快递以速度快、安全性高成为急件或贵重物品的首选。本题目要求对航空货运管理系统进行类设计,具体说明参看说明文件。
OO第九周作业题目说明.pdf

输入格式:
按如下顺序分别输入客户信息、货物信息、航班信息以及订单信息。

客户编号
客户姓名
客户电话
客户地址
运送货物数量
[货物编号
货物名称
货物宽度
货物长度
货物高度
货物重量
]//[]内的内容输入次数取决于“运送货物数量”,输入不包含“[]”
航班号
航班起飞机场
航班降落机场
航班日期(格式为YYYY-MM-DD)
航班最大载重量
订单编号
订单日期(格式为YYYY-MM-DD)
发件人地址
发件人姓名
发件人电话
收件人地址
收件人姓名
收件人电话
输出格式:
如果订单中货物重量超过航班剩余载重量,程序输出The flight with flight number:航班号 has exceeded its load capacity and cannot carry the order. ,程序终止运行。
如果航班载重量可以承接该订单,输出如下:
客户:姓名(电话)订单信息如下:

航班号:
订单号:
订单日期:
发件人姓名:
发件人电话:
发件人地址:
收件人姓名:
收件人电话:
收件人地址:
订单总重量(kg):
微信支付金额:

货物明细如下:

明细编号 货物名称 计费重量 计费费率 应交运费
1 ...
2 ...
注:输出中实型数均保留1位小数。

输入样例:
在这里给出一组输入。例如:

10001
郭靖
13807911234
南昌航空大学
2
101
发电机
80
60
40
80
102
信号发生器
55
70
60
45
MU1234
昌北国际机场
大兴国际机场
2025-04-22
1000
900001
2025-04-22
南昌大学
洪七公
18907912325
北京大学
黄药师
13607912546
输出样例:
在这里给出相应的输出。例如:

客户:郭靖(13807911234)订单信息如下:

航班号:MU1234
订单号:900001
订单日期:2025-04-22
发件人姓名:洪七公
发件人电话:18907912325
发件人地址:南昌大学
收件人姓名:黄药师
收件人电话:13607912546
收件人地址:北京大学
订单总重量(kg):125.0
微信支付金额:3350.0

货物明细如下:

明细编号 货物名称 计费重量 计费费率 应交运费
1 发电机 80.0 25.0 2000.0
2 信号发生器 45.0 30.0 1350.0

代码

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Customer{
    private String customerId;
    private String name;
    private String phone;
    private String address;
    private List<Goods> goodsList;

    public Customer(String customerId, String name, String phone, String address) {
        this.customerId = customerId;
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.goodsList = new ArrayList<>();
    }

    public void addGoods(Goods goods) {
        goodsList.add(goods);
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    public List<Goods> getGoodsList() {
        return goodsList;
    }
}

class Goods {
    private String goodsId;
    private String name;
    private double width;
    private double length;
    private double height;
    private double weight;

    public Goods(String goodsId, String name, double width, double length, double height, double weight) {
        this.goodsId = goodsId;
        this.name = name;
        this.width = width;
        this.length = length;
        this.height = height;
        this.weight = weight;
    }

    public String getName() {
        return name;
    }

    public double getWeight() {
        return weight;
    }

    public double calculateVolumeWeight() {
        return (width * length * height) / 6000;
    }

    public double getChargeableWeight() {
        return Math.max(weight, calculateVolumeWeight());
    }

    public double getRate(){
        if(getChargeableWeight() > 0&&getChargeableWeight() < 20){
            return 35;
        }else if(getChargeableWeight() >=20&&getChargeableWeight() < 50) {
            return 30;
        }else if(getChargeableWeight() >=50&&getChargeableWeight() < 100){
            return 25;
        }else if(getChargeableWeight() >=100){
            return 15;
        }
        return 0;
    }

    public double getShippingFee() {
        return getChargeableWeight() * getRate();
    }
}

class Flight {
    private String flightNumber;
    private String departureAirport;
    private String arrivalAirport;
    private String date;
    private double maxWeight;
    private double currentWeight;

    public Flight(String flightNumber, String departureAirport, String arrivalAirport, String date, double maxWeight) {
        this.flightNumber = flightNumber;
        this.departureAirport = departureAirport;
        this.arrivalAirport = arrivalAirport;
        this.date = date;
        this.maxWeight = maxWeight;
        this.currentWeight = 0;
    }

    public String getFlightNumber() {
        return flightNumber;
    }

    public double getMaxWeight() {
        return maxWeight;
    }

    public double getCurrentWeight() {
        return currentWeight;
    }

    public boolean canCarry(double weight) {
        return currentWeight + weight <= maxWeight;
    }

    public void addWeight(double weight) {
        this.currentWeight += weight;
    }
}

class Order {
    private String orderId;
    private String orderDate;
    private String senderAddress;
    private String senderName;
    private String senderPhone;
    private String receiverAddress;
    private String receiverName;
    private String receiverPhone;
    private Customer customer;
    private Flight flight;

    public Order(String orderId, String orderDate, String senderAddress, String senderName, String senderPhone,
                 String receiverAddress, String receiverName, String receiverPhone, Customer customer, Flight flight) {
        this.orderId = orderId;
        this.orderDate = orderDate;
        this.senderAddress = senderAddress;
        this.senderName = senderName;
        this.senderPhone = senderPhone;
        this.receiverAddress = receiverAddress;
        this.receiverName = receiverName;
        this.receiverPhone = receiverPhone;
        this.customer = customer;
        this.flight = flight;
    }

    public double getTotalWeight() {
        double totalWeight = 0;
        for (Goods goods : customer.getGoodsList()) {
            totalWeight += goods.getChargeableWeight();
        }
        return totalWeight;
    }

    public double getTotalShippingFee() {
        double totalFee = 0;
        for (Goods goods : customer.getGoodsList()) {
            totalFee += goods.getShippingFee();
        }
        return totalFee;
    }

    public void printOrderDetails() {
        System.out.printf("客户:%s(%s)订单信息如下:\n", customer.getName(), customer.getPhone());
        System.out.println("-----------------------------------------");
        System.out.printf("航班号:%s\n", flight.getFlightNumber());
        System.out.printf("订单号:%s\n", orderId);
        System.out.printf("订单日期:%s\n", orderDate);
        System.out.printf("发件人姓名:%s\n", senderName);
        System.out.printf("发件人电话:%s\n", senderPhone);
        System.out.printf("发件人地址:%s\n", senderAddress);
        System.out.printf("收件人姓名:%s\n", receiverName);
        System.out.printf("收件人电话:%s\n", receiverPhone);
        System.out.printf("收件人地址:%s\n", receiverAddress);
        System.out.printf("订单总重量(kg):%.1f\n", getTotalWeight());
        System.out.printf("微信支付金额:%.1f\n\n", getTotalShippingFee());

        System.out.println("货物明细如下:");
        System.out.println("-----------------------------------------");
        System.out.println("明细编号	货物名称	计费重量	计费费率	应交运费");

        int index = 1;
        for (Goods goods : customer.getGoodsList()) {
            System.out.printf("%d	%s	%.1f	%.1f	%.1f\n",
                    index++,
                    goods.getName(),
                    goods.getChargeableWeight(),
                    goods.getRate(),
                    goods.getShippingFee());
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String customerId = scanner.nextLine();
        String customerName = scanner.nextLine();
        String customerPhone = scanner.nextLine();
        String customerAddress = scanner.nextLine();
        int goodsCount = Integer.parseInt(scanner.nextLine());

        Customer customer = new Customer(customerId, customerName, customerPhone, customerAddress);
        for (int i = 0; i < goodsCount; i++) {
            String goodsId = scanner.nextLine();
            String goodsName = scanner.nextLine();
            double width = Double.parseDouble(scanner.nextLine());
            double length = Double.parseDouble(scanner.nextLine());
            double height = Double.parseDouble(scanner.nextLine());
            double weight = Double.parseDouble(scanner.nextLine());

            Goods goods = new Goods(goodsId, goodsName, width, length, height, weight);
            customer.addGoods(goods);
        }

        // 输入航班信息
        String flightNumber = scanner.nextLine();
        String departureAirport = scanner.nextLine();
        String arrivalAirport = scanner.nextLine();
        String flightDate = scanner.nextLine();
        double maxWeight = Double.parseDouble(scanner.nextLine());

        Flight flight = new Flight(flightNumber, departureAirport, arrivalAirport, flightDate, maxWeight);

        // 输入订单信息
        String orderId = scanner.nextLine();
        String orderDate = scanner.nextLine();
        String senderAddress = scanner.nextLine();
        String senderName = scanner.nextLine();
        String senderPhone = scanner.nextLine();
        String receiverAddress = scanner.nextLine();
        String receiverName = scanner.nextLine();
        String receiverPhone = scanner.nextLine();

        Order order = new Order(orderId, orderDate, senderAddress, senderName, senderPhone,
                receiverAddress, receiverName, receiverPhone, customer, flight);

        // 计算订单总重量
        double totalWeight = order.getTotalWeight();

        // 检查航班是否超载
        if (!flight.canCarry(totalWeight)) {
            System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.\n",
                    flight.getFlightNumber());
        } else {
            // 更新航班载重
            flight.addWeight(totalWeight);
            // 输出订单信息
            order.printOrderDetails();
        }

        scanner.close();
    }
}

第二次的航空货运管理系统问题则是在上一个的基础上加了几个货物的子类,定义了Payment接口,定义三个类分别实现微信支付,支付宝支付,现金支付。

**踩坑心得**
![](https://img2024.cnblogs.com/blog/3635251/202505/3635251-20250526203242594-1946995975.png)
刚开始没有分析清楚各类中应该包含哪些信息并且类与类该是如何的调用关系,比如Order类要不要调用Goods类,它只需要调用其他类即可,又或者是Customer类调用Goods类时需要怎么调用它的方法。

**总结**
这两次题目集很好地检测了继承与多态等知识的掌握度如何,通过这两次题目集我也发现我对这些知识掌握程度不高,需要多写代码分析思路,理解各面向对象基本原则在实践中的运用。
posted @ 2025-05-26 20:39  24201238-卓周旋  阅读(38)  评论(0)    收藏  举报