Homework 5 - Polymorphism and Exception Handling

Due: Wednesday, March 3, 2009 at 5:00pm

Coding Conventions

Use the same coding conventions as described in Homework 1. Additionally, make sure to indent each section of the class in the class definition.

Assignment

The purpose of this assignment is to modify Homework 4 to support polymorphism and exception handling. If you did not complete Homework 4, you can modify the solution.

Polymorphism

Change setEmployee() and printEmployee() to be polymorphic functions. Make printEmployee() in Employee a pure virtual function instead of printing out "Undefined employee".

Exception Handling

Rather than returning a Boolean on the mutator functions (e.g. setSSN, setSalary and so on), you will use exception handling. Create the following exception classes: badSSN, badWeek, badRate, badHour and badSalary. Within each mutator function, if the input is bad, you will throw the corresponding exception class instead of returning false. For example, if the SSN is not exactly 9 digits, you will throw the badSSN exception class.

In each setEmployee() function, you will have a try-catch block for each of the mutator functions. You will keep asking the user for input while there is an exception thrown. The pseudocode for doing this for the SSN is as follows:

do
  ask the user for the SSN
  read the SSN into a temporary character array
  set flag to true
  try 
    call setSSN
  catch badSSN
    output that the SSN is bad and must be exactly 9 digits
    set flag to false
while flag is false
Similar logic will be used for the other mutator functions.

NOTE: You do not need to do exception handling for setName() as it just truncates names that are too long.

Main Function

Replace the main() from Homework 4 with a main() that implements the following pseudocode to test the polymorphic functions:
create an Employee pointer
while true
  ask the user if they wish to enter a hourly or salaried employee, tell them to enter "exit" to end the loop
  read their response into a character array

  if the user typed "hourly"
    try
      use new to allocate a new hourlyEmployee to the Employee pointer
    catch bad_alloc
      output that the allocation failed
      exit program
  else if the user typed "salaried"
    try
      use new to allocate a new salariedEmployee to the Employee pointer
    catch bad_alloc
      output that the allocation failed
      exit program
  else if the user typed "exit"
    exit the loop with the break statement
  else
    output that the user selected an invalid employee type
    continue to the next iteration of the loop

  call setEmployee() on the Employee pointer
  call printEmployee() on the Employee pointer
  delete the object pointed to by the Employee pointer
end-while
This is a variation on a menu loop where the user types commands instead of menu options. You should use strcmp() from the cstring library to compare what they typed to the available commands.

Your code should use robust input for reading strings and integers. The provided solution for Homework 4 also shows how to do robust input.