找回密码
 立即注册
首页 业界区 安全 第二次Blog作业-航空货运管理系统

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

撇瞥 2025-5-30 13:36:28
题目
某航空公司“航空货运管理系统”中的空运费的计算涉及多个因素,通常包括货物重量/体积、运输距离、附加费用、货物类型、客户类型以及市场供需等。
本次作业主要考虑货物重量/体积,以下是具体的计算方式和关键要点:
一、计费重量的确定
空运以实际重量(GrossWeight)和体积重量(VolumeWeight)中的较高者作为计费重量。
计算公式:
体积重量(kg)= 货物体积(长×宽×高,单位:厘米)÷6000
示例:
若货物实际重量为80kg,体积为120cm×80cm×60cm,则:
体积重量 =(120×80×60)÷6000=96kg
计费重量取96kg(因96kg>80kg)。
二、基础运费计算
费率(Rate):航空公司或货代根据航线、货物类型、市场行情等制定(如
CNY 30/kg)。本次作业费率采用分段计算方式:
1.png

公式:基础运费 = 计费重量 × 费率
三、题目说明
本次题目模拟某客户到该航空公司办理一次货运业务的过程:
航空公司提供如下信息:
航班信息(航班号,航班起飞机场所在城市,航班降落机场所在城市,航班日期,航班最大载重量)
客户填写货运订单并进行支付,需要提供如下信息:
 客户信息(姓名,电话号码等)
 货物信息(货物名称,货物包装长、宽、高尺寸,货物重量等)
 运送信息(发件人姓名、电话、地址,收件人姓名、电话、地址,所选航班号,订单日期)
 支付方式(支付宝支付、微信支付)
注:一个货运订单可以运送多件货物,每件货物均需要根据重量及费率单独计费。
程序需要从键盘依次输入填写订单需要提供的信息,然后分别生成订单信息报表及货物明细报表。
四、题目要求
本次题目重点考核面向对象设计原则中的单一职责原则、里氏代换原则、开闭原则以及合成复用原则
设计因素:单一职责原则(40%)、里氏代换原则(20%)、开闭原则(20%)、
合成复用原则(20%)
输入格式:
按如下顺序分别输入客户信息、货物信息、航班信息以及订单信息。
客户编号
客户姓名
客户电话
客户地址
运送货物数量
[货物编号
货物名称
货物宽度
货物长度
货物高度
货物重量
]//[]内的内容输入次数取决于“运送货物数量”,输入不包含“[]”
航班号
航班起飞机场
航班降落机场
航班日期(格式为YYYY-MM-DD)
航班最大载重量
订单编号
订单日期(格式为YYYY-MM-DD)
发件人地址
发件人姓名
发件人电话
收件人地址
收件人姓名
收件人电话
输出格式:
如果订单中货物重量超过航班剩余载重量,程序输出The flight with flight number:航班号 has exceeded its load capacity and cannot carry the order. ,程序终止运行。
如果航班载重量可以承接该订单,输出如下:
点击查看代码
  1. 客户:姓名(电话)订单信息如下:
  2. -----------------------------------------
  3. 航班号:
  4. 订单号:
  5. 订单日期:
  6. 发件人姓名:
  7. 发件人电话:
  8. 发件人地址:
  9. 收件人姓名:
  10. 收件人电话:
  11. 收件人地址:
  12. 订单总重量(kg):
  13. 微信支付金额:
  14. 货物明细如下:
  15. -----------------------------------------
  16. 明细编号    货物名称    计费重量    计费费率    应交运费
  17. 1    ...
  18. 2    ...
复制代码
输入样例:点击查看代码
  1. 10001
  2. 郭靖
  3. 13807911234
  4. 南昌航空大学
  5. 2
  6. 101
  7. 发电机
  8. 80
  9. 60
  10. 40
  11. 80
  12. 102
  13. 信号发生器
  14. 55
  15. 70
  16. 60
  17. 45
  18. MU1234
  19. 昌北国际机场
  20. 大兴国际机场
  21. 2025-04-22
  22. 1000
  23. 900001
  24. 2025-04-22
  25. 南昌大学
  26. 洪七公
  27. 18907912325
  28. 北京大学
  29. 黄药师
  30. 13607912546
复制代码
输出样例:点击查看代码
  1. 客户:郭靖(13807911234)订单信息如下:
  2. -----------------------------------------
  3. 航班号:MU1234
  4. 订单号:900001
  5. 订单日期:2025-04-22
  6. 发件人姓名:洪七公
  7. 发件人电话:18907912325
  8. 发件人地址:南昌大学
  9. 收件人姓名:黄药师
  10. 收件人电话:13607912546
  11. 收件人地址:北京大学
  12. 订单总重量(kg):125.0
  13. 微信支付金额:3350.0
  14. 货物明细如下:
  15. -----------------------------------------
  16. 明细编号    货物名称    计费重量    计费费率    应交运费
  17. 1    发电机    80.0    25.0    2000.0
  18. 2    信号发生器    45.0    30.0    1350.0
复制代码
我的源码
点击查看代码
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. class Cargo {
  5.     private int id;
  6.     private String name;
  7.     private double width;
  8.     private double length;
  9.     private double height;
  10.     private double weight;
  11.     public Cargo(int id, String name, double width, double length, double height, double weight) {
  12.         this.id = id;
  13.         this.name = name;
  14.         this.width = width;
  15.         this.length = length;
  16.         this.height = height;
  17.         this.weight = weight;
  18.     }
  19.     // 计算体积重量
  20.     public double calculateVolumeWeight() {
  21.         return (width * length * height) / 6000;
  22.     }
  23.     // 获取计费重量
  24.     public double getChargeableWeight() {
  25.         double volumeWeight = calculateVolumeWeight();
  26.         return Math.max(weight, volumeWeight);
  27.     }
  28.     // 获取计费费率
  29.     public double getRate() {
  30.         double chargeableWeight = getChargeableWeight();
  31.         if (chargeableWeight < 20) {
  32.             return 35;
  33.         } else if (chargeableWeight < 50) {
  34.             return 30;
  35.         } else if (chargeableWeight < 100) {
  36.             return 25;
  37.         } else {
  38.             return 15;
  39.         }
  40.     }
  41.     // 计算应交运费
  42.     public double calculateFreight() {
  43.         return getChargeableWeight() * getRate();
  44.     }
  45.     public int getId() {
  46.         return id;
  47.     }
  48.     public String getName() {
  49.         return name;
  50.     }
  51.     public double getChargeableWeightFormatted() {
  52.         return Math.round(getChargeableWeight() * 10) / 10.0;
  53.     }
  54.     public double getRateFormatted() {
  55.         return Math.round(getRate() * 10) / 10.0;
  56.     }
  57.     public double getCalculateFreightFormatted() {
  58.         return Math.round(calculateFreight() * 10) / 10.0;
  59.     }
  60. }
  61. class Flight {
  62.     private String flightNumber;
  63.     private String departureCity;
  64.     private String arrivalCity;
  65.     private String flightDate;
  66.     private double maxLoad;
  67.     public Flight(String flightNumber, String departureCity, String arrivalCity, String flightDate, double maxLoad) {
  68.         this.flightNumber = flightNumber;
  69.         this.departureCity = departureCity;
  70.         this.arrivalCity = arrivalCity;
  71.         this.flightDate = flightDate;
  72.         this.maxLoad = maxLoad;
  73.     }
  74.     public String getFlightNumber() {
  75.         return flightNumber;
  76.     }
  77.     public double getMaxLoad() {
  78.         return maxLoad;
  79.     }
  80. }
  81. class Order {
  82.     private int orderId;
  83.     private String orderDate;
  84.     private String senderAddress;
  85.     private String senderName;
  86.     private String senderPhone;
  87.     private String recipientAddress;
  88.     private String recipientName;
  89.     private String recipientPhone;
  90.     private List<Cargo> cargoList;
  91.     private double totalWeight;
  92.     public Order(int orderId, String orderDate, String senderAddress, String senderName, String senderPhone,
  93.                  String recipientAddress, String recipientName, String recipientPhone) {
  94.         this.orderId = orderId;
  95.         this.orderDate = orderDate;
  96.         this.senderAddress = senderAddress;
  97.         this.senderName = senderName;
  98.         this.senderPhone = senderPhone;
  99.         this.recipientAddress = recipientAddress;
  100.         this.recipientName = recipientName;
  101.         this.recipientPhone = recipientPhone;
  102.         this.cargoList = new ArrayList<>();
  103.         this.totalWeight = 0;
  104.     }
  105.     public void addCargo(Cargo cargo) {
  106.         cargoList.add(cargo);
  107.         totalWeight += cargo.getChargeableWeight();
  108.     }
  109.     public int getOrderId() {
  110.         return orderId;
  111.     }
  112.     public String getOrderDate() {
  113.         return orderDate;
  114.     }
  115.     public String getSenderAddress() {
  116.         return senderAddress;
  117.     }
  118.     public String getSenderName() {
  119.         return senderName;
  120.     }
  121.     public String getSenderPhone() {
  122.         return senderPhone;
  123.     }
  124.     public String getRecipientAddress() {
  125.         return recipientAddress;
  126.     }
  127.     public String getRecipientName() {
  128.         return recipientName;
  129.     }
  130.     public String getRecipientPhone() {
  131.         return recipientPhone;
  132.     }
  133.     public double getTotalWeightFormatted() {
  134.         return Math.round(totalWeight * 10) / 10.0;
  135.     }
  136.     public double calculateTotalFreight() {
  137.         double totalFreight = 0;
  138.         for (Cargo cargo : cargoList) {
  139.             totalFreight += cargo.calculateFreight();
  140.         }
  141.         return totalFreight;
  142.     }
  143.     public double getTotalFreightFormatted() {
  144.         return Math.round(calculateTotalFreight() * 10) / 10.0;
  145.     }
  146. }
  147. class Customer {
  148.     private int id;
  149.     private String name;
  150.     private String phone;
  151.     private String address;
  152.     public Customer(int id, String name, String phone, String address) {
  153.         this.id = id;
  154.         this.name = name;
  155.         this.phone = phone;
  156.         this.address = address;
  157.     }
  158.     public String getName() {
  159.         return name;
  160.     }
  161.     public String getPhone() {
  162.         return phone;
  163.     }
  164. }
  165. public class Main {
  166.     public static void main(String[] args) {
  167.         Scanner scanner = new Scanner(System.in);
  168.         // 输入客户信息
  169.         int customerId = scanner.nextInt();
  170.         String customerName = scanner.next();
  171.         String customerPhone = scanner.next();
  172.         String customerAddress = scanner.next();
  173.         Customer customer = new Customer(customerId, customerName, customerPhone, customerAddress);
  174.         // 输入货物信息
  175.         int cargoNum = scanner.nextInt();
  176.         List<Cargo> cargoList = new ArrayList<>();
  177.         for (int i = 0; i < cargoNum; i++) {
  178.             int cargoId = scanner.nextInt();
  179.             String cargoName = scanner.next();
  180.             double width = scanner.nextDouble();
  181.             double length = scanner.nextDouble();
  182.             double height = scanner.nextDouble();
  183.             double weight = scanner.nextDouble();
  184.             Cargo cargo = new Cargo(cargoId, cargoName, width, length, height, weight);
  185.             cargoList.add(cargo);
  186.         }
  187.         // 输入航班信息
  188.         String flightNumber = scanner.next();
  189.         String departureCity = scanner.next();
  190.         String arrivalCity = scanner.next();
  191.         String flightDate = scanner.next();
  192.         double maxLoad = scanner.nextDouble();
  193.         Flight flight = new Flight(flightNumber, departureCity, arrivalCity, flightDate, maxLoad);
  194.         // 输入订单信息
  195.         int orderId = scanner.nextInt();
  196.         String orderDate = scanner.next();
  197.         String senderAddress = scanner.next();
  198.         String senderName = scanner.next();
  199.         String senderPhone = scanner.next();
  200.         String recipientAddress = scanner.next();
  201.         String recipientName = scanner.next();
  202.         String recipientPhone = scanner.next();
  203.         Order order = new Order(orderId, orderDate, senderAddress, senderName, senderPhone,
  204.                 recipientAddress, recipientName, recipientPhone);
  205.         for (Cargo cargo : cargoList) {
  206.             order.addCargo(cargo);
  207.         }
  208.         if (order.getTotalWeightFormatted() > flight.getMaxLoad()) {
  209.             System.out.println("The flight with flight number: " + flight.getFlightNumber() + " has exceeded its load capacity and cannot carry the order.");
  210.         } else {
  211.             System.out.println("客户:" + customer.getName() + "(" + customer.getPhone() + ")订单信息如下:");
  212.             System.out.println("-----------------------------------------");
  213.             System.out.println("航班号:" + flight.getFlightNumber());
  214.             System.out.println("订单号:" + order.getOrderId());
  215.             System.out.println("订单日期:" + order.getOrderDate());
  216.             System.out.println("发件人姓名:" + order.getSenderName());
  217.             System.out.println("发件人电话:" + order.getSenderPhone());
  218.             System.out.println("发件人地址:" + order.getSenderAddress());
  219.             System.out.println("收件人姓名:" + order.getRecipientName());
  220.             System.out.println("收件人电话:" + order.getRecipientPhone());
  221.             System.out.println("收件人地址:" + order.getRecipientAddress());
  222.             System.out.println("订单总重量(kg):" + order.getTotalWeightFormatted());
  223.             System.out.println("微信支付金额:" + order.getTotalFreightFormatted());
  224.             System.out.println("\n货物明细如下:");
  225.             System.out.println("-----------------------------------------");
  226.             System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
  227.             for (int i = 0; i < cargoList.size(); i++) {
  228.                 Cargo cargo = cargoList.get(i);
  229.                 System.out.println((i + 1) + "\t" + cargo.getName() + "\t" + cargo.getChargeableWeightFormatted() + "\t" + cargo.getRateFormatted() + "\t" + cargo.getCalculateFreightFormatted());
  230.             }
  231.         }
  232.     }
  233. }
复制代码
输入点击查看代码
  1. 10001
  2. 郭靖
  3. 13807911234
  4. 南昌航空大学
  5. 2
  6. 101
  7. 发电机
  8. 80
  9. 60
  10. 40
  11. 80
  12. 102
  13. 信号发生器
  14. 55
  15. 70
  16. 60
  17. 45
  18. MU1234
  19. 昌北国际机场
  20. 大兴国际机场
  21. 2025-04-22
  22. 1000
  23. 900001
  24. 2025-04-22
  25. 南昌大学
  26. 洪七公
  27. 18907912325
  28. 北京大学
  29. 黄药师
  30. 13607912546
复制代码
输出点击查看代码
  1. 客户:郭靖(13807911234)订单信息如下:
  2. -----------------------------------------
  3. 航班号:MU1234
  4. 订单号:900001
  5. 订单日期:2025-04-22
  6. 发件人姓名:洪七公
  7. 发件人电话:18907912325
  8. 发件人地址:南昌大学
  9. 收件人姓名:黄药师
  10. 收件人电话:13607912546
  11. 收件人地址:北京大学
  12. 订单总重量(kg):125.0
  13. 微信支付金额:3350.0
  14. 货物明细如下:
  15. -----------------------------------------
  16. 明细编号        货物名称        计费重量        计费费率        应交运费
  17. 1        发电机        80.0        25.0        2000.0
  18. 2        信号发生器        45.0        30.0        1350.0
复制代码
<ol>
Cargo 类方法分析
核心业务方法
calculateVolumeWeight()
功能:根据公式 体积重量 = (长×宽×高)/6000 计算货物的体积重量。
设计:单一职责原则的体现,专注于体积重量计算。
getChargeableWeight()
功能:比较实际重量和体积重量,取较大值作为计费重量。
设计:依赖 calculateVolumeWeight(),符合合成复用原则。
getRate()
功能:根据计费重量分阶段计算费率(如  flight.getMaxLoad()) {    System.out.println("航班超载...");} else {    // 输出订单信息}[/code]设计原则遵循情况总结
1.单一职责原则
优点:多数类的方法专注于单一功能(如 Cargo 类的计算方法)。
不足:AirCargoSystem.main() 承担过多职责。
2.开闭原则
不足:Cargo.getRate() 使用条件判断,新增费率需修改原有代码。
改进建议:使用策略模式或工厂模式封装费率计算逻辑。
3.合成复用原则
优点:Order 类通过组合 Cargo 对象实现功能复用。
改进建议
1.重构 AirCargoSystem.main()
将输入处理、业务验证和输出逻辑拆分为独立方法或类。
示例:
点击查看代码
  1. // 输入处理与业务逻辑耦合
  2. if (order.getTotalWeightFormatted() > flight.getMaxLoad()) {
  3.     System.out.println("航班超载...");
  4. } else {
  5.     // 输出订单信息
  6. }
复制代码
2.优化费率计算(开闭原则)
使用策略模式:
点击查看代码
  1. private static Order createOrderFromInput(Scanner scanner) {
  2.     // 封装输入逻辑
  3. }
  4. private static void validateOrder(Order order, Flight flight) {
  5.     // 封装验证逻辑
  6. }
复制代码
3.增强错误处理
添加输入验证(如日期格式、重量合法性),避免程序崩溃。
4.提高代码复用性
将格式化逻辑(如 getXXFormatted())提取为工具类方法。
2.png

3.png

第二题源码
点击查看代码
  1. interface RateStrategy {
  2.     double calculateRate(double weight);
  3. }
  4. class TieredRateStrategy implements RateStrategy {
  5.     // 实现分阶段费率计算
  6. }
复制代码
4.png

5.png


来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册