题目集8~9总结blog
(1)前言
1.本次题目集中的题目综合考查了 Java 面向对象编程的核心概念(继承、多态、封装)、集合框架的使用、输入输出处理以及业务逻辑设计。
2.本次题目集主要考察了继承以及接口等内容。题量适中,整体题目由简入繁,很适合新手对所学知识点掌握与了解。
(2)设计与分析
1.源码
点击查看代码
package pta;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 读取客户信息(处理含空格的地址)
String customerType = input.next();
String customerID = input.next();
String customerName = input.next();
String customerPhone = input.next();
input.nextLine(); // 消耗剩余换行符
String customerAddress = input.nextLine();
Custom custom;
if (customerType.equals("Individual")) {
custom = new IndividualCustom(customerID, customerName, customerPhone, customerAddress);
} else {
custom = new CorporateCustom(customerID, customerName, customerPhone, customerAddress);
}
// 读取货物信息
String cargoType = input.next();
int cargosNumber = input.nextInt();
List<Cargo> cargoList = new ArrayList<>();
for (int i = 0; i < cargosNumber; i++) {
String cargoID = input.next();
String cargoName = input.next();
int width = input.nextInt();
int length = input.nextInt();
int height = input.nextInt();
int weight = input.nextInt();
Cargo cargo;
if (cargoType.equals("Normal")) {
cargo = new NormalCargo(cargoID, cargoName, width, length, height, weight);
} else if (cargoType.equals("Expedite")) {
cargo = new ExpediteCargo(cargoID, cargoName, width, length, height, weight);
} else {
cargo = new DangerousCargo(cargoID, cargoName, width, length, height, weight);
}
cargoList.add(cargo);
}
// 读取航班信息(处理含横线的日期)
String flightNumber = input.next();
String departureAirport = input.next();
String arrivalAirport = input.next();
input.nextLine(); // 消耗剩余换行符
String flightDate = input.nextLine();
double maxLoad = input.nextDouble();
double currentLoad = 0;
Flight flight = new Flight(flightNumber, departureAirport, arrivalAirport, flightDate, maxLoad, currentLoad);
// 读取订单信息(处理含空格的姓名和地址)
String orderID = input.next();
String orderDate = input.next();
input.nextLine(); // 消耗剩余换行符
String setName = input.nextLine(); // 发件人姓名(含空格)
String setPhone = input.next();
input.nextLine(); // 消耗剩余换行符
String setAddress = input.nextLine(); // 发件人地址(含空格)
String getName = input.nextLine(); // 收件人姓名(含空格)
String getPhone = input.next();
input.nextLine(); // 消耗剩余换行符
String getAddress = input.nextLine(); // 收件人地址(含空格)
String payWay = input.next();
// 创建订单(注意构造参数顺序)
Order order = new Order(
orderID,
orderDate,
setName,
setPhone,
setAddress,
getName,
getPhone,
getAddress,
payWay,
custom,
flight,
cargoList
);
// 检查航班载重
if (!flight.canCarry(order.getTotalWeight())) {
System.out.println("The flight with flight number:" + flight.getFlightNumber() + " has exceeded its load capacity and cannot carry the order. ");
return;
}
// 输出订单信息
System.out.println("客户:" + custom.getName() + "(" + custom.getPhone() + ")" + "订单信息如下:");
System.out.println("-----------------------------------------");
System.out.println("航班号:" + flight.getFlightNumber());
System.out.println("订单号:" + order.getOrderID());
System.out.println("订单日期:" + order.getDate());
System.out.println("发件人姓名:" + order.getSetName());
System.out.println("发件人电话:" + order.getSetPhone());
System.out.println("发件人地址:" + order.getSetAddress());
System.out.println("收件人姓名:" + order.getGetName());
System.out.println("收件人电话:" + order.getGetPhone());
System.out.println("收件人地址:" + order.getGetAddress());
System.out.println("订单总重量(kg):" + String.format("%.1f", order.getTotalWeight()));
// 输出支付信息
String payment = order.getPayWay();
if (payment.equals("ALiPay")) {
System.out.println("支付宝支付金额:" + String.format("%.1f", order.getTotalFees()));
} else if (payment.equals("Wechat")) {
System.out.println("微信支付金额:" + String.format("%.1f", order.getTotalFees()));
} else if (payment.equals("Cash")) {
System.out.println("现金支付金额:" + String.format("%.1f", order.getTotalFees()));
}
// 输出货物明细
System.out.println("\n货物明细如下:");
System.out.println("-----------------------------------------");
System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
for (int i = 0; i < order.getCargoList().size(); i++) {
Cargo cargo = order.getCargoList().get(i);
double weight = cargo.getTrueWeight();
double rate = cargo.getRate();
double fee = weight * rate * custom.getDiscountRate();
System.out.println((i + 1) + "\t" + cargo.getName() + "\t" + String.format("%.1f", weight) + "\t\t" + String.format("%.2f", rate) + "\t\t" + String.format("%.1f", fee));
}
}
}
// 客户抽象类
abstract class Custom {
private String customerID;
private String name;
private String phone;
private String address;
public Custom() {}
public Custom(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 String getName() { return name; }
public String getPhone() { return phone; }
public String getAddress() { return address; }
public abstract double getDiscountRate();
}
// 个人客户
class IndividualCustom extends Custom {
public IndividualCustom(String customerID, String name, String phone, String address) {
super(customerID, name, phone, address);
}
@Override
public double getDiscountRate() {
return 0.9;
}
}
// 企业客户
class CorporateCustom extends Custom {
public CorporateCustom(String customerID, String name, String phone, String address) {
super(customerID, name, phone, address);
}
@Override
public double getDiscountRate() {
return 0.8;
}
}
// 货物抽象类
abstract class Cargo {
private String cargoID;
private String name;
private int width;
private int length;
private int height;
private int weight;
public Cargo() {}
public Cargo(String cargoID, String name, int width, int length, int height, int weight) {
this.cargoID = cargoID;
this.name = name;
this.width = width;
this.length = length;
this.height = height;
this.weight = weight;
}
public String getCargoID() { return cargoID; }
public String getName() { return name; }
public int getWidth() { return width; }
public int getLength() { return length; }
public int getHeight() { return height; }
public int getWeight() { return weight; }
public double getVolumeWeight() {
return (width * height * length) / 6000.0;
}
public abstract double getRate();
public abstract double getTrueWeight();
}
// 普通货物
class NormalCargo extends Cargo {
public NormalCargo(String cargoID, String name, int width, int length, int height, int weight) {
super(cargoID, name, width, length, height, weight);
}
@Override
public double getRate() {
double weight = getTrueWeight();
if (weight < 20) return 0.35;
else if (weight < 50) return 0.30;
else if (weight < 100) return 0.25;
else return 15; // 可能此处应为0.15(原代码可能存在笔误)
}
@Override
public double getTrueWeight() {
double volumeWeight = getVolumeWeight();
return volumeWeight > getWeight() ? volumeWeight : getWeight();
}
}
// 加急货物
class ExpediteCargo extends Cargo {
public ExpediteCargo(String cargoID, String name, int width, int length, int height, int weight) {
super(cargoID, name, width, length, height, weight);
}
@Override
public double getRate() {
double weight = getTrueWeight();
if (weight < 20) return 0.60;
else if (weight < 50) return 0.50;
else if (weight < 100) return 0.40;
else return 0.30;
}
@Override
public double getTrueWeight() {
double volumeWeight = getVolumeWeight();
return volumeWeight > getWeight() ? volumeWeight : getWeight();
}
}
// 危险货物
class DangerousCargo extends Cargo {
public DangerousCargo(String cargoID, String name, int width, int length, int height, int weight) {
super(cargoID, name, width, length, height, weight);
}
@Override
public double getRate() {
double weight = getTrueWeight();
if (weight < 20) return 0.80;
else if (weight < 50) return 0.50;
else if (weight < 100) return 0.30;
else return 0.20;
}
@Override
public double getTrueWeight() {
double volumeWeight = getVolumeWeight();
return volumeWeight > getWeight() ? volumeWeight : getWeight();
}
}
// 航班类
class Flight {
private String flightNumber;
private String departureAirport;
private String arriveAirport;
private String date;
private double maxLoad;
private double currentLoad;
public Flight() {}
public Flight(String flightNumber, String departureAirport, String arriveAirport, String date, double maxLoad, double currentLoad) {
this.flightNumber = flightNumber;
this.departureAirport = departureAirport;
this.arriveAirport = arriveAirport;
this.date = date;
this.maxLoad = maxLoad;
this.currentLoad = currentLoad;
}
public String getFlightNumber() { return flightNumber; }
public double getMaxLoad() { return maxLoad; }
public double getCurrentLoad() { return currentLoad; }
public boolean canCarry(double weight) {
return maxLoad - currentLoad >= weight;
}
public void updateCurrentLoad(double weight) {
currentLoad += weight;
}
}
// 订单类
class Order {
private String orderID;
private String date;
private String setName;
private String setPhone;
private String setAddress;
private String getName;
private String getPhone;
private String getAddress;
private String payWay;
private double totalWeight;
private double totalFees;
private Custom custom;
private Flight flight;
private List<Cargo> cargoList;
public Order(String orderID, String date, String setName, String setPhone, String setAddress,
String getName, String getPhone, String getAddress, String payWay,
Custom custom, Flight flight, List<Cargo> cargoList) {
this.orderID = orderID;
this.date = date;
this.setName = setName;
this.setPhone = setPhone;
this.setAddress = setAddress;
this.getName = getName;
this.getPhone = getPhone;
this.getAddress = getAddress;
this.payWay = payWay;
this.custom = custom;
this.flight = flight;
this.cargoList = cargoList;
calculateOrderDetails(); // 自动计算总重量和费用
}
private void calculateOrderDetails() {
totalWeight = 0;
totalFees = 0;
for (Cargo cargo : cargoList) {
double weight = cargo.getTrueWeight();
double rate = cargo.getRate();
totalWeight += weight;
totalFees += weight * rate;
}
totalFees *= custom.getDiscountRate(); // 应用客户折扣
}
public String getOrderID() { return orderID; }
public String getDate() { return date; }
public String getSetName() { return setName; }
public String getSetPhone() { return setPhone; }
public String getSetAddress() { return setAddress; }
public String getGetName() { return getName; }
public String getGetPhone() { return getPhone; }
public String getGetAddress() { return getAddress; }
public String getPayWay() { return payWay; }
public double getTotalWeight() { return totalWeight; }
public double getTotalFees() { return totalFees; }
public List<Cargo> getCargoList() { return cargoList; }
}
2.分析结果
![image]()
3.类图
![image]()
根据分析结果显示,我的代码有374行代码,68个语句,分支语句占11.8%,方法调用语句有51条,注释行占4.5%。类和接口数量为1,每个类的方法数为1,平均每个方法有49个语句,最复杂方法的行号为7,复杂度为7,最深代码块的行号为39,最大代码块深度为4,平均代码块深度为1.08。
根据分析结果表明我的代码的复杂度较高,特别是main方法,其复杂度达到了7,这可能意味着该方法承担了过多的职责,需要进一步拆分以提高代码的可维护性和可读性。此外,代码中注释的比例较低,建议增加注释以提高代码的可读性。这段代码虽然实现了基本的货运订单处理功能,但在代码结构和注释方面还有改进空间。通过重构和增加注释,可以进一步提高代码的质量和可维护性。
迭代
1.源码
点击查看代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 读取客户信息
String customerID = input.nextLine().trim();
String name = input.nextLine().trim();
String phone = input.nextLine().trim();
String address = input.nextLine().trim();
Customer customer = new Customer(customerID, name, phone, address);
// 读取货物信息
int cargoCount = Integer.parseInt(input.nextLine().trim());
List<Cargo> cargos = new ArrayList<>();
for (int i = 0; i < cargoCount; i++) {
String cargoID = input.nextLine().trim();
String cargoName = input.nextLine().trim();
double width = Double.parseDouble(input.nextLine().trim());
double length = Double.parseDouble(input.nextLine().trim());
double height = Double.parseDouble(input.nextLine().trim());
double weight = Double.parseDouble(input.nextLine().trim());
Cargo cargo = new Cargo(cargoID, cargoName, width, length, height, weight);
cargos.add(cargo);
}
// 读取航班信息
String flightNumber = input.nextLine().trim();
String departureAirport = input.nextLine().trim();
String arrivalAirport = input.nextLine().trim();
String date = input.nextLine().trim();
double maxLoad = Double.parseDouble(input.nextLine().trim());
Flight flight = new Flight(flightNumber, departureAirport, arrivalAirport, date, maxLoad);
// 读取订单信息
String orderID = input.nextLine().trim();
String orderDate = input.nextLine().trim();
String senderAddress = input.nextLine().trim();
String senderName = input.nextLine().trim();
String senderPhone = input.nextLine().trim();
String receiverAddress = input.nextLine().trim();
String receiverName = input.nextLine().trim();
String receiverPhone = input.nextLine().trim();
// 创建订单对象
Order order = new Order(orderID, orderDate, senderAddress, senderName, senderPhone,
receiverAddress, receiverName, receiverPhone, flight, cargos);
// 检查航班是否能承载货物
if (!flight.canCarry(order.getTotalWeight())) {
System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.\n", flight.getFlightNumber());
return;
}
// 输出订单信息
System.out.printf("客户:%s(%s)订单信息如下:\n", customer.getName(), customer.getPhone());
System.out.println("-----------------------------------------");
System.out.printf("航班号:%s\n", order.getFlight().getFlightNumber());
System.out.printf("订单号:%s\n", order.getOrderID());
System.out.printf("订单日期:%s\n", order.getOrderDate());
System.out.printf("发件人姓名:%s\n", order.getSenderName());
System.out.printf("发件人电话:%s\n", order.getSenderPhone());
System.out.printf("发件人地址:%s\n", order.getSenderAddress());
System.out.printf("收件人姓名:%s\n", order.getReceiverName());
System.out.printf("收件人电话:%s\n", order.getReceiverPhone());
System.out.printf("收件人地址:%s\n", order.getReceiverAddress());
System.out.printf("订单总重量(kg):%.1f\n", order.getTotalWeight());
System.out.printf("微信支付金额:%.1f\n", order.getTotalFreight());
// 输出货物明细
System.out.println("\n货物明细如下:");
System.out.println("-----------------------------------------");
System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
for (int i = 0; i < cargos.size(); i++) {
Cargo cargo = cargos.get(i);
System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f\n", i + 1, cargo.getName(), cargo.getChargeableWeight(), cargo.getRate(), cargo.getFreight());
}
input.close();
}
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public void setName(String name) {
this.name = name;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
}
class Cargo {
private String cargoID;
private String name;
private double width;
private double length;
private double height;
private double weight;
public Cargo() {
}
public Cargo(String cargoID, String name, double width, double length, double height, double weight) {
this.cargoID = cargoID;
this.name = name;
this.width = width;
this.length = length;
this.height = height;
this.weight = weight;
}
public String getCargoID() {
return cargoID;
}
public void setCargoID(String cargoID) {
this.cargoID = cargoID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public double getWeight() {
return weight;
}
public double getVolumeWeight() {
return (width * length * height) / 6000;
}
public double getChargeableWeight() {
return Math.max(weight, getVolumeWeight());
}
public double getRate() {
double chargeableWeight = getChargeableWeight();
if (chargeableWeight < 20) {
return 35.0;
} else if (chargeableWeight < 50) {
return 30.0;
} else if (chargeableWeight < 100) {
return 25.0;
} else {
return 15.0;
}
}
public double getFreight() {
return getChargeableWeight() * getRate();
}
}
class Flight {
private String flightNumber;
private String departureAirport;
private String arrivalAirport;
private String date;
private double maxLoad;
private double currentLoad;
public Flight(String flightNumber, String departureAirport, String arrivalAirport, String date, double maxLoad) {
this.flightNumber = flightNumber;
this.departureAirport = departureAirport;
this.arrivalAirport = arrivalAirport;
this.date = date;
this.maxLoad = maxLoad;
this.currentLoad = 0;
}
public String getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(String flightNumber) {
this.flightNumber = flightNumber;
}
public void setDepartureAirport(String departureAirport) {
this.departureAirport = departureAirport;
}
public String getDepartureAirport() {
return departureAirport;
}
public String getArrivalAirport() {
return arrivalAirport;
}
public void setArrivalAirport(String arrivalAirport) {
this.arrivalAirport = arrivalAirport;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public double getMaxLoad() {
return maxLoad;
}
public void setMaxLoad(double maxLoad) {
this.maxLoad = maxLoad;
}
public double getCurrentLoad() {
return currentLoad;
}
public void setCurrentLoad(double currentLoad) {
this.currentLoad = currentLoad;
}
public boolean canCarry(double totalWeight) {
return currentLoad + totalWeight <= maxLoad;
}
public void addLoad(double totalWeight) {
currentLoad += totalWeight;
}
}
class Order {
private String orderID;
private String orderDate;
private String senderAddress;
private String senderName;
private String senderPhone;
private String receiverAddress;
private String receiverName;
private String receiverPhone;
private Flight flight;
private List<Cargo> cargos;
private double totalWeight;
private double totalFreight;
public Order(String orderID, String orderDate, String senderAddress, String senderName, String senderPhone,
String receiverAddress, String receiverName, String receiverPhone, Flight flight, List<Cargo> cargos) {
this.orderID = orderID;
this.orderDate = orderDate;
this.senderAddress = senderAddress;
this.senderName = senderName;
this.senderPhone = senderPhone;
this.receiverAddress = receiverAddress;
this.receiverName = receiverName;
this.receiverPhone = receiverPhone;
this.flight = flight;
this.cargos = cargos;
this.totalWeight = 0;
this.totalFreight = 0;
for (Cargo cargo : cargos) {
totalWeight += cargo.getChargeableWeight();
totalFreight += cargo.getFreight();
}
}
public Order() {
}
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 getSenderAddress() {
return senderAddress;
}
public void setSenderAddress(String senderAddress) {
this.senderAddress = senderAddress;
}
public String getSenderName() {
return senderName;
}
public void setSenderName(String senderName) {
this.senderName = senderName;
}
public String getSenderPhone() {
return senderPhone;
}
public void setSenderPhone(String senderPhone) {
this.senderPhone = senderPhone;
}
public String getReceiverAddress() {
return receiverAddress;
}
public void setReceiverAddress(String receiverAddress) {
this.receiverAddress = receiverAddress;
}
public String getReceiverName() {
return receiverName;
}
public void setReceiverName(String receiverName) {
this.receiverName = receiverName;
}
public String getReceiverPhone() {
return receiverPhone;
}
public void setReceiverPhone(String receiverPhone) {
this.receiverPhone = receiverPhone;
}
public Flight getFlight() {
return flight;
}
public void setFlight(Flight flight) {
this.flight = flight;
}
public double getTotalWeight() {
return totalWeight;
}
public void setTotalWeight(double totalWeight) {
this.totalWeight = totalWeight;
}
public double getTotalFreight() {
return totalFreight;
}
public void setTotalFreight(double totalFreight) {
this.totalFreight = totalFreight;
}
public List<Cargo> getCargos() {
return cargos;
}
public void setCargos(List<Cargo> cargos) {
this.cargos = cargos;
}
}
2分析结果
![image]()
3.类图
![image]()
从提供的度量分析结果来看,我的代码有374行代码,68个语句,分支语句占11.8%,方法调用语句有51条,注释行占4.5%。类和接口数量为1,每个类的方法数为1,平均每个方法有49个语句,最复杂方法的行号为7,复杂度为7,最深代码块的行号为39,最大代码块深度为4,平均代码块深度为1.08。
Main方法的复杂度较高,可能需要进一步拆分以提高代码的可维护性和可读性。而且代码中注释的比例较低,建议增加注释以提高代码的可读性。在异常处理、代码注释和优化方面还有改进空间,总体来说,我的代码虽然实现了航空货运订单的基本处理功能,但在异常处理、代码注释和优化方面还有改进空间。
(3)踩坑心得
1. 一开始接触这道题目的时候,被题目的复杂程度所难道,不知道如何分析题目要求,对类的设计没有头绪。后面在网上查找资料,才对题目有了初步认识,后面在类与类之间的关系及调用方面出现困难。导致运行结果出现答案错误。最后在主方法设计的时候,输入格式的不对导致输入数据不匹配我所定义的变量类型,出现错误。
2.面对第二次迭代设计时,前面的问题基本都解决了。但是在写子类方法的时候总是会因为传参出现问题(super方法)。而且因为不仔细的原因,第一次题目出现的输入数据与变量类型不匹配的问题再次出现。
3.然后在编写代码的时候没养成添加注释的习惯,导致很容易模糊类与类之间的关系及方法的调用。
(4)改进建议
1.增加注释:两次代码都存在缺乏注释的问题,建议为每个类、方法和复杂逻辑添加详细的注释,解释代码的功能和设计思路,提高代码的可读性和可维护性。
2.优化复杂方法:对于复杂度较高的方法,如Main.main()和OrderItem.getRate(),建议进行重构,分解为多个小方法,降低方法的复杂度,提高代码的可读性和可测试性。
3.完善输入验证:在读取输入数据时,增加对输入数据的合法性验证,确保输入数据的完整性和正确性,避免因非法输入导致的异常或错误结果。
(5)总结
在完成题目集8至9的过程中,我经历了一次全面的Java面向对象编程实践,这不仅加深了我对继承、多态、封装等核心概念的理解,也锻炼了我的集合框架操作和业务逻辑设计能力。通过分析和迭代,我意识到代码注释对于提高代码可读性和可维护性的重要性,同时也发现main方法的复杂度过高,需要拆分以简化逻辑。在解决问题的过程中,我学会了如何更有效地设计类结构,理解了类与类之间的正确调用关系,并意识到了输入验证的必要性。这些经验对于避免程序因非法输入而产生异常至关重要。总结这次编程练习,我认识到了编写高质量代码的重要性,这包括详细注释、优化复杂方法和完善输入验证。这些经验将有助于我在未来的开发工作中编写出更加健壮和易于维护的代码,同时也提升了我解决实际问题的能力。
发表于
2025-05-25 16:13
云潮
阅读( 16)
评论()
收藏
举报
|