编写类InsuranceCheck和自定义异常类AgeException。用2010年减去某人的出

编写类InsuranceCheck和自定义异常类AgeException。用2010年减去某人的出生年份计算其年龄。然后用年龄减去16计算其驾龄。如果驾龄少于4年的驾驶员,每年需缴纳2000元的保险费,其他人则交付1000元,如果未满16周岁,则不需要保险,并且引发异常(年龄太小 ,不用保险!

//filename:InsuranceCheck.java
public class InsuranceCheck {
    public Integer calacMoneyByBornYear(int bornYear) throws AgeException {
        int money = 1000;
        int years = this.year - bornYear;
        
        if (years - 16 < 0) {
            throw new AgeException("年龄太小,不用保险!");
        } else if (years - 16 < 4) {
            money = 2000;
        } else if (years - 16 >= 4) {
            money = 1000;
        }
        
        return money;
    }
    public InsuranceCheck() {}
    public InsuranceCheck(int year) {
        this.year = year;
    }
    private int year = 2010;
}

//filename:AgeException.java
public class AgeException extends Throwable {
    public AgeException() {}
    public AgeException(String message) {
        super(message);
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-20
public class InsuranceCheck { public Integer calacMoneyByBornYear(int bornYear) throws AgeException { int money = 1000; int years = this.year - bornYear; if (years - 16 < 0) { throw new AgeException("年龄太小,不用保险!"); } else if (years - 16 < 4) { money = 2000; } else if (years - 16 >= 4) { money = 1000; } return money; } public InsuranceCheck() {} public InsuranceCheck(int year) { this.year = year; } private int year = 2010;} //filename:AgeException.javapublic class AgeException extends Throwable { public AgeException() {} public AgeException(String message) { super(message); }}
相似回答
大家正在搜