Exercise 10.12: Payroll Modification Modify the payroll system of Figs 10.4 –10.
ID: 3581627 • Letter: E
Question
Exercise 10.12: Payroll Modification Modify the payroll system of Figs 10.4 –10.9 to include private instance variable birthdate in class Employee. Use class Date of Fig 8.7 to represent an employee’s birthday. Add get methods to class Date. Assume that payroll is processed once per month. Create an array of Employee variables to store references to the various employee objects. In a loop, calculate the payroll for each Employee (polymorphic ally), and add a $100.00 bonus to the persons payroll amount if the current month is the one in which the Employee’s birthdate occurs. Using Paul & HarveyDeitel's Book Java: How to Program 10th edition. need this soon.
Explanation / Answer
Date.java
package payrollsystems;
public class Date
{
private int Months; // Monthss
private int dayy; // Monthss
private int years; // years
// constructor:checking proper value of Months;
// check dayy
public Date( int theMonths, int theDayy, int theYears )
{
Months = theMonths; // Months validation
years = theYears; // years validation
dayy = checkDayy( theDayy ); // dayy validation
} // constructor date end
public void setMonths(int theMonths)
{
Months = theMonths;
}
// confirmation of proper Months value
public int getMonths()
{
if ( Months > 0 && Months <= 12 ) // validation of Months
return Months;
return 0;
} // end the method ofcheckMonths
// utility method to confirm proper dayy value based on Months and years
private int checkDayy( int testDayy )
{
int dayysPerMonths[] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// check if dayy in range for Months
if ( testDayy > 0 && testDayy <= dayysPerMonths[ Months ] )
return testDayy;
// checking for leap years
if ( Months == 2 && testDayy == 29 && ( years % 400 == 0 ||
( years % 4 == 0 && years % 100 != 0 ) ) )
return testDayy;
return 0; // maintains object in consistent state
} // checkDayy method end
// date:Months/dayy/years
public String toString()
{
return String.format( "%d/%d/%d", Months, dayy, years );
} //
} //
Employeess.java
package payrollsystems;
public abstract class Employeess
{
private String fname;
private String lname;
private String socialSecurityNumbers;
private Date birthdayyy;
// constructor with three-argument
public Employeess( String first, String last, String ssn , Date DayyOfBirth)
{
fname = first;
lname = last;
socialSecurityNumbers = ssn;
birthdayyy = DayyOfBirth;
} // end three-argument Employeess constructor
// seting first name
public void setFname( String first )
{
fname = first;
} // end the method ofof setFname
// first name is returned
public String getFname()
{
return fname;
} // end the method of getFname
// set last name
public void setLname( String last )
{
lname = last;
} // end the method of setLname
// return last name
public String getLname()
{
return lname;
} // end the method of getLname
// seting social security number
public void setSocialSecurityNumbers( String ssn )
{
socialSecurityNumbers = ssn; // should validate ssn number
} // end the method of setSocialSecurityNumbers
// social security number is returned
public String getSocialSecurityNumbers()
{
return socialSecurityNumbers;
} // end the method of getSocialSecurityNumbers
// set birthdayyy
public void setBirthdayyy(Date DayyOfBirth)
{
birthdayyy = DayyOfBirth;
}// end method
// geting birthdayyy
public Date getBirthdayyy()
{
return birthdayyy;
}
// return String representation of Employeess object
@Override
public String toString()
{
return String.format( "%s %s social security numbers: %s birthdayyy: %s ",
getFname(), getLname(), getSocialSecurityNumbers(), getBirthdayyy());
} // end the method of toString
// method overridden is done her
public abstract double earnings(); // does not have implementation here
} // abstract class Employeess is ended
HourlyEmployees.java
package payrollsystems;
public class HourlyEmployees extends Employees
{
private double Wagess; // Wages per hour
private double hourss; // hourss worked for week
// constructor with five-arguments
public HourlyEmployees( String first, String last, String ssn, Date DayyOfBirth, double hourlyWages, double hourssWorked )
{
super( first, last, ssn, DayyOfBirth);
setWages( hourlyWages ); // validate and store hourly Wages
setHourss( hourssWorked ); // validate and store hourss worked
} // end constructor
// seting Wages
public final void setWages( double hourlyWages )
{
Wages = ( hourlyWages < 0.0 ) ? 0.0 : hourlyWages;
} // end the method of setWages
// Wages returned here
public double getWages()
{
return Wages;
} // end the method ofgetWages
// seting hourss worked
public final void setHourss( double hourssWorked )
{
hourss = ( ( hourssWorked >= 0.0 ) && ( hourssWorked <= 168.0 ) ) ?
hourssWorked : 0.0;
} // end the method of setHourss
// hourss worked returned
public double getHourss()
{
return hourss;
} // end the method of getHourss
// calculating earnings
public double earnings()
{
if ( getHourss() <= 40 ) // no overtime
return getWages() * getHourss();
else
return 40 * getWages() + ( getHourss() - 40 ) * getWages() * 1.5;
} // end the method ofearnings
// return String representation of HourlyEmployees object
@Override
public String toString()
{
return String.format( "hourly Employees: %s %s: $%,.2f; %s: %,.2f",
super.toString(), "hourly Wages", getWages(),
"hourss worked", getHourss() );
} // end the method of toString
} // HourlyEmployees class ended
SalariedEmployees.java
package payrollsystems;
public class SalariedEmployees extends Employees
{
private double weeklySalary;
// constructor with four-arguments
public SalariedEmployees( String first, String last, String ssn, Date DayyOfBirth, double salary )
{
super( first, last, ssn, DayyOfBirth); // Employees constructor is passed
setWeeklySalary( salary ); // validating and storing salary
} // end four-argument SalariedEmployees constructor
// seting salary
public final void setWeeklySalary( double salary )
{
weeklySalary = salary < 0.0 ? 0.0 : salary;
} // end the method of setWeeklySalary
// salary is returned
public double getWeeklySalary()
{
return weeklySalary;
} // end the method of getWeeklySalary
// calculating earnings
public double earnings()
{
return getWeeklySalary();
} // end the method of earnings
// returning String representation of SalariedEmployees object
@Override
public String toString()
{
return String.format( "salaried Employees: %s %s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary() );
} // end the method of toString
} // end class SalariedEmployees
CommissionEmployees.java
package payrollsystems;
public class CommissionEmployees extends Employees
{
private double grossSales; // gross sales weekly
private double commissionRate; // commission in percentage
// constructor with five-arguments
public CommissionEmployees( String first, String last, String ssn, Date DayyOfBirth, double sales, double rate )
{
super( first, last, ssn, DayyOfBirth);
setGrossSales( sales );
setCommissionRate( rate );
} // ending five-argument CommissionEmployees constructor
// seting commission rate
public final void setCommissionRate( double rate )
{
commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
} // end the method ofsetCommissionRate
// return commission rate
public double getCommissionRate()
{
return commissionRate;
} // end the method of getCommissionRate
// seting gross sales amount
public final void setGrossSales( double sales )
{
grossSales = ( sales < 0.0 ) ? 0.0 : sales;
} // end the method of setGrossSales
// gross sales amount is returned
public double getGrossSales()
{
return grossSales;
} // end the method of getGrossSales
// calculation of earnings
public double earnings()
{
return getCommissionRate() * getGrossSales();
} // end the method ofearnings
// returning String representation of CommissionEmployees object
@Override
public String toString()
{
return String.format( "%s: %s %s: $%,.2f; %s: %.2f",
"commission Employees", super.toString(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate() );
} // end the method oftoString
} // end class CommissionEmploye
BasePlusCommissionEmployees.java
package payrollsystems;
public class BasePlusCommissionEmployees extends CommissionEmployees
{
private double baseSalary; // base salary per week initialized
// constructor with six-arguments
public BasePlusCommissionEmployees( String first, String last,
String ssn, Date DayyOfBirth,double sales, double rate, double salary )
{
super( first, last, ssn, DayyOfBirth,sales, rate );
setBaseSalary( salary ); // validation and store base salary
} // ending of six-argument BasePlusCommissionEmployees constructor
// seting base salary
public final void setBaseSalary( double salary )
{
baseSalary = ( salary < 0.0 ) ? 0.0 : salary; // non-negative integer
} // end the method of setBaseSalary
// base salary is returned
public double getBaseSalary()
{
return baseSalary;
} // end the method of getBaseSalary
// calculation of earnings; override method earnings in CommissionEmployees
@Override
public double earnings()
{
return getBaseSalary() + super.earnings();
} // end the method of earnings
// return String representation of BasePlusCommissionEmployees object
@Override
public String toString()
{
return String.format( "%s %s; %s: $%,.2f",
"base-salaried", super.toString(),
"base salary", getBaseSalary() );
} // end the method of toString
} // end class of BasePlusCommissionEmployees
PieceWorker.java
package payrollsystems;
public class PieceWorker extends Employees {
private double Wages; //Wages per piece
private double Piece; //number of Piece produced
// constructor with five-arguments
public PieceWorker( String first, String last, String ssn, Date DayyOfBirth, double Wages, double Piece )
{
super( first, last, ssn, DayyOfBirth);
setWages( Wages ); // validation and storing Wages
setPiece( Piece ); // validation and storing Piece;
} // end five-argument HourlyEmployees constructor
// seting Wages
public final void setWages( double Wages )
{
Wages = ( Wages < 0.0 ) ? 0.0 : Wages;
} // end the method of setWages
// Wages returned
public double getWages()
{
return Wages;
} // end the method of getWages
// seting Piece
public final void setPiece( double Piece )
{
Piece = ( ( Piece >= 0.0 ) && ( Piece <= 168.0 ) ) ?
Piece : 0.0;
} // end the method of setPiece
// Piece returned
public double getPiece()
{
return Piece;
} // end the method ofgetPiece
// calculate earnings; override abstract method earnings in Employees
public double earnings()
{
return getWages() * getPiece();
} // end the method of earnings
// return String representation of PieceWorker object
@Override
public String toString()
{
return String.format( "Piece Worker: %s %s: $%,.2f; %s: %,.2f",
super.toString(), "Wages per piece", getWages(),
"Number of Piece produced", getPiece() );
} // end the method of toString
}
PayrollSystemTest.java
package payrollsystems;
public class PayrollSystemTest
{
public static void main( String args[] )
{
// creating subclass objects here
Date currentDate = new Date(6,20,2010);
System.out.printf( "Current Date is: %s ", currentDate.toString() );
System.out.println("###################################");
SalariedEmployees salariedEmployees =
new SalariedEmployees( "laxmi", "Smiths", "111-11-1111", new Date(15,1,2014),800.00 );
HourlyEmployees hourlyEmployees =
new HourlyEmployees( "sundari", "Prices", "222-22-2222", new Date(6,15,1888),16.75, 40 );
CommissionEmployees commissionEmployees =
new CommissionEmployees(
"Sue", "sanjeev", "333-33-3333", new Date(8,25,1974),10000, .06 );
BasePlusCommissionEmployees basePlusCommissionEmployees =
new BasePlusCommissionEmployees(
"Bobs", "Lewis", "444-44-4444", new Date(9,27,1978),5000, .04, 300 );
PieceWorker pieceWorker = new PieceWorker("Ralphs", "Tangs", "777-223-987",
new Date(6,25,1985), 213, 16);
System.out.println( "Employeess processed individually: " );
System.out.printf( "%s %s: $%,.2f ",
salariedEmployees, "earned", salariedEmployees.earnings() );
System.out.printf( "%s %s: $%,.2f ",
hourlyEmployees, "earned", hourlyEmployees.earnings() );
System.out.printf( "%s %s: $%,.2f ",
commissionEmployees, "earned", commissionEmployees.earnings() );
System.out.printf( "%s %s: $%,.2f ",
basePlusCommissionEmployees,
"earned", basePlusCommissionEmployees.earnings() );
System.out.printf( "%s %s: $%,.2f ",
pieceWorker, "earned", pieceWorker.earnings() );
// creatingfour-element Employees array
Employees Employeess[] = new Employees[ 5 ];
// initialize array with Employeess
Employeess[ 0 ] = salariedEmployees;
Employeess[ 1 ] = hourlyEmployees;
Employeess[ 2 ] = commissionEmployees;
Employeess[ 3 ] = basePlusCommissionEmployees;
Employeess[ 4 ] = pieceWorker;
System.out.println("………………….");
System.out.println( "Employeess processed polymorphically: " );
// generically process each element in array Employeess
for ( Employees currentEmployees : Employeess )
{
System.out.println( currentEmployees ); // invokes toString
// determination of whether element is a BasePlusCommissionEmployees
if ( currentEmployees instanceof BasePlusCommissionEmployees )
{
// downcasting Employees reference to
// BasePlusCommissionEmployees reference
BasePlusCommissionEmployees Employees =
( BasePlusCommissionEmployees ) currentEmployees;
double oldBaseSalary = Employees.getBaseSalary();
Employees.setBaseSalary( 1.10 * oldBaseSalary );
System.out.printf(
"new base salary with 10%% increase is: $%,.2f ",
Employees.getBaseSalary() );
} // end if
if(currentDate.getMonths()==currentEmployees.getBirthdayyy().getMonths())
{
System.out.printf("earned $%,.2f. %s ", currentEmployees.earnings() + 100.00,
"Note: added a $100 bonus to your payroll amount for birthdayyy!!!" );
}else{
System.out.printf("earned $%,.2f ", currentEmployees.earnings() );
}
} // for ended
// get type name of each object in Employeess array
for ( int j = 0; j < Employeess.length; j++ )
System.out.printf( "Employees %d is a %s ", j,
Employeess[ j ].getClass().getSimpleName() );
} // main ednde
} // class PayrollSystemTest ended
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.