NEFU-2023-JAVA实验六

题目

分析设计
模拟向货船上装载集装箱。
每个集装箱必须包含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();
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烟雨平生9527

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值