A company pays its employees on a weekly basis. The employees are of 3 types: Sa
ID: 3684084 • Letter: A
Question
A company pays its employees on a weekly basis. The employees are of 3 types: Salaried employees are paid a fixed weekly salary regardless of the hours worked, hourly employees are paid by the hour and receive overtime pay (1.5 )for all hours worked in excess of 40 hours, commission employees are paid a percentage of their sales. This application is to calculate the payroll polymorphic ally.
Create an abstract class named Employee which has three private members (lastName, firstName, and SSN). Employee class contains one abstract function (pure virstual fuction) called Earnings() (returns weekly pay) which will be implemented by its derived classes (SalariedEmployee, HourlyEmployee, and CommissionEmployee). Besides inheriting from its base class, SalariedEmployee has weeklySalary field, HourlyEmployee has hourlyWage and hoursWorked fields, CommissionEmployee has grossSales and commRate fields. Write Constructors ( including copy constrcutor ), getter for each data member and one satData() function to set the values for all fields, print () function for ach class to display employee and earning information, and implement Earnings() function for derived class. Use main() to test all classes and display weekly earnings.
Explanation / Answer
ClientEmployee.java
import java.util.ArrayList;
import java.util.Random;
import java.text.DecimalFormat;
public class ClientEmployee
{
public static void main (String[] args)
{
//calling construtors.
ArrayList<Employee> list = new ArrayList<Employee>();
Random rand = new Random();
DecimalFormat pricePattern = new DecimalFormat("$#,###,##0.00");
//single arrays for first and last name.
String [] fName = {"John", "Jane", "Mike", "Jeff", "Sam"};
String [] lName = {"Jones", "Smith", "Johnson", "Doe"};
//nested loop to polulate employee array.
for (int i = 0; i < 10; i++)
{
int value = rand.nextInt(4);//generate a random number from 0-3.
if (value == 0)//check, making this instance salary type.
{
String first = fName[rand.nextInt(5)];
String last = lName[rand.nextInt(4)];
String sSN = " ";
for (int j = 0; j < 9; j++)
{
int x = rand.nextInt(10);
sSN += x;
}
double salary = rand.nextInt(10000) + rand.nextDouble();
Employee e = new SalariedEmployee(first, last, sSN, salary);
list.add(e);
}
else if (value == 1)//check, making this instance hourly type.
{
String first = fName[rand.nextInt(5)];
String last = lName[rand.nextInt(4)];
String sSN = " ";
for (int j = 0; j < 9; j++)
{
int x = rand.nextInt(10);
sSN += x;
}
double wage = rand.nextInt(100) + rand.nextDouble();
int hours = rand.nextInt(60) + 1;
Employee e = new HourlyEmployee(first, last, sSN, wage, hours);
list.add(e);
}
else if (value == 2)//check, making this instance commission type.
{
String first = fName[rand.nextInt(5)];
String last = lName[rand.nextInt(4)];
String sSN = " ";
for (int j = 0; j < 9; j++)
{
int x = rand.nextInt(10);
sSN += x;
}
double sales = rand.nextInt(100000) + rand.nextDouble();
double commission = rand.nextDouble();
Employee e = new CommissionEmployee(first, last, sSN, sales, commission);
list.add(e);
}
else//making last number as base plus commission.
{
String first = fName[rand.nextInt(5)];
String last = lName[rand.nextInt(4)];
String sSN = " ";
for (int j = 0; j < 9; j++)
{
int x = rand.nextInt(10);
sSN += x;
}
double sales = rand.nextInt(100000) + rand.nextDouble();
double commission = rand.nextInt(10) + rand.nextDouble();
double salary = rand.nextInt(100000) + rand.nextDouble();
}
}
//for loop to display output for the array.
for (Employee e: list)
{
System.out.println(e);
System.out.println("The total earnings for the week is: " + pricePattern.format(e.getEarnings()) + " ");
}
}
}
Employee.java
import java.text.DecimalFormat;
public abstract class Employee
{
DecimalFormat MONEY = new DecimalFormat("$#,###,##0.00");
//instance varibles.
private String firstName;
private String lastName;
private String sSN;
//default constructor.
public Employee()
{
firstName = "Unknown";
lastName = "Unknown";
sSN = "000000000";
}
//overloaded contructor.
public Employee (String xFirstName, String xLastName, String xSSN)
{
firstName = xFirstName;
lastName = xLastName;
sSN = xSSN;
}
//mutator method for first name.
public void setFirstName(String newFirstName)
{
firstName = newFirstName;
}
//mutator method for last name.
public void setLastName(String newLastName)
{
lastName = newLastName;
}
//mutator method for ssn.
public void setSSN(String newSSN)
{
sSN = newSSN;
}
//access method for first name.
public String getFirstName()
{
return firstName;
}
//access method for last name.
public String getLastName()
{
return lastName;
}
//access method for ssn.
public String getSSN()
{
return sSN;
}
//toString method.
public String toString()
{
return ("Employee: " + lastName + ", " + firstName + " SSN:" + sSN);
}
//abstract access method for earnings.
public abstract double getEarnings();
}
HourlyEmployee.java
public class HourlyEmployee extends Employee
{
//instance variables.
private double wage;
private int hours;
private final int fullTime = 40;
private final double overTime = 1.5;
//default construtor.
public HourlyEmployee()
{
super();
wage = 0.0;
hours = 0;
}
//overloaded construtor.
public HourlyEmployee(String fName, String lName, String sSN, double xWage, int xHours)
{
super(fName, lName, sSN);
setWage(xWage);
setHours(xHours);
}
//mutator method for wage.
public void setWage(double newWage)
{
if(newWage >=0)
wage = newWage;
else
System.err.println("Wages cannot be negative");
}
//mutator method for hours.
public void setHours(int newHours)
{
if (newHours >=0)
hours = newHours;
else
System.err.println("Hours cannot be negative");
}
//access method for wage.
public double getWage()
{
return wage;
}
//access method for hours.
public int getHours()
{
return hours;
}
//overriding toString method.
public String toString()
{
return super.toString() + "; wages are: " + MONEY.format(wage) + " and hours are: " + hours;
}
//overriding acces earnings.
public double getEarnings()
{
double earnings;
if (hours <= fullTime)
earnings = wage * hours;
else
earnings = (wage * fullTime) + ((wage * overTime) * (hours - fullTime));
return earnings;
}
}
CommissionEmployee.java
import java.text.DecimalFormat;
public class CommissionEmployee extends Employee
{
DecimalFormat percentPattern = new DecimalFormat("#0.0%");
//instance variables.
private double grossSales;
private double commissionRate;
//default constructor.
public CommissionEmployee()
{
super();
grossSales = 0.0;
commissionRate = 0.0;
}
//overloaded constructor.
public CommissionEmployee(String fName, String lName, String sSN, double xGrossSales, double xCommissionRate)
{
super(fName, lName, sSN);
setGrossSales(xGrossSales);
setCommissionRate(xCommissionRate);
}
//mutator method.
public void setGrossSales(double newGrossSales)
{
if (newGrossSales >= 0)
grossSales = newGrossSales;
else
System.err.println("Gross sales cannot be negative.");
}
//mutator method.
public void setCommissionRate(double newCommissionRate)
{
if (newCommissionRate >= 0)
commissionRate = newCommissionRate;
else
System.err.println("Commission rate cannot be negative.");
}
//access method.
public double getGrossSales()
{
return grossSales;
}
//access method.
public double getCommissionRate()
{
return commissionRate;
}
//overriding toString.
public String toString()
{
return super.toString() + "; gross sales are: " + MONEY.format(grossSales) + " and commission rate is: " + percentPattern.format(commissionRate);
}
//overriding getEarnings.
public double getEarnings()
{
double earnings = commissionRate * grossSales;
return earnings;
}
}
SalariedEmployee.java
public class SalariedEmployee extends Employee
{
//intance variable.
private double weeklySalary;
//default constructor.
public SalariedEmployee()
{
super();
weeklySalary = 0.0;
}
//overloaded constructor.
public SalariedEmployee(String fName, String lName, String sSN,double xWeeklySalary)
{
super(fName, lName, sSN);
setWeeklySalary (xWeeklySalary);
}
//mutator method for weekly salary.
public void setWeeklySalary(double newWeeklySalary)
{
if (newWeeklySalary >= 0)
weeklySalary = newWeeklySalary;
else
System.err.println("Weekly salary connot be negative");
}
//access method for weekly salary.
public double getWeeklySalary()
{
return weeklySalary;
}
//overriding method.
public double getEarnings()
{
return weeklySalary;
}
//overriding toString method.
public String toString()
{
return super.toString() + "; weekly salary is: " + MONEY.format(weeklySalary);
}
}
sample output
Employee: Johnson, Mike SSN: 935562575; gross sales are: $86,118.14 and commission rate is: 24.9%
The total earnings for the week is: $21,470.74
Employee: Smith, John SSN: 651709911; wages are: $86.92 and hours are: 13
The total earnings for the week is: $1,129.94
Employee: Jones, Jeff SSN: 910106335; weekly salary is: $2,039.98
The total earnings for the week is: $2,039.98
Employee: Jones, Mike SSN: 119130117; wages are: $86.96 and hours are: 53
The total earnings for the week is: $5,173.89
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.