java异常编程题

题目:

1.编写异常类:空异常、年龄低异常、年龄高异常、 工资低异常、工资高异常、身份证非法异常。

2.编写一个员工类,

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

显示员工人数:void showTotalEmployee(),抛出空异常。当员工人数为0时,抛出此异常。

代码:

  class HightAgeException extends Exception{  //年龄高异常
	   private String s;
	   public HightAgeException(String s){
		   this.s=s;
	   }
	   public String getS() {
		   return s;
	   }
   } class LowAgeException extends Exception{  //年龄底异常
	   private String s;
	   public LowAgeException(String s){
		   this.s=s;
	   }
	   public String getS() {
		   return s;
	   }
   }
	   class HightSalaryException  extends Exception{      //工资高异常
		   private String s;
		   public HightSalaryException(String s){
			   this.s=s;
		   }
		   public String getS() {
			   return s;
		   }
	   }
	   class LowSalaryException  extends Exception{       //工资低异常
		   private String s;
		   public LowSalaryException(String s){
			   this.s=s;
		   }
		   public String getS() {
			   return s;
		   }
	   } 
	   class IDcardException  extends Exception{         //身份证异常
		   private String s;
		   public IDcardException(String s){
			   this.s=s;
		   }
		   public String getS() {
			   return s;
		   }
	   }
	   class NullException  extends Exception{        //空异常
		   private String s;
		   public NullException(String s){
			   this.s=s;
		   }
		   public String getS() {
			   return s;
		   }
	   }

                   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,int age,String name) throws Exception{
		    	if(age<18) {
		    		throw new LowAgeException("年龄太小了!");
		    	}else if(age>60) {
		    		throw new HightAgeException("年龄太大了!");
		    	}else if(name==null||name=="") {
		    		throw new NullException("输入名字不合法!");
		    	}
		    }
		    public Employee(double salary,String idCard) throws Exception{
		    	if(salary<600) {
		    		throw new LowSalaryException("工资低了!");
		    	}
		    }
		    public void addSalary(double addSalary) throws HightSalaryException{
	    		 double newSalary=salary +addSalary;
	    		 if(newSalary>totalSalary ) {
	    			 throw new HightSalaryException("工资高了!");
	    		 }
	    	}
		    public void minusSalary(double minusSalary) throws LowSalaryException{
		    	double min=salary-minusSalary;
		    	if(min<600) {
		    		throw new LowSalaryException("工资低了!");
		    	}
		    }
		    public void showTotalSalary() throws NullException{
		    	if(totalSalary ==0) {
		    		throw new NullException("工资为0!");
		    	}
		    }
		    public void showTotalEmployee() throws NullException {
		    	 if(totalEmployee==0) {
		    		 throw new NullException("人数为0!");
		    	 }
		    }
		    }
public class TestExceptionEmyoeer {
	
	public static void main(String[] args)  {
		try {
		Employee e1=new Employee(12,34,"张三");
		Employee e2=new Employee(10000,"1234556");
		e2.addSalary(1200);
		e2.minusSalary(100);
	}catch(Exception e) {
		e.printStackTrace();
	}
	}
}

你可能感兴趣的:(Java)