Casting issues

I am really stuck here and just going in circles trying to figure out what appears to be a relatively simple problem.

I have the following classes in my ongoing program:

Corporation - This is a concrete test class (User interface)

EmployeeUtilities - This is a concrete worker class which has the methods to do stuff to my employee objects

Employee- this is an abstract class

SalaryEmployee- a class with salaried employee methods which extends employee

HourlyEmployee-a class with hourly employee methods which extends employee

There are other but those are the ones I am dealing with right now. I am tasked to use the Corporation class to call a method in the EmployeeUtilities class which will find a certain employee and return the object if found, then use that object to set the number of hours the employee worked. I am stuck on casting issues with sending the object back and forth and would appreciate a little help. I will post some snippets of code that i think are relevant but if more is needed just ask and I will post more. This entire program is probably 1000 lines of code in 11 or 12 classes and I was hoping not to have to post it all but i could certainly email it to someone if need be.

Corporation class - Dealing with case 3

import java.util.*;

publicclass Corporation

{

publicstaticvoid main(String[]args)

{

//Instantiate Scanner, ArrayList and objects

Scanner scan=new Scanner(System.in);

ArrayList <Employee> employeeCollection=new ArrayList<Employee>();

EmployeeUtilities employeeObj=new EmployeeUtilities();

SalaryEmployee salaryObj=null;

HourlyEmployee hourlyObj=null;

EmployeeUtilities temp=null;

//Declare variables

boolean another=true;

int yesNo=0;

int choice=0;

String nameToFind=null;

boolean goodData=false;

//Begin menu loop

while(another)

{

System.out.print("\nPlease Choose from the following options:"+

"\n\n1)Enter a salaried employee"+

"\n2)Enter an hourly employee"+

"\n3)Enter hours worked for an hourly employee"+

"\n4)Get weekly pay for an employee"+

"\n5)List all employees\n6)Exit\n\nEnter choice: ");

try

{

choice=scan.nextInt();

String junk=scan.nextLine();

}

catch(InputMismatchException e)

{

System.out.println("\nThat input is not valid - Please choose 1 through 6 only!");

String junk=scan.nextLine();

}

catch(Exception e)

{

System.out.println("\nThat input is not valid - Please choose 1 through 6 only!");

String junk=scan.nextLine();

}

//Begin menu choice switch

switch(choice)

{

case 1:salaryObj=(SalaryEmployee)employeeObj.salaryEmployee();

employeeCollection.add(salaryObj);

break;

case 2:hourlyObj=(HourlyEmployee)employeeObj.hourlyEmployee();

employeeCollection.add(hourlyObj);

break;

case 3:while(!goodData)

{

System.out.print("\nPlease enter the employees last name: ");

try

{

nameToFind=scan.nextLine();

}

catch(Exception e)

{

System.out.println("\nThat input is not valid - Please enter employees last name.");

}

temp=employeeObj.getEmployee(nameToFind,employeeCollection);

if(temp==null)

{

goodData=false;

}

else

{

temp.hoursWorked();

goodData=true;

}

}

break;

case 4:employeeObj.weeklyPay(employeeCollection);

break;

case 5:employeeObj.listEmployees(employeeCollection);

break;

case 6:another=false;

break;

default:System.out.println("\nThat input is not valid - Please choose 1 through 6 only!");

}//End switch

}//End menu while loop

}//End main method

}//End class

From EmployeeUtilities class -

/**

Method to find an employee by last name

*/

public EmployeeUtilities getEmployee(String last,ArrayList tempArrayList)

{

EmployeeUtilities searchedEmployee=null;

boolean found=false;

int foundIndex=-1;

for(int i=0;i<tempArrayList.size();i++)

{

Employee p=(Employee)tempArrayList.get(i);

String[]nameArray=new String[tempArrayList.size()];

nameArray[i]=p.getLast();

if(nameArray[i].equals(last))

{

found=true;

foundIndex=i;

}

}

if(found)

{

System.out.println("\nThat employee was found.");

searchedEmployee=(EmployeeUtilities)tempArrayList.get(foundIndex);

return searchedEmployee;

}

else

{

System.out.println("\nThat employee was not found, please re-enter the last name.");

System.out.println(searchedEmployee);

return searchedEmployee;

}

}

Everything compiles but on runtime i get this error:

Exception in thread "main" java.lang.ClassCastException: HourlyEmployee

at EmployeeUtilities.getEmployee(EmployeeUtilities.java:453)

at Corporation.main(Corporation.java:81)

This is the line in corporation that is having the problem -

temp=employeeObj.getEmployee(nameToFind,employeeCollection);

I understand the error but really don't know how to go about doing this correctly. I have been stuck here for hours now and would appreciate any feedback.>

[8930 byte] By [LilWiza] at [2007-11-27 1:03:15]
# 1

Hello LilWiz, the way I see it temp is referring to the employee you're getting using the method "getEmployee(nameToFind,employeeCollection)". If so, temp should be declared as an Employee and not as EmployeeUtilities. Then, the abstract method "hoursWorked" in the abstract class "Employee" is implemented whichever way it should be in each object from a class that extends the Employee class (these are the only ones that are contained in the employeeCollection). Hope this helps.

Laginimaineb.

laginimaineba at 2007-7-11 23:38:19 > top of Java-index,Java Essentials,New To Java...