Java Language Part 1: You are hired to develop a payroll system for a company. T
ID: 3849497 • Letter: J
Question
Java Language Part 1: You are hired to develop a payroll system for a company. There are three types of employees: Manager, CommissionEmployee, and HourlyWorker. The common attributes and behaviors for all Employees are firstName, lastName, employeeID (3-digit number). Managers get paid a fixed weekly salary, commission employees get paid based on base salary and commission (commission rate x sales), hourly workers get paid an hourly wage wuith time-and-a-half-1.5 times the hourly wage for hours worked over 40 hours. For this assignment, you will create a more general Employee superclass that contains these instance variables and methods and a constructor. Create three other classes (Manager, CommissionEmployee, and HourlyWorker) that all inherit from class Employee. Each subclass should contain only the instance variables and methods that are not declared in superclass Employee as shown below: Manager: weekly salary CommissionEmployee: base weekly salary, commission rate, and weekly sales HourlyWorker: hours worked, wage (that represents the wages per hour) And earnings method for all the three subclasses to calculate weekly earnings. Write a tester program called Payroll.java to test your payroll program as follows: Create an employee for each type of employee, then calculate earnings for each employee. Here is a sample output: Manager: Steve Davis $2000 Commission Employee: John Kanet $1500 (Base salary: $800, sales: $7000, Commission rate: 10%) Hourly Worker: Thomas Hill $1100 (Hourly wage: $20, hours worked: 50) Part 2: The Class Payroll: Create a class called Payroll2.java with a main method to test your classes. This method should create an array of the size of type Employee that is specified in the first line of the data file. Then, populate the array using data from the text file payroll.txt using appropriate Java I/O techniques. Each line (except the fist line-which is the number of employees in the file) of the file will look similar to one of these: #Steve Davis,2000 //manager @Thomas Hill,20,50 //hourly worker *Lisa Green,800,6000,0.10 //Commission employee Here is a sample input data 4 #Steve Davis,2000 *John Kanet,800,7000,0.10 @Thomas Hill,20,50 *Lisa Green,800,6000,0.10 Based on the first letter in each line (*, @, #) your method will know whether to create a new Manager, HourlyWorker, or CommissionWorker object. Once created, populate the object with the remaining values then store it in the array. Finally, iterate through the array of employees using the enhanced for loop syntax, and print out each employee in the following format: Manager: Steve Davis $2000 Commission Employee: John Kanet $1500 (Base salary: $80, sales: $7000, Commission rate: 10%) Hourly Worker: Thomas Hill $1100 (Hourly wage: $20, hours worked: 50)
Explanation / Answer
public abstract class Employee {
private String firstName;
private String lastName;
private int employeeID;
/**
* @param firstName
* @param lastName
* @param employeeID
*/
public Employee(String firstName, String lastName, int employeeID) {
this.firstName = firstName;
this.lastName = lastName;
this.employeeID = employeeID;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @return the employeeID
*/
public int getEmployeeID() {
return employeeID;
}
/**
* @param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @param employeeID
* the employeeID to set
*/
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}
public abstract double getPaidSalary();
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return firstName + " " + lastName + " $" + getPaidSalary();
}
}
public class Managers extends Employee {
private double weeklySalary;
/**
* @param firstName
* @param lastName
* @param employeeID
* @param weeklySalary
*/
public Managers(String firstName, String lastName, int employeeID,
double weeklySalary) {
super(firstName, lastName, employeeID);
this.weeklySalary = weeklySalary;
}
/**
* @return the weeklySalary
*/
public double getWeeklySalary() {
return weeklySalary;
}
/**
* @param weeklySalary
* the weeklySalary to set
*/
public void setWeeklySalary(double weeklySalary) {
this.weeklySalary = weeklySalary;
}
@Override
public double getPaidSalary() {
// TODO Auto-generated method stub
return weeklySalary;
}
}
public class HourlyWorker extends Employee {
private double hoursWorked;
private int wage;
/**
* @param firstName
* @param lastName
* @param employeeID
* @param hoursWorked
* @param wage
*/
public HourlyWorker(String firstName, String lastName, int employeeID,
double hoursWorked, int wage) {
super(firstName, lastName, employeeID);
this.hoursWorked = hoursWorked;
this.wage = wage;
}
/**
* @return the hoursWorked
*/
public double getHoursWorked() {
return hoursWorked;
}
/**
* @return the wage
*/
public int getWage() {
return wage;
}
/**
* @param hoursWorked
* the hoursWorked to set
*/
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
/**
* @param wage
* the wage to set
*/
public void setWage(int wage) {
this.wage = wage;
}
@Override
public double getPaidSalary() {
// TODO Auto-generated method stub
double pay;
if (hoursWorked > 40) {
pay = (40 * wage) + (1.5 * (hoursWorked - 40) * wage);
} else {
pay = wage * hoursWorked;
}
return pay;
}
}
public class CommissionEmployee extends Employee {
double weeklySalary, commissionRate, weeklySales;
/**
* @param firstName
* @param lastName
* @param employeeID
* @param weeklySalary
* @param commissionRate
* @param weeklySales
*/
public CommissionEmployee(String firstName, String lastName,
int employeeID, double weeklySalary, double commissionRate,
double weeklySales) {
super(firstName, lastName, employeeID);
this.weeklySalary = weeklySalary;
this.commissionRate = commissionRate;
this.weeklySales = weeklySales;
}
/**
* @return the weeklySalary
*/
public double getWeeklySalary() {
return weeklySalary;
}
/**
* @return the commissionRate
*/
public double getCommissionRate() {
return commissionRate;
}
/**
* @return the weeklySales
*/
public double getWeeklySales() {
return weeklySales;
}
/**
* @param weeklySalary
* the weeklySalary to set
*/
public void setWeeklySalary(double weeklySalary) {
this.weeklySalary = weeklySalary;
}
/**
* @param commissionRate
* the commissionRate to set
*/
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
/**
* @param weeklySales
* the weeklySales to set
*/
public void setWeeklySales(double weeklySales) {
this.weeklySales = weeklySales;
}
/**
* method to get paid based on base salary and commission (commission rate x
* sales)
*
* @return
*/
public double getPaidSalary() {
return weeklySalary + (commissionRate * weeklySales);
}
}
public class Payroll {
public static void main(String[] args) {
Managers managers = new Managers("Steve", "Davi", 2222, 2000);
CommissionEmployee commissionEmployee = new CommissionEmployee("John",
"Kanet", 2223, 800, 0.1, 7000);
HourlyWorker hourlyWorker = new HourlyWorker("Thomas", "Hill", 2224,
50, 20);
System.out.println("Manager:" + managers);
System.out.println("Commission Employee:" + commissionEmployee);
System.out.println("Hourly Worker:" + hourlyWorker);
}
}
/*
OUTPUT:
Manager:Steve Davi $2000.0
Commission Employee:John Kanet $1500.0
Hourly Worker:Thomas Hill $1100.0*/
import java.io.File;
import java.util.Scanner;
public class Payroll2 {
public static void main(String[] args) {
Scanner scanner = null;
try {
scanner = new Scanner(new File("inputdata.txt"));
Employee[] employees;
int n = 0;
if (scanner.hasNext())
n = Integer.parseInt(scanner.nextLine());
employees = new Employee[n];
char empType;
int i = 0;
while (scanner.hasNext()) {
String line = scanner.nextLine();
empType = line.charAt(0);
String lineArr[] = line.split(",");
String firstName = lineArr[0].split(" ")[0].substring(1);
String lastName = lineArr[0].split(" ")[1];
switch (empType) {
case '#': {
double weeklySalary = Double.parseDouble(lineArr[1]);
Managers managers = new Managers(firstName, lastName, i,
weeklySalary);
employees[i] = managers;
}
break;
case '@': {
int wage = Integer.parseInt(lineArr[1]);
int hoursWorked = Integer.parseInt(lineArr[2]);
HourlyWorker hourlyWorker = new HourlyWorker(firstName,
lastName, i, hoursWorked, wage);
employees[i] = hourlyWorker;
}
break;
case '*': {
double weeklySalary = Double.parseDouble(lineArr[1]);
double weeklySales = Double.parseDouble(lineArr[2]);
double commissionRate = Double.parseDouble(lineArr[3]);
CommissionEmployee commissionEmployee = new CommissionEmployee(
firstName, lastName, i, weeklySalary,
commissionRate, weeklySales);
employees[i] = commissionEmployee;
}
break;
default:
break;
}
i++;
}
for (int j = 0; j < employees.length; j++) {
System.out.println(employees[j].getClass() + ": "
+ employees[j]);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
OUTPUT:
class Managers: Steve Davis $2000.0
class CommissionEmployee: John Kanet $1500.0
class HourlyWorker: Thomas Hill $1100.0
class CommissionEmployee: Lisa Green $1400.0
inputdata.txt:
4
#Steve Davis,2000
*John Kanet,800,7000,0.10
@Thomas Hill,20,50
*Lisa Green,800,6000,0.10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.