0

StackPeople, I have a question. What statement could help me implement the right class before inserting it to the ArrayList. I have declared Nurse and Pilot which are Employees objects. I want each implementation of my class ArrEmp to store different Employees objects example: arrEmpNurses, arrEmpPilots,... after my class gets an example in the constructor What statement helps?? Or should I re think the problem. Thanks for your help.

THE PROBLEM IS TO FILL THE ARRAY WITH THE RIGHT CLASS (IT WILL READ FROM PLAIN TEXT AND IT NEWS TO BE NOTIFIED WhAT CLASS TO IMPLEMENT TO ADD IT)

"This code compiles, just copy paste."

import java.util.*;

public class ArrEmp {
String[][] data={ {"E1"}, {"Maria"}, {"E2"}, {"John"} }; //Data 
Employee x;
static Nurse nancy= new Nurse("01","Nancy");//this are just examples
static Pilot peter= new Pilot("02","Peter");//so the arrayEmp knows what type of employee create
ArrayList arr;

public ArrEmp(Employee x){
    this.x=x;
    arr= new ArrayList();
    fillList();//with data array
}

public void fillList(){// I would like to fill the List with Nurses. How could i do it?
    //for( String[] param: data )
        //arr.add(  ) //insert helpfull statement here
    //the goal is to have an array of Pilot and another of Nurses

}

public static void main(String[] args) {

     ArrEmp arr1= new ArrEmp( nancy );

     ArrEmp arr2= new ArrEmp( peter );
}

public static class Employee {
    String cod;

    public Employee(String cod){
        this.cod=cod;
    }
}

public static class Nurse extends Employee{
    String name;

    public Nurse(String ... para){
        super(para[0]);
        this.name=para[1];
    }
}

public static class Pilot extends Employee{
    String name;

    public Pilot(String ... para){
        super(para[0]);
        this.name=para[1];
    }
}   
}

I asked the question this way because data is actually read from Disk and ArrEmp has no idea what Employee he is reading. i need to provide an example so it builds the right employee and then insert it into the array. so new ArrEmp( nancy ) reads the file and builds Nurses and store them but new ArrEmp( nancy ) reads a file and loads Pilots on it.

EDIT SOLUTION: ESCENTIALLY I WILL CREATE A GENERIC ARRAYLIST EXTENDS EMPLOYEE, and extending classes for each Emlployee object...

corlaez
  • 1,352
  • 1
  • 15
  • 30

2 Answers2

2

Why not use generics? See: Java generics - ArrayList initialization

Essentially use

ArrayList<Nurse>

Instead of ArrayEmp(Nancy) to say it will only contain Nurses, then the language will take care of enforcing it.

Community
  • 1
  • 1
JohnB
  • 108
  • 1
  • 2
  • I asked the question this way because data is actually read from Disk and ArrEmp has no idea what Employee he is reading. i need to provide an example so it builds the right employee and then insert it into the array. so ArrEmp( nancy ) reads the file and builds nurses and store them... you get the idea. `ArrayList extends Employee> a1;` might help but still when reading it won't know what class to implement (i want to implement the childs as the real data will be different for each child) – corlaez Aug 11 '14 at 19:06
  • is there any way i could enter a String nameClass as param instead of example and then somehow with a case block pick to implement the right Class? – corlaez Aug 11 '14 at 19:26
  • Do you control the format for putting the data on disk? Generics work with serialization. – JohnB Aug 11 '14 at 21:03
  • Yeah i had to create other classes extending this one, I was just trying not to do so, but i think that's the best solution. – corlaez Aug 15 '14 at 03:45
0
public static class Employee {
    String name;
    int ID = 0;
    public Employee(String name){
        this.name = name;
    }
}

Just use ID's to denote the differentiation between all of them? You can create an ENUM and fill in legible names for differentiating between different objects. It's faster then string comparing and using instanceOf.

public static class Pilot extends Employee{
    int ID = 1;

    public Pilot(String name){
        this.name = name;
    }
}   

EDIT:

public ArrEmp(Employee x){
    if (x.ID == 1) // add to the list you want 
    else if (x.ID == 2) // add to list you want
     .... 
}
progrenhard
  • 2,333
  • 2
  • 14
  • 14
  • That seems to be leading somewhere. The problem is filling let's say i insert 1 in the constructor and that's a Pilot. how should I define fillList()? – corlaez Aug 11 '14 at 19:37
  • @lrn2java I think, you're thinking about this wrong. Let's say your reading your data you get the key pair `< 1, "name" >` All you do is have an `if ( 1 == ID ) addList( new Pilot("name"));` – progrenhard Aug 11 '14 at 19:44
  • @lrn2java I added some code that might clarify what I'm talking about. – progrenhard Aug 11 '14 at 19:50