java异常的一个题目

1.编写异常类:空异常、年龄低异常、年龄高异常、 工资低异常、工资高异常、身份证非法异常。 
2.编写一个员工类: 
(1)属性: 编号、姓名、年龄、工资、身份证号码、员工人数(10)、员工工资总额(10000)。 
(2)构造器: 
构造器1:设置编号、年龄、姓名,如果年龄小于18,抛出年龄低异常;如果年龄大于60,抛出年龄高异常;如果姓名为null或为空字符串,抛出空异常。 
构造器2:设置工资、设置身份证号码,如果工资低于600,抛出工资低异常; 如果身份证不是18位,抛出身份证非法异常。 
(3)方法 
增加工资 addSalary(double addSalary),抛出工资高异常,当增加后的工资大于员工工资总额时,抛出此异常。 
减少工资 minusSalary(double minusSalary),抛出工资低异常,当减少后的工资低于政府最低工资时,抛出工资低异常。 
显示员工工资总额方法:showTotalSalary(),抛出空异常,当工资总额为0时,抛出此异常。 
显示员工人数:showTotalEmployee(),抛出空异常,当员工人数为0时,抛出此异常 。

3.编写main主测试类。

代码如下:

package com.Test12;
//首先创建6个异常类
class LowAgeException extends Exception{
	public LowAgeException(String message) {
		super(message);
	}
}
class HeightAgeException extends Exception{
	public HeightAgeException(String message) {
		super(message);
	}
}
class NullException extends Exception{
	public NullException(String message) {
		super(message);
	}
}
class LowSalaryException extends Exception{
	public LowSalaryException(String message) {
		super(message);
	}
}
class HighSalaryException extends Exception{
	public HighSalaryException(String message) {
		super(message);
	}
}
class IdCardException extends Exception{
	public IdCardException(String message) {
		super(message);
	}
}
//员工类
class Employee{
    private int id;
    private String name;
    private int age;
    private double salary;
    private String idCard;
    private int totalEmployee = 10;
    private double totalSalary = 100000;
    public Employee(int id,String name,int age)throws Exception{
    	if(age<18) {
    		throw new LowAgeException("年龄太小了!");
    	}else if(age>60) {
    		throw new HeightAgeException("年龄太高了!");
    	}else if((name==null)||name==""){
    		throw new NullException("姓名不能为空!");
    	}
    }
    public Employee(double salary,String idCard) throws Exception{
    	this.salary=salary;
    	if(salary<600) {
    		throw new LowSalaryException("工资太低了!");
    	}else if(idCard.length()!=18) {
    		throw new IdCardException("身份证非法!");
    	}
    }
    //增加工资
    public void addSalary(double addSalary) throws Exception{
    	double nowSalary=salary+addSalary;
    	System.out.println("增加后的工资是:"+nowSalary);
    	if(nowSalary>totalSalary) {
    		throw new HighSalaryException("工资增加后太高了!");
    	}
    }
    //减少工资
    public void minusSalary(double minusSalary) throws Exception{
    	double nowSalary=salary-minusSalary;
    	System.out.println("减少后的工资是:"+nowSalary);
    	if(nowSalary<600) {
    		throw new HighSalaryException("工资减少后太低了!");
    	}
    }
    //显示员工工资总额
    public void showTotalSalary()throws Exception {
    	if(totalSalary==0) {
    		throw new IdCardException("工资总额为0!");
    	}
    }
    //显示员工人数
    public void showTotalEmployee()throws Exception {
    	if(totalEmployee==0) {
    		throw new IdCardException("员工人数为0!");
    	}
    }
}

public class Test12_6 {
	public static void main(String[] args) {
		try {
			Employee e1=new Employee(201801,"小青",26);
			Employee e2=new Employee(655,"123456789987654321");
			e2.addSalary(1000);
			e2.minusSalary(100);
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			System.out.println("欢迎使用本系统!");
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

午夜安全

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

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

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

打赏作者

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

抵扣说明:

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

余额充值