Listing 7.20 //******************************************************************** // Firm.java Author: Lewis/Loftus/Cocking // // Demonstrates polymorphism via inheritance. //******************************************************************** public class Firm { //----------------------------------------------------------------- // Creates a staff of employees for a firm and pays them. //----------------------------------------------------------------- public static void main (String[] args) { Staff personnel = new Staff(); personnel.payday(); } } //******************************************************************** // Staff.java Author: Lewis/Loftus/Cocking // // Represents the personnel staff of a particular business. //******************************************************************** public class Staff { private StaffMember[] staffList; //----------------------------------------------------------------- // Sets up the list of staff members. //----------------------------------------------------------------- public Staff () { staffList = new StaffMember[6]; staffList[0] = new Executive ("Elliot", "123 Main Line", "555-0469", "123-45-6789", 2423.07); staffList[1] = new Employee ("Dr. Kelso", "456 Off Line", "555-0101", "987-65-4321", 1246.15); staffList[2] = new Employee ("Turk", "789 Off Rocker", "555-0000", "010-20-3040", 1169.23); staffList[3] = new Hourly ("Dr. Cox", "678 Fifth Ave.", "555-0690", "958-47-3625", 10.55); staffList[4] = new Volunteer ("J.D.", "987 Suds Blvd.", "555-8374"); staffList[5] = new Volunteer ("Carla", "321 Duds Lane", "555-7282"); ((Executive)staffList[0]).awardBonus (500.00); ((Hourly)staffList[3]).addHours (40); } //----------------------------------------------------------------- // Pays all staff members. //----------------------------------------------------------------- public void payday () { double amount; for (int count=0; count < staffList.length; count++) { System.out.println (staffList[count]); amount = staffList[count].pay(); // polymorphic if (amount == 0.0) System.out.println ("Thanks!"); else System.out.println ("Paid: " + amount); System.out.println ("-----------------------------------"); } } } //******************************************************************** // StaffMember.java Author: Lewis/Loftus/Cocking // // Represents a generic staff member. //******************************************************************** abstract public class StaffMember { private String name; private String address; private String phone; //----------------------------------------------------------------- // Sets up a staff member using the specified information. //----------------------------------------------------------------- public StaffMember (String eName, String eAddress, String ePhone) { name = eName; address = eAddress; phone = ePhone; } //----------------------------------------------------------------- // Returns a string including the basic employee information. //----------------------------------------------------------------- public String toString() { String result = "Name: " + name + "\n"; result += "Address: " + address + "\n"; result += "Phone: " + phone; return result; } //----------------------------------------------------------------- // Derived classes must define the pay method for each type of // employee. //----------------------------------------------------------------- public abstract double pay(); } //******************************************************************** // Employee.java Author: Lewis/Loftus/Cocking // // Represents a general paid employee. //******************************************************************** public class Employee extends StaffMember { private String socialSecurityNumber; private double payRate; //----------------------------------------------------------------- // Sets up an employee with the specified information. //----------------------------------------------------------------- public Employee (String eName, String eAddress, String ePhone, String socSecNumber, double rate) { super (eName, eAddress, ePhone); socialSecurityNumber = socSecNumber; payRate = rate; } //----------------------------------------------------------------- // Returns the pay rate for this employee. //----------------------------------------------------------------- public double getPayRate() { return payRate; } //----------------------------------------------------------------- // Returns information about an employee as a string. //----------------------------------------------------------------- public String toString() { String result = super.toString(); result += "\nSocial Security Number: " + socialSecurityNumber; return result; } //----------------------------------------------------------------- // Returns the amount this employee should be paid. //----------------------------------------------------------------- public double pay() { return payRate; } } //******************************************************************** // Executive.java Author: Lewis/Loftus/Cocking // // Represents an executive staff member, who can earn a bonus. //******************************************************************** public class Executive extends Employee { private double bonus; //----------------------------------------------------------------- // Sets up an executive with the specified information. //----------------------------------------------------------------- public Executive (String eName, String eAddress, String ePhone, String socSecNumber, double rate) { super (eName, eAddress, ePhone, socSecNumber, rate); bonus = 0; // bonus has yet to be awarded } //----------------------------------------------------------------- // Awards the specified bonus to this executive. //----------------------------------------------------------------- public void awardBonus (double execBonus) { bonus = execBonus; } //----------------------------------------------------------------- // Computes and returns the pay for an executive, which is the // regular employee payment plus a one-time bonus. //----------------------------------------------------------------- public double pay() { double payment = super.pay() + bonus; bonus = 0; return payment; } }