关于对面向对象第8~9次作业的总结blog

前言

关于第八到九次的java大作业的内容来说,都是基于对前几次大作业中的题目的迭代和完善,主要要求我们使用多态和接口对这些题目进行拓展,对于我来说题量和难度都是适中稍微有一点点问题的水平,但在编写代码的过程中我还是碰到了不少的困难,例如过于繁杂的输出代码导致后面调试的问题等等。

设计与分析

  • 第一次货物问题的类图:
    image
    对类的分析:

  • People类:
    为题目中与人员相关的父类,代码中的Client,Deliver,Accept类都是他的子类,分别表示下单的顾客,发件人,收件人,对人员相关的信息进行管理和获取。

  • Good类:
    为将要发出的相关货物的类,其中包含货物的所有数据以及计算货物实际重量和体积重量的方法以及其余的get,set方法。

  • Operation类:
    订单类,用于处理多个货物的订单,其中有两个方法用于计算全部货物的总重量和总价格。

  • DefaultFeeCalculation类:
    用于计算单个货物的运费价格以及运费的费率。

  • Flight类:
    用于储存航班信息相关数据,继承自time类。

  • time类:
    为一普通父类,用于记录航班时间(年月日)其中包含相关的get,set方法。

  • Intend类:
    用于记录订单的时间以及订单id。

  • Main类:
    主方法,用于获取所有输入以及输出信息的类。


使用SourceMointor的分析结果:
image


从SourceMointor给出的结果就可见:

  1. 代码可读性方面
    问题:含注释代码行百分比仅 3.2% 。这意味着代码几乎没有注释,对代码理解极为不利。当其他开发人员接手,或是自己在一段时间后回顾代码时,很难快速把握代码逻辑。
    反思:在开发过程中,过于注重功能实现,忽视了代码注释的重要性。没有认识到注释是代码文档化的关键部分,能够有效降低维护成本。后续开发需遵循良好的编码规范,及时添加注释,尤其是在关键逻辑、算法实现处。
  2. 类与方法设计方面
    问题:每个类的方法数平均为 7.50 ,可能导致类的职责不够单一;每个方法平均语句数为 0.00 ,数据异常,与实际不符。
    反思:在设计类和方法时,缺乏对单一职责原则的严格遵循,没有对类的功能进行细致拆分。同时,对于代码统计工具的原理和适用场景了解不足,导致出现异常统计数据而未察觉。后续要加强对设计原则的运用,深入理解代码度量工具。
  3. 代码复杂度方面
    问题:最大复杂度为 0 ,未识别出复杂方法,可能是代码简单,也可能是统计异常;最大代码块深度为 3 ,平均代码块深度为 1.61 ,虽然嵌套程度不算高,但仍有优化空间。
    反思:对代码复杂度的把控存在不确定性,没有主动运用工具和方法来评估复杂度。在编写代码时,没有充分考虑代码结构的优化,导致可能存在潜在的复杂逻辑未被发现。今后要定期运用工具分析代码复杂度,优化代码结构。

--》在这次的大作业中没有去率先规划相关的代码结构以及代码的功能分布,导致有部分代码的功能重复,代码繁杂且行数过多,但是编写完成之后成功的跑起来了我就图省事没去进一步优化我的代码结构现在才看起来像是没有一点类之间关系的代码。


  • 第二次货物问题的类图:
    image
    对类的分析:

  • People类:
    为题目中与人员相关的父类,代码中的Client,Deliver,Accept类都是他的子类,分别表示下单的顾客,发件人,收件人,对人员相关的信息进行管理和获取。

  • Good类:
    为一普通父类,为将要发出的相关货物储存信息,其中包含货物的所有数据以及计算货物实际重量和体积重量的方法以及其余的get,set方法。

  • GoodOperation类:
    修改后的货物订单类,用于处理多个相同类型的货物的订单,其中有两个方法用于计算全部货物的总重量和总价格。

  • DefaultFeeCalculation类:
    用于计算单个货物的运费价格以及运费的费率。

  • Flight类:
    用于储存航班信息相关数据,继承自time类。

  • time类:
    为一普通父类,用于记录航班时间(年月日)其中包含相关的get,set方法。

  • Intend类:
    用于记录订单的时间以及订单id。

  • Main类:
    主方法,用于获取所有输入以及输出信息的类。

  • DangerousGood,NoramlGood,ExpediteGood类:
    为Good类的子类,分别表示三种不同的货物,其中三种类的运费费率计算互相独立,输出相关金额时单独调用其中的计算方法。


使用SourceMointor的分析结果:
image


从SourceMointor给出的结果就可见:

  1. 代码注释层面
    问题:含注释代码行百分比仅 6.6% ,注释占比依旧过少。
    反思:在编写代码过程中,对注释的重视程度不足,没有充分意识到注释是代码的重要组成部分。错误地认为代码逻辑自己清楚就行,忽略了未来维护时他人理解代码的需求。后续要养成边写代码边添加注释的习惯,按照规范对关键代码段、参数含义、算法逻辑等进行说明。

  2. 类与方法设计层面
    问题:每个类平均有 10 个方法,类的职责大概率不够单一;每个方法平均语句数为 0.00 ,数据异常。
    反思:设计类和方法时,缺乏对设计原则的严格遵循,没有深入思考类的功能边界。在追求功能实现的过程中,没有合理拆分功能,导致类承担过多职责。同时,对于代码度量工具的使用和原理了解不够,未能及时察觉统计数据异常。后续要加强对设计模式和原则的学习与应用,并且深入研究代码度量工具,确保数据的准确性和有效性。

  3. 代码复杂度层面
    问题:代码的复杂度不知道为什么没识别出来但是根据我自己的理解来看代码很繁杂;最大代码块深度达 5 ,存在较深层嵌套结构。
    反思:在代码编写时,没有主动对代码复杂度进行分析和把控,过于关注功能实现而忽视了代码结构的优化。对代码复杂度的衡量缺乏敏感度,没有借助工具定期评估。对于代码嵌套结构,没有提前规划和优化,导致出现较深层嵌套。今后要将代码复杂度评估纳入开发流程,定期使用工具分析,及时优化复杂代码结构,避免深层嵌套。

  4. 可复用性方面
    问题:从类和方法的数量及分布来看,可能存在大量功能代码分散在不同类的方法中,却未进行合理封装以便复用。比如一些通用的业务逻辑处理、数据转换等操作,可能在多个方法中重复实现。
    反思:开发过程中缺乏对代码复用性的考量,没有从整体架构层面去规划可复用的组件或方法。过于注重当前功能的快速实现,而没有站在长远角度思考如何减少重复代码,提高开发效率。后续应培养抽象思维,提取通用功能,封装成可复用的模块或工具类。
    --》这次的大作业基本是在上一次大作业的模板上修改,并没有对上次的代码进行优化,只是加上了这次作业中要求的相关需求,并没有针对性的修改代码和去将代码修改到符合我心里预期的样子。


踩坑心得

  • 若没有在一开始编写代码的时候去规划好自己的类设计以及功能的分布,后期在编写代码时会不去注意类的复用这方面的陷阱导致代码的繁杂无序;
  • 在设计继承关系时应在最大限度内将复用的变量和方法都编写进父类中以减少后期的工作量;
  • 当设计的类中存在多个不同的作用但是大体相同的方法时还是要使用父类然后重写相关代码,这样在结构上更加清晰正确
  • 在设计类的继承关系时,我直接让Client、Deliever、Accept继承自People类,没有充分考虑每个子类的独特职责。结果导致这几个子类功能重叠,没有真正发挥继承的优势。比如它们都有相似的属性和方法,却没有根据各自的业务场景进行差异化设计,在设计类继承关系前,一定要深入分析每个类的核心职责,确保子类能够清晰地扩展父类功能,遵循单一职责原则,避免出现类功能混乱的情况。
  • 我定义了NormalGood、DangerousGood、ExpediteGood三个货物子类继承自Good类,但在实际代码中,这三个子类完全没有扩展任何独特的属性或方法,仅仅是作为标记存在。这导致继承关系形同虚设,无法利用多态性简化逻辑。例如,运费计算时仍需通过字符串type判断货物类型(如if ("Dangerous".equals(type))),耦合性极高。

代码不足以及可优化处

  • 注释太少了交给其他人的话可能会浪费时间在识别我的代码的作用上。
  • 在NormalGood、DangerousGood、ExpediteGood三个货物子类的编写上有很大的问题,为了方便我根本没有用上这三个子类而是直接去用字符识别。
  • 删除有太多相同功能的方法的类而采取复用的方式去实现代码的简洁化。
总结

两次作业中的货物问题给了我启发,在后面的迭代的大作业中我应该注意代码的复用性以及简洁和注释的齐全,不在出现像这次作业中这样定义了子类但完全没用上的情况。
在对代码进行编写时我也同样的发现我对一些基础知识的运用并不娴熟,之后的练习我也会注意这方面的。
这次的渐进式练习对检验我们代码的掌握程度非常好,能让我们能更好的掌控自己编写的代码。
对于以后的大作业我会先将类的规划准备好在进行编写,不再和现在无头苍蝇一样想到哪写到哪了。


第一次货物大作业代码

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

interface FeeCalculation
{
    double calculateFee(double weight);
    double calculateFeeRate(double weight);
}

class DefaultFeeCalculation implements FeeCalculation
{
    @Override
    public double calculateFee(double weight)
    {
        if (weight < 20)
        {
            return weight * 35;
        } else if (weight < 50)
        {
            return weight * 30;
        } else if (weight < 100)
        {
            return weight * 25;
        } else
        {
            return weight * 15;
        }
    }

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

interface GoodInput
{
    Good inputGood(Scanner input);
}

class DefaultGoodInput implements GoodInput
{
    @Override
    public Good inputGood(Scanner input)
    {
        Good good = new Good();
        good.setID(input.nextInt());
        input.nextLine();
        good.setName(input.nextLine());
        good.setWidth(input.nextDouble());
        good.setLength(input.nextDouble());
        good.setHeight(input.nextDouble());
        good.setWeight(input.nextDouble());
        return good;
    }
}

class People
{
    String phoneNumber;
    String name;
    String address;

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

    // Getters and Setters
    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }
}

class Time
{
    String time;
    int year;
    int month;
    int day;

    public Time(int year, int month, int day)
    {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public Time(){}

    // Getters and Setters
    public int getYear() {
        return year;
    }

    public int getMonth() {
        return month;
    }

    public int getDay() {
        return day;
    }

    public String getTime()
    {
        return time;
    }

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

class Flight extends Time
{
    String id;
    String origin;
    String destination;
    int maxCapacity;

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

    // Getters and Setters
    public String getId()
    {
        return id;
    }

    public int getMaxCapacity()
    {
        return maxCapacity;
    }
}

class Indent extends Time
{
    String id;

    public Indent(String id, String time)
    {
        this.time = time;
        this.id = id;
    }
    // Getters and Setters
    public String getId()
    {
        return id;
    }
}

class Client extends People
{
    int id;

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

class Deliever extends People
{
    public Deliever(String phoneNumber, String name, String address)
    {
        super(phoneNumber, name, address);
    }
}

class Accept extends People
{
    public Accept(String phoneNumber, String name, String address)
    {
        super(phoneNumber, name, address);
    }
}

class Good
{
    int id;
    double weight;
    String name;
    double length;
    double width;
    double height;
    double calculateWeight;

    public Good()
    {
    }

    public void setID(int id)
    {
        this.id = id;
    }

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

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

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

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

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

    public void setCalculateWeight(double calculateWeight)
    {
        this.calculateWeight = calculateWeight;
    }

    public String getName()
    {
        return name;
    }

    public double getCalculateWeight()
    {
        return calculateWeight;
    }

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

    public double getWeight()
    {
        return weight;
    }

    public double getHeight()
    {
        return height;
    }

    public double getLength()
    {
        return length;
    }
}

class Operation
{
    ArrayList<Good> goods = new ArrayList<>();
    FeeCalculation feeCalculation = new DefaultFeeCalculation();

    public void addGood(Good good)
    {
        goods.add(good);
    }

    public void calculateWeights()
    {
        for (Good good : goods)
        {
            double volumnWeight = good.getVolumnWeight();
            good.setCalculateWeight(volumnWeight > good.getWeight() ? volumnWeight : good.getWeight());
        }
    }

    public double calculateTotalWeight() {
        double totalWeight = 0;
        for (Good good : goods) {
            totalWeight += good.getCalculateWeight();
        }
        return totalWeight;
    }

    public double calculateTotalMoney() {
        double totalMoney = 0;
        for (Good good : goods) {
            totalMoney += feeCalculation.calculateFee(good.getCalculateWeight());
        }
        return totalMoney;
    }
}

public class Main
{
    private static final DecimalFormat df = new DecimalFormat("#0.0");
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        // 输入客户信息
        int clientId = input.nextInt();
        input.nextLine();
        String clientName = input.nextLine();
        String clientPhone = input.nextLine();
        String clientAddress = input.nextLine();
        Client client = new Client(clientId, clientPhone, clientName, clientAddress);

        // 输入货物信息
        int goodsCount = input.nextInt();
        Operation operation = new Operation();
        DefaultGoodInput goodInput = new DefaultGoodInput();
        for (int i = 0; i < goodsCount; i++)
        {
            Good good = goodInput.inputGood(input);
            operation.addGood(good);
        }

        // 输入航班信息
        String flightId = input.next();
        input.nextLine();
        String flightOrigin = input.nextLine();
        String flightDestination = input.nextLine();
        String flightDateStr = input.nextLine();
        int flightMaxCapacity = input.nextInt();
        Flight flight = new Flight(flightId, flightOrigin, flightDestination,flightDateStr,flightMaxCapacity);

        // 输入订单信息
        String indentId = input.next();
        input.nextLine();
        String indentDateStr = input.nextLine();
        Indent indent = new Indent(indentId,indentDateStr );

        // 输入发件人信息
        String delieverAddress = input.nextLine();
        String delieverName = input.nextLine();
        String delieverPhone = input.nextLine();
        Deliever deliever = new Deliever(delieverPhone, delieverName, delieverAddress);

        // 输入收件人信息
        String acceptAddress = input.nextLine();
        String acceptName = input.nextLine();
        String acceptPhone = input.nextLine();
        Accept accept = new Accept(acceptPhone, acceptName, acceptAddress);

        // 计算计费重量和总费用
        operation.calculateWeights();
        double totalWeight = operation.calculateTotalWeight();
        double totalMoney = operation.calculateTotalMoney();

        // 检查航班载重量
        if (totalWeight > flight.getMaxCapacity())
        {
            System.out.println("The flight with flight number:"+flight.getId()+" has exceeded its load capacity and cannot carry the order.");
            System.exit(0);
        }

        // 输出订单信息
        System.out.println("客户:" + client.getName() + "(" + client.getPhoneNumber() + ")订单信息如下:");
        System.out.println("-----------------------------------------");
        System.out.println("航班号:" + flight.getId());
        System.out.println("订单号:" + indent.getId());
        System.out.println("订单日期:" + indent.getTime());
        System.out.println("发件人姓名:" + deliever.getName());
        System.out.println("发件人电话:" + deliever.getPhoneNumber());
        System.out.println("发件人地址:" + deliever.getAddress());
        System.out.println("收件人姓名:" + accept.getName());
        System.out.println("收件人电话:" + accept.getPhoneNumber());
        System.out.println("收件人地址:" + accept.getAddress());
        System.out.println("订单总重量(kg):" + df.format(totalWeight));
        System.out.println("微信支付金额:" + df.format(totalMoney));
        System.out.printf("\n");
        System.out.println("货物明细如下:");
        System.out.println("-----------------------------------------");
        System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
        for (int i = 0; i < operation.goods.size(); i++)
        {
            Good good = operation.goods.get(i);
            double feeRate = operation.feeCalculation.calculateFeeRate(good.getCalculateWeight());
            double freight = operation.feeCalculation.calculateFee(good.getCalculateWeight());
            System.out.printf("%d\t%s\t%s\t%s\t%s\n", (i + 1), good.getName(), df.format(good.getCalculateWeight()), df.format(feeRate), df.format(freight));        }
    }
}

第二次货物大作业代码

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

// 普通货物类
class NormalGood extends Good {}
// 危险货物类
class DangerousGood extends Good {}
// 加急货物类
class ExpediteGood extends Good {}

// 运费计算接口
interface FeeCalculation {
    double calculateFee(double weight, String type);
    double calculateFeeRate(double weight, String type);
}

// 运费计算实现类
class AFeeCalculation implements FeeCalculation {
    @Override
    public double calculateFee(double weight, String type) {
        return calculateFeeRate(weight, type) * weight;
    }

    @Override
    public double calculateFeeRate(double weight, String type) {
        if ("Normal".equals(type)) {
            if (weight < 20) {
                return 35;
            } else if (weight < 50) {
                return 30;
            } else if (weight < 100) {
                return 25;
            } else {
                return 15;
            }
        } else if ("Dangerous".equals(type)) {
            if (weight < 20) {
                return 80;
            } else if (weight < 50) {
                return 50;
            } else if (weight < 100) {
                return 30;
            } else {
                return 20;
            }
        } else {
            if (weight < 20) {
                return 60;
            } else if (weight < 50) {
                return 50;
            } else if (weight < 100) {
                return 40;
            } else {
                return 30;
            }
        }
    }
}

// 货物输入接口
interface GoodInput {
    Good inputGood(Scanner input);
}

// 默认货物输入实现类
class DefaultGoodInput implements GoodInput {
    @Override
    public Good inputGood(Scanner input) {
        Good good = new Good();
        good.setID(input.nextInt());
        input.nextLine();
        good.setName(input.nextLine());
        good.setWidth(input.nextDouble());
        good.setLength(input.nextDouble());
        good.setHeight(input.nextDouble());
        good.setWeight(input.nextDouble());
        return good;
    }
}

// 人员类
class People {
    String type;
    String phoneNumber;
    String name;
    String address;
    double sale;

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

    // 获取折扣
    public double getSale() {
        if ("Individual".equals(type)) {
            return 0.9;
        } else {
            return 0.8;
        }
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }
}

// 时间类
class Time {
    String time;
    int year;
    int month;
    int day;

    public Time(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public Time() {}

    // 获取年份
    public int getYear() {
        return year;
    }

    // 获取月份
    public int getMonth() {
        return month;
    }

    // 获取日期
    public int getDay() {
        return day;
    }

    // 获取时间字符串
    public String getTime() {
        return time;
    }

    // 设置时间字符串
    public void setTime(String time) {
        this.time = time;
    }
}

// 航班类
class Flight extends Time {
    String id;
    String origin;
    String destination;
    int maxCapacity;

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

    // 获取航班号
    public String getId() {
        return id;
    }

    // 获取最大载重量
    public int getMaxCapacity() {
        return maxCapacity;
    }
}

// 订单类
class Indent extends Time {
    String id;

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

    // 获取订单号
    public String getId() {
        return id;
    }
}

// 客户类
class Client extends People {
    int id;

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

// 发件人类
class Deliever extends People {
    public Deliever(String phoneNumber, String name, String address) {
        super(phoneNumber, name, address);
    }
}

// 收件人类
class Accept extends People {
    public Accept(String phoneNumber, String name, String address) {
        super(phoneNumber, name, address);
    }
}

// 货物类
class Good {
    int id;
    double weight;
    String name;
    double length;
    double width;
    double height;
    double calculateWeight;

    public Good() {}

    public void setID(int id) {
        this.id = id;
    }

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

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

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

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

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

    public void setCalculateWeight(double calculateWeight) {
        this.calculateWeight = calculateWeight;
    }

    public String getName() {
        return name;
    }

    public double getCalculateWeight() {
        return calculateWeight;
    }

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

    public double getWeight() {
        return weight;
    }

    public double getHeight() {
        return height;
    }

    public double getLength() {
        return length;
    }

    public double getWidth()
    {
        return width;
    }
}

// 货物操作类,负责与货物相关的操作
class GoodsOperation {
    ArrayList<Good> goods = new ArrayList<>();

    public void addGood(Good good) {
        goods.add(good);
    }

    public void calculateWeights() {
        for (Good good : goods) {
            double volumnWeight = good.getVolumnWeight();
            good.setCalculateWeight(volumnWeight > good.getWeight()? volumnWeight : good.getWeight());
        }
    }

    public double calculateTotalWeight() {
        double totalWeight = 0;
        for (Good good : goods) {
            totalWeight += good.getCalculateWeight();
        }
        return totalWeight;
    }
}

// 运费计算操作类,负责运费计算相关操作
class FreightCalculationOperation {
    FeeCalculation feeCalculation;

    public FreightCalculationOperation(FeeCalculation feeCalculation) {
        this.feeCalculation = feeCalculation;
    }

    public double calculateTotalMoney(ArrayList<Good> goods, String type, Client client) {
        double totalMoney = 0;
        for (Good good : goods) {
            totalMoney += feeCalculation.calculateFee(good.getCalculateWeight(), type) * client.getSale();
        }
        return totalMoney;
    }
}

public class Main {
    private static final DecimalFormat df = new DecimalFormat("#0.0");

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

        // 输入客户信息并验证客户类型
        String type = input.nextLine();
        if (!"Individual".equals(type) && !"Corporate".equals(type)) {
            System.out.println("无效的客户类型,请重新输入。");
            return;
        }
        int clientId = input.nextInt();
        input.nextLine();
        String clientName = input.nextLine();
        String clientPhone = input.nextLine();
        String clientAddress = input.nextLine();
        Client client = new Client(clientId, clientPhone, clientName, clientAddress);
        client.setType(type);

        // 输入货物信息并验证货物类型
        String Goodtype = input.nextLine();
        if (!"Normal".equals(Goodtype) &&!"Expedite".equals(Goodtype) &&!"Dangerous".equals(Goodtype)) {
            System.out.println("无效的货物类型,请重新输入。");
            return;
        }
        int goodsCount = input.nextInt();
        GoodsOperation goodsOperation = new GoodsOperation();
        DefaultGoodInput goodInput = new DefaultGoodInput();
        for (int i = 0; i < goodsCount; i++) {
            Good good = goodInput.inputGood(input);
            if (good.getWidth() <= 0 || good.getLength() <= 0 || good.getHeight() <= 0 || good.getWeight() <= 0) {
                System.out.println("货物尺寸或重量不能为负数,请重新输入。");
                return;
            }
            goodsOperation.addGood(good);
        }

        // 输入航班信息
        input.nextLine();
        String flightId = input.nextLine();
        String flightOrigin = input.nextLine();
        String flightDestination = input.nextLine();
        String flightDateStr = input.nextLine();
        int flightMaxCapacity = input.nextInt();
        if (flightMaxCapacity <= 0) {
            System.out.println("航班最大载重量不能为负数,请重新输入。");
            return;
        }
        Flight flight = new Flight(flightId, flightOrigin, flightDestination, flightDateStr, flightMaxCapacity);

        // 输入订单信息
        input.nextLine();
        String indentId = input.nextLine();
        String indentDateStr = input.nextLine();
        Indent indent = new Indent(indentId, indentDateStr);

        // 输入发件人信息
        String delieverAddress = input.nextLine();
        String delieverName = input.nextLine();
        String delieverPhone = input.nextLine();
        Deliever deliever = new Deliever(delieverPhone, delieverName, delieverAddress);

        // 输入收件人信息
        String acceptAddress = input.nextLine();
        String acceptName = input.nextLine();
        String acceptPhone = input.nextLine();
        String paymentMethod = input.nextLine();
        if (!"Wechat".equals(paymentMethod) &&!"ALiPay".equals(paymentMethod) &&!"Cash".equals(paymentMethod)) {
            System.out.println("无效的支付方式,请重新输入。");
            return;
        }
        Accept accept = new Accept(acceptPhone, acceptName, acceptAddress);

        // 计算计费重量和总费用
        goodsOperation.calculateWeights();
        double totalWeight = goodsOperation.calculateTotalWeight();
        FreightCalculationOperation freightCalculationOperation = new FreightCalculationOperation(new AFeeCalculation());
        double totalMoney = freightCalculationOperation.calculateTotalMoney(goodsOperation.goods, Goodtype, client);

        // 检查航班载重量
        if (totalWeight > flight.getMaxCapacity()) {
            System.out.println("The flight with flight number:" + flight.getId() + " has exceeded its load capacity and cannot carry the order.");
            System.exit(0);
        }

        // 输出订单信息
        System.out.println("客户:" + client.getName() + "(" + client.getPhoneNumber() + ")订单信息如下:");
        System.out.println("-----------------------------------------");
        System.out.println("航班号:" + flight.getId());
        System.out.println("订单号:" + indent.getId());
        System.out.println("订单日期:" + indent.getTime());
        System.out.println("发件人姓名:" + deliever.getName());
        System.out.println("发件人电话:" + deliever.getPhoneNumber());
        System.out.println("发件人地址:" + deliever.getAddress());
        System.out.println("收件人姓名:" + accept.getName());
        System.out.println("收件人电话:" + accept.getPhoneNumber());
        System.out.println("收件人地址:" + accept.getAddress());
        System.out.println("订单总重量(kg):" + df.format(totalWeight));
        System.out.print(paymentMethod.equals("Wechat")? "微信" : paymentMethod.equals("ALiPay")? "支付宝" : "现金");
        System.out.println("支付金额:" + df.format(totalMoney));
        System.out.println();
        System.out.println("货物明细如下:");
        System.out.println("-----------------------------------------");
        System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
        for (int i = 0; i < goodsOperation.goods.size(); i++) {
            Good good = goodsOperation.goods.get(i);
            double feeRate = freightCalculationOperation.feeCalculation.calculateFeeRate(good.getCalculateWeight(), Goodtype);
            double freight = freightCalculationOperation.feeCalculation.calculateFee(good.getCalculateWeight(), Goodtype);
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", (i + 1), good.getName(), good.getCalculateWeight(), feeRate, freight);
        }
    }
}

posted on 2025-05-22 00:26  微光红茶  阅读(25)  评论(0)    收藏  举报

导航