第二次Blog

一、前言

(1)知识点:题目集8~9的知识点重在考查java中对继承与多态的掌握,对抽象类与接口的区分,对容器类的运用以及代码实现。主要是对之前问题编写的程序进行重构,使编写的程序更符合面向对象设计原则。并且要求掌握分析题目所给类图并根据所给类图进行编程。
(2)题量:两个题目集分别有三道题,每道题编写的代码行数基本上是由简到繁,从最简单的类设计大约需要一百多行到两百行左右,到对代码的重构有三四百行多。
(3)难度:题目集8前两题难度中等,而最后一题航空货运管理系统较难;而题目集9难度较难,需要对知识点有较好的掌握,并要求符合面向对象设计原则。

二、设计与分析
(1)题目集8中第一题要求在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。设计如下:
对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。
其中类结构如图所示:

本题较为简单。

第二题要求在给定的汽车手动风挡玻璃雨刷程序的基础上,对程序进行重构(Refactoring),使得程序可以对功能进行扩展。设计要求符合:
类设计以及类间关系。
类的封装性、继承性以及多态性的综合应用。
接口与抽象类的使用。
本题虽不算太难,但也需要对单一职责原则有深入了解,并且要注意接口的使用,编程过程还是有点绕的。

第三题要求对航空货运管理系统进行类设计,设计要求如下:
以实际重量(GrossWeight)和体积重量(VolumeWeight)中的较高者作为计费重量。计算公式:体积重量(kg)= 货物体积(长×宽×高,单位:厘米)÷6000
费率(Rate):航空公司或货代根据航线、货物类型、市场行情等制定(如CNY 30/kg)。
输入格式如下:
客户编号
客户姓名
客户电话
客户地址
运送货物数量
[货物编号
货物名称
货物宽度
货物长度
货物高度
货物重量
]//[]内的内容输入次数取决于“运送货物数量”,输入不包含“[]”
航班号
航班起飞机场
航班降落机场
航班日期(格式为YYYY-MM-DD)
航班最大载重量
订单编号
订单日期(格式为YYYY-MM-DD)
发件人地址
发件人姓名
发件人电话
收件人地址
收件人姓名
收件人电话

输出格式如下:
客户:姓名(电话)订单信息如下:

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

货物明细如下:

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

类图如下:

最终源码如下:

点击查看代码
import java.util.Scanner;
import java.util.ArrayList;
class Customer {
    private String customerId;
    private String name;
    private String phone;
    private String address;
    public Customer() {
        
    }
    public Customer(String customerId, String name, String phone, String address) {
        this.customerId = customerId;
        this.name = name;
        this.phone = phone;
        this.address= address;
    }
    public String getCustomerId() {
        return customerId;
    }
    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}
class Good {
    private String goodId;
    private String name;
    private double width;
    private double length;
    private double height;
    private double weight;
    public Good() {
        
    }
    public Good(String goodId, String name, double width, double length, double height, double weight) {
        this.goodId = goodId;
        this.name = name;
        this.width = width;
        this.length = length;
        this.height = height;
        this.weight = weight;
    }
    public String getGoodId() {
        return goodId;
    }
    public void setGoodId(String goodId) {
        this.goodId = goodId;
    }
    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 getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
    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 getVolumeWeight() {
        return (width * length * height) / 6000;
    }
    public double getLastWeight() {
        double volumeWeight = getVolumeWeight();
        return Math.max(weight, volumeWeight);
    }
    public double getRate() {
        double lastWeight = getLastWeight();
        if (lastWeight < 20) {
            return 35;
        } else if (lastWeight < 50) {
            return 30;
        } else if (lastWeight < 100) {
            return 25;
        } else {
            return 15;
        }
    }
    public double getActualMoney() {
        return getLastWeight() * getRate();
    }
}
class Flight {
    private String flightId;
    private String startAirport;
    private String arrivalAirport;
    private String flightDate;
    private double maxLoad;
    public Flight() {
        
    }
    public Flight(String flightId, String startAirport, String arrivalAirport, String flightDate, double maxLoad) {
        this.flightId = flightId;
        this.startAirport =startAirport;
        this.arrivalAirport = arrivalAirport;
        this.flightDate = flightDate;
        this.maxLoad =maxLoad;
    }
    public String getFlightId() {
        return flightId;
    }
    public void setFlightId(String flightId) {
        this.flightId = flightId;
    }
    public String getStartAirport() {
        return startAirport;
    }
    public void setStartAirport(String startAirport) {
        this.startAirport = startAirport;
    }
    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 double getMaxLoad() {
        return maxLoad;
    }
    public void setMaxLoad(double maxLoad) {
        this.maxLoad = maxLoad;
    }
}
class Order {
    private String orderId;
    private String orderDate;
    private String sendAddress;
    private String sendName;
    private String sendPhone;
    private String receiveAddress;
    private String receiveName;
    private String receivePhone;
    private Customer customer;
    private Flight flight;
    private ArrayList<Good> goodList;
    public Order() {
        
    }
    public Order(String orderId, String orderDate, String sendAddress, String sendName, String sendPhone, String receiveAddress, String receiveName, String receivePhone, Customer customer, Flight flight, ArrayList<Good> goodList) {
        this.orderId = orderId;
        this.orderDate = orderDate;
        this.sendAddress = sendAddress;
        this.sendName = sendName;
        this.sendPhone = sendPhone;
        this.receiveAddress = receiveAddress;
        this.receiveName = receiveName;
        this.receivePhone = receivePhone;
        this.customer = customer;
        this.flight = flight;
        this.goodList = goodList;
    }
    public String getOrderId() {
        return orderId;
    }
    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
    public String getOrderDate() {
        return orderDate;
    }
    public void setOrderDate(String orderDate) {
        this.orderDate = orderDate;
    }
    public String getSendAddress() {
        return sendAddress;
    }
    public void setSendAddress(String sendAddress) {
        this.sendAddress = sendAddress;
    }
    public String getSendName() {
        return sendName;
    }
    public void setSendName(String sendName) {
        this.sendName = sendName;
    }
    public String getSendPhone() {
        return sendPhone;
    }
    public void setSendPhone(String sendPhone) {
        this.sendPhone = sendPhone;
    }
    public String getReceiveAddress() {
        return receiveAddress;
    }
    public void setReceiveAddress(String receiveAddress) {
        this.receiveAddress = receiveAddress;
    }
    public String getReceiveName() {
        return receiveName;
    }
    public void setReceiveName(String receiveName) {
        this.receiveName = receiveName;
    }
    public String getReceivePhone() {
        return receivePhone;
    }
    public void setReceivePhone(String receivePhone) {
        this.receivePhone = receivePhone;
    }
    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    public Flight getFlight() {
        return flight;
    }
    public void setFlight(Flight flight) {
        this.flight = flight;
    }
    public ArrayList<Good> getGoodList() {
        return goodList;
    }
    public void setGoodList(ArrayList<Good> goodList) {
        this.goodList = goodList;
    }
    public double getTotalWeight() {
        double totalWeight = 0;
        for (Good good : goodList) {
            totalWeight += good.getLastWeight();
        }
        return totalWeight;
    }
    public boolean isOverLoad() {
        return getTotalWeight() > flight.getMaxLoad();
    }
    public double getTotalMoney() {
        double totalMoney = 0;
        for (Good good : goodList) {
            totalMoney += good.getActualMoney();
        }
        return totalMoney;
    }
    public String getFlightId() {
        return flight.getFlightId();
    }
}
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 goodCount = Integer.parseInt(scanner.nextLine());
        ArrayList<Good> goodList = new ArrayList<>();
        for (int i = 0; i < goodCount; i ++) {
            String goodId = scanner.nextLine();
            String goodName = 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());
            Good good = new Good(goodId, goodName, width, length, height, weight);
            goodList.add(good);
        }
        String flightId = scanner.nextLine();
        String startAirport = scanner.nextLine();
        String arrivalAirport = scanner.nextLine();
        String flightDate = scanner.nextLine();
        double maxLoad = Double.parseDouble(scanner.nextLine());
        Flight flight = new Flight(flightId, startAirport, arrivalAirport, flightDate, maxLoad);
        String orderId = scanner.nextLine();
        String orderDate = scanner.nextLine();
        String sendAddress = scanner.nextLine();
        String sendName = scanner.nextLine();
        String sendPhone = scanner.nextLine();
        String receiveAddress = scanner.nextLine();
        String receiveName = scanner.nextLine();
        String receivePhone = scanner.nextLine();
        Order order = new Order(orderId, orderDate, sendAddress, sendName, sendPhone, receiveAddress, receiveName, receivePhone, customer, flight, goodList);
        if (order.isOverLoad()) {
            System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.\n", order.getFlightId());
            return;
        }
        System.out.println("客户:" + customer.getName() + "(" + customer.getPhone() + ")订单信息如下:");
        System.out.println("-----------------------------------------");
        System.out.println("航班号:" + flightId);
        System.out.println("订单号:" + orderId);
        System.out.println("订单日期:" + orderDate);
        System.out.println("发件人姓名:" + sendName);
        System.out.println("发件人电话:" + sendPhone);
        System.out.println("发件人地址:" + sendAddress);
        System.out.println("收件人姓名:" + receiveName);
        System.out.println("收件人电话:" + receivePhone);
        System.out.println("收件人地址:" + receiveAddress);
        System.out.println("订单总重量(kg):" + order.getTotalWeight());
        System.out.println("微信支付金额:" + order.getTotalMoney());
        System.out.println("\n货物明细如下:");
        System.out.println("-----------------------------------------");
        System.out.printf("明细编号\t货物名称\t计费重量\t计费费率\t应交运费\n");
        for (int i = 0; i < goodList.size(); i ++) {
            Good good = goodList.get(i);
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", i + 1, good.getName(), good.getLastWeight(), good.getRate(), good.getActualMoney());
        }
    }
}

SourceMontor的生成报表内容如下:

本题需要先做好类设计再编程,需要注意输入与输出的格式,类中的方法编程并不难,但整体程序编程很繁琐,尤其是输入与输出的格式以及注意容器类的方法的编写与运用。
(2)题目集9第一题是魔方问题,本问题中的魔方有两种,一种是正方体魔方,一种是正三棱锥魔方,其中,正方体或正三棱锥魔方是由单元正方体或正三棱锥组成,单元正方体或正三棱锥的个数由阶数(即层数)决定,即魔方边长=阶数*单元边长。利用“立体图形”问题源码,实现如下功能:
魔方有三个属性:颜色,阶数,类型(正方体魔方、正三棱锥魔方),程序要求输出魔方的颜色、表面积和体积。参考设计类图如下所示:

本题不难,但需要搞清父类与子类的关系,注意重写方法即可。

第二题是点线面问题再重构(容器类),在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。设计如下:
在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList类型的对象(若不了解泛型,可以不使用
增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
1:向容器中增加Point对象
2:向容器中增加Line对象
3:向容器中增加Plane对象
4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
0:输入结束
类图如图所示:

本题新增考查容器类,需在继承与多态的基础上结合容器类编程,在编写各类时问题不大,但编写主方法时要注意多态的运用,以及对选择结构与容器类的结合,便也能成功编写出正确的代码。

第三题在航空货运管理系统(类设计)问题的基础上加上了继承与多态,新增货物类型、客户类型与其对应的费率与折扣率。设计如下:
以实际重量(Gross Weight)和体积重量(Volume Weight)中的较高者作为计费重量。计算公式:体积重量(kg) = 货物体积(长×宽×高,单位:厘米)÷ 6000
费率(Rate):航空公司或货代根据航线、货物类型、市场行情等制定(如CNY 30/kg)。本次作业费率与货物类型有关,货物类型分为普通货物、危险货物和加急货物三种,其费率分别为:

计算公式:基础运费 = 计费重量 × 费率 × 折扣率
其中,折扣率是指不同的用户类型针对每个订单的运费可以享受相应的折扣,在本题中,用户分为个人用户和集团用户,其中个人用户可享受订单运费的 9折优惠,集团用户可享受订单运费的 8 折优惠。
输入格式如下:
客户类型[可输入项:Individual/Corporate]
客户编号
客户姓名
客户电话
客户地址
货物类型[可输入项:Normal/Expedite/Dangerous]
运送货物数量
[货物编号
货物名称
货物宽度
货物长度
货物高度
货物重量
]//[]内的内容输入次数取决于“运送货物数量”,输入不包含“[]”
航班号
航班起飞机场
航班降落机场
航班日期(格式为YYYY-MM-DD)
航班最大载重量
订单编号
订单日期(格式为YYYY-MM-DD)
发件人地址
发件人姓名
发件人电话
收件人地址
收件人姓名
收件人电话
支付方式[可输入项:Wechat/ALiPay/Cash]

输出格式如下:
客户:姓名(电话)订单信息如下:

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

货物明细如下:

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

类图如图所示:

最终源码如下:

点击查看代码
import java.util.Scanner;
import java.util.ArrayList;
abstract class Customer {
    protected String customerId;
    protected String name;
    protected String phone;
    protected String address;
    protected String customerType;
    public Customer() {
        
    }
    public Customer(String customerId, String name, String phone, String address, String customerType) {
        this.customerId = customerId;
        this.name = name;
        this.phone = phone;
        this.address= address;
        this.customerType = customerType;
    }
    public String getCustomerId() {
        return customerId;
    }
    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCustomerType() {
        return customerType;
    }
    public void setCustomerType(String customerType) {
        this.customerType = customerType;
    }
    public abstract double getDiscountRate();
}
class IndividualCustomer extends Customer {
    public IndividualCustomer() {
        
    }
    public IndividualCustomer(String customerId, String name, String phone, String address) {
        super(customerId, name, phone, address, "Individual");
    }
    @Override
    public double getDiscountRate() {
        return 0.9;
    }
}
class CorporateCustomer extends Customer {
    public CorporateCustomer() {
        
    }
    public CorporateCustomer(String customerId, String name, String phone, String address) {
        super(customerId, name, phone, address, "Corporate");
    }
    @Override
    public double getDiscountRate() {
        return 0.8;
    }
}
abstract class Good {
    protected String goodId;
    protected String name;
    protected double width;
    protected double length;
    protected double height;
    protected double weight;
    protected String goodType;
    public Good() {
        
    }
    public Good(String goodId, String name, double width, double length, double height, double weight, String goodType) {
        this.goodId = goodId;
        this.name = name;
        this.width = width;
        this.length = length;
        this.height = height;
        this.weight = weight;
        this.goodType = goodType;
    }
    public String getGoodId() {
        return goodId;
    }
    public void setGoodId(String goodId) {
        this.goodId = goodId;
    }
    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 getLength() {
        return length;
    }
    public void setLength(double length) {
        this.length = length;
    }
    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 String getGoodType() {
        return goodType;
    }
    public void setGoodType(String goodType) {
        this.goodType = goodType;
    }
    public double getVolumeWeight() {
        return (width * length * height) / 6000;
    }
    public double getLastWeight() {
        double volumeWeight = getVolumeWeight();
        return Math.max(weight, volumeWeight);
    }
    public double getActualMoney() {
        return getLastWeight() * getRate();
    }
    public abstract double getRate();
}
class NormalGood extends Good {
    public NormalGood() {
        
    }
    public NormalGood(String goodId, String name, double width, double length, double height, double weight) {
        super(goodId, name, width, length, height, weight, "Normal");
    }
    @Override
    public double getRate() {
        double lastWeight = getLastWeight();
        if (lastWeight < 20) {
            return 35;
        } else if (lastWeight < 50) {
            return 30;
        } else if (lastWeight < 100) {
            return 25;
        } else {
            return 15;
        }
    }
}
class DangerousGood extends Good {
    public DangerousGood() {
        
    }
    public DangerousGood(String goodId, String name, double width, double length, double height, double weight) {
        super(goodId, name, width, length, height, weight, "Dangerous");
    }
    @Override
    public double getRate() {
        double lastWeight = getLastWeight();
        if (lastWeight < 20) {
            return 80;
        } else if (lastWeight < 50) {
            return 50;
        } else if (lastWeight < 100) {
            return 30;
        } else {
            return 20;
        }
    }
}
class ExpediteGood extends Good {
    public ExpediteGood() {
        
    }
    public ExpediteGood(String goodId, String name, double width, double length, double height, double weight) {
        super(goodId, name, width, length, height, weight, "Expedite");
    }
    @Override
    public double getRate() {
        double lastWeight = getLastWeight();
        if (lastWeight < 20) {
            return 60;
        } else if (lastWeight < 50) {
            return 50;
        } else if (lastWeight < 100) {
            return 40;
        } else {
            return 30;
        }
    }
}
class Flight {
    private String flightId;
    private String startAirport;
    private String arrivalAirport;
    private String flightDate;
    private double maxLoad;
    public Flight() {
        
    }
    public Flight(String flightId, String startAirport, String arrivalAirport, String flightDate, double maxLoad) {
        this.flightId = flightId;
        this.startAirport =startAirport;
        this.arrivalAirport = arrivalAirport;
        this.flightDate = flightDate;
        this.maxLoad =maxLoad;
    }
    public String getFlightId() {
        return flightId;
    }
    public void setFlightId(String flightId) {
        this.flightId = flightId;
    }
    public String getStartAirport() {
        return startAirport;
    }
    public void setStartAirport(String startAirport) {
        this.startAirport = startAirport;
    }
    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 double getMaxLoad() {
        return maxLoad;
    }
    public void setMaxLoad(double maxLoad) {
        this.maxLoad = maxLoad;
    }
}
class Order {
    private String orderId;
    private String orderDate;
    private String sendAddress;
    private String sendName;
    private String sendPhone;
    private String receiveAddress;
    private String receiveName;
    private String receivePhone;
    private Customer customer;
    private Flight flight;
    private ArrayList<Good> goodList;
    private String payMethod;
    public Order() {
        
    }
    public Order(String orderId, String orderDate, String sendAddress, String sendName, String sendPhone, String receiveAddress, String receiveName, String receivePhone, Customer customer, Flight flight, ArrayList<Good> goodList, String payMethod) {
        this.orderId = orderId;
        this.orderDate = orderDate;
        this.sendAddress = sendAddress;
        this.sendName = sendName;
        this.sendPhone = sendPhone;
        this.receiveAddress = receiveAddress;
        this.receiveName = receiveName;
        this.receivePhone = receivePhone;
        this.customer = customer;
        this.flight = flight;
        this.goodList = goodList;
        this.payMethod = payMethod;
    }
    public String getOrderId() {
        return orderId;
    }
    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
    public String getOrderDate() {
        return orderDate;
    }
    public void setOrderDate(String orderDate) {
        this.orderDate = orderDate;
    }
    public String getSendAddress() {
        return sendAddress;
    }
    public void setSendAddress(String sendAddress) {
        this.sendAddress = sendAddress;
    }
    public String getSendName() {
        return sendName;
    }
    public void setSendName(String sendName) {
        this.sendName = sendName;
    }
    public String getSendPhone() {
        return sendPhone;
    }
    public void setSendPhone(String sendPhone) {
        this.sendPhone = sendPhone;
    }
    public String getReceiveAddress() {
        return receiveAddress;
    }
    public void setReceiveAddress(String receiveAddress) {
        this.receiveAddress = receiveAddress;
    }
    public String getReceiveName() {
        return receiveName;
    }
    public void setReceiveName(String receiveName) {
        this.receiveName = receiveName;
    }
    public String getReceivePhone() {
        return receivePhone;
    }
    public void setReceivePhone(String receivePhone) {
        this.receivePhone = receivePhone;
    }
    public Customer getCustomer() {
        return customer;
    }
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    public Flight getFlight() {
        return flight;
    }
    public void setFlight(Flight flight) {
        this.flight = flight;
    }
    public ArrayList<Good> getGoodList() {
        return goodList;
    }
    public void setGoodList(ArrayList<Good> goodList) {
        this.goodList = goodList;
    }
    public String getPayMethod() {
        return payMethod;
    }
    public void setPayMethod(String payMethod) {
        this.payMethod = payMethod;
    }
    public double getTotalWeight() {
        double totalWeight = 0;
        for (Good good : goodList) {
            totalWeight += good.getLastWeight();
        }
        return totalWeight;
    }
    public boolean isOverLoad() {
        return getTotalWeight() > flight.getMaxLoad();
    }
    public double getTotalMoney() {
        double totalMoney = 0;
        for (Good good : goodList) {
            totalMoney += good.getActualMoney();
        }
        return totalMoney;
    }
    public double getLastMoney() {
        double totalMoney = getTotalMoney();
        double discountRate = customer.getDiscountRate();
        double lastMoney = totalMoney * discountRate;
        return lastMoney;
    }
    public String getFlightId() {
        return flight.getFlightId();
    }
    public String getPaymentMethod() {
        switch (payMethod) {
            case "Wechat":
                return "微信";
            case "ALiPay":
                return "支付宝";
            case "Cash":
                return "现金";
            default:
                return payMethod;
        }
    }
}
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;
        if (customerType.equals("Individual")) {
            customer = new IndividualCustomer(customerId, customerName, customerPhone, customerAddress);
        } else if (customerType.equals("Corporate")) {
            customer = new CorporateCustomer(customerId, customerName, customerPhone, customerAddress);
        } else {
            return;
        }
        String goodType = scanner.nextLine();
        int goodCount = Integer.parseInt(scanner.nextLine());
        ArrayList<Good> goodList = new ArrayList<>();
        for (int i = 0; i < goodCount; i ++) {
            String goodId = scanner.nextLine();
            String goodName = 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());
            Good good;
            switch (goodType) {
                case "Normal":
                    good = new NormalGood(goodId, goodName, width, length, height, weight);
                    break;
                case "Expedite":
                    good = new ExpediteGood(goodId, goodName, width, length, height, weight);
                    break;
                case "Dangerous":
                    good = new DangerousGood(goodId, goodName, width, length, height, weight);
                    break;
                default:
                    return;
            }
            goodList.add(good);
        }
        String flightId = scanner.nextLine();
        String startAirport = scanner.nextLine();
        String arrivalAirport = scanner.nextLine();
        String flightDate = scanner.nextLine();
        double maxLoad = Double.parseDouble(scanner.nextLine());
        Flight flight = new Flight(flightId, startAirport, arrivalAirport, flightDate, maxLoad);
        String orderId = scanner.nextLine();
        String orderDate = scanner.nextLine();
        String sendAddress = scanner.nextLine();
        String sendName = scanner.nextLine();
        String sendPhone = scanner.nextLine();
        String receiveAddress = scanner.nextLine();
        String receiveName = scanner.nextLine();
        String receivePhone = scanner.nextLine();
        String payMethod = scanner.nextLine();
        Order order = new Order(orderId, orderDate, sendAddress, sendName, sendPhone, receiveAddress, receiveName, receivePhone, customer, flight, goodList, payMethod);
        if (order.isOverLoad()) {
            System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.\n", order.getFlightId());
            return;
        }
        System.out.println("客户:" + customer.getName() + "(" + customer.getPhone() + ")订单信息如下:");
        System.out.println("-----------------------------------------");
        System.out.println("航班号:" + flightId);
        System.out.println("订单号:" + orderId);
        System.out.println("订单日期:" + orderDate);
        System.out.println("发件人姓名:" + sendName);
        System.out.println("发件人电话:" + sendPhone);
        System.out.println("发件人地址:" + sendAddress);
        System.out.println("收件人姓名:" + receiveName);
        System.out.println("收件人电话:" + receivePhone);
        System.out.println("收件人地址:" + receiveAddress);
        System.out.println("订单总重量(kg):" + order.getTotalWeight());
        System.out.printf("%s支付金额:%.1f\n", order.getPaymentMethod(), order.getLastMoney());
        System.out.println("\n货物明细如下:");
        System.out.println("-----------------------------------------");
        System.out.printf("明细编号\t货物名称\t计费重量\t计费费率\t应交运费\n");
        for (int i = 0; i < goodList.size(); i ++) {
            Good good = goodList.get(i);
            System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", i + 1, good.getName(), good.getLastWeight(), good.getRate(), good.getActualMoney());
        }
    }
}

SourceMontor的生成报表内容如图所示:

本题对面向对象设计原则要求较高,需设计好类与类之间的关系,以及正确运用继承和多态便能解决这道题。

三、踩坑心得
1.对于编程,输入与输出格式尤其严格,无论是一个字符的中英文模式不一样还是缺失一个空格都会使程序报错且难以察觉,在题目集8的航空货运管理系统一题中,一开始提交后发现格式错误,却一直找不出错误的原因,结果最后发现是输出格式中对于两个中文词语之间的空格需要用制表符输出而不是直接输出空格,如图所示:

2.有时候对于变量的作用域不是我们想当然的那样,在使用的时候定义,而应在使用前定义最稳妥,在题目集9的航空货运管理系统一题中,在主方法中在定义Order类型的变量中,包含参数customer,而我并没有在一开始就定义customer,导致customer的作用域不在其中,因此出现编译错误的问题,所以一定到注意变量初始化的问题。

四、改进建议
1.编程前应该先做好类设计,最好能参考类图编程,能够提高效率。
2.编程时要时刻注意中英文模式的变化,以免到最后出现编译错误的问题而花上大量时间与精力寻找问题。
3.编程时要注意变量初始化的问题,保证其作用域覆盖到其方法上。
4.编程时要遵守面向对象设计原则,合理运用继承与多态,以及抽象类和接口,以满足各种问题的需求。

五、总结
通过这两次题目集,我掌握了继承、多态的核心思想,并深刻理解了继承与多态在代码可扩展性中的关键作用,学会了通过抽象类和子类设计可扩展系统,理解开闭原则在实际项目中的应用,掌握了从需求分析到类图设计,再到代码实现的完整流程。今后我还要深入学习与理解抽象类和接口,并加强学习容器类,学会运用PlantUML插件生成类图、用SourceMonitor分析所写代码,提升我的设计与分析能力。

posted @ 2025-05-25 22:44  憾长存  阅读(19)  评论(0)    收藏  举报