I am trying to test encapsulation.
I have madetwo Objects Department and Employee.
Department get passed a instance of Employee, then to test for encapsulation I follow these rules
1.Display the Employee details
2.Display the department details
3.Change the values in the Employee objec
4.Display the Department details again (The information should not change)
5.Again display the Employee details (The information should be changed here).
This works but am I getting the idea of encapsulation wrong by creating a new instance of employee1????
Or
Should I be setting the values for true encapsulation
employee1.setName("Sam")
This changes the second display() call of Department name to Sam.
//Main
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 );
Department mainDepartment = new Department("Main Floor", employee1);
//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();
}
}
//Employee Class
package question1;
public class Employee {
private String ppsNum;
private String name;
private double salary;
//Parameterized constructor
public Employee(String ppsNum, String name, double salary) {
this.ppsNum = ppsNum;
this.name = name;
this.salary = 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");
}}
//Department Class
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());
}
}