Docsity

Java Payroll System: Implementing Polymorphism with Employee Classes, Lab Reports of Computer Science

Partial preview of the text

Download Java Payroll System: Implementing Polymorphism with Employee Classes and more Lab Reports Computer Science in PDF only on Docsity!

CS

Java Handout For Java Lab

Polymorphism Help

Example: A Payroll System

Using Polymorphism

  • Abstract Methods and Polymorphism
    • Abstract superclass Employee
      • Method earnings and toString applies to all employees
    • Concrete Employee subclasses declared final
      • Boss
      • CommissionWorker
      • PieceWorker
      • HourlyWorker

Example: A Payroll System

Using Polymorphism (Employee)

21 // get last name

22 public String getLastName()

24 return lastName;

27 public String toString()

29 return firstName + ' ' + lastName;

32 // Abstract method that must be implemented for each

33 // derived class of Employee from which objects

34 // are instantiated.

35 public abstract double earnings();

37 } // end class Employee

Example: A Payroll System

Using Polymorphism (Boss)

1 // Boss.java 2 // Boss class derived from Employee. 3 4 public final class Boss extends Employee { 5 private double weeklySalary; 6 7 // constructor for class Boss 8 public Boss( String first, String last, double salary ) 9 { 10 super( first, last ); // call superclass constructor 11 setWeeklySalary( salary ); 12 } 13 14 // set Boss's salary 15 public void setWeeklySalary( double salary ) 16 { 17 weeklySalary = ( salary > 0? salary : 0 ); 18 } 19 20 // get Boss's pay

Example: A Payroll System Using

Polymorphism (CommissionWorker)

1 // CommissionWorker.java 2 // CommissionWorker class derived from Employee 3 4 public final class CommissionWorker extends Employee { 5 private double salary; // base salary per week 6 private double commission; // amount per item sold 7 private int quantity; // total items sold for week 8 9 // constructor for class CommissionWorker 10 public CommissionWorker( String first, String last, 11 double salary, double commission, int quantity ) 12 { 13 super( first, last ); // call superclass constructor 14 setSalary( salary ); 15 setCommission( commission ); 16 setQuantity( quantity ); 17 } 18 19 // set CommissionWorker's weekly base salary 20 public void setSalary( double weeklySalary )

Example: A Payroll System Using

Polymorphism (CommissionWorker)

22 salary = ( weeklySalary > 0? weeklySalary : 0 ); 23 } 24 25 // set CommissionWorker's commission 26 public void setCommission( double itemCommission ) 27 { 28 commission = ( itemCommission > 0? itemCommission : 0 ); 29 } 30 31 // set CommissionWorker's quantity sold 32 public void setQuantity( int totalSold ) 33 { 34 quantity = ( totalSold > 0? totalSold : 0 ); 35 } 36 37 // determine CommissionWorker's earnings 38 public double earnings() 39 { 40 return salary + commission * quantity;

Example: A Payroll System Using

Polymorphism (PieceWorker)

1 // PieceWorker.java 2 // PieceWorker class derived from Employee 3 4 public final class PieceWorker extends Employee { 5 private double wagePerPiece; // wage per piece output 6 private int quantity; // output for week 7 8 // constructor for class PieceWorker 9 public PieceWorker( String first, String last, 10 double wage, int numberOfItems ) 11 { 12 super( first, last ); // call superclass constructor 13 setWage( wage ); 14 setQuantity( numberOfItems); 15 } 16 17 // set PieceWorker's wage 18 public void setWage( double wage ) 19 { 20 wagePerPiece = ( wage > 0? wage : 0 );

Example: A Payroll System Using

Polymorphism (PieceWorker)

23 // set number of items output 24 public void setQuantity( int numberOfItems ) 25 { 26 quantity = ( numberOfItems > 0? numberOfItems : 0 ); 27 } 28 29 // determine PieceWorker's earnings 30 public double earnings() 31 { 32 return quantity * wagePerPiece; 33 } 34 35 public String toString() 36 { 37 return "Piece worker: " + super.toString(); 38 } 39 40 } // end class PieceWorker

Example: A Payroll System Using

Polymorphism (HourlyWorker)

23 // Set the hours worked

24 public void setHours( double hoursWorked ) 25 { 26 hours = ( hoursWorked >= 0 && hoursWorked < 168? 27 hoursWorked : 0 );

28 } 29 30 // Get the HourlyWorker's pay 31 public double earnings() { return wage * hours; }

32 33 public String toString() 34 { 35 return "Hourly worker: " + super.toString();

36 } 37 38 } // end class HourlyWorker

Example: A Payroll System

Using Polymorphism (Test)

1 // Test.java 2 // Driver for Employee hierarchy 3 4 // Java core packages 5 import java.text.DecimalFormat; 6 7 // Java extension packages 8 import javax.swing.JOptionPane; 9 10 public class Test { 11 12 // test Employee hierarchy 13 public static void main( String args[] ) 14 { 15 Employee employee; // superclass reference 16 String output = ""; 17 18 Boss boss = new Boss( "John", "Smith", 800.0 ); 19

Example: A Payroll System

Using Polymorphism (Test)

40 // Employee reference to a CommissionWorker 41 employee = commissionWorker; 42 43 output += employee.toString() + " earned $" + 44 precision2.format( employee.earnings() ) + "\n" + 45 commissionWorker.toString() + " earned $" + 46 precision2.format( 47 commissionWorker.earnings() ) + "\n"; 48 49 // Employee reference to a PieceWorker 50 employee = pieceWorker; 51 52 output += employee.toString() + " earned $" + 53 precision2.format( employee.earnings() ) + "\n" + 54 pieceWorker.toString() + " earned $" + 55 precision2.format( pieceWorker.earnings() ) + "\n"; 56 57 // Employee reference to an HourlyWorker 58 employee = hourlyWorker; 59

Example: A Payroll System

Using Polymorphism (Test)

60 output += employee.toString() + " earned $" +

61 precision2.format( employee.earnings() ) + "\n" +

62 hourlyWorker.toString() + " earned $" +

63 precision2.format( hourlyWorker.earnings() ) + "\n";

65 JOptionPane.showMessageDialog( null, output,

66 "Demonstrating Polymorphism",

67 JOptionPane.INFORMATION_MESSAGE );

69 System.exit( 0 );

72 } // end class Test