Java Language Part 1: You are hired to develop a payroll system for a company. T
ID: 3904784 • 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
Below is your code.
Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
Employee.java
public class Employee {
String firstName, lastName;
int employeeID;
public Employee(String firstName, String lastName, int employeeID) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.employeeID = employeeID;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}
public int weeklyEarnings() {
return Integer.MIN_VALUE;
}
}
Manager.java
public class Manager extends Employee {
int weeklySalary;
public Manager(String firstName, String lastName, int employeeID, int weeklySalary) {
super(firstName, lastName, employeeID);
this.weeklySalary = weeklySalary;
}
public int getWeeklySalary() {
return weeklySalary;
}
public void setWeeklySalary(int weeklySalary) {
this.weeklySalary = weeklySalary;
}
public int weeklyEarnings() {
return weeklySalary;
}
}
CommissionEmployee.java
public class CommissionEmployee extends Employee {
double commissionRate;
int sales, baseWeekSalary;
public CommissionEmployee(String firstName, String lastName, int employeeID, int baseWeekSalary, int sales,
double commissionRate) {
super(firstName, lastName, employeeID);
this.commissionRate = commissionRate;
this.sales = sales;
this.baseWeekSalary = baseWeekSalary;
}
public double getCommissionRate() {
return commissionRate;
}
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
public int getSales() {
return sales;
}
public void setSales(int sales) {
this.sales = sales;
}
public int getBaseWeekSalary() {
return baseWeekSalary;
}
public void setBaseWeekSalary(int baseWeekSalary) {
this.baseWeekSalary = baseWeekSalary;
}
public int weeklyEarnings() {
double earnings = baseWeekSalary + (sales * commissionRate);
return (int) earnings;
}
}
HourlyWorker.java
public class HourlyWorker extends Employee {
int hoursWorked, wage;
public HourlyWorker(String firstName, String lastName, int employeeID, int wage, int hoursWorked) {
super(firstName, lastName, employeeID);
this.hoursWorked = hoursWorked;
this.wage = wage;
}
public int getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(int hoursWorked) {
this.hoursWorked = hoursWorked;
}
public int getWage() {
return wage;
}
public void setWage(int wage) {
this.wage = wage;
}
public int weeklyEarnings() {
double earnings;
if (hoursWorked > 40) {
int extraHours = hoursWorked - 40;
earnings = (wage * 40) + (wage * extraHours * 1.5);
} else {
earnings = wage * hoursWorked;
}
return (int) earnings;
}
}
Part1 - Payroll.java
public class Payroll {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Create objects of all the three classes. Since nothing is mentioned
// about employeeID ,taking it as any value.
Manager obj1 = new Manager("Steve", "Davis ", 101, 2000);
HourlyWorker obj2 = new HourlyWorker("Thomas ", "Hill", 102, 20, 50);
CommissionEmployee obj3 = new CommissionEmployee("John", "Kanet", 103, 800, 7000, .10);
System.out.println("Manager: " + obj1.getFirstName() + " " + obj1.getLastName() + " $" + obj1.weeklyEarnings());
System.out.println(
"Hourly Worker: " + obj2.getFirstName() + " " + obj2.getLastName() + " $" + obj2.weeklyEarnings()
+ "(Hourly wage: $" + obj2.getWage() + ", hours worked: $" + obj2.getHoursWorked() + ")");
System.out.println("Commission Employee: " + obj3.getFirstName() + " " + obj3.getLastName() + " $"
+ obj3.weeklyEarnings() + "(Base Salary: $" + obj3.getBaseWeekSalary() + ", sales: $" + obj3.getSales()
+ ", Commission rate: " + obj3.getCommissionRate() + ")");
}
}
Part2 - Payroll2.java
public class Payroll2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// Read file data.txt
BufferedReader bufferedReader = new BufferedReader(new FileReader("data.txt"));
// Read first line to get size of type Employee
String line = bufferedReader.readLine();
int size = Integer.valueOf(line);
// Create array of type Employee
Employee[] employee = new Employee[size];
int index = 0;
while ((line = bufferedReader.readLine()) != null) {
// Split line on the basis of comma to get values
String tokens[] = line.trim().split(",");
String firstName, lastName;
// Since name is seperated by space , split it by " " and get
// first name and last name.
String names[] = tokens[0].split(" ");
firstName = names[0];
lastName = names[1];
// For manager class
if (firstName.charAt(0) == '#') {
int weeklyEarning = Integer.valueOf(tokens[1]);
employee[index] = new Manager(firstName, lastName, 1, weeklyEarning);
}
// For hourly worker class
else if (firstName.charAt(0) == '@') {
int wage = Integer.valueOf(tokens[1]);
int hoursWorked = Integer.valueOf(tokens[2]);
employee[index] = new HourlyWorker(firstName, lastName, 1, wage, hoursWorked);
} // For commission employee class
else {
int baseWeekSalary = Integer.valueOf(tokens[1]);
int sales = Integer.valueOf(tokens[2]);
double commissionRate = Double.valueOf(tokens[3]);
employee[index] = new CommissionEmployee(firstName, lastName, 1, baseWeekSalary, sales,
commissionRate);
}
// Since nothing is mentioned about employeeID ,taking it as any
// value above while creating objects.
index++;
}
bufferedReader.close();
// Read employee array
for (Employee e : employee) {
String designation;
// Assign value of designation according to symbol
if (e.getFirstName().charAt(0) == '#') {
designation = "Manager";
System.out.println(designation + ": " + e.getFirstName().substring(1) + " " + e.getLastName() + " $"
+ e.weeklyEarnings());
} else if (e.getFirstName().charAt(0) == '@') {
designation = "Hourly Worker";
System.out.println(designation + ": " + e.getFirstName().substring(1) + " " + e.getLastName() + " $"
+ e.weeklyEarnings() + "(Hourly wage: $" + ((HourlyWorker) e).getWage()
+ ", hours worked: $" + ((HourlyWorker) e).getHoursWorked() + ")");
} else {
designation = "Commission Employee";
System.out.println(designation + ": " + e.getFirstName().substring(1) + " " + e.getLastName() + " $"
+ e.weeklyEarnings() + "(Base Salary: $" + ((CommissionEmployee) e).getBaseWeekSalary()
+ ", sales: $" + ((CommissionEmployee) e).getSales() + ", Commission rate: "
+ ((CommissionEmployee) e).getCommissionRate() + ")");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.