In: Computer Science
A Payroll System (it is worth a total of 20 points)
You are hired by a small company to design a payroll system to perform payroll calculations based on the type of employee (manager and hourly worker). Managers are paid a fixed weekly salary regardless of the number of hours worked; hourly workers are paid by the the hours and receives overtime pay (50% more overtime pay, but overtime cannot exceed 20 hours per week).
Both managers and hourly workers have something in common. For example, their names. However, the way each person’s earnings are calculated depends on the type of employee, so you decided to define the common features in a more generic class named Employee, then create two subclasses, Manager and HourlyWorker for manager and hourly worker, respectively.
Design an abstract class named Employee.java with the following data members and methods:
Data members: firstName, lastName
All getter and setter methods
Constructor takes first name and last name as parameters.
toString() methods to return name (Steve Davis, for example)
earnings() to return earnings (double) (think about if it is appropriate to be defined as abstract)
Create Manager.java and make it a subclass of Employee. This class should have its own data member: weeklySalary (double):
Getter and setter method for the new data member
Implement earnings() method to return weekly salary (be sure to validate weekly salary, the salary range is between $800 and $2000, for any other numbers, assign 0)
Override toString() method to type of employee followed by manage name. For example, Manage: Steve Davis
Create HourlyWorker.java and make it a subclass of Employee:
Data members: wage (per hours), hours (hours worked for a week)
Getter and setter methods (remember, number of hours worked per week cannot exceed 60 hours. If a number over 60 is entered, it will be reduced to 60 hours; wage range: $25 - $50, all other numbers are considered as invalid and assign default wage, which is $35 per hour)
Implements earnings() method
Override toString() to return employee type and name (for example, Hourly Worker: John Kanet)
Write a tester class named Tester.java to test your payroll system. Your tester class will read data file (employee.txt) and create employee objects, then print payment for each employee.
The sample input file:
#Steve Davis
1200
*John Kanet
35.5
50
*Matthew Williams
25
40
#Alex Bowers
5000
*Atanas Radenski
-45
80
a line starting with # is a manager, followed by weekly salary; a line starting with * is a hourly worker, followed by wage and hours.
Sample output:
Manager: Steve Davis
Earned: 1200.0
Hourly Worker: John Kanet
Earned: 1952.5
Hourly Worker: Matthew Williams
Earned: 1000.0
Manager: Alex Bowers
Earned: 0.0
Hourly Worker: Atanas Radenski
Earned: 2450.0
//Employee.java
//Employee class that contains first and last names
//abstract method for earnings
public abstract class Employee
{
private String firstName;
private String lastName;
public Employee(String firstName, String
lastName)
{
this.firstName=firstName;
this.lastName=lastName;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lastName)
{
this.lastName=lastName;
}
public String getLastName()
{
return lastName;
}
@Override
public String toString()
{
return firstName+"
"+lastName;
}
public abstract double earnings();
}
--------------------------------------------------------------------------------------------------
//Manager.java extends the Employee
public class Manager extends Employee
{
private double weeklySalary;
//Manager class constructor
public Manager(String firstName, String
lastName,
double
weeklySalary)
{
super(firstName, lastName);
if(weeklySalary>800 &&
weeklySalary<2000)
this.weeklySalary=weeklySalary;
else
this.weeklySalary=0;
}
@Override
public String toString() {
return "Manager :
"+super.toString()+"\n"+earnings();
}
//Overird earnings method that return weekly
salary
@Override
public double earnings() {
return weeklySalary;
}
}
--------------------------------------------------------------------------------------------------
//HourlyWorker.java
//extends the employee class
public class HourlyWorker extends Employee
{
private double wage;
private double hours;
public HourlyWorker(String firstName, String
lastName,
double wage,
double hours)
{
super(firstName, lastName);
setWage(wage);
setHours(hours);
this.hours=hours;
}
public void setWage(double wage)
{
if(wage>=25 &&
wage<=50)
this.wage=wage;
else
//Set default
value if wage is out of range
this.wage=35;
}
public void setHours(double hours)
{
if(hours<60)
this.hours=hours;
else
//Set default
value if hours is out of range
this.hours=60;
}
@Override
public String toString() {
return "Hourly Worker :
"+super.toString()+"\n"+earnings();
}
@Override
public double earnings() {
return wage*hours;
}
}
--------------------------------------------------------------------------------------------------
/**
* The java program Tester that reads the employee.txt file
* and reads employee data into an array of Employee type
* and prints the employees to console.
* */
//Tester.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Tester
{
public static void main(String[] args)
{
String file="employee.txt";
Scanner filescanner=null;
String firstName;
char ch;
String name;
double earn=0;
String lastName;
Employee[] emps=new
Employee[5];
int index=0;
try
{
//ope a
file
filescanner=
new Scanner(new
File(file));
//read file
until end of the file
while(filescanner.hasNextLine())
{
firstName=filescanner.next();
ch=firstName.charAt(0);
double wage,hours;
switch(ch)
{
case '#':
name=firstName.substring(1,
firstName.length());
lastName=filescanner.next();
earn=filescanner.nextDouble();
emps[index]=new Manager(name,
lastName, earn);
break;
case '*':
name=firstName.substring(1,
firstName.length());
lastName=filescanner.next();
wage=filescanner.nextDouble();
hours=filescanner.nextDouble();
emps[index]=new
HourlyWorker(name, lastName, wage, hours);
break;
}
index++;
}
//print
employees to console
for (Employee
employee : emps)
{
System.out.println(employee.toString());
}
}
catch (FileNotFoundException
e)
{
System.out.println(e.getMessage());
}
}
}
--------------------------------------------------------------------------------------------------
Input file : employee.txt
#Steve Davis
1200
*John Kanet
35.5
50
*Matthew Williams
25
40
#Alex Bowers
5000
*Atanas Radenski
-45
80
--------------------------------------------------------------------------------------------------
Sample output:
Manager : Steve Davis
1200.0
Hourly Worker : John Kanet
1775.0
Hourly Worker : Matthew Williams
1000.0
Manager : Alex Bowers
0.0
Hourly Worker : Atanas Radenski
2800.0