// employees of different types
// gets bonus in birth month
public abstract class Employee {
private String firstName;
private String lastName;
private String socialSecurityNumber;
private Date birthday;
/
public Employee( String first, String last, String ssn, Date b )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
birthday = b;
}
public void setFirstName( String first )
{
firstName = first;
}
public String getFirstName()
{
return firstName;
}
public void setLastName( String last )
{
lastName = last;
}
public String getLastName()
{
return lastName;
}
public void setSocialSecurityNumber( String number )
{
socialSecurityNumber = number; // should validate
}
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
}
public Date getBirthday()
{return birthday;
}
public String toString()
{
return getFirstName() + " " + getLastName() +
"\nsocial security number: " + getSocialSecurityNumber() + "birthday " + getBirthday()+"\n";
}
public abstract double earnings();
} // end abstract class Employee
__________________________________________
public class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary; // base salary per week
// constructor
public BasePlusCommissionEmployee( String first, String last,
String socialSecurityNumber, Date b, double grossSalesAmount,
double rate, double baseSalaryAmount )
{
super( first, last, socialSecurityNumber, b, grossSalesAmount, rate );
setBaseSalary( baseSalaryAmount );
}
// set base-salaried commission employee's base salary
public void setBaseSalary( double salary )
{
baseSalary = salary < 0.0 ? 0.0 : salary;
}
// return base-salaried commission employee's base salary
public double getBaseSalary()
{
return baseSalary;
}
// calculate base-salaried commission employee's earnings;
// override method earnings in CommissionEmployee
public double earnings()
{
return getBaseSalary() + super.earnings();
}
// return String representation of BasePlusCommissionEmployee
public String toString()
{
return "\nbase-salaried commission employee: " + super.toString();
//super.getFirstName() + " " + super.getLastName() +
// "\nsocial security number: " + super.getSocialSecurityNumber()+ " "+super.getBirthday();
}
} // end class BasePlusCommissionEmployee
___________________________________________
public class CommissionEmployee extends Employee {
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
// constructor
public CommissionEmployee( String first, String last,
String socialSecurityNumber, Date b,
double grossWeeklySales, double percent )
{
super( first, last, socialSecurityNumber ,b);
setGrossSales( grossWeeklySales );
setCommissionRate( percent );
}
// set commission employee's rate
public void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
}
// return commission employee's rate
public double getCommissionRate()
{
return commissionRate;
}
// set commission employee's weekly base salary
public void setGrossSales( double sales )
{
grossSales = sales < 0.0 ? 0.0 : sales;
}
// return commission employee's gross sales amount
public double getGrossSales()
{
return grossSales;
}
// calculate commission employee's pay;
// override abstract method earnings in Employee
public double earnings()
{
return getCommissionRate() * getGrossSales();
}
// return String representation of CommissionEmployee object
public String toString()
{
return "\ncommission employee: " + super.toString();
}
} // end class CommissionEmployee
_______________________________________________
public class HourlyEmployee extends Employee {
private double wage; // wage per hour
private double hours; // hours worked for week
// constructor
public HourlyEmployee( String first, String last,
String socialSecurityNumber, Date b, double hourlyWage, double hoursWorked )
{
super( first, last, socialSecurityNumber, b );
setWage( hourlyWage );
setHours( hoursWorked );
}
// set hourly employee's wage
public void setWage( double wageAmount )
{
wage = wageAmount < 0.0 ? 0.0 : wageAmount;
}
// return wage
public double getWage()
{
return wage;
}
// set hourly employee's hours worked
public void setHours( double hoursWorked )
{
hours = ( hoursWorked >= 0.0 && hoursWorked <= 168.0 ) ?
hoursWorked : 0.0;
}
// return hours worked
public double getHours()
{
return hours;
}
// calculate hourly employee's pay;
// override abstract method earnings in Employee
public double earnings()
{
if ( hours <= 40 ) // no overtime
return wage * hours;
else
return 40 * wage + ( hours - 40 ) * wage * 1.5;
}
// return String representation of HourlyEmployee object
public String toString()
{
return "\nhourly employee: " + super.toString();
}
} // end class HourlyEmployee
______________________________________________
public class SalariedEmployee extends Employee {
private double weeklySalary;
// constructor
public SalariedEmployee( String first, String last,
String socialSecurityNumber, Date b,double salary )
{
super( first, last, socialSecurityNumber,b );
setWeeklySalary( salary );
}
// set salaried employee's salary
public void setWeeklySalary( double salary )
{
weeklySalary = salary < 0.0 ? 0.0 : salary;
}
// return salaried employee's salary
public double getWeeklySalary()
{
return weeklySalary;
}
// calculate salaried employee's pay;
// override abstract method earnings in Employee
public double earnings()
{
return getWeeklySalary();
}
// return String representation of SalariedEmployee object
public String toString()
{
return "\nsalaried employee: " + super.toString();
}
} // end class SalariedEmployee
__________________________________________
public class Date {
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
public Date( int theMonth, int theDay, int theYear )
{
month = theMonth ;
year = theYear;
day = theDay ;
} // end Date constructor
public String toString()
{
return month + "/" + day + "/" + year;
}
public int getMonth()
{ return month;
}
} // end class Date
_______________________________________
import javax.swing.JOptionPane;
public class PayrollSystemTest {
public static void main( String[] args )
{
Employee employees[] = new Employee[ 4 ];
employees[ 0 ] = new SalariedEmployee( "John", "Smith",
"111-11-1111",new Date (12,10,1970), 800.00 );
employees[ 1 ] = new CommissionEmployee( "Sue", "Jones",
"222-22-2222",new Date (10,10,1970), 10000, .06 );
employees[ 2 ] = new BasePlusCommissionEmployee( "Bob", "Lewis",
"333-33-3333", new Date (9,10,1970),5000, .04, 300 );
employees[ 3 ] = new HourlyEmployee( "Karen", "Price",
"444-44-4444",new Date (8,10,1970), 16.75, 40 );
String output = "";
// generically process each element in array employees
for ( int i = 0; i < employees.length; i++ )
{
output += employees[ i ].toString() + "\n";
if (employees[i].getBirthday().getMonth() == 10)
output +="Earnings = " +employees[i].earnings()+ " Birthday Bonus " + (employees[i].earnings() *.10 ) + " = " + (employees[i].earnings()* .10 + employees[i].earnings()) + "\n";
else
output += "Earnings = "+employees[i].earnings();
output += "\n";
}
JOptionPane.showMessageDialog( null, output ); // display output all at once
System.exit( 0 );
} // end main
} // end class PayrollSystemTest