Develop a Java application to calculate the monthly paycheck
Develop a Java application to calculate the monthly paychecks for a number of different types of employees. The employee types are created in a subclass array based on parent base class Employee. Initial code is provided for each class and for a driver class.
You will need to compile each Java source file (provided below) separately, in its own file since they are public classes, to create a .class file for each, ideally keeping them all in the same directory.
You should first compile and execute the original classes and then make your customer modifications for this program.
You should compile Date.java first, then Employee.java, then CommissionEmployee.java, then BasePlusCommission.java, then HourlyEmployee.java, then SalariedEmployee.java, and finally PayrollSystemTest.java. And maintain this compilation order when you make your customized modifications to these classes later.
As part of the modifications you need to make to the supplied code, you are to prompt the user for all the data that is now provided in the code for the class PayrollSystemTest (i.e. instead of using these values from their declaration in the code, get data from prompting the user for it, one value at a time). All program output, including prompts and results and all program input must be done from the PayrollSystemTest class.
In addition you need to add a new private instance field \"birthDate\" to the Employee class. The “birthDate” field must be a Date data type, not a String! Add get and set methods to Employee class for this new birthDate field.
The birthDate field must be determined from separate integer values, supplied by the user, for the birth month, day, and year. As with all other inputs, these three birth date components must be prompted from and input to class PayrollSystemTest. Once input, these three parameter values must then be supplied to the appropriate modified employee subclass constructor, and then the subclass constructors supply these three values to their parent class, a modified Employee class constructor.
The single birthDate field of a Date data type must be declared and initialized in the Employee class.
In class PayrollSystemTest create an array of Employee variables to store references to the various employee objects. In a loop, calculate the monthly paycheck amount for each Employee (polymorphically), and add a $100.00 bonus to the person\'s monthly payroll amount if the current month (i.e. November) is the month in which the Employee\'s birthday occurs.
Your program should input data and create the five employees below on the version of the program you submit for grading. Your program must calculate their monthly salaries for November (assumed to be the \"current\" month). Assume that the monthly salary is simply four times the weekly salary.
one Salaried Employee with a weekly salary of $1000 and a birthday of January 1, 1960,
one Salaried Employee with a weekly salary of $1000 and a birthday in this month (November 1, 1961)
one Commission Employee Worker with a gross weekly sales of $12,000 and a 5% commission rate and a birthday of February 1, 1962,
one Base Plus Commission Employee with a gross weekly sales of $10,000, a 3% commission, and a $500 base weekly salary with a birthday of March 1, 1963, and
one Hourly Employee with a wage of $20/hour working for 40 hours and a birthday of April 1, 1964
You should make up your own employee names for the five required employees above. Make sure you create the employees in the order above.
In prompting the user for all input values for the five employees above, your program
essentially replaces the existing main() method of PayrollSystemTest.java. You may user whatever input class you want (e.g. JOptionPane, Scanner). Keep all other lines of the code in PayrollSystemTest as they are originally, including the code that gives a 10% increase to the Base Plus Commission employee.
You will probably find it helpful to prompt for the worker type first. You may
hardcode \"November\" as the special bonus month.
Since the outputs are to be for monthly paychecks, just multiply the weekly
salaries, weekly output quantities, and weekly hours by 4. Display your results
for each worker\'s salary after all data for that employee type has been entered
and the salary determined.
Solution
public class PayrollSystemTest
{
public static void main( String args[] )
{
// create subclass objects
SalariedEmployee salariedEmployee =
new SalariedEmployee( \"John\", \"Smith\", \"111-11-1111\", 800.00 );
HourlyEmployee hourlyEmployee =
new HourlyEmployee( \"Karen\", \"Price\", \"222-22-2222\", 16.75, 40 );
CommissionEmployee commissionEmployee =
new CommissionEmployee(
\"Sue\", \"Jones\", \"333-33-3333\", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee =
new BasePlusCommissionEmployee(
\"Bob\", \"Lewis\", \"444-44-4444\", 5000, .04, 300 );
System.out.println( \"Employees processed individually:\ \" );
System.out.printf( \"%s\ %s: $%,.2f\ \ \",
salariedEmployee, \"earned\", salariedEmployee.earnings() );
System.out.printf( \"%s\ %s: $%,.2f\ \ \",
hourlyEmployee, \"earned\", hourlyEmployee.earnings() );
System.out.printf( \"%s\ %s: $%,.2f\ \ \",
commissionEmployee, \"earned\", commissionEmployee.earnings() );
System.out.printf( \"%s\ %s: $%,.2f\ \ \",
basePlusCommissionEmployee,
\"earned\", basePlusCommissionEmployee.earnings() );
// create four-element Employee array
Employee employees[] = new Employee[ 4 ];
// initialize array with Employees
employees[ 0 ] = salariedEmployee;
employees[ 1 ] = hourlyEmployee;
employees[ 2 ] = commissionEmployee;
employees[ 3 ] = basePlusCommissionEmployee;
System.out.println( \"Employees processed polymorphically:\ \" );
// generically process each element in array employees
for ( Employee currentEmployee : employees )
{
System.out.println( currentEmployee ); // invokes toString
// determine whether element is a BasePlusCommissionEmployee
if ( currentEmployee instanceof BasePlusCommissionEmployee )
{
// downcast Employee reference to
// BasePlusCommissionEmployee reference
BasePlusCommissionEmployee employee =
( BasePlusCommissionEmployee ) currentEmployee;
double oldBaseSalary = employee.getBaseSalary();
employee.setBaseSalary( 1.10 * oldBaseSalary );
System.out.printf(
\"new base salary with 10%% increase is: $%,.2f\ \",
employee.getBaseSalary() );
} // end if
System.out.printf(
\"earned $%,.2f\ \ \", currentEmployee.earnings() );
} // end for
// get type name of each object in employees array
for ( int j = 0; j < employees.length; j++ )
System.out.printf( \"Employee %d is a %s\ \", j,
employees[ j ].getClass().getName() );
} // end main
} // end class PayrollSystemTest
// Employee.java
// Employee abstract superclass.
public abstract class Employee
{
private String firstName;
private String lastName;
private String socialSecurityNumber;
// three-argument constructor
public Employee( String first, String last, String ssn )
{
firstName = first;
lastName = last;
socialSecurityNumber = ssn;
} // end three-argument Employee constructor
// set first name
public void setFirstName( String first )
{
firstName = first;
} // end method setFirstName
// return first name
public String getFirstName()
{
return firstName;
} // end method getFirstName
// set last name
public void setLastName( String last )
{
lastName = last;
} // end method setLastName
// return last name
public String getLastName()
{
return lastName;
} // end method getLastName
// set social security number
public void setSocialSecurityNumber( String ssn )
{
socialSecurityNumber = ssn; // should validate
} // end method setSocialSecurityNumber
// return social security number
public String getSocialSecurityNumber()
{
return socialSecurityNumber;
} // end method getSocialSecurityNumber
// return String representation of Employee object
public String toString()
{
return String.format( \"%s %s\ social security number: %s\",
getFirstName(), getLastName(), getSocialSecurityNumber() );
} // end method toString
// abstract method overridden by subclasses
public abstract double earnings(); // no implementation here
} // end abstract class Employee
// Date.java
// Date class declaration.
public class Date
{
private int month; // 1-12
private int day; // 1-31 based on month
private int year; // any year
// constructor: call checkMonth to confirm proper value for month;
// call checkDay to confirm proper value for day
public Date( int theMonth, int theDay, int theYear )
{
month = checkMonth( theMonth ); // validate month
year = theYear; // could validate year
day = checkDay( theDay ); // validate day
System.out.printf(
\"Date object constructor for date %s\ \", this );
} // end Date constructor
// utility method to confirm proper month value
private int checkMonth( int testMonth )
{
if ( testMonth > 0 && testMonth <= 12 ) // validate month
return testMonth;
else // month is invalid
{
System.out.printf(
\"Invalid month (%d) set to 1.\", testMonth );
return 1; // maintain object in consistent state
} // end else
} // end method checkMonth
// utility method to confirm proper day value based on month and year
private int checkDay( int testDay )
{
int daysPerMonth[] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// check if day in range for month
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
// check for leap year
if ( month == 2 && testDay == 29 && ( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
System.out.printf( \"Invalid day (%d) set to 1.\", testDay );
return 1; // maintain object in consistent state
} // end method checkDay
// return a String of the form month/day/year
public String toString()
{
return String.format( \"%d/%d/%d\", month, day, year );
} // end method toString
} // end class Date






