I need help creating an error message when the user enter a number less than zer
ID: 3621007 • Letter: I
Question
I need help creating an error message when the user enter a number less than zero for the amount of hours worked and another error message when the user enter a number less than 6 for the pay rate. Here is the code so far:import java.util.Scanner;
class Payroll {
/**
* employeeID. An array of seven integer to hold employee identification
* numbers. the array should be intialized with the following numbers:
* 5658845, 4520125, 7895122, 877541, 8451277, 1302850, 7580489
*/
private int[] employeeID = { 5658845, 4520125, 7895122, 877541, 8451277,
1302850, 7580489 };
/**
* hours. An array of seven integers to hold the number of hours worked by
* each employee.
*/
private int[] hours = new int[7];
/**
* payRate. An array of seven doubles to hold each employee's hourly pay
* rate
*/
private double[] payRate = new double[7];
/**
* wages. An array of seven doubles to hold each employee's gross wages.
*/
private double[] wages = new double[7];
/*
* Appropriate accessor and mutator methods
*/
public int getEmployeeID(int index) {
return employeeID[index];
}
public int getHours(int index) {
return hours[index];
}
public double getPayRate(int index) {
return payRate[index];
}
public double getWages(int index) {
return wages[index];
}
public void setEmployeeID(int index, int employeeID) {
this.employeeID[index] = employeeID;
}
public void setHours(int index, int hours) {
this.hours[index] = hours;
}
public void setPayRate(int index, double payRate) {
this.payRate[index] = payRate;
}
public void setWages(int index, double wages) {
this.wages[index] = wages;
}
public double calculateGrossPay(int theEmployeeID) {
double grossPay = 0;
// find the index of the employee
int employeeIndex = -1;
for (int i = 0; i < employeeID.length; i++) {
if (employeeID[i] == theEmployeeID) {
employeeIndex = i;
break;
}
}
// calculate the gross pay (only if the employee was found)
if (employeeIndex != -1) {
int h = hours[employeeIndex];
double r = payRate[employeeIndex];
// calculate the gross pay
grossPay = (h * r);
}
return grossPay;
}
}
public class PayrollProject {
public static void main(String[] args) {
System.out.println("==============================");
System.out.println(" Payroll Application ");
System.out.println("==============================");
Payroll payroll = new Payroll();
Scanner kb = new Scanner(System.in);
int hours;
double payRate;
double wages;
int employeeID;
for (int i = 0; i < 7; i++) {
System.out.println("Employee #" + (i + 1));
// gets the hours
do {
System.out.print(" Hours: ");
hours = Integer.parseInt(kb.nextLine());
} while (hours < 0);
payroll.setHours(i, hours);
// gets the pay rate
do {
System.out.print(" Pay Rate: ");
payRate = Double.parseDouble(kb.nextLine());
} while (payRate < 6.00);
payroll.setPayRate(i, payRate);
// performs the calculations
employeeID = payroll.getEmployeeID(i);
wages = payroll.calculateGrossPay(employeeID);
payroll.setWages(i, wages);
}
// It should then display each employee's identification number and
// gross wages.
System.out.println("==============================");
for (int i = 0; i < 7; i++) {
System.out.print("Employee ID: " + payroll.getEmployeeID(i));
System.out.println(" Gross Wages: " + payroll.getWages(i));
}
}
}
Explanation / Answer
please rate - thanks
import java.util.Scanner;
public class PayrollProject {
public static void main(String[] args) {
System.out.println("==============================");
System.out.println(" Payroll Application ");
System.out.println("==============================");
Payroll payroll = new Payroll();
Scanner kb = new Scanner(System.in);
int hours;
double payRate;
double wages;
int employeeID;
for (int i = 0; i < 7; i++) {
System.out.println("Employee #" + (i + 1));
// gets the hours
do {
System.out.print(" Hours: ");
hours = Integer.parseInt(kb.nextLine());
if(hours<0)
System.out.println("must be >=0");
} while (hours < 0);
payroll.setHours(i, hours);
// gets the pay rate
do {
System.out.print(" Pay Rate: ");
payRate = Double.parseDouble(kb.nextLine());
if(payRate<6)
System.out.println("must be >=6");
} while (payRate < 6.00);
payroll.setPayRate(i, payRate);
// performs the calculations
employeeID = payroll.getEmployeeID(i);
wages = payroll.calculateGrossPay(employeeID);
payroll.setWages(i, wages);
}
// It should then display each employee's identification number and
// gross wages.
System.out.println("==============================");
for (int i = 0; i < 7; i++) {
System.out.print("Employee ID: " + payroll.getEmployeeID(i));
System.out.println(" Gross Wages: " + payroll.getWages(i));
}
}
}
----------------------------------------------------------
class Payroll {
/**
* employeeID. An array of seven integer to hold employee identification
* numbers. the array should be intialized with the following numbers:
* 5658845, 4520125, 7895122, 877541, 8451277, 1302850, 7580489
*/
private int[] employeeID = { 5658845, 4520125, 7895122, 877541, 8451277,
1302850, 7580489 };
/**
* hours. An array of seven integers to hold the number of hours worked by
* each employee.
*/
private int[] hours = new int[7];
/**
* payRate. An array of seven doubles to hold each employee's hourly pay
* rate
*/
private double[] payRate = new double[7];
/**
* wages. An array of seven doubles to hold each employee's gross wages.
*/
private double[] wages = new double[7];
/*
* Appropriate accessor and mutator methods
*/
public int getEmployeeID(int index) {
return employeeID[index];
}
public int getHours(int index) {
return hours[index];
}
public double getPayRate(int index) {
return payRate[index];
}
public double getWages(int index) {
return wages[index];
}
public void setEmployeeID(int index, int employeeID) {
this.employeeID[index] = employeeID;
}
public void setHours(int index, int hours) {
this.hours[index] = hours;
}
public void setPayRate(int index, double payRate) {
this.payRate[index] = payRate;
}
public void setWages(int index, double wages) {
this.wages[index] = wages;
}
public double calculateGrossPay(int theEmployeeID) {
double grossPay = 0;
// find the index of the employee
int employeeIndex = -1;
for (int i = 0; i < employeeID.length; i++) {
if (employeeID[i] == theEmployeeID) {
employeeIndex = i;
break;
}
}
// calculate the gross pay (only if the employee was found)
if (employeeIndex != -1) {
int h = hours[employeeIndex];
double r = payRate[employeeIndex];
// calculate the gross pay
grossPay = (h * r);
}
return grossPay;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.