JAVA 2 Payroll Class Write a Payroll class that uses the fol
JAVA
2. Payroll Class
Write a Payroll class that uses the following arrays as fields:
• employeeId. An array of seven integers to hold employee identification numbers. The
array should be initialized with the following numbers:
5658845 4520125 7895122 8777541
8451277 1302850 7580489
• hours. An array of seven integers to hold the number of hours worked by each
employee
• payRate. An array of seven 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 displays each employee number and asks
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.
Hello i need help with this code this is what i got so far:
=================================================================================================
This is how the output suppose to look:
Output
Enter the hours worked by employee number 5658845: 55
Enter the hourly pay rate for employee number 5658845: 29
Enter the hours worked by employee number 4520125: 40
Enter the hourly pay rate for employee number 4520125: 8.5
Enter the hours worked by employee number 7895122: 40
Enter the hourly pay rate for employee number 7895122: 250
Enter the hours worked by employee number 8777541: 40
Enter the hourly pay rate for employee number 8777541: 60
Enter the hours worked by employee number 8451277: 40
Enter the hourly pay rate for employee number 8451277: 21
Enter the hours worked by employee number 1302850: 40
Enter the hourly pay rate for employee number 1302850: 100
Enter the hours worked by employee number 7580489: 40
Enter the hourly pay rate for employee number 7580489: -1
ERROR: Enter 6.00 or greater for pay rate: 7
PAYROLL DATA
============
Employee ID: 5658845
Gross pay: $1,595.00
Employee ID: 4520125
Gross pay: $340.00
Employee ID: 7895122
Gross pay: $10,000.00
Employee ID: 8777541
Gross pay: $2,400.00
Employee ID: 8451277
Gross pay: $840.00
Employee ID: 1302850
Gross pay: $4,000.00
Employee ID: 7580489
Gross pay: $280.00
But i keep getting 0 at the beginning can someone help me please.
Thank you in advance
Solution
public class Payroll {
private int[] employeeID = {5658845, 4520125, 7895122,
8477541, 8451277, 1302850,
7580489
};
//Number of hours worked by each employee
private int[] hours = new int[7];
//Employee\'s hourly pay rate
private double[] payRate = new double[7];
//Employee\'s gross wages.
private double[] wages = new double[7];
//accessor and mutator methods
//returns id of index
public int getEmployeeID(int index) {
return employeeID[index];
}
//returns hours
public int getHours(int index) {
return hours[index];
}
//returns payrate
public double getPayRate(int index) {
return payRate[index];
}
//returns wages
public double getWages(int index) {
return wages[index];
}
//sets ID
public void setEmployeeID(int index, int ID) {
employeeID[index] = ID;
}
//Sets Hours
public void setHours(int index, int hour) {
hours[index] = hour;
}
//Sets Rate
public void setPayRate(int index, double payrate) {
payRate[index] = payrate;
}
//Sets Wages
public void setWages(int index, double wage) {
wages[index] = wage;
}
//Gross pay for each employee with identification
public double calculateGrossPay(int theEmployeeID) {
double grossPay = 0;
int employeeIndex = - 1; //index of the employee
for (int i = 0; i < employeeID.length; i++) {
if (employeeID[i] == theEmployeeID) {
employeeIndex = i;
break;
}
}
if (employeeIndex != - 1) {
//gross pay for if employee was found.
int h = hours[employeeIndex];
double r = payRate[employeeIndex];
grossPay = (h * r); //calculate the gross pay
}
return grossPay;
}
}
--------------------------------------------------------------------------------------------------------------
/**
* Modified java program that prompts user to enter
* hours and pay rate and then prints the gross pay
* to console for each employee
* */
import java.text.DecimalFormat;
import java.util.Scanner;
public class payRollTest {
public static void main(String[] args) {
//System.out.println(\" Payroll Application \");
Payroll payroll = new Payroll();
Scanner kb = new Scanner(System.in);
//Variable declaration
int hours;
double payRate;
double wages;
//Intialize employeeid array
int[] employeeID = {5658845, 4520125, 7895122,
8477541, 8451277, 1302850,
7580489
};
//Loop repeats for all 7 employees
for (int i = 0; i < 7; i++) {
//System.out.println(\"Employee :\" + (i + 1) );
do {
//get the hours
System.out.println(\"Enter the hours worked by employee number \" + employeeID[i]+\":\");
hours = Integer.parseInt(kb.nextLine());
}
while (hours < 0);
payroll.setHours(i, hours);
do {
//get the payrate
System.out.println(\"Enter the hourly pay rate for employee number :\" + employeeID[i]+\":\" );
payRate = Double.parseDouble(kb.nextLine());
//print error message if rate is < 6
if(payRate<6.00)
System.out.println(\"ERROR: Enter 6.00 or greater for pay rate: \");
}while (payRate < 6.00);
payroll.setPayRate(i, payRate);
int empid = payroll.getEmployeeID(i);
wages = payroll.calculateGrossPay(empid);
payroll.setWages(i, wages);
}
//Cretae a instance of decimal format to display comma
//separated values
DecimalFormat df=new DecimalFormat(\"##,###.00\");
//Display each employee\'s identification number
//and gross wages.
System.out.println(\" PAYROLL DATA \");
System.out.println(\" ============= \");
for (int i = 0; i < 7; i++) {
System.out.println(\"\\tEmployee ID: \" + payroll.getEmployeeID(i));
System.out.println(\"\\tGross pay: $\" + df.format(payroll.getWages(i)));
}
//Exit program
System.exit(0);
}
}
--------------------------------------------------------------------------------------------------------------
Sample output:
Enter the hours worked by employee number 5658845:
55
Enter the hourly pay rate for employee number :5658845:
29
Enter the hours worked by employee number 4520125:
40
Enter the hourly pay rate for employee number :4520125:
8.5
Enter the hours worked by employee number 7895122:
40
Enter the hourly pay rate for employee number :7895122:
250
Enter the hours worked by employee number 8477541:
40
Enter the hourly pay rate for employee number :8477541:
60
Enter the hours worked by employee number 8451277:
40
Enter the hourly pay rate for employee number :8451277:
21
Enter the hours worked by employee number 1302850:
40
Enter the hourly pay rate for employee number :1302850:
100
Enter the hours worked by employee number 7580489:
40
Enter the hourly pay rate for employee number :7580489:
-1
ERROR: Enter 6.00 or greater for pay rate:
Enter the hourly pay rate for employee number :7580489:
7
PAYROLL DATA
=============
Employee ID: 5658845
Gross pay: $1,595.00
Employee ID: 4520125
Gross pay: $340.00
Employee ID: 7895122
Gross pay: $10,000.00
Employee ID: 8477541
Gross pay: $2,400.00
Employee ID: 8451277
Gross pay: $840.00
Employee ID: 1302850
Gross pay: $4,000.00
Employee ID: 7580489
Gross pay: $280.00




