Java编写异常类练习

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

public class HighAgeException extends Exception  {
	public HighAgeException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public HighAgeException(String msg) {
		super(msg);
	}

}
public class LowAgeException extends Exception {
	public LowAgeException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public LowAgeException(String msg){
		super(msg);
	}
}
public class HighSalaryException extends Exception {

		public HighSalaryException() {
			super();
			// TODO Auto-generated constructor stub
		}
		public HighSalaryException(String msg) {
			super(msg);
		}
	
}
public class LowSalaryException extends Exception {

	public LowSalaryException() {
		super();
		// TODO Auto-generated constructor stub
	}
	public LowSalaryException(String msg) {
		super(msg);
	}
}
public class IdException extends Exception {
	public IdException() {
		super();
		// TODO Auto-generated constructor stub
	}

	public IdException(String msg) {
		super(msg);
	}
}
public class NullException extends Exception {
	public NullException() {
		super();
		// TODO Auto-generated constructor stub
	}
	public NullException(String msg) {
		super(msg);
	}
}
public class Employee {
	private int no;
	private String name;
	private int age;
	private double salary;
	private String id;
	private int num;
	private double TotalSalary;
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getSalary() {
		return salary;
	}
	public void setSalary(double salary) {
		this.salary = salary;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public int getNum() {
		return num;
	}
	public void setNum(int num) {
		this.num = num;
	}
	public double getTotalSalary() {
		return TotalSalary;
	}
	public void setTotalSalary(double TotalSalary) {
		this.TotalSalary = TotalSalary;
	}
	public Employee() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Employee(int no, String name, int age, double salary, String id, int num, double sumSalary) {
		super();
		this.no = no;
		this.name = name;
		this.age = age;
		this.salary = salary;
		this.id = id;
		this.num = num;
		this.TotalSalary = TotalSalary;
	}
	

    public Employee(int no,String name,int age)throws Exception{
    	if(age<18) {
    		throw new LowAgeException("年龄太小了!");
    	}else if(age>60) {
    		throw new HighAgeException("年龄太高了!");
    	}else if((name==null)||name==""){
    		throw new NullException("姓名不能为空!");
    	}
    }
    

    public Employee(double salary,String id) throws Exception{
    	this.salary=salary;
    	if(salary<600) {
    		throw new LowSalaryException("工资太低了!");
    	}
    }

    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 LowSalaryException("工资太低了!");
    	}
    }

    public void showTotalSalary() throws Exception {
    	if(TotalSalary==0) {
    		throw new NullException("工资总额为0!");
    	}
    }

    public void showTotalEmployee()throws Exception {
    	if(num==0) {
    		throw new NullException("员工人数为0!");
    	}
    }

}
public class Test {
	public static void main(String[] args) {
		try{
			Employee em1=new Employee(01,"泷谷源治",19);
			Employee em2=new Employee(500,"1234567890123456");
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
}

你可能感兴趣的:(Java编写异常类练习)