Skip to content

Commit 4f4415b

Browse files
authored
Merge pull request #75 from adityacosmos24/main
Chain of Responsibility Pattern Implementation (C++)
2 parents 54f8183 + 804c33e commit 4f4415b

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

Src/ATM_CASH_DESPENSER/COR.cpp

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// Abstract Handler (Base Class)
5+
class MoneyHandler {
6+
protected:
7+
MoneyHandler *nextHandler;
8+
9+
public:
10+
MoneyHandler() {
11+
this->nextHandler = nullptr;
12+
}
13+
14+
void setNextHandler(MoneyHandler *next) {
15+
nextHandler = next;
16+
}
17+
18+
virtual void dispense(int amount) = 0;
19+
};
20+
21+
class ThousandHandler : public MoneyHandler {
22+
private:
23+
int numNotes;
24+
25+
public:
26+
ThousandHandler(int numNotes) {
27+
this->numNotes = numNotes;
28+
}
29+
30+
void dispense(int amount) override {
31+
int notesNeeded = amount / 1000;
32+
33+
if(notesNeeded > numNotes) {
34+
notesNeeded = numNotes;
35+
numNotes = 0;
36+
}
37+
else {
38+
numNotes -= notesNeeded;
39+
}
40+
41+
if(notesNeeded > 0)
42+
cout << "Dispensing " << notesNeeded << " x ₹1000 notes.\n";
43+
44+
int remainingAmount = amount - (notesNeeded * 1000);
45+
if(remainingAmount > 0) {
46+
if(nextHandler != nullptr) nextHandler->dispense(remainingAmount);
47+
else {
48+
cout << "Remaining amount of " << remainingAmount << " cannot be fulfilled (Insufficinet fund in ATM)\n";
49+
}
50+
}
51+
}
52+
};
53+
54+
// Concrete Handler for 500 Rs Notes
55+
class FiveHundredHandler : public MoneyHandler {
56+
private:
57+
int numNotes;
58+
59+
public:
60+
FiveHundredHandler(int numNotes) {
61+
this->numNotes = numNotes;
62+
}
63+
64+
void dispense(int amount) override {
65+
int notesNeeded = amount / 500;
66+
67+
if(notesNeeded > numNotes) {
68+
notesNeeded = numNotes;
69+
numNotes = 0;
70+
}
71+
else {
72+
numNotes -= notesNeeded;
73+
}
74+
75+
if(notesNeeded > 0)
76+
cout << "Dispensing " << notesNeeded << " x ₹500 notes.\n";
77+
78+
int remainingAmount = amount - (notesNeeded * 500);
79+
if(remainingAmount > 0) {
80+
if(nextHandler != nullptr) nextHandler->dispense(remainingAmount);
81+
else {
82+
cout << "Remaining amount of " << remainingAmount << " cannot be fulfilled (Insufficinet fund in ATM)\n";
83+
}
84+
}
85+
}
86+
};
87+
88+
// Concrete Handler for 200 Rs Notes
89+
class TwoHundredHandler : public MoneyHandler {
90+
private:
91+
int numNotes;
92+
93+
public:
94+
TwoHundredHandler(int numNotes) {
95+
this->numNotes = numNotes;
96+
}
97+
98+
void dispense(int amount) override {
99+
int notesNeeded = amount / 200;
100+
101+
if(notesNeeded > numNotes) {
102+
notesNeeded = numNotes;
103+
numNotes = 0;
104+
}
105+
else {
106+
numNotes -= notesNeeded;
107+
}
108+
109+
if(notesNeeded > 0)
110+
cout << "Dispensing " << notesNeeded << " x ₹200 notes.\n";
111+
112+
int remainingAmount = amount - (notesNeeded * 200);
113+
if(remainingAmount > 0) {
114+
if(nextHandler != nullptr) nextHandler->dispense(remainingAmount);
115+
else {
116+
cout << "Remaining amount of " << remainingAmount << " cannot be fulfilled (Insufficinet fund in ATM)\n";
117+
}
118+
}
119+
}
120+
};
121+
122+
// Concrete Handler for 100 Rs Notes
123+
class HundredHandler : public MoneyHandler {
124+
private:
125+
int numNotes;
126+
127+
public:
128+
HundredHandler(int numNotes) {
129+
this->numNotes = numNotes;
130+
}
131+
132+
void dispense(int amount) override {
133+
int notesNeeded = amount / 100;
134+
135+
if(notesNeeded > numNotes) {
136+
notesNeeded = numNotes;
137+
numNotes = 0;
138+
}
139+
else {
140+
numNotes -= notesNeeded;
141+
}
142+
143+
if(notesNeeded > 0)
144+
cout << "Dispensing " << notesNeeded << " x ₹100 notes.\n";
145+
146+
int remainingAmount = amount - (notesNeeded * 100);
147+
if(remainingAmount > 0) {
148+
if(nextHandler != nullptr) nextHandler->dispense(remainingAmount);
149+
else {
150+
cout << "Remaining amount of " << remainingAmount << " cannot be fulfilled (Insufficinet fund in ATM)\n";
151+
}
152+
}
153+
}
154+
};
155+
156+
// Client Code
157+
int main() {
158+
// Creating handlers for each note type
159+
MoneyHandler* thousandHandler = new ThousandHandler(3);
160+
MoneyHandler* fiveHundredHandler = new FiveHundredHandler(5);
161+
MoneyHandler* twoHundredHandler= new TwoHundredHandler(10);
162+
MoneyHandler* hundredHandler= new HundredHandler(20);
163+
164+
// Setting up the chain of responsibility
165+
thousandHandler->setNextHandler(fiveHundredHandler);
166+
fiveHundredHandler->setNextHandler(twoHundredHandler);
167+
twoHundredHandler->setNextHandler(hundredHandler);
168+
169+
int amountToWithdraw = 4000;
170+
171+
// Initiating the chain
172+
cout << "\nDispensing amount: ₹" << amountToWithdraw << endl;
173+
thousandHandler->dispense(amountToWithdraw);
174+
175+
return 0;
176+
}

Src/ATM_CASH_DESPENSER/compile.bat

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@echo off
2+
echo Compiling C++ Chain of Responsibility Application...
3+
4+
REM Compile the main program
5+
g++ -std=c++11 -o cor COR.cpp
6+
7+
if %errorlevel% equ 0 (
8+
echo Compilation successful!
9+
echo Running the Chain of Responsibility application...
10+
echo.
11+
cor.exe
12+
) else (
13+
echo Compilation failed!
14+
echo Please check for any syntax errors.
15+
)
16+
17+
pause
18+

Src/ATM_CASH_DESPENSER/cor.exe

51.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)