I have an Employee and a Job class, only up to 10 employees can be attached to a job so I store all the employees doing a specific job in the job class within a List (of max size = 10)

The problem is, that the program is meant to allow all the employees to be printed onto the terminal and since all the employees are being stored like this, each employee will get printed several times.

The only solution I can think to this is to initially have all "registered employees" stored somewhere as well as a list of employees per job. But this will make it even more difficult; when employees need to be deleted, they will need to be deleted from the initial list as well as the list of employees attached to a job.

Any helpful suggestions/ideas?

Edited 5 Years Ago by asif49: n/a

We did cover this before. Your Company/Employer class should contain a list of all the Employees ans well as a list of all the Jobs. Use the list of Employees to print Employees, and the list of Jobs to print Jobs.
When you assign a Job to an Employee add the Employee to the Job's list of "who is doing this job" Employees, and also add the Job to the Employee's list of "Jobs I am doing". Then if you delete an Employee you can easily see which Js they have assigned, and remove them from those Jobs.

This issue is different to the one I previously asked about, and for a totally different project. I just use the Employees/Jobs example because it makes it easier for me to explain the design implementation.

Based on what you're saying, When someone wants to delete an employee (for example), they will need to delete it from the initial list (Company class) AND from every job's list of employees in the Job class. Because if an employee is deleted, it shouldn't appear in any jobs either.

Isn't it bad coding practise to update details in several different places?

Edited 5 Years Ago by asif49: n/a

It's generally bad practice to duplicate data, but that's not what's happening here. Each Employee is a single instance somewhere in the JVM's memory and you have references to it from Company (because the comnpany employs him) and Job (because he's assigned to it). You could have employees with no jobs assigned (eg freshly hired), or Jobs being done by outside contractors who are not employed by the company. The two things are separate. Deleting an Employee from the Company is a different admin process from removing him from a Job, so you need to do both.

(Before you mention it, yes, I did suggest duplicating data by having a list of Jobs for each Employee and a list of Employees for each Job. That's a judgement call based on whether or not the extra work in maintaining both is justified by the reduced complexity of the rest of the code. Others may disagree.)

I only want to do this if there are no alternative ways which doesnt require duplication of data, anyone know any?

employee.addJob(specificJob);

the specificJob will get added to another list, that's duplicating.

A reference to the existing Job will be added to the Employee. The Job is not duplicated. There is still only one Job instance involved. The reference is a data item that defines a relationship between the Employee and the Job, and that is not duplicated data.

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