Thursday, October 28, 2010

Lab 17 -- Solution -- Class: Driver



import java.util.Scanner;

public class Driver {

 public static void main(String[] args)
 {
  boolean valid;
  int num_Employees;
  Employee[] data = new Employee[100];
  int salary_sum = 0;
  double avg_salary;
  
  Scanner keyboard = new Scanner(System.in);
  
  do
  {
   System.out.println("Number of Employees:");
   num_Employees = keyboard.nextInt();
  }while(num_Employees < 0 || num_Employees > 100);
  
  int e_count = 1;
  String ssnInput;
  int sal;
  
  while(e_count <= num_Employees)
  {
   Employee e = new Employee();
   
   // Enter Employee Details
   e.EnterEmployeeDetails(e_count);
   
   salary_sum += e.getSalary();
   
   // Verification of SSN (Length and Character)
   do
   {
    valid = true;
    
    try
    {
     String str = e.getSSN();
     int count = 0;
     
     // Check for SSNLength Exception
     for(int i=0; i < str.length(); i++)
     {
      if(str.charAt(i) != '-' && str.charAt(i) != ' ')
       count++;
     }
     
     if(count != 9)
     { valid = false;
      throw new SSNLengthException();
     }
     
     /* **************************************** */
     
     for(int j = 0; j < str.length(); j++ )
     {
      if(str.charAt(j) != '-' && str.charAt(j) != ' ')
      {
       if(!Character.isDigit(str.charAt(j)))
       {
        valid = false;
        throw new SSNCharacterException();
       }
      }
     }
     
    }
    catch(SSNLengthException exp)
    {
     System.out.println(exp.getMessage());
     e.EnterEmployeeSSN(e_count);
    }
    catch(SSNCharacterException expr)
    {
     System.out.println(expr.getMessage());
     e.EnterEmployeeSSN(e_count);
    }
    
    
   }while(valid == false);
   
   data[e_count - 1] = e;
   
   e_count++;
  } // end of while
  
  avg_salary = (double)salary_sum / num_Employees;
  
  for(int k=0; k < num_Employees; k++)
  {
   if(data[k].getSalary() > avg_salary)
   {
    System.out.printf("EmpNumber %d: %s ABOVE SALARY  %n", k+1, data[k].toString());
   }
   else
    System.out.printf("EmpNumber %d: %s BELOW SALARY %n", k+1, data[k].toString());
  }
  
  
 }
}

No comments: