Answer:
Step-by-step explanation:
The following code is very long and is split into the 4 classes that were requested/mentioned in the question: Department, Employee, Address, and Test. Each one is its own object with its own constructor. Getters and Setters were not created since they were not requested and the constructor handles all of the variable creation. Address makes apartment variable optional by overloading the constructor with a new one if no argument is passed upon creation.
package sample;
import sample.Employee;
import java.util.ArrayList;
class Department {
String name = "";
double budget = 0;
ArrayList<Employee> employees = new ArrayList<>();
void Department(String name, double budget, ArrayList<Employee> employees) {
this.name = name;
this.budget = budget;
this.employees = employees;
}
}
------------------------------------------------------------------------------------------------------------
package sample;
public class Employee {
String FirstName, LastName;
Address address = new Address();
public void Employee(String FirstName, String LastName, Address address) {
this.FirstName = FirstName;
this.LastName = LastName;
this.address = address;
}
}
-------------------------------------------------------------------------------------------------------------
package sample;
class Address {
String Street, City;
int apartment;
char[] state;
public void Address(String street, String city, char[] state, int apartment) {
this.Street = street;
this.City = city;
this.state = state;
this.apartment = apartment;
}
public void Address(String street, String city, char[] state) {
this.Street = street;
this.City = city;
this.state = state;
this.apartment = 0;
}
}
-------------------------------------------------------------------------------------------------------------
package sample;
public class Test extends Department {
public void add(Department department, Employee employee) {
department.employees.add(employee);
}
public void delete(Department department, Employee employee) {
for (int x = 0; x < department.employees.size(); x++) {
if (department.employees.get(x) == employee) {
department.employees.remove(x);
}
}
}
public void Print(Department department, Employee employee) {
for (int x = 0; x < department.employees.size(); x++) {
if (department.employees.get(x) == employee) {
System.out.println(employee);
}
}
}
public void Search (Department department, Employee employee) {
for (int x = 0; x < department.employees.size(); x++) {
if (department.employees.get(x) == employee) {
System.out.println("Employee is located in index: " + x);
}
}
}
}