Payroll-Inheritance02: Using the Employee Subclasses - Exercise

Contact Us or call 1-877-932-8228
Payroll-Inheritance02: Using the Employee Subclasses - Exercise

Payroll-Inheritance02: Using the Employee Subclasses

Duration: 45 to 60 minutes.
  1. In the main method of Payroll, reinstate the array of Employee objects.
  2. Create instances of the non-exempt employees, exempt employees, and contract employees to fill the array. You can ask which type of employee using getPromptedChar, then break up the existing sections of code that enter one specific type as in:
    boolean  badType; 
    do {
    	badType = false;
    	char empType = KeyboardReader.getPromptedChar("Enter employee type (E, N, C): ");
    	switch (emptype) {
    		case 'E':
    		case 'e':
    			(existing code to enter one ExemptEmployee,
    			modified to use current array element as the variable)
    			break;
    		case 'N':
    		case 'n':
    			(existing code to enter one NonexemptEmployee,
    			modified to use current array element as the variable)
    			break;
    		case 'C':
    		case 'c':
    			(existing code to enter one ContractEmployee,
    			modified to use current array element as the variable)
    			break;
    		default:
    			badType = true;
    			(Print error message)	
    	}
    } while (badType);
    
    You will see a slightly different approach in the solution code, which avoids duplicating the code to read the name and department data.
  3. Create a report that lists all employees, grouped by type, by looping through the array three times.
    • The first time, show all exempt employees and their pay information .
    • The second time. print only the non-exempt employees and their pay information.
    • The third time. print contract employees and their pay information.
    • Since you'll be writing the same loop multiple times, you could try both indexed loops and for-each loops (the solution uses for-each loops).

Solution:

Solutions/Payroll-Inheritance02/Payroll.java
import employees.*;
import util.*;

public class Payroll {  
	public static void main(String[] args) {

---- C O D E   O M I T T E D ----
		for (int i = 0; i < e.length; i++) {
			char type = 
				KeyboardReader.getPromptedChar("Enter type: E, N, or C: ");
			if (type != 'e' && type != 'E' && 
					type != 'n' && type != 'N' && 
					type != 'c' && type != 'C') {
				System.out.println("Please enter a valid type");
				i--;
				continue;        
			}
			fName = KeyboardReader.getPromptedString("Enter first name: ");
			lName = KeyboardReader.getPromptedString("Enter last name: ");
			do {
				dept = KeyboardReader.getPromptedInt("Enter department: ");
				if (dept <= 0) System.out.println("Department must be >= 0");
			} while (dept <= 0);
			do {
				payRate = KeyboardReader.getPromptedDouble("Enter pay rate: ");
				if (payRate < 0.0) System.out.println("Pay rate must be >= 0");
			} while (payRate < 0.0);

			switch (type) {
				case 'e':
				case 'E': e[i] = new ExemptEmployee(fName, lName, dept, payRate);
									break;
				case 'n':
				case 'N': do {
										hours = 
											KeyboardReader.getPromptedDouble("Enter hours: ");
										if (hours < 0.0) 
											System.out.println("Hours must be >= 0");
									} while (hours < 0.0);
									e[i] = new NonexemptEmployee(fName, lName, dept, 
												payRate, hours);
									break;
				case 'c':
				case 'C': do {
										hours = 
											KeyboardReader.getPromptedDouble("Enter hours: ");
										if (hours < 0.0) 
											System.out.println("Hours must be >= 0");
									} while (hours < 0.0);
									e[i] = new ContractEmployee(fName, lName, dept, 
												payRate, hours);
			}

		}

		System.out.println();
		System.out.println("Exempt Employees");
		System.out.println("====== =========");
		for (Employee emp : e) {
			if (emp instanceof ExemptEmployee) {
				System.out.println(emp.getPayInfo());
			}
		}
		System.out.println();
		System.out.println("Nonexempt Employees");
		System.out.println("======== =========");
		for (Employee emp : e) {
			if (emp instanceof NonexemptEmployee) {
				System.out.println(emp.getPayInfo());
			}
		}
		System.out.println();
		System.out.println("Contract Employees");
		System.out.println("======== =========");
		for (Employee emp : e) {
			if (emp instanceof ContractEmployee) {
				System.out.println(emp.getPayInfo());
			}
		}
	}
}

We reduced the amount of code by recognizing that some of the data entry logic is common to all three types - asking for first and last names and department before the switch. This required some additional shuffling of declarations, plus some restructuring to avoid asking for the name information all over again if the employee type is not valid (we used continue to avoid processing an employee if the type wasn't valid, and backed up the loop counter to go over the same array location on the next iteration).

For the report, the same logic is essentially repeated three times:

System.out.println();
System.out.println("Exempt Employees");
System.out.println("====== =========");
for (Employee emp : e) {
	if (e instanceof ExemptEmployee) {
		System.out.println(e[i].getPayInfo());
	}
}

The instanceof test enables us to isolate just one type of employee to print; the others will be skipped over.

Next