Hello again, I have a slight problem that's probably easy to fix but I can't figure it out. For the program, my professor is looking for an output like:
ERROR: Enter 0 or greater for hours:
If the user enters like -10 for example. I've tried throwing in a while loop to check that condition but it throws that hours is not initialized. And when I do initialize hours, and I enter -10, it does not prompt me an error message asking me to enter again. So just to be safe, I've left everything alone because I don't wanna mess it up. But here's my code:
PayRoll Code
package chapter7; public class Payroll { public final int NUM_EMPLOYEES = 7; private int[] employeeID = { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489 }; private int[] hours = new int[NUM_EMPLOYEES]; private double[] payRate = new double[NUM_EMPLOYEES]; private double[] wages = new double[NUM_EMPLOYEES]; public void setEmployeeID(int index, int ID) { employeeID[index] = ID; } public void setHours(int index, int hour) { hours[index] = hour; } public void setPayRate(int index, double payrate) { payRate[index] = payrate; } public void setWages(int index, double wage) { wages[index] = wage; } public int getEmployeeID(int index) { return employeeID[index]; } public double getHours(int index) { return hours[index]; } public double getPayRate(int index) { return payRate[index]; } public double getWages(int index) { return wages[index]; } public double calculateGrossPay(int theEmployeeID) { double grossPay = 0; int employeeIndex = -1; for (int i = 0; i < employeeID.length; i++) { if (employeeID[i] == theEmployeeID) { employeeIndex = i; break; } } if (employeeIndex != -1) { int h = hours[employeeIndex]; double r = payRate[employeeIndex]; grossPay = (h * r); } return grossPay; } }
PayRoll Demo Code
package chapter7; import java.util.Scanner; public class PayrollDemo { public static void main(String[] args) { Payroll payroll = new Payroll(); Scanner keyboard = new Scanner(System.in); int hours; int employeeID; double payRate; double wages; for (int i = 0; i < 7; i++) { do { System.out.println("Enter the Hours Worked by Employee Number " + payroll.getEmployeeID(i)); hours = Integer.parseInt(keyboard.nextLine()); } while (hours < 0); payroll.setHours(i, hours); do { System.out.println("Enter the Hourly Pay Rate for Employee Number " + payroll.getEmployeeID(i)); payRate = Double.parseDouble(keyboard.nextLine()); } while (payRate < 6.00); payroll.setPayRate(i, payRate); employeeID = payroll.getEmployeeID(i); wages = payroll.calculateGrossPay(employeeID); payroll.setWages(i, wages); } for (int i = 0; i < 7; i++) { System.out.println("Employee ID: " + payroll.getEmployeeID(i)); System.out.println("\t Gross Pay: $ " + payroll.getWages(i)); } keyboard.close(); } }
--- Update ---
Nevermind, I figured it out. Knew it was something simple. Sorry to waste a thread.