第二次blog作业-航空货运管理系统总结

航空货运管理系统

前言

这两次作业是对继承等知识的考察,但总来说不是非常难,但最大的不同是这两次作业,没有给我们类图作为参考,需要自己设计类来进行作业,在起初,我只是按照输出的不同部分进行分组,其实这是非常不熟练的表现,在五一前后我也对array list不是十分的熟练导致第一次作业完成需要借助ai的帮忙,这也导致我最后完成的十分糊涂,其实我自己也十分清楚我没有对这次作业的核心搞明白,下面我们来详细的分析两次作业

设计与分析

第一次航空货运管理

第一次得知这次的题是没有类图的,我觉得这次的题可能也和上次的电梯一样十分困难,但是我仔细分析了一下题目发现,不是十分的困难,只需要将它给予的输入部分分类就可以十分有条理的设计出框架。

按照上面不同类型我将代码的类分为了

customer,goods,flight,和order类,起初并没有使用继承这个方法,只是建四个独立的类,但是后来自己完成并不会使用Array List这一知识点,下面是我的代码:

 

第一次代码
 import java.util.Scanner;

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

        int customerId = sc.nextInt();
        sc.nextLine();
        String customerName = sc.nextLine();
        String customerPhone = sc.nextLine();
        String customerAddress = sc.nextLine();
        Customer customer = new Customer(customerId, customerName, customerPhone, customerAddress);

        int goodsCount = sc.nextInt();
        sc.nextLine();
        Goods[] goodsArray = new Goods[goodsCount];
        for (int i = 0; i < goodsCount; i++) {
            int goodsId = sc.nextInt();
            sc.nextLine();
            String goodsName = sc.nextLine();
            int goodsWidth = sc.nextInt();
            int goodsLength = sc.nextInt();
            int goodsHeight = sc.nextInt();
            double goodsWeight = sc.nextDouble();
            sc.nextLine();
            goodsArray[i] = new Goods(goodsName, goodsWidth, goodsLength, goodsHeight, goodsWeight);
        }

        String flightNumber = sc.nextLine();
        String departureAirport = sc.nextLine();
        String arrivalAirport = sc.nextLine();
        String flightDate = sc.nextLine();
        int maxLoadCapacity = sc.nextInt();
        sc.nextLine();
        Flight flight = new Flight(flightNumber, departureAirport, arrivalAirport, flightDate, maxLoadCapacity);

        String orderNumber = sc.nextLine();
        String orderDate = sc.nextLine();
        String senderAddress = sc.nextLine();
        String senderName = sc.nextLine();
        String senderPhone = sc.nextLine();
        String receiverAddress = sc.nextLine();
        String receiverName = sc.nextLine();
        String receiverPhone = sc.nextLine();
        Order order = new Order(orderNumber, orderDate, senderAddress, senderName, senderPhone, receiverAddress, receiverName, receiverPhone);

        double totalWeight = 0;
        for (Goods goods : goodsArray) {
            totalWeight += getChargeableWeight(goods);
        }
        if (totalWeight > flight.getMaxLoadCapacity()) {
            System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.\n", flight.getFlightNumber());
            return;
        }

        System.out.println("客户:" + customer.getCustomerName() + "(" + customer.getCustomerPhone() + ")订单信息如下:");
        System.out.println("-----------------------------------------");
        System.out.println("航班号:" + flight.getFlightNumber());
        System.out.println("订单号:" + order.getOrderNumber());
        System.out.println("订单日期:" + order.getOrderDate());
        System.out.println("发件人姓名:" + order.getSenderName());
        System.out.println("发件人电话:" + order.getSenderPhone());
        System.out.println("发件人地址:" + order.getSenderAddress());
        System.out.println("收件人姓名:" + order.getReceiverName());
        System.out.println("收件人电话:" + order.getReceiverPhone());
        System.out.println("收件人地址:" + order.getReceiverAddress());
        System.out.println("订单总重量(kg):" + totalWeight);
        System.out.println("微信支付金额:" + order.calculatePayment(goodsArray));

        System.out.println("\n货物明细如下:");
        System.out.println("-----------------------------------------");
        System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
        for (int i = 0; i < goodsCount; i++) {
            Goods goods = goodsArray[i];
            double chargeableWeight = getChargeableWeight(goods);
            double chargeableRate = calculateChargeableRate(goods);
            double freight = chargeableWeight * chargeableRate;
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", i + 1, goods.getGoodsName(), chargeableWeight, chargeableRate, freight);
        }
    }

    public static double getChargeableWeight(Goods goods) {
        double volumeWeight =0;
        if((goods.getGoodsWidth() * goods.getGoodsLength() * goods.getGoodsHeight()) / 6000.0>goods.getGoodsWeight()){
            volumeWeight=(goods.getGoodsWidth() * goods.getGoodsLength() * goods.getGoodsHeight()) / 6000.0;
        }
        else{
            volumeWeight = goods.getGoodsWeight();
        }
        return volumeWeight;
    }

    public static double calculateChargeableRate(Goods goods) {
        double volume = getChargeableWeight(goods);
        if (volume < 20) {
            return 35.0;
        } else if (volume < 50 && volume >= 20) {
            return 30.0;
        } else if(volume < 100 && volume >= 50) {
            return 25.0;
        } else{
            return 15.0;
        }
    }
}

class Customer {
    private int customerId;
    private String customerName;
    private String customerPhone;
    private String customerAddress;

    public Customer() {
    }

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

    // Getter and Setter methods
    public int getCustomerId() {
        return customerId;
    }

    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerPhone() {
        return customerPhone;
    }

    public void setCustomerPhone(String customerPhone) {
        this.customerPhone = customerPhone;
    }

    public String getCustomerAddress() {
        return customerAddress;
    }

    public void setCustomerAddress(String customerAddress) {
        this.customerAddress = customerAddress;
    }
}

class Goods {
    private String goodsName;
    private int goodsWidth;
    private int goodsLength;
    private int goodsHeight;
    private double goodsWeight;

    public Goods() {
    }

    public Goods(String goodsName, int goodsWidth, int goodsLength, int goodsHeight, double goodsWeight) {
        this.goodsName = goodsName;
        this.goodsWidth = goodsWidth;
        this.goodsLength = goodsLength;
        this.goodsHeight = goodsHeight;
        this.goodsWeight = goodsWeight;
    }

    // Getter and Setter methods
    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public int getGoodsWidth() {
        return goodsWidth;
    }

    public void setGoodsWidth(int goodsWidth) {
        this.goodsWidth = goodsWidth;
    }

    public int getGoodsLength() {
        return goodsLength;
    }

    public void setGoodsLength(int goodsLength) {
        this.goodsLength = goodsLength;
    }

    public int getGoodsHeight() {
        return goodsHeight;
    }

    public void setGoodsHeight(int goodsHeight) {
        this.goodsHeight = goodsHeight;
    }

    public double getGoodsWeight() {
        return goodsWeight;
    }

    public void setGoodsWeight(double goodsWeight) {
        this.goodsWeight = goodsWeight;
    }
}

class Flight {
    private String flightNumber;
    private String departureAirport;
    private String arrivalAirport;
    private String flightDate;
    private int maxLoadCapacity;

    public Flight() {
    }

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

    // Getter and Setter methods
    public String getFlightNumber() {
        return flightNumber;
    }

    public void setFlightNumber(String flightNumber) {
        this.flightNumber = flightNumber;
    }

    public String getDepartureAirport() {
        return departureAirport;
    }

    public void setDepartureAirport(String departureAirport) {
        this.departureAirport = departureAirport;
    }

    public String getArrivalAirport() {
        return arrivalAirport;
    }

    public void setArrivalAirport(String arrivalAirport) {
        this.arrivalAirport = arrivalAirport;
    }

    public String getFlightDate() {
        return flightDate;
    }

    public void setFlightDate(String flightDate) {
        this.flightDate = flightDate;
    }

    public int getMaxLoadCapacity() {
        return maxLoadCapacity;
    }

    public void setMaxLoadCapacity(int maxLoadCapacity) {
        this.maxLoadCapacity = maxLoadCapacity;
    }
}

class Order {
    private String orderNumber;
    private String orderDate;
    private String senderAddress;
    private String senderName;
    private String senderPhone;
    private String receiverAddress;
    private String receiverName;
    private String receiverPhone;

    public Order() {
    }

    public Order(String orderNumber, String orderDate, String senderAddress, String senderName, String senderPhone, String receiverAddress, String receiverName, String receiverPhone) {
        this.orderNumber = orderNumber;
        this.orderDate = orderDate;
        this.senderAddress = senderAddress;
        this.senderName = senderName;
        this.senderPhone = senderPhone;
        this.receiverAddress = receiverAddress;
        this.receiverName = receiverName;
        this.receiverPhone = receiverPhone;
    }

    // Getter and Setter methods
    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public String getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(String orderDate) {
        this.orderDate = orderDate;
    }

    public String getSenderAddress() {
        return senderAddress;
    }

    public void setSenderAddress(String senderAddress) {
        this.senderAddress = senderAddress;
    }

    public String getSenderName() {
        return senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    public String getSenderPhone() {
        return senderPhone;
    }

    public void setSenderPhone(String senderPhone) {
        this.senderPhone = senderPhone;
    }

    public String getReceiverAddress() {
        return receiverAddress;
    }

    public void setReceiverAddress(String receiverAddress) {
        this.receiverAddress = receiverAddress;
    }

    public String getReceiverName() {
        return receiverName;
    }

    public void setReceiverName(String receiverName) {
        this.receiverName = receiverName;
    }

    public String getReceiverPhone() {
        return receiverPhone;
    }

    public void setReceiverPhone(String receiverPhone) {
        this.receiverPhone = receiverPhone;
    }

    public double calculatePayment(Goods[] goodsArray) {
        double totalPayment = 0;
        for (Goods goods : goodsArray) {
            double chargeableWeight = Main.getChargeableWeight(goods);
            double chargeableRate = Main.calculateChargeableRate(goods);
            totalPayment += chargeableWeight * chargeableRate;
        }
        return totalPayment;
    }
}

 

可见编写的十分生疏,并没有使用复杂的知识点,并且在最后输出的地方也是用的简单的println,甚至有些类中的方法并没有使用

下面是我代码的分析结果

代码分析
 Metrics Details For File 'Main.java'
--------------------------------------------------------------------------------------------

Parameter				Value
=========				=====
Project Directory			D:\idl\pta\src\
Project Name				11
Checkpoint Name				Baseline
File Name				Main.java
Lines					375
Statements				229
Percent Branch Statements		4.4
Method Call Statements			53
Percent Lines with Comments		1.1
Classes and Interfaces			5
Methods per Class			11.20
Average Statements per Method		2.59
Line Number of Most Complex Method	93
Name of Most Complex Method		Main.calculateChargeableRate()
Maximum Complexity			7
Line Number of Deepest Block		18
Maximum Block Depth			3
Average Block Depth			1.72
Average Complexity			1.21

--------------------------------------------------------------------------------------------
Most Complex Methods in 5 Class(es):	Complexity, Statements, Max Depth, Calls

Customer.Customer()			1, 4, 2, 0
Customer.Customer()			1, 0, 0, 0
Customer.getCustomerAddress()		1, 1, 2, 0
Customer.getCustomerId()		1, 1, 2, 0
Customer.getCustomerName()		1, 1, 2, 0
Customer.getCustomerPhone()		1, 1, 2, 0
Customer.setCustomerAddress()		1, 1, 2, 0
Customer.setCustomerId()		1, 1, 2, 0
Customer.setCustomerName()		1, 1, 2, 0
Customer.setCustomerPhone()		1, 1, 2, 0
Flight.Flight()				1, 5, 2, 0
Flight.Flight()				1, 0, 0, 0
Flight.getArrivalAirport()		1, 1, 2, 0
Flight.getDepartureAirport()		1, 1, 2, 0
Flight.getFlightDate()			1, 1, 2, 0
Flight.getFlightNumber()		1, 1, 2, 0
Flight.getMaxLoadCapacity()		1, 1, 2, 0
Flight.setArrivalAirport()		1, 1, 2, 0
Flight.setDepartureAirport()		1, 1, 2, 0
Flight.setFlightDate()			1, 1, 2, 0
Flight.setFlightNumber()		1, 1, 2, 0
Flight.setMaxLoadCapacity()		1, 1, 2, 0
Goods.getGoodsHeight()			1, 1, 2, 0
Goods.getGoodsLength()			1, 1, 2, 0
Goods.getGoodsName()			1, 1, 2, 0
Goods.getGoodsWeight()			1, 1, 2, 0
Goods.getGoodsWidth()			1, 1, 2, 0
Goods.Goods()				1, 5, 2, 0
Goods.Goods()				1, 0, 0, 0
Goods.setGoodsHeight()			1, 1, 2, 0
Goods.setGoodsLength()			1, 1, 2, 0
Goods.setGoodsName()			1, 1, 2, 0
Goods.setGoodsWeight()			1, 1, 2, 0
Goods.setGoodsWidth()			1, 1, 2, 0
Main.calculateChargeableRate()		7, 9, 3, 1
Main.getChargeableWeight()		2, 5, 3, 4
Main.main()				5, 59, 3, 46
Order.calculatePayment()		2, 6, 3, 2
Order.getOrderDate()			1, 1, 2, 0
Order.getOrderNumber()			1, 1, 2, 0
Order.getReceiverAddress()		1, 1, 2, 0
Order.getReceiverName()			1, 1, 2, 0
Order.getReceiverPhone()		1, 1, 2, 0
Order.getSenderAddress()		1, 1, 2, 0
Order.getSenderName()			1, 1, 2, 0
Order.getSenderPhone()			1, 1, 2, 0
Order.Order()				1, 8, 2, 0
Order.Order()				1, 0, 0, 0
Order.setOrderDate()			1, 1, 2, 0
Order.setOrderNumber()			1, 1, 2, 0
Order.setReceiverAddress()		1, 1, 2, 0
Order.setReceiverName()			1, 1, 2, 0
Order.setReceiverPhone()		1, 1, 2, 0
Order.setSenderAddress()		1, 1, 2, 0
Order.setSenderName()			1, 1, 2, 0
Order.setSenderPhone()			1, 1, 2, 0

--------------------------------------------------------------------------------------------
Block Depth				Statements

0					6
1					78
2					119
3					26
4					0
5					0
6					0
7					0
8					0
9+					0
--------------------------------------------------------------------------------------------

 

分析结果表示:类和接口数量:有 5 个。平均每个类的方法数:11.2 个 。平均每个方法的语句数:2.59 条。最复杂方法:是 Main.calculateChargeableRate() ,其行数为 93 行,复杂度达 7 ,复杂度较高,可能维护起来较困难。最大代码块深度:为 3 ,平均代码块深度 1.72 。在可读性和可维护性上存在明显不足,这其实是我代码存在的问题。

第二次航空货运管理系统

这一次我吸取的上次的教训,但是这次一次的题目我觉得相较于上次一片空白来说我熟练了好多,因为大体框架我并没有修改,只是我运用自己的见解,在部分类中添加了有关其“type”的变量,我在这其中犯得最傻的问题可能就是我在验证两个字符串是否相同时用的是“==”这种方式,不知道是我学术不精,还是我对于c语言的习惯,始终在这里报错,我交给ai后我才修改了这个问题,下面是我的代码:

第二次代码
 import java.util.Scanner;
import java.util.InputMismatchException;

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

        // 读取客户信息
        String customertype = sc.nextLine();
        int customerId = sc.nextInt();
        sc.nextLine();
        String customerName = sc.nextLine();
        String customerPhone = sc.nextLine();
        String customerAddress = sc.nextLine();
        Customer customer = new Customer(customertype, customerId, customerName, customerPhone, customerAddress);

        String goodstype = sc.nextLine();
        int goodsCount = 0;
        try {
            goodsCount = sc.nextInt();
        } catch (InputMismatchException e) {
            System.out.println("Error: Goods count must be an integer.");
            sc.next(); // 清除错误输入
            return;
        }
        sc.nextLine(); // 清除换行符
        Goods[] goodsArray = new Goods[goodsCount];
        for (int i = 0; i < goodsCount; i++) {
            int goodsId = 0;
            try {
                goodsId = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("Error: Goods ID must be an integer.");
                sc.next(); // 清除错误输入
                return;
            }
            sc.nextLine(); // 清除换行符
            String goodsName = sc.nextLine();
            int goodsWidth = 0, goodsLength = 0, goodsHeight = 0;
            try {
                goodsWidth = sc.nextInt();
                goodsLength = sc.nextInt();
                goodsHeight = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("Error: Goods dimensions must be integers.");
                sc.next(); // 清除错误输入
                return;
            }
            double goodsWeight = 0.0;
            try {
                goodsWeight = sc.nextDouble();
            } catch (InputMismatchException e) {
                System.out.println("Error: Goods weight must be a number.");
                sc.next(); // 清除错误输入
                return;
            }
            sc.nextLine(); // 清除换行符
            goodsArray[i] = new Goods(goodstype, goodsName, goodsWidth, goodsLength, goodsHeight, goodsWeight);
        }

        // 读取航班信息
        String flightNumber = sc.nextLine();
        String departureAirport = sc.nextLine();
        String arrivalAirport = sc.nextLine();
        String flightDate = sc.nextLine();
        int maxLoadCapacity = 0;
        try {
            maxLoadCapacity = sc.nextInt();
        } catch (InputMismatchException e) {
            System.out.println("Error: Max load capacity must be an integer.");
            sc.next(); // 清除错误输入
            return;
        }
        sc.nextLine(); // 清除换行符
        Flight flight = new Flight(flightNumber, departureAirport, arrivalAirport, flightDate, maxLoadCapacity);

        // 读取订单信息
        String orderNumber = sc.nextLine();
        String orderDate = sc.nextLine();
        String senderAddress = sc.nextLine();
        String senderName = sc.nextLine();
        String senderPhone = sc.nextLine();
        String receiverAddress = sc.nextLine();
        String receiverName = sc.nextLine();
        String receiverPhone = sc.nextLine();
        String payway = sc.nextLine();
        Order order = new Order(orderNumber, orderDate, senderAddress, senderName, senderPhone, receiverAddress, receiverName, receiverPhone, payway, customer);

        // 计算总重量
        double totalWeight = 0;
        for (Goods goods : goodsArray) {
            totalWeight += getChargeableWeight(goods);
        }

        // 检查航班载重是否超出
        if (totalWeight > flight.getMaxLoadCapacity()) {
            System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.\n", flight.getFlightNumber());
            return;
        }

        // 输出客户和订单信息
        System.out.println("客户:" + customer.getCustomerName() + "(" + customer.getCustomerPhone() + ")订单信息如下:");
        System.out.println("-----------------------------------------");
        System.out.println("航班号:" + flight.getFlightNumber());
        System.out.println("订单号:" + order.getOrderNumber());
        System.out.println("订单日期:" + order.getOrderDate());
        System.out.println("发件人姓名:" + order.getSenderName());
        System.out.println("发件人电话:" + order.getSenderPhone());
        System.out.println("发件人地址:" + order.getSenderAddress());
        System.out.println("收件人姓名:" + order.getReceiverName());
        System.out.println("收件人电话:" + order.getReceiverPhone());
        System.out.println("收件人地址:" + order.getReceiverAddress());
        System.out.println("订单总重量(kg):" + totalWeight);
        System.out.println(order.getPayway() + "支付金额:" + order.calculatePayment(goodsArray));

        // 输出货物明细
        System.out.println("\n货物明细如下:");
        System.out.println("-----------------------------------------");
        System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
        for (int i = 0; i < goodsCount; i++) {
            Goods goods = goodsArray[i];
            double chargeableWeight = getChargeableWeight(goods);
            double chargeableRate = calculateChargeableRate(goods);
            double freight = chargeableWeight * chargeableRate;
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", i + 1, goods.getGoodsName(), chargeableWeight, chargeableRate, freight);
        }
    }

    public static double getChargeableWeight(Goods goods) {
        double volumeWeight = Math.max((goods.getGoodsWidth() * goods.getGoodsLength() * goods.getGoodsHeight()) / 6000.0, goods.getGoodsWeight());
        return volumeWeight;
    }

    public static double calculateChargeableRate(Goods goods) {
        double volume = getChargeableWeight(goods);
        switch (goods.getGoodstype()) {
            case "Normal":
                return volume < 20 ? 35.0 : volume < 50 ? 30.0 : volume < 100 ? 25.0 : 15.0;
            case "Dangerous":
                return volume < 20 ? 80.0 : volume < 50 ? 50.0 : volume < 100 ? 30.0 : 20.0;
            case "Expedite":
                return volume < 20 ? 60.0 : volume < 50 ? 50.0 : volume < 100 ? 40.0 : 30.0;
            default:
                return 0;
        }
    }
}

class Customer {
    private String customertype;
    private int customerId;
    private String customerName;
    private String customerPhone;
    private String customerAddress;

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

    public int getCustomerId() {
        return customerId;
    }

    public String getCustomerName() {
        return customerName;
    }

    public String getCustomerPhone() {
        return customerPhone;
    }

    public String getCustomerAddress() {
        return customerAddress;
    }

    public String getCustomertype() {
        return customertype;
    }
}

class Goods {
    private String goodstype;
    private String goodsName;
    private int goodsWidth;
    private int goodsLength;
    private int goodsHeight;
    private double goodsWeight;

    public Goods(String goodstype, String goodsName, int goodsWidth, int goodsLength, int goodsHeight, double goodsWeight) {
        this.goodstype = goodstype;
        this.goodsName = goodsName;
        this.goodsWidth = goodsWidth;
        this.goodsLength = goodsLength;
        this.goodsHeight = goodsHeight;
        this.goodsWeight = goodsWeight;
    }

    public String getGoodstype() {
        return goodstype;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public int getGoodsWidth() {
        return goodsWidth;
    }

    public int getGoodsLength() {
        return goodsLength;
    }

    public int getGoodsHeight() {
        return goodsHeight;
    }

    public double getGoodsWeight() {
        return goodsWeight;
    }
}

class Flight {
    private String flightNumber;
    private String departureAirport;
    private String arrivalAirport;
    private String flightDate;
    private int maxLoadCapacity;

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

    public String getFlightNumber() {
        return flightNumber;
    }

    public int getMaxLoadCapacity() {
        return maxLoadCapacity;
    }
}

class Order {
    private String orderNumber;
    private String orderDate;
    private String senderAddress;
    private String senderName;
    private String senderPhone;
    private String receiverAddress;
    private String receiverName;
    private String receiverPhone;
    private String payway;
    private Customer customer;

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

    public String getOrderNumber() {
        return orderNumber;
    }

    public String getOrderDate() {
        return orderDate;
    }

    public String getSenderName() {
        return senderName;
    }

    public String getSenderPhone() {
        return senderPhone;
    }

    public String getSenderAddress() {
        return senderAddress;
    }

    public String getReceiverName() {
        return receiverName;
    }

    public String getReceiverPhone() {
        return receiverPhone;
    }

    public String getReceiverAddress() {
        return receiverAddress;
    }

    public String getPayway() {
        if ("Wechat".equals(payway)) {
            return "微信";
        } else if ("ALiPay".equals(payway)) {
            return "支付宝";
        } else if ("Cash".equals(payway)) {
            return "现金";
        }
        return payway;
    }

    public double calculatePayment(Goods[] goodsArray) {
        double totalPayment = 0;
        for (Goods goods : goodsArray) {
            double chargeableWeight = Main.getChargeableWeight(goods);
            double chargeableRate = Main.calculateChargeableRate(goods);
            totalPayment += chargeableWeight * chargeableRate;
        }
        if ("Individual".equals(customer.getCustomertype())) {
            totalPayment *= 0.9;
        } else if ("Corporate".equals(customer.getCustomertype())) {
            totalPayment *= 0.8;
        }
        return totalPayment;
    }
}

 

其实我在第二次的作业中并没有加入其他的类,我只是在原有的基础上添加变量改动的很小,但其实我知道我这样导致我的代码,可改动的空间就很小,这样维护性也会很差,我并没有使用继承和抽象类的方法,下面是我这次代码的分析结果:

代码分析
 Metrics Details For File 'Main.java'
--------------------------------------------------------------------------------------------

Parameter				Value
=========				=====
Project Directory			D:\idl\pta\src\
Project Name				a
Checkpoint Name				Baseline
File Name				Main.java
Lines					333
Statements				209
Percent Branch Statements		9.1
Method Call Statements			63
Percent Lines with Comments		4.5
Classes and Interfaces			5
Methods per Class			5.80
Average Statements per Method		6.48
Line Number of Most Complex Method	5
Name of Most Complex Method		Main.main()
Maximum Complexity			8
Line Number of Deepest Block		30
Maximum Block Depth			4
Average Block Depth			1.85
Average Complexity			1.27

--------------------------------------------------------------------------------------------
Most Complex Methods in 5 Class(es):	Complexity, Statements, Max Depth, Calls

Customer.Customer()			1, 5, 2, 0
Customer.getCustomerAddress()		1, 1, 2, 0
Customer.getCustomerId()		1, 1, 2, 0
Customer.getCustomerName()		1, 1, 2, 0
Customer.getCustomerPhone()		1, 1, 2, 0
Customer.getCustomertype()		1, 1, 2, 0
Flight.Flight()				1, 5, 2, 0
Flight.getFlightNumber()		1, 1, 2, 0
Flight.getMaxLoadCapacity()		1, 1, 2, 0
Goods.getGoodsHeight()			1, 1, 2, 0
Goods.getGoodsLength()			1, 1, 2, 0
Goods.getGoodsName()			1, 1, 2, 0
Goods.getGoodstype()			1, 1, 2, 0
Goods.getGoodsWeight()			1, 1, 2, 0
Goods.getGoodsWidth()			1, 1, 2, 0
Goods.Goods()				1, 6, 2, 0
Main.main()				8, 69, 4, 43
Order.getOrderDate()			1, 1, 2, 0
Order.getOrderNumber()			1, 1, 2, 0
Order.getReceiverAddress()		1, 1, 2, 0
Order.getReceiverName()			1, 1, 2, 0
Order.getReceiverPhone()		1, 1, 2, 0
Order.getSenderAddress()		1, 1, 2, 0
Order.getSenderName()			1, 1, 2, 0
Order.getSenderPhone()			1, 1, 2, 0
Order.Order()				1, 10, 2, 0

--------------------------------------------------------------------------------------------
Block Depth				Statements

0					9
1					67
2					94
3					25
4					14
5					0
6					0
7					0
8					0
9+					0
--------------------------------------------------------------------------------------------
这份 Java 代码的主要问题集中在Main.main()方法:高复杂度风险main()方法复杂度达 8(平均仅 1.27),包含 69 行代码和 43 次方法调用,存在嵌套过深(最深 4 层)的问题,会导致可读性差、测试困难。结构失衡:5 个类中,其他方法复杂度均为 1(简单 getter / 构造器),而main()独占大部分逻辑,违背单一职责原则。注释不足:尽管注释行占比 4.5%,但核心逻辑(如main())可能缺乏必要注释,影响维护。改进建议:将main()中的业务逻辑拆分到独立方法或类中,减少嵌套层级,同时为复杂逻辑添加注释。
显然这次的代码是在上一次的基础上更改的,也让错误或者说漏洞更一步加深,我其实也知道我应该把代码好好的更新一下,但由于我觉得直接在“屎山代码”上直接加东西更方便,更便捷我就直接添加了,我觉得我还是要更好的,维护代码将代码写的更简便边界,可读性变强,使之满足各种原则。

踩坑心得:

我觉得这两次作业完成起来并不是很难,甚至来说不如其他有类图的难,但是要想把它变得更好甚至完美,十分之困难,因为我对于这些原则的掌握没有那么熟练甚至来说都没有入门。

还有在一些包的使用十分不熟练,比如在比较两个字符串是否相等时,我就没有第一时间明白其的用法,还需要靠ai来帮我实现,还有在标注释上我也是没有那么多,导致可读性非常差,甚至自己再来写都不清楚对应什么有意思。

改进建议:

我对于这两次的代码修改来说,应该优先重构Main.main()方法,将复杂逻辑拆分到独立的 Service 类中(如OrderService),降低方法复杂度;为关键业务逻辑(如calculateChargeableRate())添加注释,使用清晰变量名提升可读性;对数据类(如Customer)添加业务方法,遵循单一职责原则;通过接口定义契约(如OrderProcessor)实现开闭原则,新增功能时优先扩展而非修改原有代码;最后建立代码检查规范防止问题复发。

总结:

对于这两次的作业,我其实完成起来十分的快,并且也看起来是满分,但是我明白我还有十分多的不足,比如单一原则,开闭原则,接口,继承多态等等,如果我明白了这些知识点,并且熟练掌握,我觉得我这两次的作业我会完成得十分有条理,并十分轻松,我觉得我的编程道路任重而道远,但是通过这一次blog作业,也让我重新审视了自己,我应该减少对于ai的依赖,我还是应该强化自身,并且认真完成任务,更应该用更加简洁更加干练的方法,使自己的能力有提高。

posted @ 2025-05-25 13:28  石佰合  阅读(17)  评论(0)    收藏  举报