Answer to Question #272724 in Java | JSP | JSF for Alisha

Question #272724

Build a class Department having id (int), address (String) and projects (array list of projects) and workers (array list of workers) a. Provide parameterized constructor Department (String address) which initialized id randomly and array lists as empty array lists b. Provide getters for id and address and setter for address only c. Provide 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.


1
Expert's answer
2021-11-28T18:41:37-0500
import java.util.ArrayList;
import java.util.Random;

public class Department {
    private int id;
    private String address;
    private ArrayList<Project> projects;
    private ArrayList<Worker> workers;

    public Department(String address) {
        id = new Random().nextInt(Integer.MAX_VALUE);
        this.address = address;
        projects = new ArrayList<>();
        workers = new ArrayList<>();
    }

    public int getId() {
        return id;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void addWorker(int id, String name, double salRate) {
        workers.add(new Worker(id, name, salRate));
    }

    public void addProject(int id, String description, double budget) {
        projects.add(new Project(id, description, budget));
    }

    public void addWorkerToProject(int projId, int wId) {
        Worker worker = null;
        for (Worker workerL : workers) {
            if (workerL.getId() == wId) {
                worker = workerL;
                break;
            }
        }
        if (worker != null) {
            for (Project projectL : projects) {
                if (projectL.getId() == projId) {
                    project.addMember(worker);
                    break;
                }
            }
        }
    }
}

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