题目集8~9总结性Blog

题目集8~9总结性Blog

一 前言

这两次题目集涉及知识点较少,体量较少,难度也偏低。
主要考察了继承与多态的应用,以及通过继承和多态实现程序的可扩展性
如对点线面问题进行重构,以实现继承与多态的技术性需求。
对雨刷程序进行重构(Refactoring),使得程序可以对功能进行扩展等。
当然还有对航空货运管理系统进行类设计以达到符合继承与多态的要求

二 设计与分析

题目集08——航空货运管理系统(类设计)

题目要求:“航空货运管理系统”题目说明
输入要求:按顺序分别输入客户信息、货物信息、航班信息以及订单信息
按如下顺序分别输入客户信息、货物信息、航班信息以及订单信息。

客户编号
客户姓名
客户电话
客户地址
运送货物数量
[货物编号
货物名称
货物宽度
货物长度
货物高度
货物重量
]//[]内的内容输入次数取决于“运送货物数量”,输入不包含“[]”
航班号
航班起飞机场
航班降落机场
航班日期(格式为YYYY-MM-DD)
航班最大载重量
订单编号
订单日期(格式为YYYY-MM-DD)
发件人地址
发件人姓名
发件人电话
收件人地址
收件人姓名
收件人电话

输出要求如下:

客户:姓名(电话)订单信息如下:

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

货物明细如下:

明细编号 货物名称 计费重量 计费费率 应交运费
1 ...
2 ...

我的代码:

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

//客户类
class Customer {
    private String customerId;      //编号
    private String customerName;    //姓名
    private String customerPhone;   //电话
    private String customerAddress; //地址

    public Customer(String customerId,String customerName,String customerPhone,String customerAddress) {
        this.customerId = customerId;
        this.customerName = customerName;
        this.customerPhone = customerPhone;
        this.customerAddress = customerAddress;
    }

    public String getCustomerName() {
        return customerName;
    }

    public String getCustomerPhone() {
        return customerPhone;
    }

}
//货物类
class Cargo {
    private String cargoId; // 货物编号
    private String cargoName; // 货物名称
    private double width; // 货物宽度
    private double length; // 货物长度
    private double height; // 货物高度
    private double weight; // 货物重量

    public Cargo(String cargoId, String cargoName, double width, double length, double height, double weight) {
        this.cargoId = cargoId;
        this.cargoName = cargoName;
        this.width = width;
        this.length = length;
        this.height = height;
        this.weight = weight;
    }

    public String getCargoId() {
        return cargoId;
    }
    public String getCargoName() {
        return cargoName;
    }
    public double getWidth() {
        return width;
    }
    public double getLength() {
        return length;
    }
    public double getHeight() {
        return height;
    }
    public double getWeight() {
        return weight;
    }
    //  返回计费重量
    public double getChargeableWeight(){
        double grossWeight = getWeight();
        double volumeWeight = getLength() * getHeight() * getWidth() / 6000;
        return Math.max(grossWeight,volumeWeight);
    }
    //  返回计费费率
    public double getChargeableRate(double chargeableWeight){
        if(chargeableWeight >= 100) {
            return 15;
        }else if(chargeableWeight >= 50) {
            return 25;
        }else if(chargeableWeight >= 20) {
            return 30;
        }else{
            return 35;
        }
    }
}
//航班类
class Flight {
    private String flightNumber; // 航班号
    private String departureAirport; // 航班起飞机场
    private String arrivalAirport; // 航班降落机场
    private String flightDate; // 航班日期(格式为 YYYY-MM-DD)
    private double maxLoadWeight; // 航班最大载重量

    public Flight(String flightNumber, String departureAirport, String arrivalAirport, String flightDate, double maxLoadWeight) {
        this.flightNumber = flightNumber;
        this.departureAirport = departureAirport;
        this.arrivalAirport = arrivalAirport;
        this.flightDate = flightDate;
        this.maxLoadWeight = maxLoadWeight;
    }

    public String getFlightNumber() {
        return flightNumber;
    }
    public double getMaxLoadWeight() {
        return maxLoadWeight;
    }
}
//订单类
class Order {
    private Customer customer;
    private Flight flight;
    private String orderId; // 订单编号
    private String orderDate; // 订单日期(格式为 YYYY-MM-DD)
    private String senderAddress; // 发件人地址
    private String senderName; // 发件人姓名
    private String senderPhone; // 发件人电话
    private String recipientAddress; // 收件人地址
    private String recipientName; // 收件人姓名
    private String recipientPhone; // 收件人电话
    private List<Cargo> cargos; // 运送的货物列表

    public Order(Customer customer,Flight flight,String orderId, String orderDate, String senderAddress, String senderName, String senderPhone,
                 String recipientAddress, String recipientName, String recipientPhone) {
        this.customer = customer;
        this.flight = flight;
        this.orderId = orderId;
        this.orderDate = orderDate;
        this.senderAddress = senderAddress;
        this.senderName = senderName;
        this.senderPhone = senderPhone;
        this.recipientAddress = recipientAddress;
        this.recipientName = recipientName;
        this.recipientPhone = recipientPhone;
        this.cargos = new ArrayList<>();
    }

    //检查超重
    public boolean cggCargo(Cargo cargo) {
        double totalWeight = 0;
        for (Cargo c : cargos) {
            totalWeight += c.getChargeableWeight();
        }
        totalWeight += cargo.getChargeableWeight();
        if (totalWeight > flight.getMaxLoadWeight()) {
            return false; // 超过最大载重量
        }
        cargos.add(cargo);
        return true; // 未超过最大载重量
    }

    //添加货物
    public void addCargo(Cargo cargo){
        this.cargos.add(cargo);
    }

    public void shuchuxinxi() {
        double totalWeight = 0;
        double totalCost = 0;

        // 遍历所有货物,计算总重量和总费用
        for (int i = 0; i < cargos.size(); i++) {
            Cargo cargo = cargos.get(i);
            double chargeableWeight = cargo.getChargeableWeight();
            double rate = cargo.getChargeableRate(chargeableWeight);
            double cost = chargeableWeight * rate;
            totalWeight += chargeableWeight;
            totalCost += cost;
        }

        // 输出订单信息
        System.out.println("客户:" + customer.getCustomerName() + "(" + customer.getCustomerPhone() + ")订单信息如下:");
        System.out.println("-----------------------------------------");
        System.out.println("航班号:" + flight.getFlightNumber());
        System.out.println("订单号:" + orderId);
        System.out.println("订单日期:" + orderDate);
        System.out.println("发件人姓名:" + senderName);
        System.out.println("发件人电话:" + senderPhone);
        System.out.println("发件人地址:" + senderAddress);
        System.out.println("收件人姓名:" + recipientName);
        System.out.println("收件人电话:" + recipientPhone);
        System.out.println("收件人地址:" + recipientAddress);
        System.out.printf("订单总重量(kg):%.1f%n", totalWeight);
        System.out.printf("微信支付金额:%.1f%n", totalCost);

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

        for (int i = 0; i < cargos.size(); i++) {
            Cargo cargo = cargos.get(i);
            double chargeableWeight = cargo.getChargeableWeight();
            double rate = cargo.getChargeableRate(chargeableWeight);
            double cost = chargeableWeight * rate;
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f%n", i + 1, cargo.getCargoName(), chargeableWeight, rate, cost);
        }
    }
}


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();

        Customer customer = new Customer(customerId, customerName, customerPhone, customerAddress);

        int cargoCount = Integer.parseInt(scanner.nextLine());
        List<Cargo> cargos = new ArrayList<>();
        for (int i = 0; i < cargoCount; i++) {
            String cargoId = scanner.nextLine();
            String cargoName = 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());
            Cargo cargo = new Cargo(cargoId, cargoName, width, length, height, weight);
            cargos.add(cargo);
        }

        String flightNumber = scanner.nextLine();
        String departureAirport = scanner.nextLine();
        String arrivalAirport = scanner.nextLine();
        String flightDate = scanner.nextLine();
        double maxLoadWeight = Double.parseDouble(scanner.nextLine());
        Flight flight = new Flight(flightNumber, departureAirport, arrivalAirport, flightDate, maxLoadWeight);

        String orderId = scanner.nextLine();
        String orderDate = scanner.nextLine();
        String senderAddress = scanner.nextLine();
        String senderName = scanner.nextLine();
        String senderPhone = scanner.nextLine();
        String recipientAddress = scanner.nextLine();
        String recipientName = scanner.nextLine();
        String recipientPhone = scanner.nextLine();

        Order order = new Order(customer, flight, orderId, orderDate, senderAddress, senderName, senderPhone,
                recipientAddress, recipientName, recipientPhone);

        for (Cargo cargo : cargos) {
            if (!order.cggCargo(cargo)) {
                System.out.println("The flight with flight number:" + flight.getFlightNumber() + " has exceeded its load capacity and cannot carry the order.");
                return; // 终止程序运行
            }
        }

        // 输出订单信息和货物明细
        order.shuchuxinxi();
    }
}

类图设计:

image

代码质量分析:

image
显然,我的代码有许多不足,有许多的可优化部分,但为了赶时间无视了可能存在的错误,后期我会继续改进

题目集08——雨刷程序功能扩展设计

在给定的汽车手动风挡玻璃雨刷程序的基础上,对程序进行重构(Refactoring),使得程序可以对功能进行扩展
部分给出代码:
https://images.ptausercontent.com/dca9d940-3997-4ede-bc7d-99407b679357.pdf
雨刷类设计要求:
https://images.ptausercontent.com/3ed581e4-efb0-4fe5-ae31-f9e09c6feb6d.pdf
题目总体而言较简单,创建共同父类以实现继承与多态即可
输入格式:
输入共2行,第一行为一个整型数字,取值范围为[1,2],其中1代表表1所描述的雨刷系统,2代表表2所描述的雨刷系统;第二行为若干个用一个或多个空格分开且以数字0结束的整型数字,取值范围为[1,4],其中1代表控制杆升档操作、2代表控制杆降档操作、3代表刻度盘升刻度操作、4代表刻度盘降刻度操作、0代表操作结束(输入时只要遇到0即认为输入结束)。

输出格式:
程序的输出行数根据每一次对控制杆/刻度盘操作次数而定,每一次对控制杆/刻度盘进行了操作,则输出一行数据。格式为:操作类型/控制杆当前档位/刻度盘当前刻度/雨刷当前速度
其中,操作类型共四种,分别为Lever up、Lever down、Dial up、Dial down;控制杆当前档位显示中文内容,例如停止、间歇、低速、高速、超高速(表2);刻度盘当前刻度显示为数值,例如1、2、3、4、5(4、5见表2);雨刷当前速度显示为整型数值。
我的代码类图图表:
image

简单跳过雨刷类设计(Q^Q)

题目集08——点线面问题重构(继承与多态)

题目要求:

1.对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
2.再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
3.在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。

我的代码:

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

class Element {
    public Element(){}
    public void display() {
    }
}
// Point类的定义
class Point extends Element{
    private double x; // 横坐标
    private double y; // 纵坐标
    // 构造方法
    public Point(double x, double y) {
        if (x <= 0 || x > 200 || y <= 0 || y > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        this.x = x;
        this.y = y;
    }
    // getter方法和setter方法
    public double getX() {
        return x;
    }
    public void setX(double x) {
        if (x <= 0 || x > 200) {
            System.out.println("Wrong Format");
            return;
        }
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        if (y <= 0 || y > 200) {
            System.out.println("Wrong Format");
            return;
        }
        this.y = y;
    }
    // 显示信息的方法
    public void display() {
        System.out.printf("(%.2f,%.2f)%n", x, y);
    }
}

// Line类的定义
class Line extends Element{
    private Point point1;
    private Point point2;
    private String color; // 颜色
    // 构造方法
    public Line(Point point1, Point point2, String color) {
        this.point1 = point1;
        this.point2 = point2;
        this.color = color;
    }
    // getter方法和setter方法
    public Point getPoint1() {
        return point1;
    }
    public void setPoint1(Point point1) {
        this.point1 = point1;
    }
    public Point getPoint2() {
        return point2;
    }
    public void setPoint2(Point point2) {
        this.point2 = point2;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    // 计算线段长度的方法
    public double getDistance() {
        double dx = point1.getX() - point2.getX();
        double dy = point1.getY() - point2.getY();
        return Math.sqrt(dx * dx + dy * dy);
    }
    // 显示线段信息的方法
    public void display() {
        point1.display();
        point2.display();
        System.out.println("The line's color is:" + color);
        System.out.println("The line's begin point's Coordinate is:");
        point1.display();
        System.out.println("The line's end point's Coordinate is:");
        point2.display();
        System.out.println("The line's length is:" + String.format("%.2f", getDistance()));
    }
}

class Plane extends Element {
    private String color;
    
    public Plane(String color) {
        super();
        this.color = color;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public void display() {
        System.out.println("The Plane's color is:" + color);
    }
}

// Main类,包含main方法
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 输入起始坐标
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
        scanner.nextLine(); // 消耗掉之前的换行符
        String color = scanner.nextLine();
        // 创建对象
        Line line = new Line(new Point(x1, y1), new Point(x2, y2), color);
        Plane plane = new Plane(color);
        
        line.display();
        plane.display();
        scanner.close();
    }
}

代码类图:

image

题目集09——航空货运管理系统(继承与多态)

输入格式

客户类型[可输入项:Individual/Corporate]
客户编号
客户姓名
客户电话
客户地址
货物类型[可输入项:Normal/Expedite/Dangerous]
运送货物数量
[货物编号
货物名称
货物宽度
货物长度
货物高度
货物重量
]//[]内的内容输入次数取决于“运送货物数量”,输入不包含“[]”
航班号
航班起飞机场
航班降落机场
航班日期(格式为YYYY-MM-DD)
航班最大载重量
订单编号
订单日期(格式为YYYY-MM-DD)
发件人地址
发件人姓名
发件人电话
收件人地址
收件人姓名
收件人电话
支付方式[可输入项:Wechat/ALiPay/Cash]

输出格式

客户:姓名(电话)订单信息如下:
航班号:
订单号:
订单日期:
发件人姓名:
发件人电话:
发件人地址:
收件人姓名:
收件人电话:
收件人地址:
订单总重量(kg):
[微信/支付宝/现金]支付金额:

货物明细如下:
明细编号 货物名称 计费重量 计费费率 应交运费
1 ...
2 ...

我的代码:

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

//客户类
abstract class Customer {
    private String customerId;      //编号
    private String customerName;    //姓名
    private String customerPhone;   //电话
    private String customerAddress; //地址
    protected double customerDiscount; //折扣
    public Customer(String customerId, String customerName, String customerPhone, String customerAddress) {
        this.customerId = customerId;
        this.customerName = customerName;
        this.customerPhone = customerPhone;
        this.customerAddress = customerAddress;
    }
    public String getCustomerName() {
        return customerName;
    }
    public String getCustomerPhone() {
        return customerPhone;
    }
    public abstract double getCustomerDiscount();
}
//个人用户
class IndividualCustomer extends Customer{
    private final double customerDiscount = 0.9;
    public IndividualCustomer(String customerId,String customerName,String customerPhone,String customerAddress){
        super(customerId,customerName,customerPhone,customerAddress);
    }
    public double getCustomerDiscount() {
        return customerDiscount;
    }
}
//集团用户
class GroupCustomers extends Customer{
    private final double customerDiscount = 0.8;
    public GroupCustomers(String customerId, String customerName, String customerPhone, String customerAddress){
        super(customerId,customerName,customerPhone,customerAddress);
    }
    public double getCustomerDiscount() {
        return customerDiscount;
    }
}


//货物类
abstract class Cargo {
    private String cargoId; // 货物编号
    private String cargoName; // 货物名称
    private double width; // 货物宽度
    private double length; // 货物长度
    private double height; // 货物高度
    private double weight; // 货物重量

    public Cargo(String cargoId, String cargoName, double width, double length, double height, double weight) {
        this.cargoId = cargoId;
        this.cargoName = cargoName;
        this.width = width;
        this.length = length;
        this.height = height;
        this.weight = weight;
    }

    public String getCargoId() {
        return cargoId;
    }

    public String getCargoName() {
        return cargoName;
    }

    public double getWidth() {
        return width;
    }

    public double getLength() {
        return length;
    }

    public double getHeight() {
        return height;
    }

    public double getWeight() {
        return weight;
    }

    //  返回计费重量
    protected double getChargeableWeight() {
        double grossWeight = getWeight();
        double volumeWeight = getLength() * getHeight() * getWidth() / 6000;
        return Math.max(grossWeight, volumeWeight);
    }

    //  返回计费费率
    public abstract double getChargeableRate(double chargeableWeight);

    //返回基础运费(无折扣)
    public double getBasicShippingFee() {
        double chargeableWeight = getChargeableWeight();
        double rate = getChargeableRate(chargeableWeight);
        return rate * chargeableWeight;
    }
}

//普通货物
class NormalCargo extends Cargo{
    public NormalCargo(String cargoId, String cargoName, double width, double length, double height, double weight) {
        super(cargoId,cargoName,width,length,height,weight);
    }
    @Override
    public double getChargeableRate(double chargeableWeight){
        if(chargeableWeight >= 100) {
            return 15;
        }else if(chargeableWeight >= 50) {
            return 25;
        }else if(chargeableWeight >= 20) {
            return 30;
        }else{
            return 35;
        }
    }
}
//危险货物
class DangerousCargo extends Cargo{
    public DangerousCargo(String cargoId, String cargoName, double width, double length, double height, double weight) {
        super(cargoId,cargoName,width,length,height,weight);
    }
    @Override
    public double getChargeableRate(double chargeableWeight){
        if(chargeableWeight >= 100) {
            return 20;
        }else if(chargeableWeight >= 50) {
            return 30;
        }else if(chargeableWeight >= 20) {
            return 50;
        }else{
            return 80;
        }
    }
}

//加急货物
class ExpediteCargo extends Cargo{
    public ExpediteCargo(String cargoId, String cargoName, double width, double length, double height, double weight) {
        super(cargoId,cargoName,width,length,height,weight);
    }
    @Override
    public double getChargeableRate(double chargeableWeight){
        if(chargeableWeight >= 100) {
            return 30;
        }else if(chargeableWeight >= 50) {
            return 40;
        }else if(chargeableWeight >= 20) {
            return 50;
        }else{
            return 60;
        }
    }
}
//航班类
class Flight {
    private String flightNumber; // 航班号
    private String departureAirport; // 航班起飞机场
    private String arrivalAirport; // 航班降落机场
    private String flightDate; // 航班日期(格式为 YYYY-MM-DD)
    private double maxLoadWeight; // 航班最大载重量

    public Flight(String flightNumber, String departureAirport, String arrivalAirport, String flightDate, double maxLoadWeight) {
        this.flightNumber = flightNumber;
        this.departureAirport = departureAirport;
        this.arrivalAirport = arrivalAirport;
        this.flightDate = flightDate;
        this.maxLoadWeight = maxLoadWeight;
    }

    public String getFlightNumber() {
        return flightNumber;
    }
    public double getMaxLoadWeight() {
        return maxLoadWeight;
    }
    public String getFlightDate() {
        return flightDate;
    }
    public String getArrivalAirport() {
        return arrivalAirport;
    }
    public String getDepartureAirport() {
        return departureAirport;
    }

}
//订单类
class Order {
    private Customer customer;//客户
    private Flight flight;//航班
    private String zhifu;  //支付方式
    private String orderId; // 订单编号
    private String orderDate; // 订单日期(格式为 YYYY-MM-DD)
    private String senderAddress; // 发件人地址
    private String senderName; // 发件人姓名
    private String senderPhone; // 发件人电话
    private String recipientAddress; // 收件人地址
    private String recipientName; // 收件人姓名
    private String recipientPhone; // 收件人电话
    private List<Cargo> cargos; // 运送的货物列表

    public Order(Customer customer,Flight flight,String orderId, String orderDate, String senderAddress, String senderName, String senderPhone,
                 String recipientAddress, String recipientName, String recipientPhone,String zhifu) {
        this.customer = customer;
        this.flight = flight;
        this.orderId = orderId;
        this.orderDate = orderDate;
        this.senderAddress = senderAddress;
        this.senderName = senderName;
        this.senderPhone = senderPhone;
        this.recipientAddress = recipientAddress;
        this.recipientName = recipientName;
        this.recipientPhone = recipientPhone;
        this.cargos = new ArrayList<>();
        this.zhifu = zhifu;
    }

    //检查超重
    public boolean cggCargo(Cargo cargo) {
        double totalWeight = 0;
        for (Cargo c : cargos) {
            totalWeight += c.getChargeableWeight();
        }
        totalWeight += cargo.getChargeableWeight();
        if (totalWeight > flight.getMaxLoadWeight()) {
            return false; // 超过最大载重量
        }
        cargos.add(cargo);
        return true; // 未超过最大载重量
    }

    //添加货物
    public void addCargo(Cargo cargo){
        this.cargos.add(cargo);
    }

    public void shuchuxinxi() {
        double totalWeight = 0;
        double totalCost = 0;
        // 遍历所有货物,计算总重量和总费用
        for (int i = 0; i < cargos.size(); i++) {
            Cargo cargo = cargos.get(i);
            double chargeableWeight = cargo.getChargeableWeight();//计费重量
            double rate = cargo.getChargeableRate(chargeableWeight);//费率
            double cost = chargeableWeight * rate;//总金额
            totalWeight += chargeableWeight;//总重量
            totalCost += cost * customer.getCustomerDiscount();
        }

        // 输出订单信息
        System.out.println("客户:" + customer.getCustomerName() + "(" + customer.getCustomerPhone() + ")订单信息如下:");
        System.out.println("-----------------------------------------");
        System.out.println("航班号:" + flight.getFlightNumber());
        System.out.println("订单号:" + orderId);
        System.out.println("订单日期:" + orderDate);
        System.out.println("发件人姓名:" + senderName);
        System.out.println("发件人电话:" + senderPhone);
        System.out.println("发件人地址:" + senderAddress);
        System.out.println("收件人姓名:" + recipientName);
        System.out.println("收件人电话:" + recipientPhone);
        System.out.println("收件人地址:" + recipientAddress);
        System.out.printf("订单总重量(kg):%.1f%n", totalWeight);
        System.out.printf("%s支付金额:%.1f%n",zhifu,totalCost);

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

        for (int i = 0; i < cargos.size(); i++) {
            Cargo cargo = cargos.get(i);
            double chargeableWeight = cargo.getChargeableWeight();
            double rate = cargo.getChargeableRate(chargeableWeight);
            double cost = chargeableWeight * rate;
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f%n", i + 1, cargo.getCargoName(), chargeableWeight, rate, cost);
        }
    }
}


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

        //客户类型
        String customerType = scanner.nextLine();
        String customerId = scanner.nextLine();//编号
        String customerName = scanner.nextLine();//名字
        String customerPhone = scanner.nextLine();//电话
        String customerAddress = scanner.nextLine();//地址

        Customer customer = null;

        if("Individual".equals(customerType)) {
            //新建客户
            customer = new IndividualCustomer(customerId, customerName, customerPhone, customerAddress);
        } else if("Corporate".equals(customerType)){
            customer = new GroupCustomers(customerId, customerName, customerPhone, customerAddress);
        } else {
            System.out.println("Invalid customer type. Please enter 'Individual' or 'Corporate'.");
            return;
        }


        //货物类型
        String cargoType = scanner.nextLine();
        int cargoCount = Integer.parseInt(scanner.nextLine());
        List<Cargo> cargos = new ArrayList<>();
        for (int i = 0; i < cargoCount; i++) {
            String cargoId = scanner.nextLine();//编号
            String cargoName = 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());//重

            if("Normal".equals(cargoType)) {
                Cargo cargo = new NormalCargo(cargoId, cargoName, width, length, height, weight);
                cargos.add(cargo);
            } else if("Expedite".equals(cargoType)) {
                Cargo cargo = new ExpediteCargo(cargoId, cargoName, width, length, height, weight);
                cargos.add(cargo);
            } else if("Dangerous".equals(cargoType)) {
                Cargo cargo = new DangerousCargo(cargoId, cargoName, width, length, height, weight);
                cargos.add(cargo);
            }else {
                System.out.println("Invalid cargo type. Please enter 'Normal', 'Expedite', or 'Dangerous'.");
                return;
            }
        }

        //航班
        String flightNumber = scanner.nextLine();//航班号
        String departureAirport = scanner.nextLine();//起飞机场
        String arrivalAirport = scanner.nextLine();//降落机场
        String flightDate = scanner.nextLine();//日期
        double maxLoadWeight = Double.parseDouble(scanner.nextLine());//最大载重
        Flight flight = new Flight(flightNumber, departureAirport, arrivalAirport, flightDate, maxLoadWeight);

        //订单
        String orderId = scanner.nextLine();//编号
        String orderDate = scanner.nextLine();//日期
        String senderAddress = scanner.nextLine();//地址
        String senderName = scanner.nextLine();//姓名
        String senderPhone = scanner.nextLine();//电话
        String recipientAddress = scanner.nextLine();
        String recipientName = scanner.nextLine();
        String recipientPhone = scanner.nextLine();

        //支付方式
        String zhifu = scanner.nextLine().trim().toLowerCase();
        String zhifyfshi = " ";
        if ("wechat".equals(zhifu)) {
            zhifyfshi = "微信";
        } else if ("alipay".equals(zhifu)) {
            zhifyfshi = "支付宝";
        } else if ("cash".equals(zhifu)) {
            zhifyfshi = "现金";
        }
        Order order = new Order(customer, flight, orderId, orderDate, senderAddress, senderName, senderPhone,
                recipientAddress, recipientName, recipientPhone,zhifyfshi);

        for (Cargo cargo : cargos) {
            if (!order.cggCargo(cargo)) {
                System.out.println("The flight with flight number:" + flight.getFlightNumber() + " has exceeded its load capacity and cannot carry the order.");
                return; // 终止程序运行
            }
        }

        // 输出订单信息和货物明细
        order.shuchuxinxi();
    }
}
#### 代码类图:

image

代码质量分析:

image
1.分支语句百分比(Percent Branch Statements):11.1% ,说明代码中分支逻辑(如 if - else 、 switch 等)占比相对不算高,代码逻辑结构不算特别复杂,有待改进
2.含注释行百分比(Percent Lines with Comments):17.0% ,表明约 1/6 的代码行有注释,代码有一定注释量,有点少
3.每个类的平均方法数(Methods/Class):8.00 ,平均每个类包含 8 个方法,类的功能相对丰富
4.每个方法的平均语句数(Avg Stmts/Method):6.75 ,方法内平均语句数量适中
5.最大复杂度(Max Complexity):5 ,代码中最复杂方法的复杂度为 5 ,复杂度不算极高
6.平均复杂度(Avg Complexity):1.67 ,整体代码的平均复杂度较低,代码相对容易维护和理解
7.平均深度(Avg Depth):1.13 ,表明整体代码逻辑嵌套的平均深度较浅。

总体而言,我认为我这次程序做的不错,

三 踩坑心得

这我就有很多话要说了
首先是,题目集09的第一题魔方问题,类的设计和方法还好,严格满足了题目要求与类图设计要求,但是在正三棱锥魔方的基元数量算法我想了一个下午,电脑查AI,手机查百度也没查出一个具体的算法出来,每一个,每一个!都错了,后来我的解决方法是:不用管基元数量多少,只需要知道阶数和基元边长直接计算体积就行,想这么多反而弄巧成拙。

然后是题目集09的航空货物管理系统,一开始我像头🐖一样,搞不清折扣率该用什么方法实现,是设计一个接口?感觉有点大材小用,是添加一个父类属性?好像可以.....别看这么简单的问题,对有选择强迫症来说太难了,在我选择父类并设计及其子类后,一直按部就班的做,我认为这次没啥难度,可在我提交时却只拿了12分,我发现是忘了加超重报错,但添加后提交也只拿24分,想不通哪里出了错,明明过了样例,最后我发现是货物类型出了问题,我把加急货物子类和危险货物子类的名称写反了(我日语生,英语不好很正常吧?🤡).......修改两个子类和主类里的调用方法后就过了。

总结

在这次题目集中,主要考点是继承与多态,题目偏重点也是这方面,我学到了父类中的抽象方法到底该如何调用实现,同时我又发现了自己的严重不足,那就是算法不会,明明自己高数成绩还好,却因为逻辑处理能力太弱,无法将问题里的逻辑抽离出来,从而无法转换为数学问题并设计算法,以后要着重练习这方面。

posted @ 2025-05-20 10:19  苏俊宏  阅读(26)  评论(0)    收藏  举报