504,440 views
26 votes
26 votes
Create a Department object which contains a name (String), budget (double), and an ArrayList of Employee objects. Each Employee object contains a FirstName (String). LastName(String), and an Address object. Each Address object contains a Street (String), Apartment (int), City (String), and State as a char array of length 2. The Apartment parameter is optional but the other three are mandatory! Ensure no Address object can be created that does not have them. Finally, create a test program that allows users to Add, Delete, Print, and Search the Employees of a Department.

User Bahador Saket
by
2.6k points

1 Answer

22 votes
22 votes

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);

}

}

}

}

User Xi Liang
by
3.2k points