I have written three Java classes:
CompanyDepartmentEmployee
Each Company object contains an array of Department objects, which in turn contain arrays of Employee objects.
I want to write a method in the Employee class setQid() which will set a unique ID for that employee in the following format:
- compName_deptName_deptNumber_empFirstName_empSurname
Example:
- "Microsoft_Accounting_3_John_Smith"
My question is: how can I access the following variables from the context of my Employee class?
String nameinCompanyString nameinDepartmentint numberinDepartment
Please see my code below:
public class Company {
private String name;
private Department[] departments;
public Company(String name, Department[] departments) {
this.name = name;
this.departments = departments;
}
}
public class Department {
private String name;
private int number;
private Employee[] members;
public Department(String name, int number, Employee[] members) {
this.name = name;
this.number = number;
this.members = members;
}
}
public class Employee {
private String firstname;
private String surname;
private String qid; //A unique employee ID
public Employee(String firstname, String surname) {
this.firstname = firstname;
this.surname = surname;
setQid();
}
public void setQid() {
// How can I access the Company name, Department name, and Department number from here?
}
}
Employeedoes not have aCompany, you can create a newEmployeefrom thin air without anyCompanydefined anywhere.Companyhas aDepartmentwhich has anEmployeeright? There's no way to access data in this direction?Employee, no.