nchu大作业一:NCHU_航空器配载与货运管理系统1
【作业背景】
在航空业中,航班起飞前需要进行严格的“配载”,即计算飞机的旅客、货物、行李的重量分布,计算出飞机的重心位置,以确保飞行安全。本系列作业将围绕这一真实场景展开。
【本次作业需求说明】
设计一个基础的航班货运配载模块。系统需要记录航班的基本信息(航班号、最大起飞重量、最大业载重量)。地勤人员可以按照货物重量从高到低向该航班添加货物(货物名称、重量)。系统需要实时计算当前已装载的总重量,并判断是否超载。
【设计要求】
类设计必须符合单一职责原则(SRP),参考类图如下所示(仅作参考,可自行加工具类)。
注意:类设计如果违背了SRP,此题不得分。
1.设计与分析:
先确定可以分成几个类别来写,以及每个类别对应的属性和方法有哪些 。
总体可以分成5个类别,分别是:
Fight(航班类){属性1:航班号,属性2:最大承载重量};方法{获取航班号(getFightNo),获取最大承载重量(getmaxWeight)}
cargo(货物类){属性1:货物名称,属性2:货物重量};方法{获取货物名称(getCargoname),获取货物重量(getCargoWeight)};
loadManifest(装载舱单清单){属性1:货物的列表(是一个动态数组,方便后续输出)};方法{增加货物(addcargo),总重量的计算(totalWeight),排序(getCargoList)(注意这里的排序方法只能是选择排序,效率更好)};
sort(排序类,因为loadManifest里面的方法太多,可以把排序分出来){属性:货物的列表};方法{通过选择排序的方法将对象互换,可以使用到ArrayList的set(位置,要换的对象)};
isoverload(是否超载){属性:总的重量}{方法:与最大承载重量比较并输出};
2.解释和心得:
(1)面向对象编程中,不同功能拆分成不同类,实体类保存数据,工具类完成排序,代码结构清晰,增加注释,方便后期维护
(2)ArrayList和c语言里面的数组访问方式不一样,数组用[下标],集合读取.get(下标)、修改.set(下标,对象),下标只是数字,不能直接当做对象传入 set 方法,这个点很容易编译报错。
(3)
sortList.set(i,sortList.get(maxindex));//将i位置的对象变成maxindex的 sortList.set(maxindex,temp);
这是动态数组的交换对象,因为cargo里面存在两个属性,但是一半c语言里面交换的方式是不太匹配的,使用的方法是sortList(自己定义的变量).set(想要换到哪个位置,想要换的内容);
(4)方法调用时,形参实参要匹配,有参数的方法调用时必须传入对应类型的数据,否则直接编译失败。写代码时注意检查方法签名,不要写多余的变量名。
(5)整体而言,这个相对较简单,只需要想清楚分为几个类别,每个类别需要干什么事情即可.
import java.util.Scanner; import java.util.ArrayList; import java.util.List; import java.util.Comparator; class Flight{//航班类 private String flightNO;//航班号 private Double maxWeight;//航班最大载重重量 public Flight(String flightNO,Double maxWeight){//初始化 this.flightNO=flightNO; this.maxWeight=maxWeight; } public String getFlightNO()//获取航班号 {return flightNO;} public Double getMaxWeight() {return maxWeight;}//获取最大载重重量 } class cargo{//货物类 private String cargoname;//货物名称 private Double cargoWeight;//货物重量 public cargo(String cargoname,Double cargoWeight){ this.cargoname=cargoname; this.cargoWeight=cargoWeight; } public String getCargoname()//获取货物名称 {return cargoname;} public Double getCargoWeight() {return cargoWeight;}//获取货物重量 } class loadManifest{//装载舱单清单 private List<cargo> CargoList;//货物的列表 public loadManifest() { CargoList=new ArrayList<>(); } public void addcargo(cargo cargo){//增加货物 CargoList.add(cargo);//动态数组的增加函数 } public Double totalWeight() { Double sum=0.0; for(cargo c:CargoList)//循环遍历Cargo数组 { sum+=c.getCargoWeight(); } return sum; } public List<cargo> getCargoList()//获得从小到大的排序 { sort SORT=new sort(); return SORT.sortList(CargoList); } } class sort{ public List<cargo>sortList(List<cargo> CargoList) { List<cargo> sortList=new ArrayList<>(CargoList); int len=sortList.size(); for(int i=0;i<len-1;i++)//使用选择排序进行顺序的调换 { int maxindex=i; for(int j=i;j<len;j++) { if(sortList.get(j).getCargoWeight()>sortList.get(maxindex).getCargoWeight()) { maxindex=j; } } cargo temp=sortList.get(i); sortList.set(i,sortList.get(maxindex));//将i位置的对象变成maxindex的 sortList.set(maxindex,temp); } return sortList; } } class isoverload{//判断是否超载 Double totalWeight; public void Isoverload(Double totalWeight,Double maxWeight) {this.totalWeight=totalWeight; if(totalWeight>maxWeight) { System.out.println("警告:严重超载"); } else { System.out.println("配载状态:正常"); } } } public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); String flightNO=sc.nextLine(); Double maxWeight=sc.nextDouble(); int n=0; n=sc.nextInt(); loadManifest LoadManifest=input(sc,n); Print(LoadManifest,maxWeight); } public static void Print(loadManifest LoadManifest,Double maxWeight) { List<cargo>c=LoadManifest.getCargoList();//获取最终的货物清单 if(c==null)return ; for(cargo a:c)//循环输出货物的名称和重量 { System.out.printf("货物[%s 重量:%.1fkg]\n",a.getCargoname(),a.getCargoWeight()); } System.out.printf("总重量: %.1fkg / 最大载重: %.1fkg\n",LoadManifest.totalWeight(),maxWeight); isoverload IsoverLoad=new isoverload();//是否超载 IsoverLoad.Isoverload(LoadManifest.totalWeight(),maxWeight); } public static loadManifest input(Scanner sc,int n) { loadManifest LoadManifest=new loadManifest();//获取清单; while(n>0)//循环输入货物名称和重量 { //放入货物类里面 String cargoName=sc.next(); Double cargoWeight=sc.nextDouble(); cargo cargo=new cargo(cargoName,cargoWeight); LoadManifest.addcargo(cargo);//往舱单里面增加货物 n--; } sc.close(); return LoadManifest; } }