1

I have written three Java classes:

  • Company
  • Department
  • Employee

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 name in Company
  • String name in Department
  • int number in Department

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?
    }
}
4
  • 2
    You can't. In your current setup an Employee does not have a Company, you can create a new Employee from thin air without any Company defined anywhere. Commented Jul 17, 2021 at 14:20
  • @luk2302 But the Company has a Department which has an Employee right? There's no way to access data in this direction? Commented Jul 17, 2021 at 14:23
  • 2
    Not if you just have reference to the Employee, no. Commented Jul 17, 2021 at 14:27
  • Guess I will have to rethink my class structure then. Thank you both. Commented Jul 17, 2021 at 14:29

1 Answer 1

2

You need to have a reference for the Department in your Employee class and a reference for the Company in your Department, only then will your relationships amongst your entities will be complete.

public class Company {

    private String name;
    private Department[] departments;

    public Company(String name, Department[] departments) {

        this.name = name;
        this.departments = departments;
    }
    //add getters and setters
}

public class Department {

    private String name;
    private int number;
    private Employee[] members;
    private Company company;

    public Department(String name, int number, Employee[] members) {

        this.name = name;
        this.number = number;
        this.members = members;
    }


    //add getters and setters
}

public class Employee {

    private String firstname;
    private String surname;
    private String qid; //A unique employee ID
    private Department department;

    public Employee(String firstname, String surname) {

        this.firstname = firstname;
        this.surname = surname;
        setQid();
    }

    public void setQid() {
     
      qid = department.getCompany().getName + "_" + department.getName() ....

    }

    //add getters and setters
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.