diff --git a/Src/ATM_CASH_DESPENSER/COR.cpp b/Src/ATM_CASH_DESPENSER/COR.cpp new file mode 100644 index 0000000..b1f2e75 --- /dev/null +++ b/Src/ATM_CASH_DESPENSER/COR.cpp @@ -0,0 +1,176 @@ +#include +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; +} diff --git a/Src/ATM_CASH_DESPENSER/compile.bat b/Src/ATM_CASH_DESPENSER/compile.bat new file mode 100644 index 0000000..e599dae --- /dev/null +++ b/Src/ATM_CASH_DESPENSER/compile.bat @@ -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 + diff --git a/Src/ATM_CASH_DESPENSER/cor.exe b/Src/ATM_CASH_DESPENSER/cor.exe new file mode 100644 index 0000000..e1b9b9f Binary files /dev/null and b/Src/ATM_CASH_DESPENSER/cor.exe differ