In: Computer Science
Problem Description An organization has a number of buildings and employees. Every employee has an office in one of the buildings. The application will store the employees, the buildings, and the relationships between employees and buildings. Entity interface The Entity interface represents an employee or a building. Every object of type Entity has a single method, with the following characteristics. • Name: getName() • Returns the name of the entity (a String object) • No parameters Employee class This class implements the Entity interface. Every Employee object has a single attribute named name. Besides this field, the class has the following constructor and methods. • A constructor that takes as parameter, only the employee’s name and stores it appropriately. • A method named print() that prints the employee’s name. The method has no parameters and returns nothing. • The appropriate method to satisfy the interface Entity. Building class This class implements the Entity interface. Every Building object has an attribute named name. Besides this field, the class also uses a collection class (see below) to store the employees housed in the building. In addition, the class has the following constructor and methods. • A constructor that takes as parameter, only the building’s name and stores it appropriately. • A method named addEmployee() to add an employee to the collection that stores the list of employees housed in the building. The method returns nothing and has a single parameter of type Employee. • A method named print() that prints the building and the names of employees housed in the building The method has no parameters and returns nothing. • The appropriate method to satisfy the interface Entity. EntityList interface This interface represents a list of Entity objects and has two methods. • print(): The method has no parameters and returns nothing. The method prints every entity stored in the list. • addEntity(): The method has a single parameter of type Entity and returns nothing. It stores the Entity object supplied as parameter. The class should have a no-arg (default or otherwise) constructor. BuildingList class Implements the EntityList interface appropriately to store Building objects. You will need to cast objects as needed. Figure this out yourself. The class should have a no-arg (default or otherwise) constructor. EmployeeList class Implements the EntityList interface appropriately to store Employee objects. You will need to cast objects as needed. Figure this out yourself. Driver class Your driver class should do the following actions: • Create four Employee objects with your choice of employee names. • Create two Building objects with your choice of building names. • Store all Building and Employee objects in the proper list objects. • Store two of the employees in one of the buildings and the other two in the second building. • Invoke the print() method of EmployeeList to print the employees. • Invoke the print() method of BuildingList to print the buildings.
/**************************Entity.java**********************/
public interface Entity {
public String getName();
}
/******************************Employee.java************************/
public class Employee implements Entity {
//private data field
private String name;
/**
*
* @param name
*/
public Employee(String name) {
super();
this.name = name;
}
@Override
public String getName() {
return name;
}
public void print() {
System.out.println("Employee
Name: " + name);
}
}
/*************************Building.java*******************************/
import java.util.ArrayList;
public class Building implements Entity {
//private data field
private String buildingName;
private ArrayList<Employee> employees;
public Building(String buildingName) {
super();
this.buildingName =
buildingName;
employees = new
ArrayList<>();
}
//constructor
public void addEmployee(Employee employee) {
employees.add(employee);
}
//getName method
@Override
public String getName() {
return buildingName;
}
//print method
public void print() {
System.out.println("Building
name: " + buildingName);
System.out.println("The names of
employees in the building: ");
for (Employee employee : employees)
{
System.out.println(employee.getName());
}
}
}
/**************************************EntityList.java*********************************/
public interface EntityList {
public void print();
public void addEntity(Entity entity);
}
/****************************BuildingList.java*******************************./
import java.util.ArrayList;
public class BuildingList implements EntityList {
//private data field
private ArrayList<Building> buildings;
public BuildingList() {
buildings = new
ArrayList<>();
}
//print method
@Override
public void print() {
System.out.println("Building
list is: ");
for (Building building : buildings)
{
System.out.println(building.getName());
}
}
//addEntity method
@Override
public void addEntity(Entity entity) {
buildings.add((Building) entity);
}
}
/*****************************Employee.java**********************************/
import java.util.ArrayList;
public class EmployeeList implements EntityList {
//data field
private ArrayList<Employee> employees;
//no argument constructor
public EmployeeList() {
employees = new
ArrayList<>();
}
//print method
@Override
public void print() {
System.out.println("Employee
list is: ");
for (Employee employee : employees)
{
System.out.println(employee.getName());
}
}
//add entity method
@Override
public void addEntity(Entity entity) {
employees.add((Employee)
entity);
}
}
/******************************Driver.java*******************************/
public class Driver {
public static void main(String[] args) {
// create 4 employee
object
Employee employee1 = new
Employee("Virat");
Employee employee2 = new
Employee("Dhoni");
Employee employee3 = new
Employee("Kohli");
Employee employee4 = new
Employee("Jadeja");
// create two buildings
Building building1 = new
Building("ICC");
Building building2 = new
Building("BCCI");
// add employees to buildings
building1.addEmployee(employee1);
building1.addEmployee(employee2);
building2.addEmployee(employee3);
building2.addEmployee(employee4);
// create building list
BuildingList buildingList = new
BuildingList();
buildingList.addEntity(building1);
buildingList.addEntity(building2);
// create employee list
EmployeeList employeeList = new
EmployeeList();
employeeList.addEntity(employee1);
employeeList.addEntity(employee2);
employeeList.addEntity(employee3);
employeeList.addEntity(employee4);
// print information
employeeList.print();
buildingList.print();
}
}
/*****************************output********************************/
Employee list is:
Virat
Dhoni
Kohli
Jadeja
Building list is:
ICC
BCCI
Please let me know if you have any doubt or modify the anser, Thanks :)