Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions Src/ATM_CASH_DESPENSER/COR.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
#include <iostream>
using namespace std;

// Abstract Handler (Base Class)
class MoneyHandler {
protected:
MoneyHandler *nextHandler;

public:
MoneyHandler() {
this->nextHandler = nullptr;
}

void setNextHandler(MoneyHandler *next) {
nextHandler = next;
}

virtual void dispense(int amount) = 0;
};

class ThousandHandler : public MoneyHandler {
private:
int numNotes;

public:
ThousandHandler(int numNotes) {
this->numNotes = numNotes;
}

void dispense(int amount) override {
int notesNeeded = amount / 1000;

if(notesNeeded > numNotes) {
notesNeeded = numNotes;
numNotes = 0;
}
else {
numNotes -= notesNeeded;
}

if(notesNeeded > 0)
cout << "Dispensing " << notesNeeded << " x ₹1000 notes.\n";

int remainingAmount = amount - (notesNeeded * 1000);
if(remainingAmount > 0) {
if(nextHandler != nullptr) nextHandler->dispense(remainingAmount);
else {
cout << "Remaining amount of " << remainingAmount << " cannot be fulfilled (Insufficinet fund in ATM)\n";
}
}
}
};

// Concrete Handler for 500 Rs Notes
class FiveHundredHandler : public MoneyHandler {
private:
int numNotes;

public:
FiveHundredHandler(int numNotes) {
this->numNotes = numNotes;
}

void dispense(int amount) override {
int notesNeeded = amount / 500;

if(notesNeeded > numNotes) {
notesNeeded = numNotes;
numNotes = 0;
}
else {
numNotes -= notesNeeded;
}

if(notesNeeded > 0)
cout << "Dispensing " << notesNeeded << " x ₹500 notes.\n";

int remainingAmount = amount - (notesNeeded * 500);
if(remainingAmount > 0) {
if(nextHandler != nullptr) nextHandler->dispense(remainingAmount);
else {
cout << "Remaining amount of " << remainingAmount << " cannot be fulfilled (Insufficinet fund in ATM)\n";
}
}
}
};

// Concrete Handler for 200 Rs Notes
class TwoHundredHandler : public MoneyHandler {
private:
int numNotes;

public:
TwoHundredHandler(int numNotes) {
this->numNotes = numNotes;
}

void dispense(int amount) override {
int notesNeeded = amount / 200;

if(notesNeeded > numNotes) {
notesNeeded = numNotes;
numNotes = 0;
}
else {
numNotes -= notesNeeded;
}

if(notesNeeded > 0)
cout << "Dispensing " << notesNeeded << " x ₹200 notes.\n";

int remainingAmount = amount - (notesNeeded * 200);
if(remainingAmount > 0) {
if(nextHandler != nullptr) nextHandler->dispense(remainingAmount);
else {
cout << "Remaining amount of " << remainingAmount << " cannot be fulfilled (Insufficinet fund in ATM)\n";
}
}
}
};

// Concrete Handler for 100 Rs Notes
class HundredHandler : public MoneyHandler {
private:
int numNotes;

public:
HundredHandler(int numNotes) {
this->numNotes = numNotes;
}

void dispense(int amount) override {
int notesNeeded = amount / 100;

if(notesNeeded > numNotes) {
notesNeeded = numNotes;
numNotes = 0;
}
else {
numNotes -= notesNeeded;
}

if(notesNeeded > 0)
cout << "Dispensing " << notesNeeded << " x ₹100 notes.\n";

int remainingAmount = amount - (notesNeeded * 100);
if(remainingAmount > 0) {
if(nextHandler != nullptr) nextHandler->dispense(remainingAmount);
else {
cout << "Remaining amount of " << remainingAmount << " cannot be fulfilled (Insufficinet fund in ATM)\n";
}
}
}
};

// Client Code
int main() {
// Creating handlers for each note type
MoneyHandler* thousandHandler = new ThousandHandler(3);
MoneyHandler* fiveHundredHandler = new FiveHundredHandler(5);
MoneyHandler* twoHundredHandler= new TwoHundredHandler(10);
MoneyHandler* hundredHandler= new HundredHandler(20);

// Setting up the chain of responsibility
thousandHandler->setNextHandler(fiveHundredHandler);
fiveHundredHandler->setNextHandler(twoHundredHandler);
twoHundredHandler->setNextHandler(hundredHandler);

int amountToWithdraw = 4000;

// Initiating the chain
cout << "\nDispensing amount: ₹" << amountToWithdraw << endl;
thousandHandler->dispense(amountToWithdraw);

return 0;
}
18 changes: 18 additions & 0 deletions Src/ATM_CASH_DESPENSER/compile.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@echo off
echo Compiling C++ Chain of Responsibility Application...

REM Compile the main program
g++ -std=c++11 -o cor COR.cpp

if %errorlevel% equ 0 (
echo Compilation successful!
echo Running the Chain of Responsibility application...
echo.
cor.exe
) else (
echo Compilation failed!
echo Please check for any syntax errors.
)

pause

Binary file added Src/ATM_CASH_DESPENSER/cor.exe
Binary file not shown.
Loading