soe
soe

Reputation: 55

Can I Hash Map 3 class in java?

courses = new ArrayList<Course>();
units = new HashMap<Integer,Unit>();
staffs = new HashMap<Integer,Staff>();

Can I map unit class and staff class to course class like this? I want to store the data into dat file as serialized data.

My scenario is like this. Course have many units and course have 1 director and 1 deputy director which are from staff class.

I did like the first one. The problem is that I got a class called database.java and in there I have a method like this.

public boolean setDatabase(ArrayList<Course> iCourse, Map<Integer,Unit> iUnit)

So if I use this:

courses = new ArrayList<Course>();
units = new HashMap<Integer,Unit>();
staffs = new HashMap<Integer,Staff>();

Can I try this below?

public boolean setDatabase(ArrayList<Course> iCourse, Map<Integer,Unit> iUnit, Map<Integer,Staff> iStaff) 

Upvotes: 0

Views: 109

Answers (1)

Adeel Ansari
Adeel Ansari

Reputation: 39907

Try this,

public final class Course {
    Staff director;
    Staff deputyDirector;
    List<Unit> units;
    ...
}

Or if you want to force Director and Deputy Director, then you can subclass your Staff class like below,

public final class Director extends Staff {
    ...
}

public final class DeputyDirector extends Staff {
    ...
}

Note: Don't make your Staff class final, otherwise you can't make these subclasses.

And then let your Course class use those specific classes, like below,

public final class Course {
    Director director;
    DeputyDirector deputyDirector;
    List<Unit> units;
    ...
}

[Edited]

I have no idea what setDatabase() method suppose to do. I would suggest you to come up with a meaningful name. However, if you follow my suggestion you would be able to define your method as follows,

public boolean setDatabase(List<Course> courses) {
    ...
}

Course object will have related Staff and Unit objects associated with it.

Upvotes: 5

Related Questions