I have to make a program where there are employees who are created then can be assigned to multiple jobs, here's how I've done it...

MAIN class

Employee [] e = new Employee[10]; //class to contain/manipulate 10 individual employees
       Job [] j = new Job[10]; //class contain/manipulate 10 individual job types
       AllJobs alljobs = new AllJobs(); //separate class to keep all the above jobs
       AllEmployees allemployees = new AllEmployees(); //separate class to contain all employees

       e[0] = new Employee("John");
       e[1] = new Employee("Dave");
       e[2] = new Employee("Mike");
       j[0] = new Job("Washing up");
       j[1] = new Job("Coding");
       j[2] = new Job("Cleaning dishes");

for (int i = 0; i < e.length; i++) {
/*for loop places loops through employees placing them into AllEmployees*/
}

for (int i = 0; i < j.length; i++) {
/*for loop places loops through jobs placing them into AllJobs*/
}

/*Then an allJobs method which is in the Employee class prints all these jobs*/

alljobs.printAll();
allemployees.printAll();

/*To assign multiple jobs to each employee I have a assignJob method within the Employee class so I do...*/

e[0].assignJob(j[2]); //assigns employee John to Washing up, the Employee class for each person contains an ArrayList that contains a list of all jobs a specific emploee is attached to. This is so I can do the following...

Take in employee name: String a
Take in job name: String b

for loop to look through employees till position of entered employee's name is found, then for loop to find the position of entered job's name then do...

e[a].assignJob(j(b); //assign employee at position [a] to job at position [b]

My main problem here is am I using unnecessary classes when I have a separate class for Employee and AllEmployees and Job and AllJobs?

Also, is it possible to put the many for loops I have into classes?

PLEASE PLEASE PLEASE help!

Edited 5 Years Ago by asif49: n/a

I suggest three classes - Employee and Job, just like you have now, plus Employer (or Company if you prefer). Think about it as if these were real things - you have a Company, and the Company manages a number of Employees and a number of Jobs and assigns Jobs to Employees - so the collections allEmployees and allJobs belong inside the Company class, as do the methods for assigning Jobs to Employees, adding new Employees or Jobs, finding Employees or Jobs by name etc.
You'll find that Employee and Job are very simple classes - little more than data holders. Most of the methods (and the for loops inside those methods) will be in the Company class.

I was thinking that myself actually, but then I thought what would the Company class need to hold?

The Employee and Jobs class has setTitle, getTitle and so on... what role may the Company class play in this design?

I think I already answered that: "the collections allEmployees and allJobs belong inside the Company class, as do the methods for assigning Jobs to Employees, adding new Employees or Jobs, finding Employees or Jobs by name etc."

If you just have a simple console user interface you could put the code for that in Company too, or you could have a fourth class that contains all the user interface (that's what I would do; I'd have the main method in that class also). If you have GUI, it definitely should be in its own class(es).

This article has been dead for over six months. Start a new discussion instead.