Answer to Question #272720 in Java | JSP | JSF for Saba

Question #272720


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

workers (array list of workers) 


c.

a method addWorker(int id, String name, double salRate) that creates an 

instance of Worker and adds it to the workers list 

d. Provide a method addProject(int id, String description, double budget) that creates a 

project and adds it to the project list 

e. Provide method addWorkerToProject(int projId, int wId) that searches for the project 

with projId from the projects list and worker with wId from the members lists and then 

adds the member to the project (you will call appropriate method of the project class 

that will add the member to the project. See blow the project class for details. 

f. Provide a method getProjectCost(int pid) which searches for the project with the given 

id from the projects list and then returns its total cost (you can call a method from the 

project class to get the total cost… see the project class for details) 



1
Expert's answer
2021-11-28T11:44:32-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