Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Object Array help

$
0
0
Hello I am working on this assignment and I cannot seem to figure out what I am doing wrong. I am trying to create an object array of Employees named payroll and fill it with employees. I have done this before but clearly am missing something this time as I keep getting nullPointerException. Help. here is my code thus far.. Apologies for slight disorganization.
PS. I have been toggling the different elements of array from [i] to [0] and [current] just to check if my error was there. I know the way I have it setup won't fill the whole array ATM.
payrolltest.java
package payrolltest;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

public class PayrollTest {
    
        public static void main(String[] args) throws IOException
                { 
       
       	int choice;
       	Payroll payroll = null;
        
        	do
    	{
    		choice = menu();
    		switch(choice)
    		{
    			case 1: // create a new Payroll 
    					payroll = createNewPayroll(payroll);
    					break;
    			case 2: // create a payroll from a file
    					payroll = createPayrollFromFile(payroll);
    					break;
    			case 3: // print payroll to the screen
    					printPayroll(payroll);
    					break;
    			case 4: // output the total wages
    					giveTotalWages(payroll);
    					break;
    			case 5: // find an employee
    					findEmployee(payroll);
    					break;
    			case 6: // change an employee
    					changeEmployee(payroll);
    					break;
    			case 7: //delete and employee
    					deleteEmployee(payroll);	
     					break;
    			case 8: // add an employee
    					addEmployee(payroll);
    					break;
    			case 9: // save the payroll
    					savePayroll(payroll);
    					break;
       			case 10:System.out.println("Good-bye");
    					break;
			default: //Error message
       		}	
    	}while (choice != 10);
    }
public static int menu()
        {
            int choice;
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Please enter a menu choice");
            System.out.println(" 1: Create new Payroll\n 2: Create new Payroll"
                    + " from file\n 3: Print Payroll to screen\n 4: Give total wages"
                    + "\n 5: Find employee\n 6: Change employee\n 7: Delete employee"
                    + "\n 8: Add employee\n 9: Save payroll\n10: exit payroll");
            choice = keyboard.nextInt();
            return choice;
            
        }

public static Payroll createNewPayroll(Payroll payroll)
        
        {
          payroll = new Payroll();
          return payroll;
        }
 private static Payroll createPayrollFromFile(Payroll payroll) throws IOException
    {
        payroll = new Payroll("payroll.txt");
        return payroll;
    }

    private static void printPayroll(Payroll payroll) 
    {
        System.out.print(payroll.toString());
    }

    private static void giveTotalWages(Payroll payroll) 
    {
        double total;
        total = payroll.getTotalWages();
        System.out.println("The total wages are: " + total);
    }
    private static void findEmployee(Payroll payroll) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void changeEmployee(Payroll payroll) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void deleteEmployee(Payroll payroll) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void addEmployee(Payroll payroll) 
    {
    Scanner keyboard = new Scanner(System.in);
    
    int id;
    String first;
    String last;
    int hours;
    double payRate;
    
    Employee emp = new Employee();
    System.out.println("Please enter employee ID");
    emp.setEmployeeId(Integer.parseInt(keyboard.nextLine()));
    
    System.out.println("Please enter employees first name");
    emp.setFirstName(keyboard.nextLine());
    
    System.out.println("please enter last name");
    emp.setLastName(keyboard.nextLine());
    
   
    System.out.println("Please enter employees hours");
    emp.setHours(keyboard.nextInt());
    
    System.out.println("Please enter employess pay rate");
    emp.setPayRate(keyboard.nextDouble());
    
    
    
    payroll.addEmployee(emp);
    
  
    }

    private static void savePayroll(Payroll payroll) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
        
            
}
        


Payroll.java

package payrolltest;

import java.io.*;
import java.util.StringTokenizer;
import java.text.DecimalFormat;
import java.util.Scanner;


public class Payroll {
    
    private Employee[] payroll;
    private final int NUM_EMP = 7;
    private int numEmployees;
    int current = 0;
    private DecimalFormat formatter = new DecimalFormat("#0.00");
    Scanner keyboard = new Scanner(System.in);
    


public Payroll()
{
    
System.out.println("how many employees");
numEmployees = keyboard.nextInt();
payroll = new Employee[numEmployees];
}

public Payroll(String filename) throws IOException
{
   int id;
   String first;
   String last;
   int hours;
   double payRate;
   
   File file = new File(filename);
   Scanner inputFile = new Scanner(file);
   payroll = new Employee[NUM_EMP];

   
  while(inputFile.hasNext())
  {
StringTokenizer strToken = new StringTokenizer(inputFile.nextLine()," ");

while (strToken.hasMoreTokens())
{
     
   
    id = Integer.parseInt(strToken.nextToken());
    first = strToken.nextToken();
    last = strToken.nextToken();
    hours = Integer.parseInt(strToken.nextToken());
    payRate = Double.parseDouble(strToken.nextToken());
    
    
    
    Employee emp = new Employee(id,first,last,hours,payRate);
   
            payroll[0] = emp;
            
}
  }
  
    
}

public double getTotalWages()
{
    double total= 0;
    for (int i = 0; i <NUM_EMP; i++)
    {
        total += payroll[i].getPayRate();
    }
    return total;
}
public void addEmployee(Employee emp)
{

    payroll[current] = emp;
    current++;
}


    @Override     
    public String toString()   
            
{
    String roll;
    roll = payroll[current].getFirstName() + payroll[current].getLastName();
    return roll;
    
}

    
}


Employee.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package payrolltest;
import java.text.DecimalFormat;
/**
 *
 * @author Richard
 */
public class Employee {
    
    private int employeeId;
    private String firstName;
    private String lastName;
    private int hours;
    private double payRate;
    
    private DecimalFormat formatter = new DecimalFormat("#0.00");

public Employee()
{
    
}
public Employee(int id, String first, String last)
{
    id = employeeId;
    first = firstName;
    last = lastName;
}
public Employee(int id, String first, String last,int hr, double pRate)
{
    id = employeeId;
    first = firstName;
    last = lastName;
    hr = hours;
    pRate = payRate;
    
}
public void setEmployeeId(int id)
{
    employeeId = id;
}
public void setFirstName(String name)
{
    firstName = name;
}
public void setLastName(String name)
{
    lastName = name;
}
public void setHours(int hours)
{
   this.hours = hours;
}
public void setPayRate(double rate)
{
    payRate = rate;
}

public int getId()
{
    return employeeId;
}

public String getFirstName()
{
    return firstName;
}
public String getLastName()
{
    return lastName;
}
public int getHours()
{
    return hours;
}
public double getPayRate()
{
    return payRate;
}

    
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles