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 days. if over
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 setNextApproverLeaveApprover nextApprover;
void approveLeaveLeaveRequest leaveRequest;
Classes:
package ChainOfResponsibiltyDemo;
LeaveRequest class
class LeaveRequest
private int days;
public LeaveRequestint days
this.days days;
public int getDays
return days;
package ChainOfResponsibiltyDemo;
class TeamLead implements LeaveApprover
private LeaveApprover nextApprover;
public void setNextApproverLeaveApprover nextApprover
this.nextApprover nextApprover;
public void approveLeaveLeaveRequest leaveRequest
if leaveRequestgetDays
System.out.printlnLeave approved by Team Lead";
else if nextApprover null
nextApprover.approveLeaveleaveRequest;
else
System.out.printlnLeave rejected";
package ChainOfResponsibiltyDemo;
class DepartmentManager implements LeaveApprover
private LeaveApprover nextApprover;
public void setNextApproverLeaveApprover nextApprover
this.nextApprover nextApprover;
public void approveLeaveLeaveRequest leaveRequest
if leaveRequestgetDays
System.out.printlnLeave approved by Department Manager";
else if nextApprover null
nextApprover.approveLeaveleaveRequest;
else
System.out.printlnLeave rejected";
package ChainOfResponsibiltyDemo;
class HRManager implements LeaveApprover
private LeaveApprover nextApprover;
public void setNextApproverLeaveApprover nextApprover
this.nextApprover nextApprover;
public void approveLeaveLeaveRequest leaveRequest
if leaveRequestgetDays
System.out.printlnLeave approved by HR Manager";
else if nextApprover null
nextApprover.approveLeaveleaveRequest;
else
System.out.printlnLeave rejected";
Driver
package ChainOfResponsibiltyDemo;
public class CoRDriver
public static void mainString args
LeaveApprover teamLead new TeamLead;
LeaveApprover departmentManager new DepartmentManager;
LeaveApprover hrManager new HRManager;
Set up the chain of responsibility
teamLead.setNextApproverdepartmentManager;
departmentManager.setNextApproverhrManager;
Create a leave request test with and to see the resu
LeaveRequest leaveRequest new LeaveRequest;
Send the leave request for approval
teamLead.approveLeaveleaveRequest;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
