Results 1 to 4 of 4
  1. #1
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default Need help with Payroll demo program

    I am slowly but surely getting the hang of Java. I love Java, and want to become certified in the foreseeable future. Anyway, here is a class that I have written:
    Java Code:
    public class payroll {
     
    // final integer of number of employees.
    final int NUM_EMPLOYEES = 7;
    
    // Array for holding employee IDs.
    private int[] employeeIDs = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
     
    // Array referencing number of worked hours for each employee.
    private int[] hours = new int[NUM_EMPLOYEES];
     
    // Array referencing gross payrate for each employee.
    private double[] payRate = new double[NUM_EMPLOYEES];
     
    // Array referencing wages for each employee.
    private double[] wages = new double[NUM_EMPLOYEES];
     
        
        public payroll (int[] employeeIDArray, int[] hourArray, double[] payrateArray, double[] wagesArray){
     
      employeeIDs = employeeIDArray;
      hours = hourArray;
      payRate = payrateArray;
      wages = wagesArray;
        }
        
        /** setHours method sets how many hours that employee worked.*/
        
        public void setHours(int[] empHours){
            for (int x = 0; x < hours.length; x++){
                System.out.println("Please enter hours for " + employeeIDs[x] + ": ");
                empHours[x] = hours[x];
            }
        }
            
        public void setPayrate(double[] empPayrate){
            for (int x = 0; x < payRate.length; x++){
                System.out.println("Please enter payrate for " + employeeIDs[x] + ": ");
                empPayrate[x] = payRate[x];
            }
        }
        
        public void setWages(double[] empWages){
            for (int x = 0; x < wages.length; x++){
                System.out.println("Please enter wages for " + employeeIDs[x] + ": ");
                empWages[x] = wages[x];
            }
        }
        
        public double getEmployeehours(int i){
            return employeeIDs[i];
        }
        
        public double getPayrate(int i){
            return payRate[i];
        }
        
        public double getHours(int i){
            return hours[i];
        }
        
        public double getWages(int i){
            return wages[i];
        }
        
        public double calculateGrossPay(int employeeID){
            int employeeNotFound = -1;
        for (int index = 0; index < employeeIDs.length; index++ )
        {
            if (employeeIDs[index] == employeeID)
                employeeNotFound = index;}
        
            double h = hours[employeeNotFound];
            double r = payRate[employeeNotFound];
            
            double grosspay = h * r;
            
            
        return grosspay;
        
        }
    }
    and here is the demo program:
    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    public class PayrollDEMO {
        
        public static void main(String[] args) {
            
            int[] array = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489}; // array of emp IDs
            int[] hoursArray = new int[7]; // array to hold emp hours
            double[] payrateArray = new double[7]; // array to hold emp hourly payrate
            double[] wagesArray = new double[7]; // array to hold emp wages (if applicable)
            char yes = 'y'; // validation to store values in wagesArray
            
            /* Creating Payroll object to store values 
             * and get results.
             */
             
            payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
            
            // for loop displays all emp IDs to allow user to enter corresponding information.
            
            for (int empIDs : array)
                System.out.println("Here are the employee IDs to reference: " + empIDs);
            
            Scanner keyboard = new Scanner("System.in"); // creating scanner object for user input.
            
            /* for loop allows user to enter employee's wages/payrate/hours. 
             * Steps through each corresponding array, allowing for correct user input.
             *
             * inner if-else statement determines whether user entered 'y' for salary input.
             * Then for loop steps through the hoursArray to store hours worked for each employee.
             */
             
            for (int x = 0; x < array.length; x++){
                System.out.println("IF EMPLOYEE IS SALARIED, PLEASE ENTER 'y': ");
                    
                    if (yes == 'y')
                    payrateArray[x] = payrollObject.setPayrate();
                    else
                    wagesArray[x] =    payrollObject.setWages();
                
            hoursArray[x] =    payrollObject.setHours();
                payrollObject.hoursValidation();
                payrollOBject.payrateValidation();
                }
            
            
            }
        }
    * I realized I did not create a DecimalFormat object even with the import statement on top. I will do so as soon as I get this program to even compile!

    My question is, when I compile it (I'm using terminal in Mac OSX), it gives me several errors (11 to be exact):
    PayrollDEMO.java:18: '.class' expected
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: <identifier> expected
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: illegal start of expression
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: ';' expected
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: not a statement
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: ';' expected
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: <identifier> expected
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: illegal start of expression
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: ';' expected
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: not a statement
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^
    PayrollDEMO.java:18: ';' expected
    payroll payrollObject = new payroll(int[] array, int[] hoursArray, double[] payrateArray, double[] wagesArray);
    ^

    Can I not have multiple arrays as parameters in my constructor? I'm confused... Any and all help is gladly appreciated :)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: Need help with Payroll demo program

    When you call the constructor, leave off the types and just include the argument names.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    smoovejayy is offline Jonathan
    Join Date
    Feb 2013
    Location
    Bronx, NY
    Posts
    10
    Rep Power
    0

    Default Re: Need help with Payroll demo program

    So I'm back! I kept fixing things, then found more errors! I'm so frustrated and confused. Here is my code now:
    Java Code:
    import java.util.Scanner;
    
    public class payroll {
     
    // final integer of number of employees.
    final int NUM_EMPLOYEES = 7;
    
    // Array for holding employee IDs.
    private int[] employeeIDs = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
     
    // Array referencing number of worked hours for each employee.
    private int[] hours = new int[NUM_EMPLOYEES];
     
    // Array referencing gross payrate for each employee.
    private double[] payRate = new double[NUM_EMPLOYEES];
     
    // Array referencing wages for each employee.
    private double[] wages = new double[NUM_EMPLOYEES];
     
        
        public payroll (int[] employeeIDArray, int[] hourArray, double[] payrateArray, double[] wagesArray){
     
      employeeIDs = employeeIDArray;
      hours = hourArray;
      payRate = payrateArray;
      wages = wagesArray;
        }
        
        /** setHours method sets how many hours that employee worked.*/
        
        public void setHours(int[] empHours){
            Scanner keyboard = new Scanner(System.in);
            
            for (int x = 0; x < hours.length; x++){
                System.out.println("Please enter hours for " + employeeIDs[x] + ": ");
                hours[x] = empHours[x];
            }
            hoursValidation();
        }
            
        public void setPayrate(double[] empPayrate){
            Scanner keyboard = new Scanner(System.in);
                    
            for (int x = 0; x < payRate.length; x++){
                System.out.println("Please enter payrate for " + employeeIDs[x] + ": ");
                payRate[x] = empPayrate[x];
            }
        }
        
        public void setWages(double[] empWages){
            Scanner keyboard = new Scanner(System.in);
            
            for (int x = 0; x < wages.length; x++){
                System.out.println("Please enter wages for " + employeeIDs[x] + ": ");
                wages[x] = empWages[x];
            }
            wagesValidation();
        }
        
        public double getEmployeehours(int i){
            return employeeIDs[i];
        }
        
        public double getPayrate(int i){
            return payRate[i];
        }
        
        public double getHours(int i){
            return hours[i];
        }
        
        public double getWages(int i){
            return wages[i];
        }
        
        public double calculateGrossPay(int[] employeeID){
            double grosspay = 0;
            
            for (int index = 0; index < employeeID.length; index++){
        
            double h = hours[index];
            double r = payRate[index];
            
            grosspay = h * r;}
            
        return grosspay;
        }
        
    	public void hoursValidation(){
    		Scanner keyboard = new Scanner(System.in);
    		
    		for (int x = 0; x < 6; x++){
    			if (hours[x] < 0){
    				System.out.println("You have entered an invalid entry. Please enter amount of hours greater than or equal to zero: ");
    				hours[x] = keyboard.nextInt();}
    			}			
    			}
    	
    	public void wagesValidation(){
    		Scanner keyboard = new Scanner(System.in);
    		
    		for (int x = 0; x < 6; x++){
    			if (wages[x] < 6.00){
    				System.out.println("You have entered an invalid entry. Please enter wages greater than or equal to $6.00: ");
    				wages[x] = keyboard.nextInt();}
    				}
    				}
    }
    and the demo:
    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    
    public class PayrollDEMO {
        
        public static void main(String[] args) {
            
            int[] array = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489}; // array of emp IDs
            int[] hoursArray = new int[7]; // array to hold emp hours
            double[] payrateArray = new double[7]; // array to hold emp hourly payrate (if applicable)
            double[] wagesArray = new double[7]; // array to hold emp wages (if applicable)
            String yes = ""; // validation to store values in wagesArray
            
            /* Creating Payroll object to store values 
             * and get results.
             */
             
            payroll payrollObject = new payroll(array, hoursArray, payrateArray, wagesArray);
            
            // for loop displays all emp IDs to allow user to enter corresponding information.
            
            System.out.println("Here are the employee IDs to reference: ");
            for (int empIDs : array)
            	System.out.println(empIDs);
                
            Scanner keyboard = new Scanner("System.in"); // creating scanner object for user input.
            
            /* for loop allows user to enter employee's wages/payrate/hours. 
             * Steps through each corresponding array, allowing for correct user input.
             *
             * inner if-else statement determines whether user entered 'y' for salary input.
             * Then for loop steps through the hoursArray to store hours worked for each employee.
             */
             
            for (int x = 0; x < array.length; x++){
                System.out.println("IF EMPLOYEE IS SALARIED, PLEASE ENTER 'y': ");
                yes = keyboard.nextLine();
                
                if (yes == "y"){
                	payrollObject.setHours(hoursArray);
                	payrollObject.setPayrate(payrateArray);}
                
                else{
                	payrollObject.setHours(hoursArray);
                	payrollObject.setWages(wagesArray);} 
    
    keyboard.nextLine();
                	}
            
           	System.out.println("For each employee, here are their gross pays/wages: ");
            
            for (int var : array)
            	payrollObject.calculateGrossPay(array);
            
            }
        }
    When I did a couple of test runs, the program used to just display everything I was supposed to enter and terminate. Then I changed the char data type to a String instead so I can read the input, and now when I run this is what I usually get (I put random numbers for testing):
    Here are the employee IDs to reference:
    5658845
    4520125
    7895122
    8777541
    8451277
    1302850
    7580489
    IF EMPLOYEE IS SALARIED, PLEASE ENTER 'y':
    Please enter hours for 5658845:
    Please enter hours for 4520125:
    Please enter hours for 7895122:
    Please enter hours for 8777541:
    Please enter hours for 8451277:
    Please enter hours for 1302850:
    Please enter hours for 7580489:
    Please enter wages for 5658845:
    Please enter wages for 4520125:
    Please enter wages for 7895122:
    Please enter wages for 8777541:
    Please enter wages for 8451277:
    Please enter wages for 1302850:
    Please enter wages for 7580489:
    You have entered an invalid entry. Please enter wages greater than or equal to $6.00:
    8
    You have entered an invalid entry. Please enter wages greater than or equal to $6.00:
    0
    You have entered an invalid entry. Please enter wages greater than or equal to $6.00:
    9
    You have entered an invalid entry. Please enter wages greater than or equal to $6.00:
    99
    You have entered an invalid entry. Please enter wages greater than or equal to $6.00:
    9
    You have entered an invalid entry. Please enter wages greater than or equal to $6.00:
    99
    IF EMPLOYEE IS SALARIED, PLEASE ENTER 'y':
    Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1516)
    at PayrollDEMO.main(PayrollDEMO.java:37)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,376
    Rep Power
    5

    Default Re: Need help with Payroll demo program

    One major problem is that your set routines are inconsistently written. In some cases, it looks like you want to read input from the terminal and then store it in an array. At other places it appears to be passed via a filled in array. However you do it, you you then try to separately validate the input separately by iterating thru the array. A more efficient way is to prompt for each value and validate it then. If it is okay, then store it, otherwise, prompt again.

    I recommend that you write a small program to practice populating an array by prompting for info and then validating it. That program can then be used as a guide to populate the other arrays as needed.

    Here is a start:
    Java Code:
    int n = 6; // number of employees
    int c = 0; // current employee
    while (c < n) {
        // prompt for input
        // get input
        // is input okay?
       // if so store in array and increment c
       // else print error message and return to prompt again
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Payroll Program using GUI
    By RiterHeng in forum New To Java
    Replies: 1
    Last Post: 05-17-2012, 07:04 AM
  2. Payroll Program 3 question
    By cloudent01 in forum New To Java
    Replies: 11
    Last Post: 02-03-2012, 10:07 PM
  3. Payroll program.. School.. Help Please!
    By eddyb5 in forum New To Java
    Replies: 2
    Last Post: 06-05-2011, 05:49 PM
  4. payroll program 2
    By joshwey in forum New To Java
    Replies: 13
    Last Post: 05-13-2011, 09:27 AM
  5. Payroll program
    By bigley04 in forum Eclipse
    Replies: 7
    Last Post: 12-06-2010, 04:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •