Worker having id(int), name(Sting), hoursWorked(int), salRate (double).a method addHours(int h) that increase the number of hoursWorked by the provided value.getCurrentSalary()that calculates and returns the current salary i.e. the hoursWorked times the salRate. class Project with id(int),desc (String), duration (int), budget (double) and members (ArrayList of Workers).methodaddMember(Worker m) that adds the worker instance to the members array list.a method Worker getMember(int wId) that searches for the worker with the given id in the members list and returns instance.Provide a method addMemberHours(int wId,int h) that finds the Worker with the wID in the members list and then adds worker hours to the instance. These many hours should also be added to the duration of this project,getTotalCost() that calculates the total cost of the project by summing the budget and the salary of each member of the project. The salary of each member can be found by calculating an appropriate method of the Worker class.
public class Worker {
private int id;
private String name;
private int hoursWorked;
private double salRate;
public void addHours(int h) {
hoursWorked += h;
}
public double getCurrentSalary() {
return hoursWorked * salRate;
}
public int getId() {
return id;
}
}
import java.util.ArrayList;
public class Project {
private int id;
private String desc;
private int duration;
private double budget;
private ArrayList<Worker> members;
public void addMember(Worker m) {
members.add(m);
}
public Worker getMember(int wId) {
for (Worker worker : members) {
if (worker.getId() == wId) {
return worker;
}
}
return null;
}
public void addMemberHours(int wId, int h) {
Worker worker = getMember(wId);
if (worker != null) {
worker.addHours(h);
duration += h;
}
}
public double getTotalCost() {
double salary = 0;
for (Worker worker : members) {
salary += worker.getCurrentSalary();
}
return salary + budget;
}
}
Comments
Leave a comment