Design a PayRoll class that has data members for an employee’s first and last na
ID: 3802849 • Letter: D
Question
Design a PayRoll class that has data members for an employee’s first and last name, hourly pay rate, number of hours worked. The default constructor will set the first and last name to empty string, and hours worked and pay rate to zero. The class must have mutator functions to set each of the data members of the class and accessors to access them. The class should also have the following member functions:
Driver program:
Write a program with an array of seven PayRoll objects. The program should read the name (first and last), pay rate and the number of hours for each employee and display each employee information followed by the employee’s gross pay, all on one line separated by blanks. Set the precision for printing the doubles to two decimal places.
Input Validation:
Explanation / Answer
/**
* The java program PayRollDriver that read
* an input file employees.txt . Then the program
* prints the first name, last name, hours worked
* pay rate and gross income to console.
*
* */
//PayRollDriver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PayRollDriver
{
public static void main(String[] args)
{
//create an array of PayRoll of size=7
PayRoll payroll[]=new PayRoll[7];
Scanner filescanner=null;
String fileName="employees.txt";
try
{
//open a file stream
filescanner=new Scanner(new File(fileName));
//calling readPayRoll
readPayRoll(payroll,filescanner);
//close filescanner object
filescanner.close();
//print to console
for (PayRoll emppayroll : payroll)
{
emppayroll.displayPayroll();
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
/*
* The method readPayRoll that takes PayRoll array and Scanner
* object and read data from the text file.
* */
private static void readPayRoll(PayRoll[] payroll, Scanner filescanner)
{
for (int i = 0; i < payroll.length; i++)
{
String firstName=filescanner.next();
String lastName=filescanner.next();
double payRate=Double.parseDouble(filescanner.next());
double hoursWorked=Double.parseDouble(filescanner.next());
payroll[i]=new PayRoll();
payroll[i].setFirstName(firstName);
payroll[i].setLastName(lastName);
payroll[i].setHourlyPayRate(payRate);
payroll[i].setHoursWorked(hoursWorked);
}
}
}
------------------------------------------------------------------------------------------------------------
/**
* The java program PayRoll that sets the first name,
* last name, hours worked,
* payrate and gross income.
* */
//PayRoll.java
public class PayRoll
{
private String firstName;
private String lastName;
private double payRate;
private double hoursWorked;
/**
Default constructor
*/
public PayRoll()
{
firstName="";
lastName="";
payRate=0;
hoursWorked=0;
}
public void setFirstName(String firstName)
{
this.firstName=firstName;
}
public void setLastName(String lastName)
{
this.lastName=lastName;
}
public void setHourlyPayRate(double payRate)
{
if(payRate<0)
this.payRate=0;
else
this.payRate = payRate;
}
public void setHoursWorked(double hoursWorked)
{
if(hoursWorked>60)
this.hoursWorked = 60;
else
this.hoursWorked = hoursWorked;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public double getPayrate()
{
return payRate;
}
public double getHoursWorked()
{
return hoursWorked;
}
public double getGrossPay()
{
return payRate * hoursWorked;
}
public void displayPayroll()
{
System.out.println(firstName+" "+lastName+" "+hoursWorked
+" "+payRate+" "+getGrossPay());
}
}
------------------------------------------------------------------------------------------------------------
employees.txt
Kimberly Copeland 2.5 45
Winifred Lynch 1.5 75
Gretchen Leonard 2.5 45
Catherine Sparks 1.5 45
Joseph Lopez 2.5 45
Carmen Adams 1.5 45
Sophie Pearson 1.5 85
------------------------------------------------------------------------------------------------------------
Output:
Kimberly Copeland 45.0 2.5 112.5
Winifred Lynch 60.0 1.5 90.0
Gretchen Leonard 45.0 2.5 112.5
Catherine Sparks 45.0 1.5 67.5
Joseph Lopez 45.0 2.5 112.5
Carmen Adams 45.0 1.5 67.5
Sophie Pearson 60.0 1.5 90.0