航空公司题目集总结Blog

前言

这两次题目涉及到了以下知识点:

  1. 面向对象编程(OOP)概念:类和对象,封装,继承,多态
  2. Java核心语法:控制结构,数据类型,字符串处理,输入输出
  3. 集合框架:ArrayList以及相关方法操作
  4. 抽象类:定义了抽象类GetFinalPrice、BillingPrinciples、ShowOrder和Payment,其中包含抽象方法,要求子类必须实现
  5. 继承与方法重写:子类(如Individual、Corporate、NormalGoods等)继承父类并重写父类的抽象方法(如getPrice()、getRate())
    通过@Override注解确保方法重写的正确性
  6. 设计模式应用:通过BillingPrinciples和GetFinalPrice的实现,不同的计费策略可以灵活替换

我觉得这两次题目算法上难度并不高,主要考察面向对象的设计能力。

设计与分析

第一次迭代

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

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

客户编号
客户姓名
客户电话
客户地址
运送货物数量
[货物编号
货物名称
货物宽度
货物长度
货物高度
货物重量
]//[]内的内容输入次数取决于“运送货物数量”,输入不包含“[]”
航班号
航班起飞机场
航班降落机场
航班日期(格式为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

我的源代码

点击查看代码
java
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Input input = new Input(sc);

        Customer customer = input.inputCustomer();
        ArrayList<Goods> goods = input.inputGoods(customer.getGoodsNumber());
        Flight flight = input.inputFlight();
        Order order = input.inputOrder();
        Sender sender = input.inputSender();
        Recipient recipient = input.inputRecipient();
        order.setFlight(flight);
        order.setCustomer(customer);
        order.setGoods(goods);
        order.setSender(sender);
        order.setRecipient(recipient);

       double totalWeight = order.totalWeight();
        if (totalWeight > flight.getMaxWeight()) {
            System.out.println("The flight with flight number:" + flight.getId() + " has exceeded its load capacity and cannot carry the order.");
            System.exit(0);
        }
        
        ShowOrder display = new DisplayOrder(order);
        display.showOrder();
    }
}
class Input {
    private Scanner scanner;

    public Input(Scanner scanner) {
        this.scanner = scanner;
    }

    public Customer inputCustomer() {
        Customer customer = new Customer();
        String customerId = scanner.nextLine();
        customer.setId(customerId);
        String customerName = scanner.nextLine();
        customer.setName(customerName);
        String customerPhoneNumber = scanner.nextLine();
        customer.setPhoneNumber(customerPhoneNumber);
        String customerAddress = scanner.nextLine();
        customer.setAddress(customerAddress);
        int goodsNumber = scanner.nextInt();
        customer.setGoodsNumber(goodsNumber);
        scanner.nextLine();
        return customer;
    }

    public ArrayList<Goods> inputGoods(int goodsNumber) {
        ArrayList<Goods> goods = new ArrayList<>();
        for (int i = 0; i < goodsNumber; i++) {
            Goods good = new Goods();
            goods.add(good);
            String id = scanner.nextLine();
            good.setId(id);
            String name = scanner.nextLine();
            good.setName(name);
            int width = scanner.nextInt();
            scanner.nextLine();
            good.setWidth(width);
            int length = scanner.nextInt();
            scanner.nextLine();
            good.setLength(length);
            int height = scanner.nextInt();
            scanner.nextLine();
            good.setHeight(height);
            int weight = scanner.nextInt();
            scanner.nextLine();
            good.setWeight(weight);
            good.setBillingPrinciples(new Goods.currentBillingPrinciples());
        }
        return goods;
    }

    public Flight inputFlight() {
        Flight flight = new Flight();
        String flightId = scanner.nextLine();
        flight.setId(flightId);
        String origin = scanner.nextLine();
        flight.setOrigin(origin);
        String destination = scanner.nextLine();
        flight.setDestination(destination);
        String time = scanner.nextLine();
        flight.setTime(time);
        int maxWeight = scanner.nextInt();
        scanner.nextLine();
        flight.setMaxWeight(maxWeight);
        return flight;
    }

    public Order inputOrder() {
        Order order = new Order();
        String orderId = scanner.nextLine();
        order.setId(orderId);
        String orderTime = scanner.nextLine();
        order.setTime(orderTime);
        return order;
    }

    public Sender inputSender() {
        Sender sender = new Sender();
        String senderAddress = scanner.nextLine();
        sender.setAddress(senderAddress);
        String senderName = scanner.nextLine();
        sender.setName(senderName);
        String senderPhoneNumber = scanner.nextLine();
        sender.setPhoneNumber(senderPhoneNumber);
        return sender;
    }

    public Recipient inputRecipient() {
        Recipient recipient = new Recipient();
        String recipientAddress = scanner.nextLine();
        recipient.setAddress(recipientAddress);
        String recipientName = scanner.nextLine();
        recipient.setName(recipientName);
        String recipientPhoneNumber = scanner.nextLine();
        recipient.setPhoneNumber(recipientPhoneNumber);
        return recipient;
    }
}

class Recipient {
    private String name;
    private String phoneNumber;
    private String address;

    public Recipient(String name, String phoneNumber, String address) {
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }

    public Recipient() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

class Flight {
    private String id;
    private String origin;
    private String destination;
    private String time;
    private int maxWeight;

    public Flight(String id, String origin, String destination, String time, int maxWeight) {
        this.id = id;
        this.origin = origin;
        this.destination = destination;
        this.time = time;
        this.maxWeight = maxWeight;
    }

    public Flight() {}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public int getMaxWeight() {
        return maxWeight;
    }

    public void setMaxWeight(int maxWeight) {
        this.maxWeight = maxWeight;
    }
}

class Customer {
    private String id;
    private String name;
    private String phoneNumber;
    private String address;
    private int goodsNumber;

    public Customer(String id, String name, String phoneNumber, String address, int goodsNumber) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.address = address;
        this.goodsNumber = goodsNumber;
    }

    public Customer() {}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getGoodsNumber() {
        return goodsNumber;
    }

    public void setGoodsNumber(int goodsNumber) {
        this.goodsNumber = goodsNumber;
    }
}

class Goods {
    private String id;
    private String name;
    private int width;
    private int length;
    private int height;
    private int weight;
    private BillingPrinciples billingPrinciples;

    public Goods(String id, String name, int width, int length, int height, int weight) {
        this.id = id;
        this.name = name;
        this.width = width;
        this.length = length;
        this.height = height;
        this.weight = weight;
        this.billingPrinciples = new currentBillingPrinciples();
    }

    public Goods() {}

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public BillingPrinciples getBillingPrinciples() {
        return billingPrinciples;
    }

    public void setBillingPrinciples(BillingPrinciples billingPrinciples) {
        this.billingPrinciples = billingPrinciples;
    }

    public double getPrice(double weight) {
        return billingPrinciples.getPrice(weight);
    }

    public double volumeWeight() {
        return length * width * height / 6000.0;
    }

    public double getActualWeight() {
        if (volumeWeight() > weight)
            return volumeWeight();
        else
            return weight;
    }

    public abstract static class BillingPrinciples {
        public double getRate(double weight) {
            return 0;
        }

        public double getPrice(double weight) {
            return weight * getRate(weight);
        }
    }

    public static class currentBillingPrinciples extends BillingPrinciples {
        @Override
        public double getRate(double weight) {
            if (weight < 20)
                return 35;
            else if (weight < 50)
                return 30;
            else if (weight < 100)
                return 25;
            else
                return 15;
        }
    }
}

class Sender {
    private String address;
    private String name;
    private String phoneNumber;

    public Sender(String address, String name, String phoneNumber) {
        this.address = address;
        this.name = name;
        this.phoneNumber = phoneNumber;
    }

    public Sender() {
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

class Order {
    private String id;
    private String time;
    private Flight flight;
    private Customer customer;
    private ArrayList<Goods> goods;
    private Sender sender;
    private Recipient recipient;

    public Order(String id, String time) {
        this.id = id;
        this.time = time;
    }

    public Order() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public Flight getFlight() {
        return flight;
    }

    public void setFlight(Flight flight) {
        this.flight = flight;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public ArrayList<Goods> getGoods() {
        return goods;
    }

    public void setGoods(ArrayList<Goods> goods) {
        this.goods = goods;
    }

    public Sender getSender() {
        return sender;
    }

    public void setSender(Sender sender) {
        this.sender = sender;
    }

    public Recipient getRecipient() {
        return recipient;
    }

    public void setRecipient(Recipient recipient) {
        this.recipient = recipient;
    }

    public double totalWeight() {
        double totalWeight = 0;
        for (Goods good : goods) {
            totalWeight += good.getActualWeight();
        }
        return totalWeight;
    }

    public double totalPrice() {
        double totalPrice = 0;
        for (Goods good : goods) {
            totalPrice += good.getPrice(good.getActualWeight());
        }
        return totalPrice;
    }
}

abstract class ShowOrder {
    protected Order order;

    public ShowOrder(Order order) {
        this.order = order;
    }

    public void showOrder() {}
}

class DisplayOrder extends ShowOrder {
    public DisplayOrder(Order order) {
        super(order);
    }

    @Override
    public void showOrder() {
        System.out.printf("客户:%s(%s)订单信息如下:\n", order.getCustomer().getName(), order.getCustomer().getPhoneNumber());
        System.out.printf("-----------------------------------------\n");
        System.out.printf("航班号:%s\n", order.getFlight().getId());
        System.out.printf("订单号:%s\n", order.getId());
        System.out.printf("订单日期:%s\n", order.getTime());
        System.out.printf("发件人姓名:%s\n", order.getSender().getName());
        System.out.printf("发件人电话:%s\n", order.getSender().getPhoneNumber());
        System.out.printf("发件人地址:%s\n", order.getSender().getAddress());
        System.out.printf("收件人姓名:%s\n", order.getRecipient().getName());
        System.out.printf("收件人电话:%s\n", order.getRecipient().getPhoneNumber());
        System.out.printf("收件人地址:%s\n", order.getRecipient().getAddress());
        System.out.printf("订单总重量(kg):%.1f\n", order.totalWeight());
        System.out.printf("微信支付金额:%.1f\n", order.totalPrice());
        System.out.printf("\n");
        System.out.printf("货物明细如下:\n");
        System.out.printf("-----------------------------------------\n");
        System.out.printf("明细编号\t货物名称\t计费重量\t计费费率\t应交运费\n");
        for (int i = 0; i < order.getGoods().size(); i++) {
            Goods good = order.getGoods().get(i);
            double actualWeight = good.getActualWeight();
            double rate = good.getBillingPrinciples().getRate(actualWeight);
            double price = good.getPrice(actualWeight);
            if (i != order.getGoods().size() - 1)
                System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", i + 1, good.getName(), actualWeight, rate, price);
            else
                System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f", i + 1, good.getName(), actualWeight, rate, price);
        }
    }
}

解释:

在类的设计上我大致都是按照题目要求来的,可以参考以下类图,运用了继承多态

代码分析:

点击查看代码详细分析(中文)
文件 “航空 1.txt” 的指标详情

参数 值
========= =====
项目目录 C:\Users\13458\Desktop\ 航空 1
项目名称 航空
检查点名称 基准线
文件名 航空 1.txt
行数 574
语句数 367
分支语句百分比 3.5
方法调用语句数 108
含注释的行百分比 0.0
类和接口数 12
每类平均方法数 7.67
每方法平均语句数 2.47
最复杂方法的行号 387
最复杂方法名称 currentBillingPrinciples.getRate ()
最大复杂度 5
最深代码块的行号 23
最大代码块深度 3
平均代码块深度 1.71
平均复杂度 1.14

12 个类中的最复杂方法: 复杂度、语句数、最大深度、调用数

BillingPrinciples.getPrice() 1, 1, 3, 1
BillingPrinciples.getRate() 1, 1, 3, 0
currentBillingPrinciples.getRate() 5, 8, 3, 0
Customer.Customer() 1, 0, 0, 0
Customer.Customer() 1, 5, 2, 0
Customer.getAddress() 1, 1, 2, 0
Customer.getGoodsNumber() 1, 1, 2, 0
Customer.getId() 1, 1, 2, 0
Customer.getName() 1, 1, 2, 0
Customer.getPhoneNumber() 1, 1, 2, 0
Customer.setAddress() 1, 1, 2, 0
Customer.setGoodsNumber() 1, 1, 2, 0
Customer.setId() 1, 1, 2, 0
Customer.setName() 1, 1, 2, 0
Customer.setPhoneNumber() 1, 1, 2, 0
DisplayOrder.DisplayOrder() 1, 1, 2, 1
DisplayOrder.showOrder() 4, 26, 3, 28
Flight.Flight() 1, 0, 0, 0
Flight.Flight() 1, 5, 2, 0
Flight.getDestination() 1, 1, 2, 0
Flight.getId() 1, 1, 2, 0
Flight.getMaxWeight() 1, 1, 2, 0
Flight.getOrigin() 1, 1, 2, 0
Flight.getTime() 1, 1, 2, 0
Flight.setDestination() 1, 1, 2, 0
Flight.setId() 1, 1, 2, 0
Flight.setMaxWeight() 1, 1, 2, 0
Flight.setOrigin() 1, 1, 2, 0
Flight.setTime() 1, 1, 2, 0
Goods.getActualWeight() 3, 4, 2, 2
Goods.getBillingPrinciples() 1, 1, 2, 0
Goods.getHeight() 1, 1, 2, 0
Goods.getId() 1, 1, 2, 0
Goods.getLength() 1, 1, 2, 0
Goods.getName() 1, 1, 2, 0
Goods.getPrice() 1, 1, 2, 1
Goods.getWeight() 1, 1, 2, 0
Goods.getWidth() 1, 1, 2, 0
Goods.Goods() 1, 0, 0, 0
Goods.Goods() 1, 7, 2, 0
Goods.setBillingPrinciples() 1, 1, 2, 0
Goods.setHeight() 1, 1, 2, 0
Goods.setId() 1, 1, 2, 0
Goods.setLength() 1, 1, 2, 0
Goods.setName() 1, 1, 2, 0
Goods.setWeight() 1, 1, 2, 0
Goods.setWidth() 1, 1, 2, 0
Goods.volumeWeight() 1, 1, 2, 0
Input.Input() 1, 1, 2, 0
Input.inputCustomer() 1, 13, 2, 11
Input.inputFlight() 1, 13, 2, 11
Input.inputGoods() 2, 22, 3, 19
Input.inputOrder() 1, 6, 2, 4
Input.inputRecipient() 1, 8, 2, 6
Input.inputSender() 1, 8, 2, 6
Main.main() 2, 19, 3, 16
Order.getCustomer() 1, 1, 2, 0
Order.getFlight() 1, 1, 2, 0
Order.getGoods() 1, 1, 2, 0
Order.getId() 1, 1, 2, 0
Order.getRecipient() 1, 1, 2, 0
Order.getSender() 1, 1, 2, 0
Order.getTime() 1, 1, 2, 0
Order.Order() 1, 0, 0, 0
Order.Order() 1, 2, 2, 0
Order.setCustomer() 1, 1, 2, 0
Order.setFlight() 1, 1, 2, 0
Order.setGoods() 1, 1, 2, 0
Order.setId() 1, 1, 2, 0
Order.setRecipient() 1, 1, 2, 0
Order.setSender() 1, 1, 2, 0
Order.setTime() 1, 1, 2, 0
Order.totalPrice() 2, 4, 3, 1
Order.totalWeight() 2, 4, 3, 1
Recipient.getAddress() 1, 1, 2, 0
Recipient.getName() 1, 1, 2, 0
Recipient.getPhoneNumber() 1, 1, 2, 0
Recipient.Recipient() 1, 0, 0, 0
Recipient.Recipient() 1, 3, 2, 0
Recipient.setAddress() 1, 1, 2, 0
Recipient.setName() 1, 1, 2, 0
Recipient.setPhoneNumber() 1, 1, 2, 0
Sender.getAddress() 1, 1, 2, 0
Sender.getName() 1, 1, 2, 0
Sender.getPhoneNumber() 1, 1, 2, 0
Sender.Sender() 1, 0, 0, 0
Sender.Sender() 1, 3, 2, 0
Sender.setAddress() 1, 1, 2, 0
Sender.setName() 1, 1, 2, 0
Sender.setPhoneNumber() 1, 1, 2, 0
ShowOrder.showOrder() 1, 0, 0, 0
ShowOrder.ShowOrder() 1, 1, 2, 0

代码块深度 语句数

0 12
1 124
2 190
3 41
4 0
5 0
6 0
7 0
8 0
9+ 0

心得:

这次航空题我缺少了paymant类,考虑不够完全,而且一开始我没用仔细审题,没有用继承,由此浪费了很多时间,最后又改了很多。

第二次迭代

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

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

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

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

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

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

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

货物明细如下:
-----------------------------------------
明细编号    货物名称    计费重量    计费费率    应交运费
1    发电机    80.0    40.0    3200.0
2    信号发生器    45.0    50.0    2250.0

我的源代码

点击查看代码
import java.util.Scanner;
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String customerSort = sc.nextLine();
        GetFinalPrice price;
        BillingPrinciples billingPrinciples;

        Customer customer = new Customer();
        String customerId = sc.nextLine();
        customer.setId(customerId);
        String customerName = sc.nextLine();
        customer.setName(customerName);
        String customerPhoneNumber = sc.nextLine();
        customer.setPhoneNumber(customerPhoneNumber);
        String customerAddress = sc.nextLine();
        customer.setAddress(customerAddress);

        String goodsSort = sc.nextLine();
        if (goodsSort.equals("Normal")) {
            billingPrinciples = new NormalGoods();
        } else if (goodsSort.equals("Expedite")) {
            billingPrinciples = new ExpediteGoods();
        } else {
            billingPrinciples = new DangerousGoods();
        }

        if (customerSort.equals("Individual")) {
            price = new Individual(billingPrinciples);
        } else {
            price = new Corporate(billingPrinciples);
        }

        int goodsNumber = sc.nextInt();
        sc.nextLine();
        customer.setGoodsNumber(goodsNumber);

        ArrayList<Goods> goods = new ArrayList<>();
        for (int i = 0; i < goodsNumber; i++) {
            Goods good = new Goods();
            good.setBillingPrinciples(billingPrinciples);
            goods.add(good);
            String id = sc.nextLine();
            good.setId(id);
            String name = sc.nextLine();
            good.setName(name);
            int width = sc.nextInt();
            sc.nextLine();
            good.setWidth(width);
            int length = sc.nextInt();
            sc.nextLine();
            good.setLength(length);
            int height = sc.nextInt();
            sc.nextLine();
            good.setHeight(height);
            int weight = sc.nextInt();
            sc.nextLine();
            good.setWeight(weight);
        }

        Flight flight = new Flight();
        String flightId = sc.nextLine();
        flight.setId(flightId);
        String origin = sc.nextLine();
        flight.setOrigin(origin);
        String destination = sc.nextLine();
        flight.setDestination(destination);
        String time = sc.nextLine();
        flight.setTime(time);
        int maxWeight = sc.nextInt();
        sc.nextLine();
        flight.setMaxWeight(maxWeight);

        Order order = new Order();
        String orderId = sc.nextLine();
        order.setId(orderId);
        String orderTime = sc.nextLine();
        order.setTime(orderTime);

        Sender sender = new Sender();
        String senderAddress = sc.nextLine();
        sender.setAddress(senderAddress);
        String senderName = sc.nextLine();
        sender.setName(senderName);
        String senderPhoneNumber = sc.nextLine();
        sender.setPhoneNumber(senderPhoneNumber);

        Recipient recipient = new Recipient();
        String recipientAddress = sc.nextLine();
        recipient.setAddress(recipientAddress);
        String recipientName = sc.nextLine();
        recipient.setName(recipientName);
        String recipientPhoneNumber = sc.nextLine();
        recipient.setPhoneNumber(recipientPhoneNumber);

        String customerPayment = sc.nextLine();
        Payment payment;
        if(customerPayment.equals("Wechat")){
            payment=new Wechat();
        }
        else if(customerPayment.equals("ALiPay")){
            payment=new ALipay();
        }
        else
            payment=new Cash();

        order.setFlight(flight);
        order.setCustomer(customer);
        order.setGoods(goods);
        order.setSender(sender);
        order.setRecipient(recipient);
        order.setGetPrice(price);
        order.setPayment(payment);

        if(flight.getMaxWeight()<order.totalWeight()){
            System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.",flight.getId());
            return;
        }

        ShowOrder displayOrder=new DisplayOrder(order);

        displayOrder.showOrder();

        sc.close();
    }
}

class Recipient {
    private String name;
    private String phoneNumber;
    private String address;

    public Recipient(String name, String phoneNumber, String address) {
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.address = address;
    }

    public Recipient() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

class Flight {
    private String id;
    private String origin;
    private String destination;
    private String time;
    private int maxWeight;

    public Flight(String id, String origin, String destination, String time, int maxWeight) {
        this.id = id;
        this.origin = origin;
        this.destination = destination;
        this.time = time;
        this.maxWeight = maxWeight;
    }

    public Flight() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOrigin() {
        return origin;
    }

    public void setOrigin(String origin) {
        this.origin = origin;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public int getMaxWeight() {
        return maxWeight;
    }

    public void setMaxWeight(int maxWeight) {
        this.maxWeight = maxWeight;
    }
}

class Customer {
    private String id;
    private String name;
    private String phoneNumber;
    private String address;
    private int goodsNumber;

    public Customer(String id, String name, String phoneNumber, String address, int goodsNumber) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.address = address;
        this.goodsNumber = goodsNumber;
    }

    public Customer() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getGoodsNumber() {
        return goodsNumber;
    }

    public void setGoodsNumber(int goodsNumber) {
        this.goodsNumber = goodsNumber;
    }
}

class Goods {
    private String id;
    private String name;
    private int width;
    private int length;
    private int height;
    private int weight;
    private BillingPrinciples billingPrinciples;

    public Goods(String id, String name, int width, int length, int height, int weight, BillingPrinciples billingPrinciples) {
        this.id = id;
        this.name = name;
        this.width = width;
        this.length = length;
        this.height = height;
        this.weight = weight;
        this.billingPrinciples = billingPrinciples;
    }

    public Goods() {
        this.billingPrinciples = new NormalGoods();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public BillingPrinciples getBillingPrinciples() {
        return billingPrinciples;
    }

    public void setBillingPrinciples(BillingPrinciples billingPrinciples) {
        this.billingPrinciples = billingPrinciples;
    }

    public double volumeWeight() {
        return length * width * height / 6000.0;
    }

    public double getActualWeight() {
        if (volumeWeight() > weight)
            return volumeWeight();
        else
            return weight;
    }

    public double getPrice() {
        return getActualWeight() * billingPrinciples.getRate(weight);
    }
}

abstract class GetFinalPrice{
    protected BillingPrinciples billingPrinciples;

    public GetFinalPrice(BillingPrinciples billingPrinciples) {
        this.billingPrinciples = billingPrinciples;
    }

    public BillingPrinciples getBillingPrinciples() {
        return billingPrinciples;
    }

    public void setBillingPrinciples(BillingPrinciples billingPrinciples) {
        this.billingPrinciples = billingPrinciples;
    }

    public GetFinalPrice() {}

    public abstract double getPrice(double totalPrice);
}

class Individual extends GetFinalPrice {
    public Individual() {
    }

    public Individual(BillingPrinciples billingPrinciples) {
        super(billingPrinciples);
    }

    @Override
    public double getPrice(double totalPrice) {
        return totalPrice*0.9;
    }

}

class Corporate extends GetFinalPrice {
    public Corporate() {
    }

    public Corporate(BillingPrinciples billingPrinciples) {
        super(billingPrinciples);
    }

    @Override
    public double getPrice(double totalPrice) {
        return totalPrice*0.8;
    }

}

abstract class BillingPrinciples {

    public BillingPrinciples() {
    }

    public double getRate(double weight) {
        return 0;
    }
}

class NormalGoods extends BillingPrinciples {
    public NormalGoods() {}

    @Override
    public double getRate(double weight) {
        if (weight < 20)
            return 35;
        else if (weight < 50)
            return 30;
        else if (weight < 100)
            return 25;
        else
            return 15;
    }
}

class DangerousGoods extends BillingPrinciples {
    public DangerousGoods() {}

    @Override
    public double getRate(double weight) {
        if (weight < 20)
            return 80;
        else if (weight < 50)
            return 50;
        else if (weight < 100)
            return 30;
        else
            return 20;
    }
}

class ExpediteGoods extends BillingPrinciples {
    public ExpediteGoods() {}

    @Override
    public double getRate(double weight) {
        if (weight < 20)
            return 60;
        else if (weight < 50)
            return 50;
        else if (weight < 100)
            return 40;
        else
            return 30;
    }
}

class Sender {
    private String address;
    private String name;
    private String phoneNumber;

    public Sender(String address, String name, String phoneNumber) {
        this.address = address;
        this.name = name;
        this.phoneNumber = phoneNumber;
    }

    public Sender() {
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

class Order {
    private String id;
    private String time;
    private Flight flight;
    private Customer customer;
    private ArrayList<Goods> goods;
    private Sender sender;
    private Recipient recipient;
    private Payment payment;
    private GetFinalPrice price;

    public Order(String id, String time, Flight flight, Customer customer, ArrayList<Goods> goods, Sender sender, Recipient recipient, Payment payment, GetFinalPrice price) {
        this.id = id;
        this.time = time;
        this.flight = flight;
        this.customer = customer;
        this.goods = goods;
        this.sender = sender;
        this.recipient = recipient;
        this.payment = payment;
        this.price = price;
    }

    public GetFinalPrice getPrice() {
        return price;
    }

    public void setGetPrice(GetFinalPrice getPrice) {
        this.price = getPrice;
    }

    public Payment getPayment() {
        return payment;
    }

    public void setPayment(Payment payment) {
        this.payment = payment;
    }

    public Order() {
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public Flight getFlight() {
        return flight;
    }

    public void setFlight(Flight flight) {
        this.flight = flight;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public ArrayList<Goods> getGoods() {
        return goods;
    }

    public void setGoods(ArrayList<Goods> goods) {
        this.goods = goods;
    }

    public Sender getSender() {
        return sender;
    }

    public void setSender(Sender sender) {
        this.sender = sender;
    }

    public Recipient getRecipient() {
        return recipient;
    }

    public void setRecipient(Recipient recipient) {
        this.recipient = recipient;
    }

    public double totalWeight() {
        double totalWeight = 0;
        for (Goods good : goods) {
            totalWeight += good.getActualWeight();
        }
        return totalWeight;
    }

    public double totalPrice() {
        double totalPrice = 0;
        for (Goods good : goods) {
            totalPrice += good.getPrice();
        }
        return totalPrice;
    }
}

abstract class ShowOrder {
    protected Order order;

    public ShowOrder(Order order) {
        this.order = order;
    }

    public void showOrder() {}
}

class DisplayOrder extends ShowOrder {

    public DisplayOrder(Order order) {
        super(order);
    }
    @Override
    public void showOrder() {
        System.out.printf("客户:%s(%s)订单信息如下:\n", order.getCustomer().getName(), order.getCustomer().getPhoneNumber());
        System.out.printf("-----------------------------------------\n");
        System.out.printf("航班号:%s\n", order.getFlight().getId());
        System.out.printf("订单号:%s\n", order.getId());
        System.out.printf("订单日期:%s\n", order.getTime());
        System.out.printf("发件人姓名:%s\n", order.getSender().getName());
        System.out.printf("发件人电话:%s\n", order.getSender().getPhoneNumber());
        System.out.printf("发件人地址:%s\n", order.getSender().getAddress());
        System.out.printf("收件人姓名:%s\n", order.getRecipient().getName());
        System.out.printf("收件人电话:%s\n", order.getRecipient().getPhoneNumber());
        System.out.printf("收件人地址:%s\n", order.getRecipient().getAddress());
        System.out.printf("订单总重量(kg):%.1f\n", order.totalWeight());
        Payment payment = order.getPayment();
        System.out.printf("%s支付金额:%.1f\n", payment.getName(),order.getPrice().getPrice(order.totalPrice()));
        System.out.printf("\n");
        System.out.printf("货物明细如下:\n");
        System.out.printf("-----------------------------------------\n");
        System.out.printf("明细编号\t货物名称\t计费重量\t计费费率\t应交运费\n");
        for (int i = 0; i < order.getGoods().size(); i++) {
            Goods good = order.getGoods().get(i);
            double actualWeight = good.getActualWeight();
            double rate = good.getBillingPrinciples().getRate(actualWeight);
            double price = good.getPrice();
            if (i != order.getGoods().size() - 1)
                System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", i + 1, good.getName(), actualWeight, rate, price);
            else
                System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f", i + 1, good.getName(), actualWeight, rate, price);
        }
    }
}

abstract class Payment {
    protected String name;

    public Payment(String name) {
        this.name = name;
    }

    public Payment() {
    }

    public String getName() {
        return name;
    }
}

class ALipay extends Payment{

    public ALipay() {
    }

    @Override
    public String getName() {
        return "支付宝";
    }
}

class Wechat extends Payment{
    public Wechat() {}

    @Override
    public String getName() {
        return "微信";
    }
}

class Cash extends Payment {
    public Cash() {}

    @Override
    public String getName() {
        return "现金";
    }
}

解释:

在第一次迭代的基础上,我改善了类的写法,移除了内部类,也根据题目要求增加了用户、支付方式、货物类,同时也满足可扩展性。具体可参考以下类图

代码分析:

点击查看代码详细分析(中文)
文件 “航空 2.txt” 的指标详情

参数 值
========= =====
项目目录 C:\Users\13458\Desktop\ 新建文件夹 \
项目名称 航空 2
检查点名称 基准线
文件名 航空 2.txt
行数 735
语句数 446
分支语句百分比 6.5
方法调用语句数 116
含注释的行百分比 0.0
类和接口数 18
每类平均方法数 6.06
每方法平均语句数 2.51
最复杂方法的行号 5
最复杂方法名称 Main.main ()
最大复杂度 11
最深代码块的行号 24
最大代码块深度 3
平均代码块深度 1.65
平均复杂度 1.27

18 个类中的最复杂方法: 复杂度、语句数、最大深度、调用数

ALipay.ALipay() 1, 0, 0, 0
BillingPrinciples.BillingPrinciples() 1, 0, 0, 0
BillingPrinciples.getRate() 1, 1, 2, 0
Corporate.Corporate() 1, 1, 2, 1
Corporate.Corporate() 1, 0, 0, 0
Corporate.getPrice() 1, 1, 2, 0
Customer.Customer() 1, 0, 0, 0
Customer.Customer() 1, 5, 2, 0
Customer.getAddress() 1, 1, 2, 0
Customer.getGoodsNumber() 1, 1, 2, 0
Customer.getId() 1, 1, 2, 0
Customer.getName() 1, 1, 2, 0
Customer.getPhoneNumber() 1, 1, 2, 0
Customer.setAddress() 1, 1, 2, 0
Customer.setGoodsNumber() 1, 1, 2, 0
Customer.setId() 1, 1, 2, 0
Customer.setName() 1, 1, 2, 0
Customer.setPhoneNumber() 1, 1, 2, 0
DangerousGoods.DangerousGoods() 1, 0, 0, 0
DangerousGoods.getRate() 5, 8, 2, 0
DisplayOrder.DisplayOrder() 1, 1, 2, 1
DisplayOrder.showOrder() 4, 27, 3, 29
ExpediteGoods.ExpediteGoods() 1, 0, 0, 0
ExpediteGoods.getRate() 5, 8, 2, 0
Flight.Flight() 1, 0, 0, 0
Flight.Flight() 1, 5, 2, 0
Flight.getDestination() 1, 1, 2, 0
Flight.getId() 1, 1, 2, 0
Flight.getMaxWeight() 1, 1, 2, 0
Flight.getOrigin() 1, 1, 2, 0
Flight.getTime() 1, 1, 2, 0
Flight.setDestination() 1, 1, 2, 0
Flight.setId() 1, 1, 2, 0
Flight.setMaxWeight() 1, 1, 2, 0
Flight.setOrigin() 1, 1, 2, 0
Flight.setTime() 1, 1, 2, 0
GetFinalPrice.getBillingPrinciples() 1, 1, 2, 0
GetFinalPrice.GetFinalPrice() 1, 0, 0, 0
GetFinalPrice.GetFinalPrice() 1, 1, 2, 0
GetFinalPrice.setBillingPrinciples() 1, 1, 2, 0
Goods.getActualWeight() 3, 4, 2, 2
Goods.getBillingPrinciples() 1, 1, 2, 0
Goods.getHeight() 1, 1, 2, 0
Goods.getId() 1, 1, 2, 0
Goods.getLength() 1, 1, 2, 0
Goods.getName() 1, 1, 2, 0
Goods.getPrice() 1, 1, 2, 2
Goods.getWeight() 1, 1, 2, 0
Goods.getWidth() 1, 1, 2, 0
Goods.Goods() 1, 1, 2, 0
Goods.Goods() 1, 7, 2, 0
Goods.setBillingPrinciples() 1, 1, 2, 0
Goods.setHeight() 1, 1, 2, 0
Goods.setId() 1, 1, 2, 0
Goods.setLength() 1, 1, 2, 0
Goods.setName() 1, 1, 2, 0
Goods.setWeight() 1, 1, 2, 0
Goods.setWidth() 1, 1, 2, 0
Goods.volumeWeight() 1, 1, 2, 0
Individual.getPrice() 1, 1, 2, 0
Individual.Individual() 1, 1, 2, 1
Individual.Individual() 1, 0, 0, 0
Main.main() 11, 100, 3, 77
NormalGoods.getRate() 5, 8, 2, 0
NormalGoods.NormalGoods() 1, 0, 0, 0
Order.getCustomer() 1, 1, 2, 0
Order.getFlight() 1, 1, 2, 0
Order.getGoods() 1, 1, 2, 0
Order.getId() 1, 1, 2, 0
Order.getPayment() 1, 1, 2, 0
Order.getPrice() 1, 1, 2, 0
Order.getRecipient() 1, 1, 2, 0
Order.getSender() 1, 1, 2, 0
Order.getTime() 1, 1, 2, 0
Order.Order() 1, 0, 0, 0
Order.Order() 1, 9, 2, 0
Order.setCustomer() 1, 1, 2, 0
Order.setFlight() 1, 1, 2, 0
Order.setGetPrice() 1, 1, 2, 0
Order.setGoods() 1, 1, 2, 0
Order.setId() 1, 1, 2, 0
Order.setPayment() 1, 1, 2, 0
Order.setRecipient() 1, 1, 2, 0
Order.setSender() 1, 1, 2, 0
Order.setTime() 1, 1, 2, 0
Order.totalPrice() 2, 4, 3, 1
Order.totalWeight() 2, 4, 3, 1
Payment.getName() 1, 1, 2, 0
Payment.Payment() 1, 0, 0, 0
Payment.Payment() 1, 1, 2, 0
Recipient.getAddress() 1, 1, 2, 0
Recipient.getName() 1, 1, 2, 0
Recipient.getPhoneNumber() 1, 1, 2, 0
Recipient.Recipient() 1, 0, 0, 0
Recipient.Recipient() 1, 3, 2, 0
Recipient.setAddress() 1, 1, 2, 0
Recipient.setName() 1, 1, 2, 0
Recipient.setPhoneNumber() 1, 1, 2, 0
Sender.getAddress() 1, 1, 2, 0
Sender.getName() 1, 1, 2, 0
Sender.getPhoneNumber() 1, 1, 2, 0
Sender.Sender() 1, 0, 0, 0
Sender.Sender() 1, 3, 2, 0
Sender.setAddress() 1, 1, 2, 0
Sender.setName() 1, 1, 2, 0
Sender.setPhoneNumber() 1, 1, 2, 0
ShowOrder.showOrder() 1, 0, 0, 0
ShowOrder.ShowOrder() 1, 1, 2, 0

代码块深度 语句数

0 20
1 152
2 236
3 38
4 0
5 0
6 0
7 0
8 0
9+ 0

心得:

在上一次题目的基础上,因为上一次我就遵循了单一职责原则、里氏代换原则、开闭原则、合成复用原则,所以我改起来速度可以比较快,但是最后一直卡在一个点过不去,结果发现是“Wechat”的c不小心大写了,浪费了很多时间,这也再次警示我一定要好好审题。

踩坑心得

第一次迭代

  • [1]

刚开始一直非零返回,后面发现是在创建货物list循环时没有每一次都重新创建一个货物实例,导致非零返回

第二次迭代

  • [1]
    错误审题将“Wechat”的c大写了,导致答案错误

  • [2 ]
    由于没有创建参数来简化代码,所以我很多地方要调用很多次方法,例如order.getPrice().getPrice(order.totalPrice()),导致错了很多次,逻辑容易混乱

改进建议

第一次迭代

点击查看代码详细分析
文件 “elevator1.java” 的度量详情
参数 值
========= =====
项目目录 C:\Users\13458\Desktop\elevator1
项目名称 elevator1
检查点名称 基线
文件名 elevator1.java
行数 197
语句数 148
分支语句百分比 25.0
方法调用语句数 85
带注释的行的百分比 0.0
类和接口数 2
每个类的方法数 6.50
每个方法的平均语句数 9.46
最复杂方法的行号 99
最复杂方法的名称 Elevator.findRequest ()
最大复杂度 22
最深代码块的行号 26
最大代码块深度 5
平均代码块深度 2.55
平均复杂度 4.46
2 个类中最复杂的方法: 复杂度、语句数、最大深度、调用次数
Elevator.addExternalRequest() 3,6,4,7
Elevator.addInternalRequest() 3,3,4,3
Elevator.checkFloor() 2,3,2,0
Elevator.closeDoor() 1,2,2,1
Elevator.Elevator() 1,7,2,0
Elevator.Elevator() 1,7,2,0
Elevator.findRequest() 22,34,4,28
Elevator.getExternalRequests() 1,1,2,0
Elevator.getInternalRequests() 1,1,2,0
Elevator.move() 10,19,5,12
Elevator.openDoor() 1,3,2,2
Elevator.processRequsts() 5,7,4,8
Main.main() 7,30,5,24
代码块深度 语句数
0 5
1 20
2 56
3 29
4 32
5 6
6 0
7 0
8 0
9 及以上 0

  • [1] 缺乏注释

带注释的行的百分比为 0.0%,这会给后续的代码维护和理解带来极大困难,尤其是对于新接手项目的开发者,很难快速理解代码的功能和设计意图。

  • [2] 方法复杂度偏高

在 “航空 2.txt” 中,Main.main() 的复杂度为 11,语句数达 100,存在大量输入逻辑和对象初始化,导致方法臃肿。

  • [3] 耦合度高

DisplayOrder.showOrder()直接调用 order 对象的多层方法(如 order.getCustomer().getName()),未缓存中间结果

我需要增加注释,拆分 Main.main() 方法,将输入逻辑、对象创建、业务处理拆分为独立方法或类(如 InputService、OrderFactory),优化类间,降低耦合度。

第二次迭代

点击查看代码详细分析
文件 “航空 2.txt” 的指标详情

参数 值
========= =====
项目目录 C:\Users\13458\Desktop\ 新建文件夹 \
项目名称 航空 2
检查点名称 基准线
文件名 航空 2.txt
行数 735
语句数 446
分支语句百分比 6.5
方法调用语句数 116
含注释的行百分比 0.0
类和接口数 18
每类平均方法数 6.06
每方法平均语句数 2.51
最复杂方法的行号 5
最复杂方法名称 Main.main ()
最大复杂度 11
最深代码块的行号 24
最大代码块深度 3
平均代码块深度 1.65
平均复杂度 1.27

18 个类中的最复杂方法: 复杂度、语句数、最大深度、调用数

ALipay.ALipay() 1, 0, 0, 0
BillingPrinciples.BillingPrinciples() 1, 0, 0, 0
BillingPrinciples.getRate() 1, 1, 2, 0
Corporate.Corporate() 1, 1, 2, 1
Corporate.Corporate() 1, 0, 0, 0
Corporate.getPrice() 1, 1, 2, 0
Customer.Customer() 1, 0, 0, 0
Customer.Customer() 1, 5, 2, 0
Customer.getAddress() 1, 1, 2, 0
Customer.getGoodsNumber() 1, 1, 2, 0
Customer.getId() 1, 1, 2, 0
Customer.getName() 1, 1, 2, 0
Customer.getPhoneNumber() 1, 1, 2, 0
Customer.setAddress() 1, 1, 2, 0
Customer.setGoodsNumber() 1, 1, 2, 0
Customer.setId() 1, 1, 2, 0
Customer.setName() 1, 1, 2, 0
Customer.setPhoneNumber() 1, 1, 2, 0
DangerousGoods.DangerousGoods() 1, 0, 0, 0
DangerousGoods.getRate() 5, 8, 2, 0
DisplayOrder.DisplayOrder() 1, 1, 2, 1
DisplayOrder.showOrder() 4, 27, 3, 29
ExpediteGoods.ExpediteGoods() 1, 0, 0, 0
ExpediteGoods.getRate() 5, 8, 2, 0
Flight.Flight() 1, 0, 0, 0
Flight.Flight() 1, 5, 2, 0
Flight.getDestination() 1, 1, 2, 0
Flight.getId() 1, 1, 2, 0
Flight.getMaxWeight() 1, 1, 2, 0
Flight.getOrigin() 1, 1, 2, 0
Flight.getTime() 1, 1, 2, 0
Flight.setDestination() 1, 1, 2, 0
Flight.setId() 1, 1, 2, 0
Flight.setMaxWeight() 1, 1, 2, 0
Flight.setOrigin() 1, 1, 2, 0
Flight.setTime() 1, 1, 2, 0
GetFinalPrice.getBillingPrinciples() 1, 1, 2, 0
GetFinalPrice.GetFinalPrice() 1, 0, 0, 0
GetFinalPrice.GetFinalPrice() 1, 1, 2, 0
GetFinalPrice.setBillingPrinciples() 1, 1, 2, 0
Goods.getActualWeight() 3, 4, 2, 2
Goods.getBillingPrinciples() 1, 1, 2, 0
Goods.getHeight() 1, 1, 2, 0
Goods.getId() 1, 1, 2, 0
Goods.getLength() 1, 1, 2, 0
Goods.getName() 1, 1, 2, 0
Goods.getPrice() 1, 1, 2, 2
Goods.getWeight() 1, 1, 2, 0
Goods.getWidth() 1, 1, 2, 0
Goods.Goods() 1, 1, 2, 0
Goods.Goods() 1, 7, 2, 0
Goods.setBillingPrinciples() 1, 1, 2, 0
Goods.setHeight() 1, 1, 2, 0
Goods.setId() 1, 1, 2, 0
Goods.setLength() 1, 1, 2, 0
Goods.setName() 1, 1, 2, 0
Goods.setWeight() 1, 1, 2, 0
Goods.setWidth() 1, 1, 2, 0
Goods.volumeWeight() 1, 1, 2, 0
Individual.getPrice() 1, 1, 2, 0
Individual.Individual() 1, 1, 2, 1
Individual.Individual() 1, 0, 0, 0
Main.main() 11, 100, 3, 77
NormalGoods.getRate() 5, 8, 2, 0
NormalGoods.NormalGoods() 1, 0, 0, 0
Order.getCustomer() 1, 1, 2, 0
Order.getFlight() 1, 1, 2, 0
Order.getGoods() 1, 1, 2, 0
Order.getId() 1, 1, 2, 0
Order.getPayment() 1, 1, 2, 0
Order.getPrice() 1, 1, 2, 0
Order.getRecipient() 1, 1, 2, 0
Order.getSender() 1, 1, 2, 0
Order.getTime() 1, 1, 2, 0
Order.Order() 1, 0, 0, 0
Order.Order() 1, 9, 2, 0
Order.setCustomer() 1, 1, 2, 0
Order.setFlight() 1, 1, 2, 0
Order.setGetPrice() 1, 1, 2, 0
Order.setGoods() 1, 1, 2, 0
Order.setId() 1, 1, 2, 0
Order.setPayment() 1, 1, 2, 0
Order.setRecipient() 1, 1, 2, 0
Order.setSender() 1, 1, 2, 0
Order.setTime() 1, 1, 2, 0
Order.totalPrice() 2, 4, 3, 1
Order.totalWeight() 2, 4, 3, 1
Payment.getName() 1, 1, 2, 0
Payment.Payment() 1, 0, 0, 0
Payment.Payment() 1, 1, 2, 0
Recipient.getAddress() 1, 1, 2, 0
Recipient.getName() 1, 1, 2, 0
Recipient.getPhoneNumber() 1, 1, 2, 0
Recipient.Recipient() 1, 0, 0, 0
Recipient.Recipient() 1, 3, 2, 0
Recipient.setAddress() 1, 1, 2, 0
Recipient.setName() 1, 1, 2, 0
Recipient.setPhoneNumber() 1, 1, 2, 0
Sender.getAddress() 1, 1, 2, 0
Sender.getName() 1, 1, 2, 0
Sender.getPhoneNumber() 1, 1, 2, 0
Sender.Sender() 1, 0, 0, 0
Sender.Sender() 1, 3, 2, 0
Sender.setAddress() 1, 1, 2, 0
Sender.setName() 1, 1, 2, 0
Sender.setPhoneNumber() 1, 1, 2, 0
ShowOrder.showOrder() 1, 0, 0, 0
ShowOrder.ShowOrder() 1, 1, 2, 0

代码块深度 语句数

0 20
1 152
2 236
3 38
4 0
5 0
6 0
7 0
8 0
9+ 0

  • [1] 缺乏注释

带注释的行的百分比为 0.0%,这会给后续的代码维护和理解带来极大困难,尤其是对于新接手项目的开发者,很难快速理解代码的功能和设计意图。

  • [2] 复杂度与语句数超标

主方法(Main.main ())严重臃肿,方法承担输入处理、对象创建、业务逻辑、输出展示等全部职责,违反单一职责原则。包含大量嵌套的 if-else 条件判断(如客户类型、货物类型、支付方式的分支),逻辑混乱。连续读取 20 + 行输入,未拆分任何子方法,导致代码可读性极差。

  • [3] 抽象类与接口滥用

类数量增至 18 个(航空 1 为 12 个),但部分抽象类(如 GetFinalPrice、Payment)仅包含单一方法,适合用接口替代。

  • [4] 耦合度强

Goods 类的 getPrice() 方法直接调用 billingPrinciples.getRate(),未隔离策略细节,导致 Goods 与计费规则强耦合。

我要多写注释,同时尽量优化复杂的方法,简化思路和写法,做到职责单一性,减少方法调用,降低耦合度。重构 Main.main (),将输入处理(如 readCustomer())、对象创建(如 createOrder())、业务逻辑(如 calculatePrice())拆分为独立方法或服务类,避免主方法直接操作细节。

总结

在完成这两次的题目集的过程中,我深刻理解了抽象类的使用,熟练了继承与多态的用法,也深入锻炼了自己的面向对象的思维。并且在这次题目中我深刻认识到了审题的重要性,花足够多的时间仔细理解读题,设计好结构比争分夺秒开始动手重要多了。而且我也在自己代码不足的地方有了一定的认识,以后肯定会在这方面多花精力。但是在遵循原则上我并没有完全遵循,还是有一些不足,会在下一次的题目集里慢慢成长改变。这次的题目也很需要耐心,有很多细节是要用心才能发现问题,这也培养了我坚持不懈和耐心仔细的品质。

posted @ 2025-05-20 10:43  Huuui  阅读(28)  评论(0)    收藏  举报