In: Computer Science
Java
Write a Payroll class as demonstrated in the following UML diagram.
Member variables of the class are:
NUM_EMPLOYEES: a constant integer variable to hold the number of employees.
employeeId. An array of integers to hold employee identification numbers.
hours. An array of integers to hold the number of hours worked by each employee
payRate. An array of doubles to hold each employee’s hourly pay rate
wages. An array of seven doubles to hold each employee’s gross wages
The class should relate the data in each array through the subscripts. For example, the number in element 0 of the hours array should be the number of hours worked by the employee whose identification number is stored in element 0 of the employeeId array. That same employee’s pay rate should be stored in element 0 of the payRate array.
In addition to the appropriate accessor and mutator methods, the class should have a method that accepts an employee’s identification number as an argument and returns the gross pay for that employee.
Demonstrate the class in a complete program that asks for the number of employees first. Then for each employee, it should ask for the employee ID. Then, displaying each employee number, it should ask the user to enter that employee’s hours and pay rate. It should then display each employee’s identification number and gross wages.
Input Validation: Do not accept negative values for hours or numbers less than 6.00 for pay rate. A sample interaction is shown below:
Enter number of employee: 3
Enter the ID of Employee # 1: 123456
Enter the hours worked by employee number 123456: 20 Enter the
hourly pay rate for employee number 123456: 4 ERROR: Enter 6.00 or
greater for pay rate: 7
Enter the ID of Employee # 2: 654321
Enter the hours worked by employee number 654321: Enter the hourly
pay rate for employee number 654321: Enter the ID of Employee # 3:
102030
Enter the hours worked by employee number 102030: Enter the hourly
pay rate for employee number 102030:
PAYROLL DATA ============ Employee ID: 123456 Gross pay: $140.00 Employee ID: 654321 Gross pay: $100.00 Employee ID: 102030 Gross pay: $300.00
code for your program is provided below. code is thoroughly explained in code comments. Output screenshot as well as screenshot of code in IDE is provided in the last for your better uderstanding. If you need any further clarification please feel free to ask in comments. I would really appreciate if you would let me know if the explanation provided is upto your satisfaction.
#####################################################################
CODE
import java.util.Scanner; //for taking input from user
public class Payroll
{
//private data members
private final int NUM_EMPLOYEES; //to store maximum employees
private int[] employeeId; //to store ids for employeees
private int[] hours; //to store number of hours
private double[] payRate; //to store pay rate for each employee
private double[] wages; //to store total wages
//constructor
Payroll(int numberOfEmployees)
{
NUM_EMPLOYEES=numberOfEmployees; //set number of employees
employeeId=new int[NUM_EMPLOYEES]; //create array with given number
hours=new int[NUM_EMPLOYEES];
payRate=new double[NUM_EMPLOYEES]; //create payrate array with given size
wages=new double[NUM_EMPLOYEES]; //create wages array with given size
}
//geter to get employee id array
public int[] getEmployeeId() {
return employeeId;
}
//setter to set employeeid array
public void setEmployeeId(int[] employeeId) {
this.employeeId = employeeId;
}
//get to get hours array
public int[] getHours() {
return hours;
}
//setter tos et number of hours
public void setHours(int[] hours) {
this.hours = hours;
}
//getter to get payrate array
public double[] getPayRate() {
return payRate;
}
//setter to set payrate array
public void setPayRate(double[] payRate) {
this.payRate = payRate;
}
//getter to get wages array
public double[] getWages() {
return wages;
}
//seter to calculate the wages using helper method grossPayCalculator
public void setWages() {
for(int i=0;i<NUM_EMPLOYEES;i++) //for each employee
{
//call method to calculate pay and enter in the wages array
wages[i]=grossPayCalculator(employeeId[i]);
}
}
//method to return pay of an employyee
public double grossPayCalculator(int id)
{
for(int i=0;i<NUM_EMPLOYEES;i++) //loop through employee
{
if(employeeId[i]==id) //if id matches
{
return hours[i]*payRate[i]; //return pay
}
}
return -1; //if there is no employye by that id then return -1
}
//overrided toString() to display all the ids and wages
public String toString()
{
String str;
str="PAYROLL DATA================";
for(int i=0;i<NUM_EMPLOYEES;i++) //for each employee
{
str+="\nEmployee ID: "+employeeId[i]; //add id of employee in string
str+=" Gross pay: $"+String.format("%.2f",wages[i]); //add gross pay in string with 2 decimal format
}
return str; //rerurn the resultant string
}
//main method
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in); //for taking input
int number=0; //to store number of employees
System.out.print("Enter number of employees: ");
number=scan.nextInt(); //read number of employees
Payroll roll=new Payroll(number); //make object of Payroll with given number of max employees
double payRate[]=new double[number]; //array to store pay rate
int hours[]=new int[number]; //array to store number of hours
int id[]=new int[number]; //array to store ids pof employees
for(int i=0;i<number;i++) //loop to enter details for each employee
{
System.out.print("\nEnter the ID of Employee # "+(i+1)+": ");
id[i]=scan.nextInt(); //read id
System.out.print("Enter the hours worked by employee number "+id[i]+": ");
hours[i]=scan.nextInt(); //read hours
while(hours[i]<0) //if hours is negative then loop again to enter data again
{
System.out.print("ERROR: Enter positive Value for Number of hours: ");
hours[i]=scan.nextInt(); //read hours again
}
System.out.print("Enter the hourly pay rate for employee number "+id[i]+": ");
payRate[i]=scan.nextDouble(); //read payrate of employee
while(payRate[i]<6) //if payrate is less than 6 loop again
{
System.out.print("ERROR: Enter 6.00 or greate for pay rate: ");
payRate[i]=scan.nextDouble(); //read payrate again
}
}
roll.setEmployeeId(id); //set the employee ids in object
roll.setHours(hours); //set the employee hours in object
roll.setPayRate(payRate); //set the employee payrates in object
roll.setWages(); //set the wages of each employee in class
System.out.println(roll); //print payroll data
scan.close();
}
}
OUTPUT
#######################################################################
CODE IN IDE