第二次Blog作业-航空货运管理系统

一、前言

题目集知识点与题量
题目集 8:以 “航空货运管理系统” 为核心,涉及类的封装、继承与多态的基础应用。重点考查基础类设计(如Product、Customer)、集合类(LinkedList)的使用,以及简单的业务逻辑实现(如计费重量计算、订单信息输出)。
题目集 9:在题目集 8 基础上深化,引入抽象类与接口的应用、多态的复杂场景(不同货物类型对应不同费率)、枚举类型(支付方式)以及更细粒度的业务逻辑(客户折扣、多支付方式)。
本次题目集难度并不大(搞的真的没什么可写的)主要是为了锻炼我们对类的封装、继承与多态的基础应用和抽象类与接口的应用,并要求按照六大原则编写(不写我扣你分啊

二、设计与分析:航空货运管理系统

(一)题目集 8 设计分析
类图分析:

代码如下:

点击查看代码
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Arrays;
class Product{
    private String id;
    private String name;
    private double width;
    private double longth;
    private double height;
    private double weight;
    public Product(){}
    public Product(String id, String name, double width, double longth, double height, double weight){
        this.id = id;
        this.name = name;
        this.width = width;
        this.longth = longth;
        this.height = height;
        this.weight = weight;
    }
    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 double getWidth(){
        return width;
    }
    public void setWidth(double width){
        this.width = width;
    }
    public double getLongth(){
        return longth;
    }
    public void setLongth(double longth){
        this.longth = longth;
    }
    public double getHeight(){
        return height;
    }
    public void setHeight(double height){
        this.height = height;
    }
    public double getWeight(){
        return weight;
    }
    public void setWeight(double weight){
        this.weight = weight;
    }
    public double chargeableWeight(){
        return Math.max(this.weight, (this.width * this.longth * this.height) / 6000);
    }
}
class ProductSum{
    private LinkedList<Product> products;
    private LinkedList<Double> rate = new LinkedList<>(Arrays.asList(35.0, 30.0, 25.0, 15.0));
    private LinkedList<Double> weight = new LinkedList<>(Arrays.asList(20.0, 50.0, 100.0));
    public ProductSum(){}
    public ProductSum(LinkedList<Product> products){
        this.products = products;
    }
    public LinkedList<Product> getProducts(){
        return this.products;
    }
    public void setProducts(LinkedList<Product> products){
        this.products = products;
    }
    public double getSumWeight(){
        double sum = 0;
        if (this.products == null) {
            return sum;
        }
        for (Product p : this.products){
            double productWeight = p.chargeableWeight();
            productWeight = Math.round(productWeight*10)/10.0;
            sum += productWeight;
        }
        return sum;
    }
    public LinkedList<Double> getAmount() {
        LinkedList<Double> amounts = new LinkedList<>();
        for (Product p : this.products) {
            double chargeableWeight = p.chargeableWeight();
            chargeableWeight = Math.round(chargeableWeight * 10) / 10.0;
            int rateIndex = 0;
            for (int i = 0; i < weight.size(); i++) {
                if (chargeableWeight < weight.get(i)) {
                    rateIndex = i;
                    break;
                }
                if (i == weight.size() - 1) {
                    rateIndex = i + 1;
                }
            }
            amounts.add(chargeableWeight * rate.get(rateIndex));
        }
        return amounts;
    }
    public double getSumAmount(){
        double sum = 0;
        LinkedList<Double> amounts = getAmount();
        for (double amount : amounts) {
            sum += amount;
        }
        return sum;
    }
}
class SetPeople{
    private String setPeopleName;
    private String setPeopleAddress;
    private String setPeoplePhone;
    public SetPeople(){}
    public SetPeople(String setPeopleAddress, String setPeopleName, String setPeoplePhone){
        this.setPeopleName = setPeopleName;
        this.setPeopleAddress = setPeopleAddress;
        this.setPeoplePhone = setPeoplePhone;
    }
    public String getSetPeopleName(){
        return this.setPeopleName;
    }
    public void setSetPeopleName(String setPeopleName){
        this.setPeopleName = setPeopleName;
    }
    public String getSetPeopleAddress(){
        return this.setPeopleAddress;
    }
    public void setSetPeopleAddress(String setPeopleAddress){
        this.setPeopleAddress = setPeopleAddress;
    }
    public String getSetPeoplePhone(){
        return this.setPeoplePhone;
    }
    public void setSetPeoplePhone(String setPeoplePhone){
        this.setPeoplePhone = setPeoplePhone;
    }
}
class GetPeople{
    private String getPeopleName;
    private String getPeopleAddress;
    private String getPeoplePhone;
    public GetPeople(){}
    public GetPeople(String getPeopleAddress, String getPeopleName, String getPeoplePhone){
        this.getPeopleName = getPeopleName;
        this.getPeopleAddress = getPeopleAddress;
        this.getPeoplePhone = getPeoplePhone;
    }
    public String getGetPeopleName(){
        return this.getPeopleName;
    }
    public String getGetPeopleAddress(){
        return this.getPeopleAddress;
    }
    public String getGetPeoplePhone(){
        return this.getPeoplePhone;
    }
    public void setGetPeopleName(String getPeopleName){
        this.getPeopleName = this.getPeopleName;
    }
    public void setGetPeopleAddress(String getPeopleAddress){
        this.getPeopleAddress = this.getPeopleAddress;
    }
    public void setGetPeoplePhone(String getPeoplePhone){
        this.getPeoplePhone = this.getPeoplePhone;
    }
}
class Customer{
    private String customerId;
    private String name;
    private String phone;
    private String address;
    private int productNum;
    public Customer(){}
    public Customer(String customerId, String name, String phone,String address){
        this.name = name;
        this.phone = phone;
        this.customerId = customerId;
        this.address = address;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getPhone(){
        return this.phone;
    }
    public void setPhone(String phone){
        this.phone = phone;
    }
    public String getCustomerId(){
        return this.customerId;
    }
    public void setCustomerId(String customerId){
        this.customerId = customerId;
    }
    public String getAddress(){
        return this.address;
    }
    public void setAddress(String address){
        this.address = address;
    }
}
class FlightOrder{
    private String flightNum;
    private String originAirport;
    private String arrivalAirport;
    private String flightDay;
    private double maximum;
    private String orderId;
    private String orderDay;
    public FlightOrder(){}
    public FlightOrder(String flightNum, String originAirport, String arrivalAirport, String flightDay, double maximum, String orderId, String orderDay){
        this.flightNum = flightNum;
        this.originAirport = originAirport;
        this.arrivalAirport = arrivalAirport;
        this.flightDay = flightDay;
        this.maximum = maximum;
        this.orderId = orderId;
        this.orderDay = orderDay;
    }
    public String getFlightNum(){
        return this.flightNum;
    }
    public void setFlightNum(String flightNum){
        this.flightNum = flightNum;
    }
    public String getOriginAirport(){
        return this.originAirport;
    }
    public void setOriginAirport(String originAirport){
        this.originAirport = originAirport;
    }
    public String getArrivalAirport(){
        return this.arrivalAirport;
    }
    public void setArrivalAirport(String arrivalAirport){
        this.arrivalAirport = arrivalAirport;
    }
    public String getFlightDay(){
        return this.flightDay;
    }
    public void setFlightDay(String flightDay){
        this.flightDay = flightDay;
    }
    public double getMaximum(){
        return this.maximum;
    }
    public void setMaximum(double maximum){
        this.maximum = maximum;
    }
    public String getOrderId(){
        return this.orderId;
    }
    public void setOrderId(String orderId){
        this.orderId = orderId;
    }
    public String getOrderDay(){
        return this.orderDay;
    }
    public void setOrderDay(String orderDay){
        this.orderDay = orderDay;
    }
}
abstract class Payment{
    private String payment;
    private double amount;
    public Payment(){
    }
    public Payment(double amount){
        this.amount = amount;
    }
    public void setAmount(double amount){
        this.amount = amount;
    }
    public void outPay(){
    }
}
class WechatPayment extends Payment{
    private String payment = "Wechat";
    private double amount;
    public WechatPayment(){
    }
    public WechatPayment(double amount){
        super(amount);
    }
    public void setAmount(double amount){
        this.amount = amount;
    }
    @Override
    public void outPay(){
        System.out.printf("微信支付金额:%.1f\n",amount);
    }
}
class AlipayPayment extends Payment{
    private String payment = "Alipay";
    private double amount;
    public AlipayPayment(){}
    public AlipayPayment(double amount){
        super(amount);
    }
    public void setAmount(double amount){
        this.amount = amount;
    }
    public void outPay(){
        System.out.printf("支付宝支付金额:%.1f\n",amount);
    }
}
class Control {
    private FlightOrder flightOrder;
    private Customer customer;
    private SetPeople setPeople;
    private GetPeople getPeople;
    private Payment payment;
    private ProductSum productSum;
    public Control(){}
    public Control(FlightOrder flightOrder,SetPeople setPeople,GetPeople getPeople,ProductSum productSum,Customer customer){
        this.flightOrder = flightOrder;
        this.setPeople = setPeople;
        this.getPeople = getPeople;
        this.productSum = productSum;
        this.customer = customer;
        this.payment = new WechatPayment();
    }
    public FlightOrder getFlightOrder(){
        return this.flightOrder;
    }
    public void setFlightOrder(FlightOrder flightOrder){
        this.flightOrder = flightOrder;
    }
    public SetPeople getSetPeople(){
        return this.setPeople;
    }
    public void setSetPeople(SetPeople setPeople){
        this.setPeople = setPeople;
    }
    public GetPeople getGetPeople(){
        return this.getPeople;
    }
    public void setGetPeople(GetPeople getPeople){
        this.getPeople = getPeople;
    }
    public Payment getPayment(){
        return this.payment;
    }
    public void setPayment(Payment payment){
        this.payment = payment;
    }
    public ProductSum getProductSum(){
        return this.productSum;
    }
    public void setProductSum(ProductSum productSum){
        this.productSum = productSum;
    }
    public Customer getCustomer(){
        return this.customer;
    }
    public void setCustomer(Customer customer){
        this.customer = customer;
    }
    public void printOut(){
        LinkedList<Product> products = getProductSum().getProducts();
        getPayment().setAmount(getProductSum().getSumAmount());
        System.out.printf("客户:%s(%s)订单信息如下:\n",getCustomer().getName(),getCustomer().getPhone());
        System.out.printf("-----------------------------------------\n");
        System.out.printf("航班号:%s\n",getFlightOrder().getFlightNum());
        System.out.printf("订单号:%s\n",getFlightOrder().getOrderId());
        System.out.printf("订单日期:%s\n",getFlightOrder().getOrderDay());
        System.out.printf("发件人姓名:%s\n",getSetPeople().getSetPeopleName());
        System.out.printf("发件人电话:%s\n",getSetPeople().getSetPeoplePhone());
        System.out.printf("发件人地址:%s\n",getSetPeople().getSetPeopleAddress());
        System.out.printf("收件人姓名:%s\n",getGetPeople().getGetPeopleName());
        System.out.printf("收件人电话:%s\n",getGetPeople().getGetPeoplePhone());
        System.out.printf("收件人地址:%s\n",getGetPeople().getGetPeopleAddress());
        System.out.printf("订单总重量(kg):%.1f\n",getProductSum().getSumWeight());
        getPayment().outPay();
        System.out.printf("\n货物明细如下:\n-----------------------------------------\n明细编号\t货物名称\t计费重量\t计费费率\t应交运费\n");
        for (int i = 0; i < products.size(); i++) {
            Product product = products.get(i);
            double amount = getProductSum().getAmount().get(i);
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f",i+1 ,product.getName() ,product.chargeableWeight(),amount/product.chargeableWeight(),amount);
            if(i!=products.size()-1){
                System.out.printf("\n");
            }
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Customer customer = new Customer(scan.nextLine(),scan.nextLine(),scan.nextLine(),scan.nextLine());
        int productNum = scan.nextInt();
        scan.nextLine();
        LinkedList<Product> products = new LinkedList<>();
        for (int i = 0; i < productNum; i++) {
            String productid = scan.nextLine();
            String productName = scan.nextLine();
            double productWidth = scan.nextDouble();
            scan.nextLine();
            double productLongth = scan.nextDouble();
            scan.nextLine();
            double productHigh = scan.nextDouble();
            scan.nextLine();
            double productWeight = scan.nextDouble();
            scan.nextLine();
            Product product = new Product(productid, productName, productWidth, productLongth, productHigh, productWeight);
            products.add(product);
        }
        ProductSum productSum = new ProductSum(products);
        String flightNum = scan.nextLine();
        String flightSet = scan.nextLine();
        String flightGet = scan.nextLine();
        String flightDay = scan.nextLine();
        double flightWeight = scan.nextDouble();
        scan.nextLine();
        String orderId = scan.nextLine();
        String orderDay = scan.nextLine();
        FlightOrder flightOrder = new FlightOrder(flightNum,flightSet,flightGet,flightDay,flightWeight,orderId,orderDay);
        if(flightWeight<productSum.getSumWeight()){
            System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.",flightNum);
            System.exit(0);
        }
        SetPeople setPeople = new SetPeople(scan.nextLine(),scan.nextLine(),scan.nextLine());
        GetPeople getPeople = new GetPeople(scan.nextLine(),scan.nextLine(),scan.nextLine());
        Control control = new Control(flightOrder,setPeople,getPeople,productSum,customer);
        control.printOut();
    }
}
代码分析图:



主要按照作业说明的要求来的但对六大基础原则还是不够熟悉所以出现了以下问题
主要问题:
设计缺陷
硬编码问题:费率和重量分段直接写在ProductSum类中,未通过配置或工厂模式管理,难以动态调整。
职责不清:Control类承担了输入处理、业务计算和输出展示的多重职责,违反单一职责原则。
继承滥用:SetPeople和GetPeople类完全相同,应合并或抽象出公共父类。
代码质量
命名不规范:longth拼写错误,应改为length。
冗余代码:Payment子类中重复定义amount字段和getAmount()方法,应移至父类。
缺少注释:代码中几乎无注释,尤其是核心业务逻辑(如计费规则),降低可维护性。
同时还被文档里没说明的东西给坑了设计缺陷
硬编码问题:费率和重量分段直接写在ProductSum类中,未通过配置或工厂模式管理,难以动态调整。
职责不清:Control类承担了输入处理、业务计算和输出展示的多重职责,违反单一职责原则。
继承滥用:SetPeople和GetPeople类完全相同,应合并或抽象出公共父类。
代码质量
命名不规范:longth拼写错误,应改为length。
冗余代码:Payment子类中重复定义amount字段和getAmount()方法,应移至父类。
缺少注释:代码中几乎无注释,尤其是核心业务逻辑(如计费规则),降低可维护性。
(二)题目集 9 改进设计
类图分析:

代码如下:

点击查看代码
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Arrays;
abstract class Product{
    private String id;
    private String name;
    private double width;
    private double longth;
    private double height;
    private double weight;
    public Product(){}
    public Product(String id, String name, double width, double longth, double height, double weight){
        this.id = id;
        this.name = name;
        this.width = width;
        this.longth = longth;
        this.height = height;
        this.weight = weight;
    }
    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 double getWidth(){
        return width;
    }
    public void setWidth(double width){
        this.width = width;
    }
    public double getLongth(){
        return longth;
    }
    public void setLongth(double longth){
        this.longth = longth;
    }
    public double getHeight(){
        return height;
    }
    public void setHeight(double height){
        this.height = height;
    }
    public double getWeight(){
        return weight;
    }
    public void setWeight(double weight){
        this.weight = weight;
    }
    public abstract LinkedList<Double> getRate();
    public double chargeableWeight(){
        return Math.max(this.weight, (this.width * this.longth * this.height) / 6000);
    }
}
class Normal extends Product{
    private LinkedList<Double> rate = new LinkedList<>(Arrays.asList(35.0, 30.0, 25.0, 15.0));
    public Normal(){}
    public Normal(String id, String name, double width, double longth, double height, double weight){
        super(id, name, width, longth, height, weight);
    }
    public LinkedList<Double> getRate(){
        return this.rate;
    }
}
class Dangerous extends Product{
    private LinkedList<Double> rate = new LinkedList<>(Arrays.asList(80.0, 50.0, 30.0, 20.0));
    public Dangerous(){}
    public Dangerous(String id, String name, double width, double longth, double height,double weight){
        super(id, name, width, longth, height, weight);
    }
    public LinkedList<Double> getRate(){
        return this.rate;
    }
}
class Expedited extends Product{
    private LinkedList<Double> rate = new LinkedList<>(Arrays.asList(60.0, 50.0, 40.0, 30.0));
    public Expedited(){}
    public Expedited(String id, String name, double width, double longth, double height, double weight){
        super(id, name, width, longth, height, weight);
    }
    public LinkedList<Double> getRate(){
        return this.rate;
    }
}
class ProductSum{
    private LinkedList<Product> products;
    private LinkedList<Double> weight = new LinkedList<>(Arrays.asList(20.0, 50.0, 100.0));
    public ProductSum(){}
    public ProductSum(LinkedList<Product> products){
        this.products = products;
    }
    public LinkedList<Product> getProducts(){
        return this.products;
    }
    public void setProducts(LinkedList<Product> products){
        this.products = products;
    }
    public double getSumWeight(){
        double sum = 0;
        if (this.products == null) {
            return sum;
        }
        for (Product p : this.products){
            double productWeight = p.chargeableWeight();
            productWeight = Math.round(productWeight*10)/10.0;
            sum += productWeight;
        }
        return sum;
    }
    public LinkedList<Double> getAmount() {
        LinkedList<Double> amounts = new LinkedList<>();
        for (Product p : this.products) {
            double chargeableWeight = p.chargeableWeight();
            chargeableWeight = Math.round(chargeableWeight * 10) / 10.0;
            int rateIndex = 0;
            for (int i = 0; i < weight.size(); i++) {
                if (chargeableWeight < weight.get(i)) {
                    rateIndex = i;
                    break;
                }
                if (i == weight.size() - 1) {
                    rateIndex = i + 1;
                }
            }
            amounts.add(chargeableWeight * p.getRate().get(rateIndex));
        }
        return amounts;
    }
    public double getSumAmount(){
        double sum = 0;
        LinkedList<Double> amounts = getAmount();
        for (double amount : amounts) {
            sum += amount;
        }
        return sum;
    }
}
class SetPeople{
    private String setPeopleName;
    private String setPeopleAddress;
    private String setPeoplePhone;
    public SetPeople(){}
    public SetPeople(String setPeopleAddress, String setPeopleName, String setPeoplePhone){
        this.setPeopleName = setPeopleName;
        this.setPeopleAddress = setPeopleAddress;
        this.setPeoplePhone = setPeoplePhone;
    }
    public String getSetPeopleName(){
        return this.setPeopleName;
    }
    public void setSetPeopleName(String setPeopleName){
        this.setPeopleName = setPeopleName;
    }
    public String getSetPeopleAddress(){
        return this.setPeopleAddress;
    }
    public void setSetPeopleAddress(String setPeopleAddress){
        this.setPeopleAddress = setPeopleAddress;
    }
    public String getSetPeoplePhone(){
        return this.setPeoplePhone;
    }
    public void setSetPeoplePhone(String setPeoplePhone){
        this.setPeoplePhone = setPeoplePhone;
    }
}
class GetPeople{
    private String getPeopleName;
    private String getPeopleAddress;
    private String getPeoplePhone;
    public GetPeople(){}
    public GetPeople(String getPeopleAddress, String getPeopleName, String getPeoplePhone){
        this.getPeopleName = getPeopleName;
        this.getPeopleAddress = getPeopleAddress;
        this.getPeoplePhone = getPeoplePhone;
    }
    public String getGetPeopleName(){
        return this.getPeopleName;
    }
    public String getGetPeopleAddress(){
        return this.getPeopleAddress;
    }
    public String getGetPeoplePhone(){
        return this.getPeoplePhone;
    }
    public void setGetPeopleName(String getPeopleName){
        this.getPeopleName = this.getPeopleName;
    }
    public void setGetPeopleAddress(String getPeopleAddress){
        this.getPeopleAddress = this.getPeopleAddress;
    }
    public void setGetPeoplePhone(String getPeoplePhone){
        this.getPeoplePhone = this.getPeoplePhone;
    }
}
abstract class Customer{
    private String customerId;
    private String name;
    private String phone;
    private String address;
    private int productNum;
    public Customer(){}
    public Customer(String customerId, String name, String phone,String address){
        this.name = name;
        this.phone = phone;
        this.customerId = customerId;
        this.address = address;
    }
    public String getName(){
        return this.name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getPhone(){
        return this.phone;
    }
    public void setPhone(String phone){
        this.phone = phone;
    }
    public String getCustomerId(){
        return this.customerId;
    }
    public void setCustomerId(String customerId){
        this.customerId = customerId;
    }
    public String getAddress(){
        return this.address;
    }
    public void setAddress(String address){
        this.address = address;
    }
    public abstract double discount();
}
class Corporate extends Customer{
    public Corporate(){}
    public Corporate(String customerId, String name, String phone, String address){
        super(customerId, name, phone, address);
    }
    public double discount(){
        return 0.8;
    }
}
class Individual extends Customer{
    public Individual(){}
    public Individual(String customerId, String name, String phone, String address){
        super(customerId, name, phone, address);
    }
    public double discount(){
        return 0.9;
    }
}
class FlightOrder{
    private String flightNum;
    private String originAirport;
    private String arrivalAirport;
    private String flightDay;
    private double maximum;
    private String orderId;
    private String orderDay;
    public FlightOrder(){}
    public FlightOrder(String flightNum, String originAirport, String arrivalAirport, String flightDay, double maximum, String orderId, String orderDay){
        this.flightNum = flightNum;
        this.originAirport = originAirport;
        this.arrivalAirport = arrivalAirport;
        this.flightDay = flightDay;
        this.maximum = maximum;
        this.orderId = orderId;
        this.orderDay = orderDay;
    }
    public String getFlightNum(){
        return this.flightNum;
    }
    public void setFlightNum(String flightNum){
        this.flightNum = flightNum;
    }
    public String getOriginAirport(){
        return this.originAirport;
    }
    public void setOriginAirport(String originAirport){
        this.originAirport = originAirport;
    }
    public String getArrivalAirport(){
        return this.arrivalAirport;
    }
    public void setArrivalAirport(String arrivalAirport){
        this.arrivalAirport = arrivalAirport;
    }
    public String getFlightDay(){
        return this.flightDay;
    }
    public void setFlightDay(String flightDay){
        this.flightDay = flightDay;
    }
    public double getMaximum(){
        return this.maximum;
    }
    public void setMaximum(double maximum){
        this.maximum = maximum;
    }
    public String getOrderId(){
        return this.orderId;
    }
    public void setOrderId(String orderId){
        this.orderId = orderId;
    }
    public String getOrderDay(){
        return this.orderDay;
    }
    public void setOrderDay(String orderDay){
        this.orderDay = orderDay;
    }
}
abstract class Payment{
    private String payment;
    private double amount;
    public Payment(){
    }
    public Payment(double amount){
        this.amount = amount;
    }
    public void setAmount(double amount){
        this.amount = amount;
    }
    public abstract void outPay();
}
class WechatPayment extends Payment{
    private String payment = "Wechat";
    private double amount;
    public WechatPayment(){
    }
    public WechatPayment(double amount){
        super(amount);
    }
    public void setAmount(double amount){
        this.amount = amount;
    }
    public double getAmount(){
        return this.amount;
    }
    @Override
    public void outPay(){
        System.out.printf("微信支付金额:%.1f\n",amount);
    }
}
class AlipayPayment extends Payment{
    private String payment = "Alipay";
    private double amount;
    public AlipayPayment(){}
    public AlipayPayment(double amount){
        super(amount);
    }
    public void setAmount(double amount){
        this.amount = amount;
    }
    public double getAmount(){
        return this.amount;
    }
    public void outPay(){
        System.out.printf("支付宝支付金额:%.1f\n",amount);
    }
}
class CashPayment extends Payment{
    private String payment = "Money";
    private double amount;
    public CashPayment(){}
    public CashPayment(double amount){
        super(amount);
    }
    public void setAmount(double amount){
        this.amount = amount;
    }
    public double getAmount(){
        return this.amount;
    }
    public void outPay(){
        System.out.printf("现金支付金额:%.1f\n",amount);
    }
}
class Control {
    private FlightOrder flightOrder;
    private Customer customer;
    private SetPeople setPeople;
    private GetPeople getPeople;
    private Payment payment;
    private ProductSum productSum;
    public Control(){}
    public Control(FlightOrder flightOrder,SetPeople setPeople,GetPeople getPeople,ProductSum productSum,Customer customer, Payment payment){
        this.flightOrder = flightOrder;
        this.setPeople = setPeople;
        this.getPeople = getPeople;
        this.productSum = productSum;
        this.customer = customer;
        this.payment = payment;
    }
    public FlightOrder getFlightOrder(){
        return this.flightOrder;
    }
    public void setFlightOrder(FlightOrder flightOrder){
        this.flightOrder = flightOrder;
    }
    public SetPeople getSetPeople(){
        return this.setPeople;
    }
    public void setSetPeople(SetPeople setPeople){
        this.setPeople = setPeople;
    }
    public GetPeople getGetPeople(){
        return this.getPeople;
    }
    public void setGetPeople(GetPeople getPeople){
        this.getPeople = getPeople;
    }
    public Payment getPayment(){
        return this.payment;
    }
    public void setPayment(Payment payment){
        this.payment = payment;
    }
    public ProductSum getProductSum(){
        return this.productSum;
    }
    public void setProductSum(ProductSum productSum){
        this.productSum = productSum;
    }
    public Customer getCustomer(){
        return this.customer;
    }
    public void setCustomer(Customer customer){
        this.customer = customer;
    }
    public void printOut(){
        LinkedList<Product> products = getProductSum().getProducts();
        getPayment().setAmount(getProductSum().getSumAmount()*customer.discount());
        System.out.printf("客户:%s(%s)订单信息如下:\n",getCustomer().getName(),getCustomer().getPhone());
        System.out.printf("-----------------------------------------\n");
        System.out.printf("航班号:%s\n",getFlightOrder().getFlightNum());
        System.out.printf("订单号:%s\n",getFlightOrder().getOrderId());
        System.out.printf("订单日期:%s\n",getFlightOrder().getOrderDay());
        System.out.printf("发件人姓名:%s\n",getSetPeople().getSetPeopleName());
        System.out.printf("发件人电话:%s\n",getSetPeople().getSetPeoplePhone());
        System.out.printf("发件人地址:%s\n",getSetPeople().getSetPeopleAddress());
        System.out.printf("收件人姓名:%s\n",getGetPeople().getGetPeopleName());
        System.out.printf("收件人电话:%s\n",getGetPeople().getGetPeoplePhone());
        System.out.printf("收件人地址:%s\n",getGetPeople().getGetPeopleAddress());
        System.out.printf("订单总重量(kg):%.1f\n",getProductSum().getSumWeight());
        getPayment().outPay();
        System.out.printf("\n货物明细如下:\n-----------------------------------------\n明细编号\t货物名称\t计费重量\t计费费率\t应交运费\n");
        for (int i = 0; i < products.size(); i++) {
            Product product = products.get(i);
            double amount = getProductSum().getAmount().get(i);
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f",i+1 ,product.getName() ,product.chargeableWeight(),amount/product.chargeableWeight(),amount);
            if(i!=products.size()-1){
                System.out.printf("\n");
            }
        }
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Customer customer = new Individual();
        String customerType = scan.nextLine();
        if(customerType.equals("Individual")){
            customer = new Individual(scan.nextLine(),scan.nextLine(),scan.nextLine(),scan.nextLine());
        }
        if(customerType.equals("Corporate")){
            customer = new Corporate(scan.nextLine(),scan.nextLine(),scan.nextLine(),scan.nextLine());
        }
        String productType = scan.nextLine();
        int productNum = scan.nextInt();
        scan.nextLine();
        Product product = new Normal();
        LinkedList<Product> products = new LinkedList<>();
        for (int i = 0; i < productNum; i++) {
            String productid = scan.nextLine();
            String productName = scan.nextLine();
            double productWidth = scan.nextDouble();
            scan.nextLine();
            double productLongth = scan.nextDouble();
            scan.nextLine();
            double productHigh = scan.nextDouble();
            scan.nextLine();
            double productWeight = scan.nextDouble();
            scan.nextLine();
            if(productType.equals("Normal")) {
                product = new Normal(productid, productName, productWidth, productLongth, productHigh, productWeight);
            }else if(productType.equals("Dangerous")){
                product = new Dangerous(productid, productName, productWidth, productLongth, productHigh, productWeight);
            }else if(productType.equals("Expedite")){
                product = new Expedited(productid, productName, productWidth, productLongth, productHigh, productWeight);
            }
            products.add(product);
        }
        ProductSum productSum = new ProductSum(products);
        String flightNum = scan.nextLine();
        String flightSet = scan.nextLine();
        String flightGet = scan.nextLine();
        String flightDay = scan.nextLine();
        double flightWeight = scan.nextDouble();
        scan.nextLine();
        String orderId = scan.nextLine();
        String orderDay = scan.nextLine();
        FlightOrder flightOrder = new FlightOrder(flightNum,flightSet,flightGet,flightDay,flightWeight,orderId,orderDay);
        if(flightWeight<productSum.getSumWeight()){
            System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.",flightNum);
            System.exit(0);
        }
        SetPeople setPeople = new SetPeople(scan.nextLine(),scan.nextLine(),scan.nextLine());
        GetPeople getPeople = new GetPeople(scan.nextLine(),scan.nextLine(),scan.nextLine());
        String payment = scan.nextLine();
        Payment payment1 = new AlipayPayment();
        if(payment.equals("ALiPay")){
            payment1 = new AlipayPayment();
        }else if(payment.equals("Wechat")){
            payment1 = new WechatPayment();
        }else if(payment.equals("Cash")){
            payment1 = new CashPayment();
        }
        Control control = new Control(flightOrder,setPeople,getPeople,productSum,customer,payment1);
        control.printOut();
    }
}
代码分析图:



主要问题:
硬编码问题:在ProductSum类中,费率(rate)和重量分段(weight)以固定的列表形式硬编码在类中。这使得在需要修改费率或重量分段时,必须直接修改代码,不利于维护和扩展。
继承关系的合理性:虽然代码使用了继承来实现不同类型的货物(Normal、Dangerous、Expedited)和客户(Corporate、Individual),但部分继承关系可以进一步优化。例如,SetPeople和GetPeople类的属性完全相同,可能可以合并为一个类,或者提取出一个公共的父类来减少代码重复。
接口设计的欠缺:对于支付方式,虽然使用了抽象类Payment及其子类(WechatPayment、AlipayPayment、CashPayment)来实现多态,但可以进一步考虑使用接口来定义支付行为,使得支付方式的扩展性更好。接口可以提供更灵活的实现方式,并且可以让不同的类实现多个支付相关的接口,增加代码的复用性。
虽然还是按照作业说明将第一份代码改进成能通过测试点的了但没有向下研究导致还是还是不够精简

三、采坑心得

1.在第8次题目集中作业题目说明中说输出总重但这个总重并不是实际的总重而是计算得来的计费重量的总重
利用测试用例错误信息:

结果这个问题卡了好久
2.在第8次题目集开始时被读取数据坑了大量的数据让我对换行读取出了问题最后发现需要在读取浮点数后要读取一行时需提前读取掉换行符

四、改进建议

1.可以将代码读取单独写一个类使main更加简便,数据的输出单独一个类实现不必放在control类里
2.部分代码冗余虽然写了父类但并没有删去子类的方法(懒了没注意)如WechatPayment、AlipayPayment、CashPayment
3.读取数据方面可以全部都用nextline将整行读取在将数据转换为所需数据,也就不用读取换行符了
4.后续若继续改进可以引入工厂模式创建ProductFactory和CustomerFactory,通过枚举类型动态创建实例,避免Main类中冗长的if-else。

五、阶段总结

(一)学习成果
多态与抽象类的深度应用:通过货物类型、客户折扣、支付方式的多态设计,理解了 “面向接口编程” 的核心思想。
集合类与泛型的熟练使用:掌握LinkedList的增删改查操作,能通过泛型避免类型转换异常。
复杂业务逻辑拆分:学会将大问题拆解为类职责(如ProductSum负责计算,Control负责流程调度),提升代码可维护性。
(二)待提升方向
设计模式的综合运用:目前仅实现简单策略模式,未来需学习工厂模式(如通过工厂创建货物对象)、单例模式(如订单编号生成器)。
性能优化意识:对于大规模货物数据,需考虑LinkedList与ArrayList的性能差异,选择更合适的数据结构。
总结
题目集 8-9 通过航空货运系统的逐步迭代,从基础类设计过渡到复杂多态场景,有效锻炼了面向对象设计能力。通过调试与重构,加深了对 “封装变化点、扩展行为” 的理解,并加深对六大基础原则的理解。未来需进一步将设计模式与实际业务结合,提升代码的可复用性与可维护性,为大型系统开发奠定基础。

posted @ 2025-05-25 20:20  24201110-韩元昊  阅读(33)  评论(0)    收藏  举报