Answer to Question #272695 in Java | JSP | JSF for User 12345

Question #272695

Build a class Department having id (int), address (String) and projects (array list of projects) and 

workers (array list of workers) 

Provide parameterized constructor Department (String address) which initialized id randomly & array lists as empty array lists. Provide method addWorker(int id, String name, double salRate) that adds instance of Worker to workers list. Provide a method addProject(int id, String description, double budget) that creates project & adds it to the project list. Provide method addWorkerToProject(int projId, int wId) that searches for the project with projId from projects list and worker with wId from members lists & adds member to project (you will call appropriate method of the project class that'll add member to project.

Provide method getProjectCost(int pid) which searches for project with given id from projects list and then returns its total cost (you call method from the project class to get total cost. Provide toString of department to outputs all details.


1
Expert's answer
2021-11-28T05:25:04-0500
class Department {
	// id (int), address (String) and projects (array list of projects) and workers
	// (array list of workers)
	private int id;
	private String address;
	private ArrayList<Project> projects;
	private ArrayList<Worker> workers;


	/**
	 * parameterized constructor Department (String address) which initialized id
	 * randomly & array lists as empty array lists.
	 * 
	 * @param address
	 */
	public Department(String address) {
		this.id = (int) Math.random() * 1000;
		this.address = address;
		this.projects = new ArrayList<Project>();
		this.workers = new ArrayList<Worker>();
	}


	/***
	 * method addWorker(int id, String name, double salRate) that adds instance of
	 * Worker to workers list.
	 * 
	 * @param id
	 * @param name
	 * @param salRate
	 */
	public void addWorker(int id, String name, double salRate) {
		this.workers.add(new Worker(id, name, salRate));
	}


	/***
	 * a method addProject(int id, String description, double budget) that creates
	 * project & adds it to the project list.
	 * 
	 * @param id
	 * @param description
	 * @param budget
	 */
	public void addProject(int id, String description, double budget) {
		this.projects.add(new Project(id, description, budget));
	}


	/***
	 * Provide method addWorkerToProject(int projId, int wId) that searches for the
	 * project with projId from projects list and worker with wId from members lists
	 * & adds member to project (you will call appropriate method of the project
	 * class that'll add member to project.
	 * 
	 * @param projId
	 * @param wId
	 */
	public boolean addWorkerToProject(int projId, int wId) {
		for (Project project : projects) {
			if (project.getId() == projId) {
				for (Worker worker : workers) {
					if (worker.getId() == wId) {
						project.addMember(worker);
						return true;
					}
				}
			}
		}
		return false;
	}


	/****
	 * method getProjectCost(int pid) which searches for project with given id from
	 * projects list and then returns its total cost (you call method from the
	 * project class to get total cost.
	 * 
	 * @param pid
	 * @return
	 */
	public double getProjectCost(int pid) {
		double totalCost = 0;
		for (Project project : projects) {
			if (project.getId() == pid) {
				totalCost += project.getTotalCost();
			}
		}
		return totalCost;
	}


	/**
	 * Override the toString to get appropriate output
	 */
	@Override
	public String toString() {
		String output = "Department ID: " + id + "\nDepartment address: " + address + "\nAll projects:\n";
		for (Project project : projects) {
			output += project.toString() + "\n\n";
		}
		output += "All workers:\n";
		for (Worker worker : workers) {
			output += worker.toString() + "\n\n";
		}
		return output;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS