Answer to Question #272697 in Java | JSP | JSF for bvn

Question #272697

Build a class Department having id(int), address(String) and Project(array list of projects) and Worker(array list of members). Provide a method addProject(int id, String description, double budget) that creates project and adds it to project list. Provide method addWorkerToProject(int projId, int wId) that searches for project with projId from projects list and worker with wId from members lists and adds member to project (you will call appropriate method of project class that will add member to project. Provide a method getProjectCost(int pid) which searches for project with given id from projects list and returns its total cost (you can call method from project class to get total cost) . Provide a method getDepartmentTotalBudget() that calculates total cost of each project of the department and adds it up to get total budget (call appropriate method of Project instances for project costs) .Provide toString of department so that it outputs all details including each project and members in each project.


1
Expert's answer
2021-11-28T11:44:29-0500
class Depart {
	private int ID;
	private arr_list<Worker> workers;
	private arr_list<Proj> proj;
	private String addres;
	public Depart(String addres) {
		this.ID = (int) Math.random() * 1000;
		this.addres = addres;
		this.proj = new arr_list<Proj>();
		this.workers = new arr_list<Worker>();
	}
	public void addProject(int ID, String description, double budget) {
		this.proj.add(new Proj(ID, description, budget));
	}
	public void addWorker(int ID, String N, double salRate) {
		this.workers.add(new Worker(ID, N, salRate));
	}
	public boolean addWorkerToProject(int projId, int wId) {
		for (Proj project : proj) {
			if (project.getId() == projId) {
				for (Worker w : workers) {
					if (w.getId() == wId) {
						project.addMember(w);
						return true;
					}
				}
			}
		}
		return false;
	}
	public double getProjectCost(int pid) {
		double totalC = 0;
		for (Proj project : proj) {
			if (project.getId() == pid) {
				totalC += project.getTotalCost();
			}
		}
		return totalC;
	}
	public String toString() {
		String output = "Department ID: " + ID + "\nDepartment address: " + addres + "\nAll projects:\n";
		for (Proj project : proj) {
			output += project.toString() + "\n\n";
		}
		output += "All workers:\n";
		for (Worker w : workers) {
			output += w.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