fzk
fzk

Reputation: 25

Nothing being stored in array/Method not functioning correctly

Purpose: "Write a payroll class using 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: 568845, 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 double 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 throught he subscipts. The class should have a method that accepts an employee's identification number as an argument and returns the gross pay for that employee."

Set/Get Methods with Grosspay Method:

public class Payroll{
   private int [] employeeID ={5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
   private int [] hours = new int [7];
   private double [] wages = new double [7];
   private double [] payRate = new double [7];


   public void setEmployeeID(int a, int emp){
      employeeID[a] = emp;
   }
   public void setHours(int a, int hr){
      hours[a] = hr;
   }
   public void setWages(int a, int wgs){
      wages[a] = wgs;
   }
   public void setPayrate(int a, int prt){
      payRate[a] = prt;
   }
   public int getEmployeeID(int a){
      return employeeID[a];
   } 
   public int getHours(int a){
      return hours[a];
   }
   public double getWages(int a){
      return wages[a];
   }
   public double getPayrate(int a){
      return payRate[a];
   }
   public void Grosspay(){
      for(int a = 0; a < 7; a++){
         wages[a] = hours[a] * payRate[a];

      }

   } 
}

Where everything is executed:

import java.util.Scanner;
public class TestPayroll{
   public static void main(String [] args){
      Scanner in = new Scanner(System.in);
      Payroll pr = new Payroll();
      int hr;
      double prt;

      for (int a = 0; a < 7; a++){
         System.out.println("Enter in the hours worked by employee " + pr.getEmployeeID(a));
         hr = in.nextInt();
         pr.setHours(a,hr);
         while(hr < 0){
            System.out.println("Enter in a number greater than 0");
            hr = in.nextInt();
            pr.setHours(a,hr);
         }
         System.out.println("Enter in the pay rate for employee " + pr.getEmployeeID(a));
         prt = in.nextDouble();
         while(prt < 6.00){
            System.out.println("Enter in a payrate greater than 6.00");
            prt = in.nextDouble();
         }

      }
      pr.Grosspay();
      for(int a = 0; a < 7; a++){
         System.out.println("Employee " + pr.getEmployeeID(a) + " has a gross pay of " + pr.getWages(a));
      }


   }

I run the program and everything works fine except when it prints out wages. Instead of the wages being the result of hours[a] * payRate[a], it prints out 0.0. I'm thinking that Grosspay method may not be functioning the way I think it should be functioning. Or it could be that I set wages[a] = wgs but wgs is not being used in the code so nothing gets stored in the wages array. So what is wrong with my code that 0.0 is being printed out? Any help is appreciated.

Upvotes: 0

Views: 93

Answers (1)

Crammeur
Crammeur

Reputation: 688

Try this

Payroll.class

private int [] employeeID ={5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
private int [] hours = new int [7];
private double [] wages = new double [7];
private double [] payRate = new double [7];


public void setEmployeeID(int a, int emp){
    employeeID[a] = emp;
}
public void setHours(int a, int hr){
    hours[a] = hr;
}
public void setWages(int a, int wgs){
    wages[a] = wgs;
}
public void setPayrate(int a, double prt){
    payRate[a] = prt;
}
public int getEmployeeID(int a){
   return employeeID[a];
}
public int getHours(int a){
   return hours[a];
}
public double getWages(int a){
   return wages[a];
}
public double getPayrate(int a){
    return payRate[a];
}
public void Grosspay(){
    for(int a = 0; a < 7; a++){
        wages[a] = hours[a] * payRate[a];
    }
}

Main.class

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Payroll pr = new Payroll();
    int hr,wgs = 0;
    double prt;

    for (int a = 0; a < 7; a++){
        System.out.println("Enter in the hours worked by employee " + pr.getEmployeeID(a));
        hr = 1;
        pr.setHours(a,hr);
        while(wgs <= 0){
            System.out.println("Enter in a number greater than 0");
            wgs = 1;
            pr.setWages(a,wgs);
        }
        System.out.println("Enter in the pay rate for employee " + pr.getEmployeeID(a));
        prt = 1;
        while(prt < 6.00){
            System.out.println("Enter in a payrate greater than 6.00");
            prt = 7;
            pr.setPayrate(a,prt);
        }

    }
    pr.Grosspay();
    for(int a = 0; a < 7; a++){
        System.out.println("Employee " + pr.getEmployeeID(a) + " has a gross pay of " + pr.getWages(a));
    }
}

Upvotes: 0

Related Questions