题目
分析设计
模拟向货船上装载集装箱。
每个集装箱必须包含ID号以及重量。
货船必须包含ID号,名称,以及能承载的总重量,可以包含承载多个集装箱。
装载业务逻辑类以及业务逻辑方法,传入指定货船,以及预装载的多个集装箱。 当多个集装箱的总重量超过货船承载重量时,抛出自定义的超重异常信息,信息要求包含货船ID号,货船名称,以及超重的重量。 但当出现异常时,此业务逻辑方法不负责处理,由调用者捕获处理(此逻辑方法仅负责处理装载,超重如何处理由调用者判断决定,其他逻辑负责处理)。 未出现异常时,将集装箱装入货船,并将货船返回。
自定义超重异常,必须被显示捕获处理的checked异常,异常必须包含异常信息。说明
应按类的类型声明定义不同包。业务逻辑方法模拟声明为静态即可。
创建Test类,主方法。创建测试方法,创建货船对象,创建若干集装箱对象,调用业务逻辑方法,捕获处理异常,测试当超重时是否捕获异常并显示异常信息; 当未超重时,打印货船名称。但无论是否超重,均打印启航的输出。(1)提交程序的所有类的代码。
包括集装箱类、货船类、装载业务逻辑类类、自定义超重异常类、Test类。
(2)提交测试代码的运行截图;
(3)说明实验中出现的问题和解决过程,写出实验的心得体会。
集装箱类
package com.experiment06.entity;
public class Container {
private Integer id;
private Double weight;
public Container(Integer id, Double weight) {
this.id = id;
this.weight = weight;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Double getWeight() {
return weight;
}
public void setWeight(Double weight) {
this.weight = weight;
}
}
货船类
package com.experiment06.entity;
import com.experiment06.entity.Container;
import java.util.ArrayList;
import java.util.List;
public class Ship {
private Integer id;
private String name;
private Double totalweight;
private List<Container> containers;
public Ship(Integer id, String name, Double totalweight) {
this.id = id;
this.name = name;
this.totalweight = totalweight;
containers = new ArrayList<>();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getTotalweight() {
return totalweight;
}
public void setTotalweight(Double totalweight) {
this.totalweight = totalweight;
}
public List<Container> getContainers() {
return containers;
}
public void setContainers(List<Container> containers) {
this.containers = containers;
}
public void addContainer(Container container){
containers.add(container);
}
}
装载业务逻辑类类
package com.experiment06.Service;
import com.experiment06.entity.Container;
import com.experiment06.entity.Ship;
import com.experiment06.exception.OverWeightException;
import java.util.List;
public class LoadService {
public static Ship loadContainers(Ship ship, List<Container> containers) throws OverWeightException {
double total = 0;
for (Container container : containers) {
total += container.getWeight();
}
if (total > ship.getTotalweight()) {
throw new OverWeightException(ship.getId(), ship.getName(), total - ship.getTotalweight());
}
for (Container container : containers) {
ship.addContainer(container);
}
return ship;
}
}
自定义异常类
package com.experiment06.exception;
public class OverWeightException extends Exception{
private Integer shipId;
private String shipName;
private double overweight;
public OverWeightException(Integer shipId, String shipName, double overweight) {
this.shipId = shipId;
this.shipName = shipName;
this.overweight = overweight;
}
public Integer getShipId() {
return shipId;
}
public void setShipId(Integer shipId) {
this.shipId = shipId;
}
public String getShipName() {
return shipName;
}
public void setShipName(String shipName) {
this.shipName = shipName;
}
public double getOverweight() {
return overweight;
}
public void setOverweight(double overweight) {
this.overweight = overweight;
}
public String getMessage() {
return "船 " + shipId + " (" + shipName + ") 超重了 " + overweight + " tons.";
}
}
TEST类
package com.experiment06;
import com.experiment06.Service.LoadService;
import com.experiment06.entity.Container;
import com.experiment06.entity.Ship;
import com.experiment06.exception.OverWeightException;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void Test01(){
Ship ship = new Ship(2001, "小船", 100.0);
List<Container> containers = new ArrayList<>();
containers.add(new Container(001, 30.0));
containers.add(new Container(002, 40.0));
containers.add(new Container(003, 40.0));
try {
LoadService.loadContainers(ship, containers);
System.out.printf("船:" + ship.getName());
} catch (OverWeightException e) {
System.out.printf(e.getMessage());
}
finally {
System.out.println(" 起航");
}
}
public static void Test02(){
Ship ship = new Ship(2002, "大船", 150.0);
List<Container> containers = new ArrayList<>();
containers.add(new Container(001, 30.0));
containers.add(new Container(002, 40.0));
containers.add(new Container(003, 40.0));
try {
LoadService.loadContainers(ship, containers);
System.out.printf("船:" + ship.getName());
} catch (OverWeightException e) {
System.out.printf(e.getMessage());
}
finally {
System.out.println(" 起航");
}
}
public static void main(String[] args) {
Test01();
Test02();
}
}