So I have done everything the task requires in the code below.
The problem I am have is about the
- Defense copy's of a Object
- Change the values in the Employee object.
For 2
I change the values but I call new which is creating a new instance of the object.
employee1 = new Employee("626347B", "Sam O'Conor", 24000);
This way the problem works as the second time I call mainDepartment.display();
the values are the same.
But this does not feel like proper encapsulation as i am creating a new object.
I was thinking of
employee1.setName("Conor Bryan);
Is how you test for encapsulation, now when I call mainDepartment.display();
the name value does change so the question is wrong.
!-----Question-----!
Display the Employee details
Display the department details
Change the values in the Employee object.
Display the Department details again (The information should not change)
Again display the Employee details (The information should be changed here).
!----Test----!
package question1;
public class Test {
public static void main(String[] args) {
//Creating a instance of both Employee and Department
Employee employee1 = new Employee("2726354E", "Bob Ings", 30000 );
//Updated Code for Department to take a copy of Employee
Employee copy = new Employee("2726354E", "Bob Ings", 30000 );
Department mainDepartment = new Department("Main Floor", copy);
//Displaying both instances of Employee and Department
employee1.display();
mainDepartment.display();
System.out.println("");
//Changing values in constructor for the instance of Employee we made earlier on
employee1 = new Employee("626347B", "Sam O'Conor", 24000);
mainDepartment.display();
System.out.println("");
System.out.println("");
employee1.display();
}
}
!----Department----!
package question1;
public class Department {
private String deptName;
private Employee employee;
private int officeNumber;
//Constructor with all three parameters
public Department(String deptName, Employee employee, int officeNumber) {
this.deptName = deptName;
this.employee = employee;
this.officeNumber = officeNumber;
}
//Constructor with the officeNumber set to 0
public Department(String deptName, Employee employee) {
this.deptName = deptName;
this.employee = employee;
this.officeNumber = 0;
}
//Displaying the instance of the object information in a anesthetically pleasing manner
public void display() {
System.out.println("Department");
Employee.seperationLine();
System.out.println("Department Name: " + getDeptName());
Employee.seperationLine();
System.out.println("Employee: " + employee.toString());
Employee.seperationLine();
System.out.println("Office Number: " + getOfficeNumber());
}
//Getters and Setters
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public int getOfficeNumber() {
return officeNumber;
}
public void setOfficeNumber(int officeNumber) {
this.officeNumber = officeNumber;
}
}
!----Employee----!
package question1;
public class Employee {
private String ppsNum;
private String name;
private double salary;
public Employee() {}
//Parameterized constructor
public Employee(String ppsNum, String name, double salary) {
this.ppsNum = ppsNum;
this.name = name;
this.salary = salary;
}
//Defensive copy constructor
public Employee(Employee copy) {
this.ppsNum = copy.ppsNum;
this.name = copy.name;
this.salary = copy.salary;
}
//Displaying the instance of the object information in a anesthetically pleasing manner
public void display() {
System.out.println("Employee Information");
seperationLine();
System.out.println("Name: " + getName());
seperationLine();
System.out.println("PPS number: " + getPpsNum());
seperationLine();
System.out.println("Salary: " + getSalary() + "0");
seperationLine();
System.out.println("\n");
}
public String toString() {
return "[ppsNum=" + ppsNum + ", name=" + name + ", salary=" + salary + "]";
}
//Getters and Setters
public String getPpsNum() {
return ppsNum;
}
public void setPpsNum(String ppsNum) {
this.ppsNum = ppsNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public static void seperationLine() {
System.out.println("------------------------");
}
}