航班订单迭代
在第十一周、十二周的作业中。我们写了订单类迭代。这次的题目相对简单。但四大原则着实困扰了我很久,不多说上实例。
第一道题目如下:
航空快递以速度快、安全性高成为急件或贵重物品的首选。本题目要求对航空货运管理系统进行类设计,具体说明参看说明文件。
OO第九周作业题目说明.pdf
输入格式:
按如下顺序分别输入客户信息、货物信息、航班信息以及订单信息。
客户编号
客户姓名
客户电话
客户地址
运送货物数量
[货物编号
货物名称
货物宽度
货物长度
货物高度
货物重量
]//[]内的内容输入次数取决于“运送货物数量”,输入不包含“[]”
航班号
航班起飞机场
航班降落机场
航班日期(格式为YYYY-MM-DD)
航班最大载重量
订单编号
订单日期(格式为YYYY-MM-DD)
发件人地址
发件人姓名
发件人电话
收件人地址
收件人姓名
收件人电话
输出格式:
如果订单中货物重量超过航班剩余载重量,程序输出The flight with flight number:航班号 has exceeded its load capacity and cannot carry the order. ,程序终止运行。
如果航班载重量可以承接该订单,输出如下:
客户:姓名(电话)订单信息如下:
航班号:
订单号:
订单日期:
发件人姓名:
发件人电话:
发件人地址:
收件人姓名:
收件人电话:
收件人地址:
订单总重量(kg):
微信支付金额:
货物明细如下:
明细编号 货物名称 计费重量 计费费率 应交运费
1 ...
2 ...
注:输出中实型数均保留1位小数。
输入样例:
在这里给出一组输入。例如:
10001
郭靖
13807911234
南昌航空大学
2
101
发电机
80
60
40
80
102
信号发生器
55
70
60
45
MU1234
昌北国际机场
大兴国际机场
2025-04-22
1000
900001
2025-04-22
南昌大学
洪七公
18907912325
北京大学
黄药师
13607912546
输出样例:
在这里给出相应的输出。例如:
客户:郭靖(13807911234)订单信息如下:
航班号:MU1234
订单号:900001
订单日期:2025-04-22
发件人姓名:洪七公
发件人电话:18907912325
发件人地址:南昌大学
收件人姓名:黄药师
收件人电话:13607912546
收件人地址:北京大学
订单总重量(kg):125.0
微信支付金额:3350.0
货物明细如下:
明细编号 货物名称 计费重量 计费费率 应交运费
1 发电机 80.0 25.0 2000.0
2 信号发生器 45.0 30.0 1350.0
这道题主要考查的知识点如下:
1、容器类的使用
2、合理规划各个类的关系
看起来很简单是吧。但这道题因为我的独断专行,导致我最后一天才完成,应为我没有看群里的费率计算方式。多么大的坑呐!!!
以下是这次题的代码:
点击查看代码
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.InputMismatchException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String clientId = sc.next();
String clientName = sc.next();
String clientPhone = sc.next();
String clientAddress = sc.next();
Client client = new Client(clientId, clientName, clientPhone, clientAddress);
int cargoCount = 0;
try {
cargoCount = sc.nextInt();
if (cargoCount <= 0) {
System.out.println("Wrong Format");
System.exit(0);
}
} catch (InputMismatchException e) {
System.out.println("Wrong Format");
System.exit(0);
}
List<Cargo> cargoList = new ArrayList<>();
for (int i = 0; i < cargoCount; ) {
try {
String cargoId = sc.next();
String cargoName = sc.next();
double width = sc.nextDouble();
double length = sc.nextDouble();
double height = sc.nextDouble();
double weight = sc.nextDouble();
if (width <= 0 || length <= 0 || height <= 0 || weight <= 0) {
System.out.println("Wrong Format");
System.exit(0);
}
Cargo cargo = new Cargo(cargoName, weight, height, length, width, cargoId);
cargoList.add(cargo);
i++;
} catch (InputMismatchException e) {
System.out.println("Wrong Format");
System.exit(0);
}
}
String flightId = sc.next();
String departure = sc.next();
String destination = sc.next();
String flightDate = sc.next();
double maxWeight = 0;
try {
maxWeight = sc.nextDouble();
if (maxWeight <= 0) {
System.out.println("Wrong Format");
System.exit(0);
}
} catch (InputMismatchException e) {
System.out.println("Wrong Format");
System.exit(0);
}
Flight flight = new Flight(departure, destination, flightDate, maxWeight, flightId);
String orderId = sc.next();
String orderDate = sc.next();
Order order = new Order(orderId, orderDate);
String senderAddress = sc.next();
String senderName = sc.next();
String senderPhone = sc.next();
Sender sender = new Sender(new User(senderAddress, senderName, senderPhone));
String recipientName = sc.next();
String recipientPhone = sc.next();
String recipientAddress = sc.next();
Recipient recipient = new Recipient(new User(recipientName, recipientPhone, recipientAddress));
double totalWeight = 0;
for (Cargo cargo : cargoList) {
totalWeight += cargo.getFinalWeight();
}
if (totalWeight > flight.getMaxWeight()) {
System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.", flight.getId());
System.exit(0);
}
double totalCost = 0;
for (Cargo cargo : cargoList) {
totalCost += cargo.getRate() * cargo.getFinalWeight();
}
System.out.printf("客户:%s(%s)订单信息如下:%n", client.getName(), client.getPhoneNumber());
System.out.println("-----------------------------------------");
System.out.println("航班号:" + flight.getId());
System.out.println("订单号:" + order.getId());
System.out.println("订单日期:" + order.getOrderDate());
sender.display();
recipient.display();
System.out.println("订单总重量(kg):" + String.format("%.1f", totalWeight));
System.out.println("微信支付金额:" + String.format("%.1f", totalCost));
System.out.println("\n货物明细如下:");
System.out.println("-----------------------------------------");
System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
for (int i = 0; i < cargoList.size(); i++) {
Cargo cargo = cargoList.get(i);
if(i!=cargoList.size()-1)
{
System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f%n",i + 1,cargo.getName(),cargo.getFinalWeight(),cargo.getRate(),cargo.getRate() * cargo.getFinalWeight());
}
else {
System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f",i + 1,cargo.getName(),cargo.getFinalWeight(),cargo.getRate(),cargo.getRate() * cargo.getFinalWeight());
}
}
}
}
class Control {
private AllCargo allCargo;
private Client client;
private Flight flight;
private Order order;
private Sender sender;
private Recipient recipient;
public Control(AllCargo allCargo, Client client, Flight flight,
Order order, Sender sender, Recipient recipient) {
this.allCargo = allCargo;
this.client = client;
this.flight = flight;
this.order = order;
this.sender = sender;
this.recipient = recipient;
}
public AllCargo getAllCargo() {
return allCargo;
}
public Client getClient() {
return client;
}
public Flight getFlight() {
return flight;
}
public Order getOrder() {
return order;
}
public Sender getSender() {
return sender;
}
public Recipient getRecipient() {
return recipient;
}
}
class AllCargo {
private List<Cargo> cargoList;
public AllCargo(List<Cargo> cargoList) {
this.cargoList = cargoList;
}
public List<Cargo> getCargoList() {
return cargoList;
}
public void setCargoList(List<Cargo> cargoList) {
this.cargoList = cargoList;
}
}
class Cargo {
private String id;
private String name;
private double grossWeight;
private double height;
private double length;
private double width;
public Cargo(String name, double grossWeight, double height,
double length, double width, String id) {
this.name = name;
this.grossWeight = grossWeight;
this.height = height;
this.length = length;
this.width = width;
this.id = id;
}
public double getFinalWeight() {
double volumWeight = (length * height * width) / 6000;
return Math.max(volumWeight, grossWeight);
}
public double getRate() {
if(getFinalWeight()<20)
{
return 35.0;
}
if(getFinalWeight()>=20&&getFinalWeight()<50)
{
return 30.0;
}
if(getFinalWeight()>=50&&getFinalWeight()<100)
{
return 25.0;
}
if(getFinalWeight()>=100)
{
return 15.0;
}
return 0;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getGrossWeight() {
return grossWeight;
}
public double getHeight() {
return height;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
}
class Flight {
private String id;
private String departureLocation;
private String landingSite;
private String flightDate;
private double maxWeight;
public Flight(String departureLocation, String landingSite,
String flightDate, double maxWeight, String id) {
this.departureLocation = departureLocation;
this.landingSite = landingSite;
this.flightDate = flightDate;
this.maxWeight = maxWeight;
this.id = id;
}
public String getId() {
return id;
}
public String getDepartureLocation() {
return departureLocation;
}
public String getLandingSite() {
return landingSite;
}
public String getFlightDate() {
return flightDate;
}
public double getMaxWeight() {
return maxWeight;
}
}
class Order {
private String id;
private String orderDate;
public Order(String id, String orderDate) {
this.id = id;
this.orderDate = orderDate;
}
public String getId() {
return id;
}
public String getOrderDate() {
return orderDate;
}
}
class User {
private String name;
private String phoneNumber;
private String location;
public User(String location,String name, String phoneNumber) {
this.location = location;
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getLocation() {
return location;
}
public void display() {
System.out.printf("姓名:%s%n", name);
System.out.printf("电话:%s%n", phoneNumber);
System.out.printf("地址:%s%n", location);
}
}
class Sender {
private User user;
public Sender(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void display() {
System.out.println("发件人姓名:"+user.getName());
System.out.println("发件人电话:"+user.getPhoneNumber());
System.out.println("发件人地址:"+user.getLocation());
}
}
class Recipient {
private User user;
public Recipient(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void display() {
System.out.println("收件人姓名:"+user.getName());
System.out.println("收件人电话:"+user.getPhoneNumber());
System.out.println("收件人地址:"+user.getLocation());
}
}
interface PaymentStrategy {
double getCost();
}
class Payment implements PaymentStrategy {
private double cost;
public Payment(double cost) {
this.cost = cost;
}
@Override
public double getCost() {
return cost;
}
}
class Alipay extends Payment {
public Alipay(double cost) {
super(cost);
}
}
class Wcpay extends Payment {
public Wcpay(double cost) {
super(cost);
}
}
class Client {
private String id;
private String name;
private String phoneNumber;
private String location;
public Client(String id, String name, String phoneNumber, String location) {
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
this.location = location;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getLocation() {
return location;
}
}

由于在类图的基础上进行了符合四大原则的修改。所以显得不一样。
答案结果如图所示:

启示:
在这次题目中,我感觉自己的代码能力有所提升。但在容器类使用的不够熟练、按照四大原则设计的习惯不够强烈。反映出了我对代码使用的不足。
第二次题目迭代:
第二次题目在第一次题目上增加了对支付方式调用、货物运输方式调用的迭代
其实相对第一道题目没有多大改变。就增加了getClass().getSimpleName()函数以及equals用法没有多大改变,相对电梯类迭代简单。
以下是本次题目的代码:
点击查看代码
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.InputMismatchException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String clientType=sc.next();
String type="Individual|Corporate";
if(!clientType.matches("(Individual|Corporate)"))
{
System.out.println("Wrong Format");
System.exit(0);
}
String clientId = sc.next();
String clientName = sc.next();
String clientPhone = sc.next();
String clientAddress = sc.next();
Client client = new Client(clientId, clientName, clientPhone, clientAddress,clientType);
String allcargoType=sc.next();
int cargoCount = 0;
try {
cargoCount = sc.nextInt();
if (cargoCount <= 0) {
System.out.println("Wrong Format");
System.exit(0);
}
} catch (InputMismatchException e) {
System.out.println("Wrong Format");
System.exit(0);
}
if(!allcargoType.matches("(Normal|Expedite|Dangerous)"))
{
System.out.println("Wrong Format");
System.exit(0);
}
List<Cargo> cargoList = new ArrayList<>();
for (int i = 0; i < cargoCount; ) {
try {
String cargoId = sc.next();
String cargoName = sc.next();
double width = sc.nextDouble();
double length = sc.nextDouble();
double height = sc.nextDouble();
double weight = sc.nextDouble();
if (width <= 0 || length <= 0 || height <= 0 || weight <= 0) {
System.out.println("Wrong Format");
System.exit(0);
}
Cargo cargo=null;
if(allcargoType.equals("Normal"))
{
cargo = new normalCargo(cargoName, weight, height, length, width, cargoId);
}
else if(allcargoType.equals("Expedite"))
{
cargo= new expediteCargo(cargoName, weight, height, length, width, cargoId);
}
else if(allcargoType.equals("Dangerous"))
{
cargo=new dangerousCargo(cargoName, weight, height, length, width, cargoId);
}
cargoList.add(cargo);
i++;
} catch (InputMismatchException e) {
System.out.println("Wrong Format");
System.exit(0);
}
}
String flightId = sc.next();
String departure = sc.next();
String destination = sc.next();
String flightDate = sc.next();
double maxWeight = 0;
try {
maxWeight = sc.nextDouble();
if (maxWeight <= 0) {
System.out.println("Wrong Format");
System.exit(0);
}
} catch (InputMismatchException e) {
System.out.println("Wrong Format");
System.exit(0);
}
Flight flight = new Flight(departure, destination, flightDate, maxWeight, flightId);
String orderId = sc.next();
String orderDate = sc.next();
Order order = new Order(orderId, orderDate);
String senderAddress = sc.next();
String senderName = sc.next();
String senderPhone = sc.next();
Sender sender = new Sender(new User(senderAddress, senderName, senderPhone));
String recipientName = sc.next();
String recipientPhone = sc.next();
String recipientAddress = sc.next();
Recipient recipient = new Recipient(new User(recipientName, recipientPhone, recipientAddress));
String payType=sc.next();
double totalWeight = 0;
for (Cargo cargo : cargoList) {
totalWeight += cargo.getFinalWeight();
}
if (totalWeight > flight.getMaxWeight()) {
System.out.printf("The flight with flight number:%s has exceeded its load capacity and cannot carry the order.", flight.getId());
System.exit(0);
}
double totalCost = 0;
for (Cargo cargo : cargoList) {
totalCost += cargo.getRate() * cargo.getFinalWeight();
}
if(clientType.equals("Individual"))
{
totalCost*=0.9;
}
else if(clientType.equals("Corporate"))
{
totalCost*=0.8;
}
System.out.printf("客户:%s(%s)订单信息如下:%n", client.getName(), client.getPhoneNumber());
System.out.println("-----------------------------------------");
System.out.println("航班号:" + flight.getId());
System.out.println("订单号:" + order.getId());
System.out.println("订单日期:" + order.getOrderDate());
sender.display();
recipient.display();
System.out.println("订单总重量(kg):" + String.format("%.1f", totalWeight));
if(payType.equals("Wechat"))
{
System.out.println("微信支付金额:" + String.format("%.1f", totalCost));
}
else if(payType.equals("ALiPay"))
{
System.out.println("支付宝支付金额:" + String.format("%.1f", totalCost));
}
else if(payType.equals("Cash"))
{
System.out.println("现金支付金额:" + String.format("%.1f", totalCost));
}
System.out.println("\n货物明细如下:");
System.out.println("-----------------------------------------");
System.out.println("明细编号\t货物名称\t计费重量\t计费费率\t应交运费");
for (int i = 0; i < cargoList.size(); i++) {
Cargo cargo = cargoList.get(i);
if(i!=cargoList.size()-1)
{
System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f%n",i + 1,cargo.getName(),cargo.getFinalWeight(),cargo.getRate(),cargo.getRate() * cargo.getFinalWeight());
}
else {
System.out.printf("%d\t%s\t%.1f\t%.1f\t%.1f",i + 1,cargo.getName(),cargo.getFinalWeight(),cargo.getRate(),cargo.getRate() * cargo.getFinalWeight());
}
}
}
}
class Control {
private AllCargo allCargo;
private Client client;
private Flight flight;
private Order order;
private Sender sender;
private Recipient recipient;
public Control(AllCargo allCargo, Client client, Flight flight,
Order order, Sender sender, Recipient recipient) {
this.allCargo = allCargo;
this.client = client;
this.flight = flight;
this.order = order;
this.sender = sender;
this.recipient = recipient;
}
public AllCargo getAllCargo() {
return allCargo;
}
public Client getClient() {
return client;
}
public Flight getFlight() {
return flight;
}
public Order getOrder() {
return order;
}
public Sender getSender() {
return sender;
}
public Recipient getRecipient() {
return recipient;
}
}
class AllCargo {
private List<Cargo> cargoList;
private String type;
public AllCargo(List<Cargo> cargoList) {
this.cargoList = cargoList;
}
public List<Cargo> getCargoList() {
return cargoList;
}
public void setCargoList(List<Cargo> cargoList) {
this.cargoList = cargoList;
}
public String getType() {
return type;
}
public void setType() {
this.type=type;
}
}
interface cargoVarious{
double getRate();
}
abstract class Cargo implements cargoVarious{
private String id;
private String name;
private double grossWeight;
private double height;
private double length;
private double width;
public Cargo(String name, double grossWeight, double height,
double length, double width, String id) {
this.name = name;
this.grossWeight = grossWeight;
this.height = height;
this.length = length;
this.width = width;
this.id = id;
}
public double getFinalWeight() {
double volumWeight = (length * height * width) / 6000;
return Math.max(volumWeight, grossWeight);
}
public abstract double getRate();
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getGrossWeight() {
return grossWeight;
}
public double getHeight() {
return height;
}
public double getLength() {
return length;
}
public double getWidth() {
return width;
}
}
class normalCargo extends Cargo{
public normalCargo(String name, double grossWeight, double height,
double length, double width, String id){
super(name,grossWeight,height,length,width,id);
}
@Override
public double getRate()
{
if(super.getFinalWeight()<20)
{
return 35;
}
if(super.getFinalWeight()>=20&&super.getFinalWeight()<50)
{
return 30;
}
if(super.getFinalWeight()>=50&&super.getFinalWeight()<100)
{
return 25;
}
if(super.getFinalWeight()>=100)
{
return 15;
}
return 0;
}
}
class expediteCargo extends Cargo{
public expediteCargo(String name, double grossWeight, double height,
double length, double width, String id){
super(name,grossWeight,height,length,width,id);
}
@Override
public double getRate()
{
if(super.getFinalWeight()<20)
{
return 60;
}
if(super.getFinalWeight()>=20&&super.getFinalWeight()<50)
{
return 50;
}
if(super.getFinalWeight()>=50&&super.getFinalWeight()<100)
{
return 40;
}
if(super.getFinalWeight()>=100)
{
return 30;
}
return 0;
}
}
class dangerousCargo extends Cargo{
public dangerousCargo(String name, double grossWeight, double height,
double length, double width, String id){
super(name,grossWeight,height,length,width,id);
}
@Override
public double getRate()
{
if(super.getFinalWeight()<20)
{
return 80;
}
if(super.getFinalWeight()>=20&&super.getFinalWeight()<50)
{
return 50;
}
if(super.getFinalWeight()>=50&&super.getFinalWeight()<100)
{
return 30;
}
if(super.getFinalWeight()>=100)
{
return 20;
}
return 0;
}
}
class Flight {
private String id;
private String departureLocation;
private String landingSite;
private String flightDate;
private double maxWeight;
public Flight(String departureLocation, String landingSite,
String flightDate, double maxWeight, String id) {
this.departureLocation = departureLocation;
this.landingSite = landingSite;
this.flightDate = flightDate;
this.maxWeight = maxWeight;
this.id = id;
}
public String getId() {
return id;
}
public String getDepartureLocation() {
return departureLocation;
}
public String getLandingSite() {
return landingSite;
}
public String getFlightDate() {
return flightDate;
}
public double getMaxWeight() {
return maxWeight;
}
}
class Order {
private String id;
private String orderDate;
public Order(String id, String orderDate) {
this.id = id;
this.orderDate = orderDate;
}
public String getId() {
return id;
}
public String getOrderDate() {
return orderDate;
}
}
class User {
private String name;
private String phoneNumber;
private String location;
public User(String location,String name, String phoneNumber) {
this.location = location;
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name)
{
this.name=name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber)
{
this.phoneNumber=phoneNumber;
}
public String getLocation() {
return location;
}
public void setLocation(String location)
{
this.location=location;
}
public void display() {
System.out.printf("姓名:%s%n", name);
System.out.printf("电话:%s%n", phoneNumber);
System.out.printf("地址:%s%n", location);
}
}
class Sender {
private User user;
public Sender(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void display() {
System.out.println("发件人姓名:"+user.getName());
System.out.println("发件人电话:"+user.getPhoneNumber());
System.out.println("发件人地址:"+user.getLocation());
}
}
class Recipient {
private User user;
public Recipient(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void display() {
System.out.println("收件人姓名:"+user.getName());
System.out.println("收件人电话:"+user.getPhoneNumber());
System.out.println("收件人地址:"+user.getLocation());
}
}
interface PaymentStrategy {
double getCost();
}
class Payment implements PaymentStrategy {
private double cost;
private String type;
public Payment(double cost,String type) {
this.cost = cost;
this.type=type;
}
@Override
public double getCost() {
return cost;
}
}
class Alipay extends Payment {
public Alipay(double cost,String type) {
super(cost,type);
}
}
class Wcpay extends Payment {
public Wcpay(double cost,String type) {
super(cost,type);
}
}
class Cashpay extends Payment{
public Cashpay(double cost,String type)
{
super(cost,type);
}
}
class Client {
private String id;
private String name;
private String phoneNumber;
private String location;
private String type;
public Client(String id, String name, String phoneNumber, String location,String type) {
this.id = id;
this.name = name;
this.phoneNumber = phoneNumber;
this.location = location;
this.type=type;
}
public void setId(String id)
{
this.id=id;
}
public String getId() {
return id;
}
public void setName(String name)
{
this.name=name;
}
public String getName() {
return name;
}
public void setPhoneNumber(String phoneNumber)
{
this.phoneNumber=phoneNumber;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setLocation(String location)
{
this.location=location;
}
public String getLocation() {
return location;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type=type;
}
}
以下是本次代码的类图:

答案结果如图所示:

这次迭代相对简单,其实除了些新知识外没什么难度。不过也确实还是有些难度的。
实验总结:
这两次题目集相对简单。不过还是反映了我在容器类掌握的知识不足。以及四大法则不会应用。其他的没什么了。希望我的编程能力能够不断地提升。

浙公网安备 33010602011771号