总结性Blog02:java航空货运管理系统
一、前言
难度:简单。两次pta全部以满分通过。
题量:少。每次能在很短的时间内完成。
知识点:面向对象程序设计原则。包括单一职责原则,开闭原则,里氏代换原则。
二、设计与分析
题目概要:
本次题目模拟某客户到该航空公司办理一次货运业务的过程。程序需要从键盘依次输入填写订单需要提供的
信息,然后分别生成订单信息报表及货物明细报表。
按要求输入后得出相应输出,重点在于面向对象的设计,使代码具有可拓展性。
题目分析:
一、第一次题目集:
1.输入格式:

2.输出格式:
·如果订单中货物重量超过航班剩余载重量,程序输出The flight with flight number:航班号 has exceeded its load capacity and cannot carry the order. ,程序终止运行。
·如果航班载重量可以承接该订单,输出如下:

3.源码:
点击查看代码
import java.util.*;
class otherInformation
{
private int customerID;//顾客编号
private String customerName;//顾客姓名
private String customertel;//顾客电话
private String customerAddress;//顾客地址
private String flightID;//航班编号
private String departure;//起飞机场
private String arrival;//降落机场
private String flightDate;//航班日期
private int maxWeight;//航班最大载重量
private int orderID;//订单编号
private String orderDate;//订单日期
private String senderAddress;//发件人地址
private String senderName;//发件人姓名
private String sendertel;//发件人电话
private String recipientAddress;//收件人地址
private String recipientName;//收件人姓名
private String recipienttel;//收件人电话
public otherInformation() {}
public otherInformation(int customerID, String customerName, String customertel, String customerAddress,
String flightID, String departure, String arrival, String flightDate, int maxWeight, int orderID,
String orderDate, String senderAddress, String senderName, String sendertel, String recipientAddress,
String recipientName, String recipienttel) {
this.customerID = customerID;
this.customerName = customerName;
this.customertel = customertel;
this.customerAddress = customerAddress;
this.flightID = flightID;
this.departure = departure;
this.arrival = arrival;
this.flightDate = flightDate;
this.maxWeight = maxWeight;
this.orderID = orderID;
this.orderDate = orderDate;
this.senderAddress = senderAddress;
this.senderName = senderName;
this.sendertel = sendertel;
this.recipientAddress = recipientAddress;
this.recipientName = recipientName;
this.recipienttel = recipienttel;
}
//setter
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public void setCustomertel(String customertel) {
this.customertel = customertel;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
public void setFlightID(String flightID) {
this.flightID = flightID;
}
public void setDeparture(String departure) {
this.departure = departure;
}
public void setArrival(String arrival) {
this.arrival = arrival;
}
public void setFlightDate(String flightDate) {
this.flightDate = flightDate;
}
public void setMaxWeight(int maxWeight) {
this.maxWeight = maxWeight;
}
public void setOrderID(int orderID) {
this.orderID = orderID;
}
public void setOrderDate(String orderDate) {
this.orderDate = orderDate;
}
public void setSenderAddress(String senderAddress) {
this.senderAddress = senderAddress;
}
public void setSenderName(String senderName) {
this.senderName = senderName;
}
public void setSendertel(String sendertel) {
this.sendertel = sendertel;
}
public void setRecipientAddress(String recipientAddress) {
this.recipientAddress = recipientAddress;
}
public void setRecipientName(String recipientName) {
this.recipientName = recipientName;
}
public void setRecipienttel(String recipienttel) {
this.recipienttel = recipienttel;
}
//getter
public int getCustomerID() {
return customerID;
}
public String getCustomerName() {
return customerName;
}
public String getCustomertel() {
return customertel;
}
public String getCustomerAddress() {
return customerAddress;
}
public String getFlightID() {
return flightID;
}
public String getDeparture() {
return departure;
}
public String getArrival() {
return arrival;
}
public String getFlightDate() {
return flightDate;
}
public int getMaxWeight() {
return maxWeight;
}
public int getOrderID() {
return orderID;
}
public String getOrderDate() {
return orderDate;
}
public String getSenderAddress() {
return senderAddress;
}
public String getSenderName() {
return senderName;
}
public String getSendertel() {
return sendertel;
}
public String getRecipientAddress() {
return recipientAddress;
}
public String getRecipientName() {
return recipientName;
}
public String getRecipienttel() {
return recipienttel;
}
}
class Show
{
private ArrayList<Goods> list;
private otherInformation other;
public Show(otherInformation other,ArrayList<Goods> list) {this.other = other;this.list = list;}
public void showOrder(Payment payment)
{
System.out.printf("客户:%s(%s)订单信息如下:\n",other.getCustomerName(),other.getCustomertel());
// 客户:郭靖(13807911234)订单信息如下:
System.out.println("-----------------------------------------");
// -----------------------------------------
System.out.printf("航班号:%s\n",other.getFlightID());
// 航班号:MU1234
System.out.printf("订单号:%d\n", other.getOrderID());
// 订单号:900001
System.out.printf("订单日期:%s\n", other.getOrderDate());
// 订单日期:2025-04-22
System.out.printf("发件人姓名:%s\n",other.getSenderName() );
// 发件人姓名:洪七公
System.out.printf("发件人电话:%s\n", other.getSendertel());
// 发件人电话:18907912325
System.out.printf("发件人地址:%s\n", other.getSenderAddress());
// 发件人地址:南昌大学
System.out.printf("收件人姓名:%s\n", other.getRecipientName());
// 收件人姓名:黄药师
System.out.printf("收件人电话:%s\n", other.getRecipienttel());
// 收件人电话:13607912546
System.out.printf("收件人地址:%s\n", other.getRecipientAddress());
// 收件人地址:北京大学
AgentGoods agent = new AgentGoods();
// 订单总重量(kg):125.0
System.out.printf("订单总重量(kg):%.1f\n",agent.calculateWeight(list));
// 微信支付金额:3350.0
payment.showPayment();
System.out.printf("%.1f\n\n",agent.calculateFee(list));
//
}
public void showgoods()
{
// 货物明细如下:
// -----------------------------------------
// 明细编号 货物名称 计费重量 计费费率 应交运费
// 1 发电机 80.0 25.0 2000.0
// 2 信号发生器 45.0 30.0 1350.0
System.out.println("货物明细如下:");
System.out.println("-----------------------------------------");
System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
for(int i=0;i<list.size();i++)
{
System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n",
i+1, list.get(i).getName(),list.get(i).getChargeableWeight(),list.get(i).getRate(),list.get(i).getFee());
}
}
}
class Goods//货物
{
private int ID;//货物编号
private String name;//货物名称
private int width;//货物宽度
private int length;//货物长度
private int heigth;//货物高度
private int weight;//货物重量
private double chargeableWeight ;//计费重量
private double fee ;//货物费用
private double rate ;//货物费率
public String getName() {
return name;
}
public double getChargeableWeight() {
return chargeableWeight;
}
public double getFee() {
return fee;
}
public double getRate() {
return rate;
}
public Goods() {}
public Goods(int iD, String name, int width, int length, int heigth, int weight)
{
this.ID = iD;
this.name = name;
this.width = width;
this.length = length;
this.heigth = heigth;
this.weight = weight;
this.chargeableWeight = calculateChargeableWeight();
this.rate = calculateRate();
this.fee = calculateFee();
}
public double calculateRate()//计算费率
{
if(chargeableWeight<20)return 35;
else if(chargeableWeight>=20&&chargeableWeight<50)return 30;
else if(chargeableWeight>=50&&chargeableWeight<100)return 25;
else if(chargeableWeight>=100)return 15;
return 0;
}
public double calculateChargeableWeight()//计算计费重量
{
int Vweight = length*width*heigth/6000;
return Vweight>=weight ? Vweight : weight;
}
public double calculateFee()//计算货物费用
{
return rate*chargeableWeight;
}
}
class AgentGoods//管理货物
{
//private ArrayList<Goods> list;
public AgentGoods() {}
//public AgentGoods(ArrayList<Goods> list) {this.list= list;}
public double calculateWeight(ArrayList<Goods> list)//计算总重量
{
double total = 0;
for(int i = 0;i < list.size() ;i++)
{
total+=list.get(i).getChargeableWeight();
}
return total;
}
public double calculateFee(ArrayList<Goods> list)//计算总费用
{
double total = 0;
for(int i = 0;i < list.size() ;i++)
{
total+=list.get(i).getFee();
}
return total;
}
public boolean determineWeight(int maxweight,ArrayList<Goods> list)//判断总重量是否超过航班载重
{
if(calculateWeight(list) > maxweight)
return true;
return false;
}
}
abstract class Payment
{
public abstract void showPayment();
}
class WeChatPay extends Payment
{
public WeChatPay() {}
@Override
public void showPayment()
{
System.out.print("微信支付金额:");
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int customerid = sc.nextInt();//客户编号
sc.nextLine();
String customername = sc.nextLine();//客户姓名
String customertel = sc.nextLine();//客户电话
String customerAddress = sc.nextLine();//客户地址
int count = sc.nextInt();//运送货物数量
ArrayList<Goods> goodslist = new ArrayList<>();
for(int i=0;i<count;i++)
{
int id = sc.nextInt();//货物编号
sc.nextLine();
String name = sc.nextLine();//货物名称
int width = sc.nextInt();//货物宽度
int length = sc.nextInt();//货物长度
int heigth = sc.nextInt();//货物高度
int weight = sc.nextInt();//货物重量
Goods item = new Goods(id,name,width,length,heigth,weight);
goodslist.add(item);
}
sc.nextLine();
String flightID = sc.nextLine();//航班号
String departure = sc.nextLine();//航班起飞机场
String arrival = sc.nextLine();//航班降落机场
String flightDate = sc.nextLine();//航班日期(格式为YYYY-MM-DD)
int maxWeight = sc.nextInt();//最大载重
int orderID = sc.nextInt();//订单编号
sc.nextLine();
String orderDate = sc.nextLine();//订单日期(格式为YYYY-MM-DD)
String senderAddress = sc.nextLine();//发件人地址
String sendername = sc.nextLine();//发件人姓名
String sendertel = sc.nextLine();//发件人电话
String recipientAddress = sc.nextLine();//收件人地址
String recipientname = sc.nextLine();//收件人姓名
String recipienttel = sc.nextLine();//收件人电话
otherInformation other = new otherInformation(customerid,customername,customertel,customerAddress,
flightID,departure,arrival,flightDate,maxWeight,orderID,orderDate,senderAddress,sendername,
sendertel,recipientAddress,recipientname,recipienttel);
Payment pay;
WeChatPay chat = new WeChatPay();
pay = chat;
AgentGoods agentgoods = new AgentGoods();
if(agentgoods.determineWeight(other.getMaxWeight(),goodslist))
{
System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.",other.getFlightID());
return;
}
else
{
Show show = new Show(other,goodslist);
show.showOrder(pay);
show.showgoods();
}
}
}
4.类图:

5.SourceMontor报表:

6.分析:
1.% Comments(注释比例) - 12.6%
2.Avg Complexity(平均复杂度) - 1.07
3.Avg Depth(平均块深度) - 1.68
4.Max Depth(最大块深度) - 3
5.Methods/Class(方法数/类) - 13.33
6.Avg Stints/Method(平均语句数/方法) - 2.60
1.注释比例不足
通过查询得知注释比例应该达到15%-25%。我的代码注释比例严重不足,且最应该注释的计算费率的地方没有注释。因为费率在这里是魔法数字(没有给出得到这个值的理由)。
2.otherinformation类职责不够清晰
通过Methods/Class指标发现平均每个类拥有较多方法。以 otherInformation 类为例,包含大量属性的 getter 和 setter 方法,使得类的方法数量较多。这可能意味着类的职责不够单一,违反了单一职责原则,一个类承担了过多不同类型的功能,不利于代码的维护和扩展。
二、第二次题目集:
1.源码:
点击查看代码
import java.util.*;
class Flight//航班类
{
private String flightID;// 航班编号
private String flightDate;// 航班日期
private String start;// 起飞机场
private String end;// 降落机场
private int maxweight;// 最大载重
public Flight(String flightID, String flightDate, String start, String end, int maxweight)
{
this.flightID = flightID;
this.flightDate = flightDate;
this.start = start;
this.end = end;
this.maxweight = maxweight;
}
public String getFlightID() {return flightID;}
public int getMaxWeight() {return maxweight;}
}
class Order//订单类
{
private String orderDate;// 订单编号
private String orderID;// 订单日期
private String shipperAddress;// 发件人地址
private String shipperName;// 发件人姓名
private String shipperTel;// 发件人电话
private String recipientAddress;// 收件人地址
private String recipientName;// 收件人姓名
private String recipientTel;// 收件人电话
public Order(String orderDate, String orderID, String shipperAddress, String shipperName, String shipperTel,
String recipientAddress, String recipientName, String recipientTel)
{
this.orderDate = orderDate;
this.orderID = orderID;
this.shipperAddress = shipperAddress;
this.shipperName = shipperName;
this.shipperTel = shipperTel;
this.recipientAddress = recipientAddress;
this.recipientName = recipientName;
this.recipientTel = recipientTel;
}
public String getOrderDate() {return orderDate;}
public void setOrderDate(String orderDate) {this.orderDate = orderDate;}
public String getOrderID() {return orderID;}
public void setOrderID(String orderID) {this.orderID = orderID;}
public String getShipperAddress() {return shipperAddress;}
public void setShipperAddress(String shipperAddress) {this.shipperAddress = shipperAddress;}
public String getShipperName() {return shipperName;}
public void setShipperName(String shipperName) {this.shipperName = shipperName;}
public String getShipperTel() {return shipperTel;}
public void setShipperTel(String shipperTel) {this.shipperTel = shipperTel;}
public String getRecipientAddress() {return recipientAddress;}
public void setRecipientAddress(String recipientAddress) {this.recipientAddress = recipientAddress;}
public String getRecipientName() {return recipientName;}
public void setRecipientName(String recipientName) {this.recipientName = recipientName;}
public String getRecipientTel() {return recipientTel;}
public void setRecipientTel(String recipientTel) {this.recipientTel = recipientTel;}
}
abstract class Client//客户类
{
private int ID;// 客户编号
private String name;// 客户姓名
private String tel;// 客户电话
private String address;// 客户地址
public Client(int ID, String name, String tel, String address)
{
this.ID = ID;
this.name = name;
this.tel = tel;
this.address = address;
}
public String getName() {return name;}
public String getTel() {return tel;}
public abstract String getType() ;
public abstract double getDiscount() ;
}
class Individual extends Client// 个人客户
{
private String type = "Individual";// 客户类型
private double discount = 0.9;// 类型折扣
public Individual(int ID, String name, String tel, String address) {super(ID, name, tel, address);}
public String getType() {return type;}
public double getDiscount() {return discount;}
}
class Corporate extends Client// 团体客户
{
private String type = "Corporate";// 客户类型
private double discount = 0.8;// 类型折扣
public Corporate(int ID, String name, String tel, String address) {super(ID, name, tel, address);}
public String getType() {return type;}
public double getDiscount() {return discount;}
}
abstract class Goods implements Calculate//货物类
{
private String type;// 货物类型
private String ID;// 货物编号
private String name;// 货物名称
private double length;// 货物长度
private double width;// 货物宽度
private double height;// 货物高度
protected double weight;// 货物重量
public Goods(String type, String ID, String name, double length, double width, double height, double weight)
{
this.type = type;
this.ID = ID;
this.name = name;
this.length = length;
this.width = width;
this.height = height;
this.weight = weight;
}
public String getName() {return name;}
public double calculate_Volume_Weight()//计算体积重量
{
return length*width*height/6000;
}
public double calculate_Real_Weight()//计算缴费重量
{
if(weight>calculate_Volume_Weight())return weight;
return calculate_Volume_Weight();
}
public double calculate_Real_Fee(double clientDiscount)//打折后
{
double realWeight = calculate_Real_Weight();
double rate = calculateRate();
return clientDiscount * realWeight * rate;
}
public double calculate_Fee()//没打折
{
return calculate_Real_Weight() * calculateRate();
}
abstract public double calculateRate();//计算费率
}
class Common extends Goods//普通货物
{
public Common(String type, String ID, String name, double length, double width, double height, double weight) {
super(type, ID, name, length, width, height, weight);
}
public double calculateRate()
{
if( weight>=0 && weight<20 )return 35.0;
else if(weight>=20 && weight<50)return 30.0;
else if(weight>=50 && weight<100)return 25.0;
else if( weight>=100 )return 15.0;
else return 0.0;
}
}
class danger extends Goods//危险货物
{
public danger(String type, String ID, String name, double length, double width, double height, double weight) {
super(type, ID, name, length, width, height, weight);
}
public double calculateRate()
{
if( weight>=0 && weight<20 )return 80.0;
else if(weight>=20 && weight<50)return 50.0;
else if(weight>=50 && weight<100)return 30.0;
else if( weight>=100 )return 20.0;
else return 0.0;
}
}
class urgent extends Goods//加急货物
{
public urgent(String type, String ID, String name, double length, double width, double height, double weight) {
super(type, ID, name, length, width, height, weight);
}
public double calculateRate()
{
if( weight>=0 && weight<20 )return 60.0;
else if(weight>=20 && weight<50)return 50.0;
else if(weight>=50 && weight<100)return 40.0;
else if( weight>=100 )return 30.0;
else return 0.0;
}
}
interface Calculate//接口
{
double calculateRate();
}
interface InPayment
{
String showPayment();
}
abstract class Payment implements InPayment
{
public Payment() {}
public abstract String showPayment();
}
class Wechat extends Payment
{
@Override
public String showPayment()
{
return "微信";
}
}
class ALiPay extends Payment
{
@Override
public String showPayment()
{
return "支付宝";
}
}
class Cash extends Payment
{
@Override
public String showPayment()
{
return "现金";
}
}
class Goodslist
{
private ArrayList<Goods> list = new ArrayList<>();//货物链表
private Client client;
public Goodslist(ArrayList<Goods> list,Client client) {this.list = list;this.client = client;}
public ArrayList<Goods> getList(){return list;}
public double calculate_total_weight()
{
double total = 0;
for(int i = 0;i<list.size();i++)
{
total += list.get(i).calculate_Real_Weight();
}
return total;
}
public double calculate_tatal_fee()
{
double total = 0;
for(int i = 0;i<list.size();i++)
{
double discount = client.getDiscount();
total += list.get(i).calculate_Real_Fee(discount);
}
return total;
}
}
class Information
{
private Goodslist list ;//货物链表
private Flight flightInformation;//航班信息
private Order orderInformation;//订单信息
private Client clientInformation;//客户信息
private Payment payment;//支付方式
public Information(Goodslist list, Flight flightInformation, Order orderInformation,
Client clientInformation, Payment payment)
{
this.list = list;
this.flightInformation = flightInformation;
this.orderInformation = orderInformation;
this.clientInformation = clientInformation;
this.payment = payment;
}
public void showInformation()
{
if(list.calculate_total_weight()<=flightInformation.getMaxWeight())
{
System.out.printf("客户:%s(%s)订单信息如下:\n",clientInformation.getName(),clientInformation.getTel());
System.out.println("-----------------------------------------");
System.out.printf("航班号:%s\n",flightInformation.getFlightID());
System.out.printf("订单号:%s\n",orderInformation.getOrderID());
System.out.printf("订单日期:%s\n",orderInformation.getOrderDate());
System.out.printf("发件人姓名:%s\n",orderInformation.getShipperName());
System.out.printf("发件人电话:%s\n",orderInformation.getShipperTel());
System.out.printf("发件人地址:%s\n",orderInformation.getShipperAddress());
System.out.printf("收件人姓名:%s\n",orderInformation.getRecipientName());
System.out.printf("收件人电话:%s\n",orderInformation.getRecipientTel());
System.out.printf("收件人地址:%s\n",orderInformation.getRecipientAddress());
System.out.printf("订单总重量(kg):%.1f\n",list.calculate_total_weight());
System.out.printf("%s支付金额:%.1f\n\n",payment.showPayment(),list.calculate_tatal_fee() );
System.out.println("货物明细如下:");
System.out.println("-----------------------------------------");
System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
for(int i=0;i<list.getList().size();i++)
{
double clientDiscount = clientInformation.getDiscount();
System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n",i+1,list.getList().get(i).getName(),
list.getList().get(i).calculate_Real_Weight(),list.getList().get(i).calculateRate(),list.getList().get(i).calculate_Fee());
}
}
else showError();
}
public void showError()
{
System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.\n",flightInformation.getFlightID());
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String customertype = sc.nextLine();//客户类型
int customerid = sc.nextInt();//客户编号
sc.nextLine();
String customername = sc.nextLine();//客户姓名
String customertel = sc.nextLine();//客户电话
String customerAddress = sc.nextLine();//客户地址
Client client = null;
if("Individual".equals(customertype))
client = new Individual(customerid,customername,customertel,customerAddress);
else if("Corporate".equals(customertype))
client = new Corporate(customerid,customername,customertel,customerAddress);
ArrayList<Goods> list = new ArrayList<>();
String type = sc.nextLine();
int count = sc.nextInt();//运送货物数量
for(int i=0;i<count;i++)
{
sc.nextLine();
String id = sc.nextLine();//货物编号
String name = sc.nextLine();//货物名称
double width = sc.nextDouble();//货物宽度
double length = sc.nextDouble();//货物长度
double height = sc.nextDouble();//货物高度
double weight = sc.nextDouble();//货物重量
switch(type)
{
case "Normal":
list.add(new Common(type,id,name,length,width,height,weight));
break;
case "Expedite":
list.add(new urgent(type,id,name,length,width,height,weight));
break;
case "Dangerous":
list.add(new danger(type,id,name,length,width,height,weight));
break;
}
}
Goodslist goodslist = new Goodslist(list,client);
sc.nextLine();
String flightID = sc.nextLine();//航班号
String departure = sc.nextLine();//航班起飞机场
String arrival = sc.nextLine();//航班降落机场
String flightDate = sc.nextLine();//航班日期(格式为YYYY-MM-DD)
int maxWeight = sc.nextInt();//最大载重
Flight flight = new Flight(flightID,flightDate,departure,arrival,maxWeight);
sc.nextLine();
String orderID = sc.nextLine();//订单编号
String orderDate = sc.nextLine();//订单日期(格式为YYYY-MM-DD)
String senderAddress = sc.nextLine();//发件人地址
String sendername = sc.nextLine();//发件人姓名
String sendertel = sc.nextLine();//发件人电话
String recipientAddress = sc.nextLine();//收件人地址
String recipientname = sc.nextLine();//收件人姓名
String recipienttel = sc.nextLine();//收件人电话
Order order = new Order(orderDate,orderID,senderAddress,sendername,sendertel,recipientAddress,recipientname,recipienttel);
String pay = sc.nextLine();
Payment payment = null;
switch(pay)
{
case "Wechat":
payment = new Wechat();
break;
case "ALiPay":
payment = new ALiPay();
break;
case "Cash":
payment = new Cash();
break;
}
Information information = new Information(goodslist,flight,order,client,payment);
information.showInformation();
sc.close();
}
}
2.类图:

3.SourceMontor报表:

4.分析:
1.% Comments(注释比例) - 14.6%
2.Avg Complexity(平均复杂度) - 2.73
3.Avg Depth(平均块深度) - 1.22
4.Max Depth(最大块深度) - 5
5.Methods/Class(方法数/类) - 6
6.Avg Stints/Method(平均语句数/方法) - 2.72
7.Max Complexity (最大复杂度) - 12
1.注释比例不足
解决方法是在类头部添加简单说明,如:
点击查看代码
/**
* 表示航班信息的类
* 包含航班编号、日期、起降机场等属性
*/
class Flight {
// 现有代码
}
2.最大复杂度过大
通过图片可以看到复杂度最高的方法为Main.calculateRate()。且复杂度为12,经过简单修改后可将复杂度降至5。
点击查看代码
//修改前,复杂度为12
public double calculateRate() {
if (weight >= 0 && weight < 20) return 35.0;//条件1
else if (weight >= 20 && weight < 50) return 30.0;//条件2
else if (weight >= 50 && weight < 100) return 25.0;//条件3
else if (weight >= 100) return 15.0;//条件4
else return 0.0;//默认情况
}
//修改后,复杂度为5
public double calculateRate() {
double rate = 0.0; // 默认情况
if(weight < 20) rate = 35.0;//条件1
else if(weight < 50) rate = 30.0;//条件2
else if(weight < 100) rate = 25.0;//条件3
else rate = 15.0; //条件4
return rate;
}
三、踩坑心得
1、第一次题目集将所有除货物外的信息整合在一个类中,虽然看似方便了本次作业,但实则违反了单一职责原则。同时也导致进行第二次作业时无法直接运用第一次作业中的类,进行重复造轮子浪费了时间。
2、本次作业的输入文本量大,类之间的关系较复杂,在写代码的时候常常需要停下来梳理类之间的关系,同时由于本人不会绘制类图,类图都是由plantUML自动生成的,所以对类之间的关系就更加不明所以。在下轮学习中,应当在写代码之前就对整体设计有所把握,边写边改耗时耗力还容易把自己绕晕。
四、改进建议
1.添加注释
两次分析都出现了注释覆盖率不足的问题,包括上一轮的学习也出现了这个问题。可能在我看来不需要添加注释的地方,为了让别人看懂我的代码,需要添加注释。这也能提高代码的可读性。
2.重视复杂度
通过这轮学习我发现if-else语句增加复杂度非常快,可能我为了在编写程序时思路更加清晰会下意识选择复杂度更高的写法。这可能就是程序员们常说的屎山代码吧。这是不正确的,代码的平均复杂度最好应该在0-5之间。
五、总结
本轮学习中很好的改进了上一轮学习中暴露出来的问题。例如拖拉的问题。本轮学习中所有任务均提前完成,效率和质量大大提升。对于单一职责的理解也在本轮学习中进一步加强。希望这些优点能在下轮学习中继续保持。

浙公网安备 33010602011771号