Question: Lab Assignment: Extending the Leave Approval Program You have been given a leave approval program that uses the Chain of Responsibility pattern to handle leave

Lab Assignment: Extending the Leave Approval Program
You have been given a leave approval program that uses the Chain of Responsibility pattern to
handle leave requests.
Demo - Chain or Responsibility Design Pattern
Your task is to extend the program by adding the following functionality:
Requirements:
Implement a new handler called CEO that represents the Chief Executive Officer in the
leave approval process. The CEO can approve any leave request less than 30 days. if over
30 days - reject leave request.
Update the Main class to include the new CEO handler in the chain of responsibility.
Modify each approver to have the system output a message that the leave request needs to
be moved to the next requester in the event the leave request exceeds the amount of leave
the approver is authorized to approve
Test your implementation by creating leave requests with various durations and verifying
that each request is approved by the appropriate handler in the chain.
Note:
You can assume that the existing classes (LeaveApprover, TeamLead,
DepartmentManager, , LeaveRequest, and Main) are already implemented
correctly.
You can use the existing code provided in the previous example as a starting point for your
implementation.
Chain of responsibility pattern:
Interface:
package ChainOfResponsibiltyDemo;
interface LeaveApprover {
void setNextApprover(LeaveApprover nextApprover);
void approveLeave(LeaveRequest leaveRequest);
}
Classes:
package ChainOfResponsibiltyDemo;
// LeaveRequest class
class LeaveRequest {
private int days;
public LeaveRequest(int days){
this.days = days;
}
public int getDays(){
return days;
}
}
package ChainOfResponsibiltyDemo;
class TeamLead implements LeaveApprover {
private LeaveApprover nextApprover;
public void setNextApprover(LeaveApprover nextApprover){
this.nextApprover = nextApprover;
}
public void approveLeave(LeaveRequest leaveRequest){
if (leaveRequest.getDays()=5){
System.out.println("Leave approved by Team Lead");
} else if (nextApprover != null){
nextApprover.approveLeave(leaveRequest);
} else {
System.out.println("Leave rejected");
}
}
}
package ChainOfResponsibiltyDemo;
class DepartmentManager implements LeaveApprover {
private LeaveApprover nextApprover;
public void setNextApprover(LeaveApprover nextApprover){
this.nextApprover = nextApprover;
}
public void approveLeave(LeaveRequest leaveRequest){
if (leaveRequest.getDays()=10){
System.out.println("Leave approved by Department Manager");
} else if (nextApprover != null){
nextApprover.approveLeave(leaveRequest);
} else {
System.out.println("Leave rejected");
}
}
}
package ChainOfResponsibiltyDemo;
class HRManager implements LeaveApprover {
private LeaveApprover nextApprover;
public void setNextApprover(LeaveApprover nextApprover){
this.nextApprover = nextApprover;
}
public void approveLeave(LeaveRequest leaveRequest){
if (leaveRequest.getDays()=15){
System.out.println("Leave approved by HR Manager");
} else if (nextApprover != null){
nextApprover.approveLeave(leaveRequest);
} else {
System.out.println("Leave rejected");
}
}
}
Driver
package ChainOfResponsibiltyDemo;
public class CoRDriver {
public static void main(String[] args){
LeaveApprover teamLead = new TeamLead();
LeaveApprover departmentManager = new DepartmentManager();
LeaveApprover hrManager = new HRManager();
// Set up the chain of responsibility
teamLead.setNextApprover(departmentManager);
departmentManager.setNextApprover(hrManager);
// Create a leave request - test with 4,14 and 20 to see the resu
LeaveRequest leaveRequest = new LeaveRequest(8);
// Send the leave request for approval
teamLead.approveLeave(leaveRequest);
}
}
Lab Assignment: Extending the Leave Approval

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!