Paul E

Paul E

  • NA
  • 5
  • 1.9k

Exception handleing

Sep 20 2013 1:11 PM
-Using the Employee class created in Exercise 5a, write an application that creates an array of fi ve Employees. Prompt the user for values for each fi eld for each Employee. If the user enters improper or invalid data,handle any exceptions that are thrown by setting the Employee's ID number to 999 and the Employee's pay rate to the $6.00 minimum. At the end of the program, display all the entered, and possibly corrected, records.-

This is my task and it has been kicking my butt all week. My code so far, and it seems to me it should work. Not sure what I am doing wrong.



namespace EmployeeException
{
    class EmployeeExceptionDemo
    {
        static void Main(string[] args)
        {
            Employee emp1 = CreateEmployee();
            Employee emp2 = CreateEmployee();
            Employee emp3 = CreateEmployee();
            Employee emp4 = CreateEmployee();
            Employee emp5 = CreateEmployee();
            Console.ReadKey();
        }

        static Employee CreateEmployee()
        {
            Employee emp = null;

            try
            {
                emp = new Employee();
            }
            catch (EmployeeException e)
            {
                Console.WriteLine(e.Message);
            }

            return emp;
        }
    }


    class EmployeeException : Exception
    {
        public EmployeeException(string idAndPayRate)
            : base(idAndPayRate + " is invalid")
        {
        }
    }

    class EmployeeIDException : Exception
  {
       public EmployeeIDException(string employeeIDAndpayRate) : base(employeeIDAndpayRate + " is invalid")
       {
       }

  }

  class Employee
  {
      string employeeIDNum;
      int payRate;

      public Employee()
      {
          this.employeeIDNum = GetID();
          this.payRate = GetRate();
      }

      public int GetID()
      {

          Console.Write("Enter ID number for Employee : ");
          int idNumber;
          int.TryParse(Console.ReadLine(), out idNumber);
         
          if (idNumber > 999)
             throw new EmployeeException(String.Format("Employee ID number {0}", idNumber));

          return idNumber;
      }

      public int GetRate()
      {
          Console.Write("Enter Employee's hourly rate : ");
          int wage;
          int.TryParse(Console.ReadLine(), out wage);
          if (wage < 6.00)
             throw new EmployeeException(String.Format("Employee's hourly wage {0}", wage));

          return wage;
      }

   }
}

Answers (5)