Last active
July 13, 2022 05:41
-
-
Save mmj-the-fighter/433df3d74206322415bfa77b10787176 to your computer and use it in GitHub Desktop.
Automated Teller Machine sim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <list> | |
#include <algorithm> | |
using namespace std; | |
struct MoneyBundle | |
{ | |
int denomination; | |
int count; | |
MoneyBundle(int d, int c) : denomination(d), count(c) {} | |
}; | |
class AutomatedTellerMachine | |
{ | |
private: | |
list<MoneyBundle*> money; | |
long totalMoney; | |
public: | |
AutomatedTellerMachine(); | |
~AutomatedTellerMachine(); | |
//void AdminDeposit(int denomination, int numberofunits){}; | |
void AdminShowStatus(); | |
void UserWithDraw(int amount); | |
private: | |
void Purge(list<MoneyBundle*> & mblist); | |
long CalculateTotal(list<MoneyBundle*> & mblist); | |
void ServRequest(list<MoneyBundle*> & servlist); | |
}; | |
AutomatedTellerMachine::AutomatedTellerMachine() | |
{ | |
money.push_back(new MoneyBundle(2000, 2)); | |
money.push_back(new MoneyBundle(1000, 4)); | |
money.push_back(new MoneyBundle(500, 1)); | |
money.push_back(new MoneyBundle(200, 3)); | |
money.push_back(new MoneyBundle(100, 10)); | |
money.push_back(new MoneyBundle(50, 5)); | |
money.push_back(new MoneyBundle(20, 5)); | |
money.push_back(new MoneyBundle(10, 8)); | |
money.push_back(new MoneyBundle(5, 10)); | |
money.push_back(new MoneyBundle(2, 10)); | |
money.push_back(new MoneyBundle(1, 1)); | |
totalMoney = CalculateTotal(money); | |
//money.push_back(new MoneyBundle(2000, 0)); | |
//money.push_back(new MoneyBundle(1000, 0)); | |
//money.push_back(new MoneyBundle(500, 0)); | |
//money.push_back(new MoneyBundle(200, 0)); | |
//money.push_back(new MoneyBundle(100, 0)); | |
//money.push_back(new MoneyBundle(50, 0)); | |
//money.push_back(new MoneyBundle(20, 0)); | |
//money.push_back(new MoneyBundle(10, 3)); | |
//money.push_back(new MoneyBundle(5, 8)); | |
//money.push_back(new MoneyBundle(2, 9)); | |
//money.push_back(new MoneyBundle(1, 0)); | |
//totalMoney = CalculateTotal(money); | |
} | |
AutomatedTellerMachine::~AutomatedTellerMachine(){ | |
Purge(money); | |
} | |
void AutomatedTellerMachine::Purge(list<MoneyBundle*> & mblist) | |
{ | |
list<MoneyBundle*>::iterator i = mblist.begin(); | |
while (i != mblist.end()){ | |
MoneyBundle* mb = *i; | |
delete mb; | |
i++; | |
} | |
mblist.clear(); | |
} | |
long AutomatedTellerMachine::CalculateTotal(list<MoneyBundle*> & mblist) | |
{ | |
long sum = 0; | |
for (list<MoneyBundle*>::iterator i = mblist.begin(); i != mblist.end(); ++i){ | |
MoneyBundle* mb = *i; | |
sum += mb->count * mb->denomination; | |
} | |
return sum; | |
} | |
void AutomatedTellerMachine::AdminShowStatus() | |
{ | |
long sum = 0; | |
cout << "***ATM STATUS***" << endl; | |
for (list<MoneyBundle*>::iterator i = money.begin(); i != money.end(); ++i){ | |
MoneyBundle* mb = *i; | |
long bundleAmount = mb->count * mb->denomination; | |
cout << "$" << mb->denomination << " * " << mb->count << " = " << bundleAmount << endl; | |
sum += bundleAmount; | |
} | |
cout << "$Total: " << sum << endl; | |
} | |
void AutomatedTellerMachine::UserWithDraw(int amount) | |
{ | |
if (totalMoney == 0){ | |
cout << "***CANNOT PROCESS REQUEST***" << endl; | |
cout << "No Cash\n"; | |
return; | |
} | |
int requestedAmount = amount; | |
if (requestedAmount <= 0){ | |
cout << "***CANNOT PROCESS REQUEST***" << endl; | |
cout << "Amount should be greater than 0\n"; | |
return; | |
} | |
if (requestedAmount > totalMoney){ | |
cout << "***CANNOT PROCESS REQUEST***" << endl; | |
cout << "Try a smaller amount\n"; | |
return; | |
} | |
list<MoneyBundle*> servlist; | |
for (list<MoneyBundle*>::iterator i = money.begin(); i != money.end(); ++i){ | |
MoneyBundle* mb = *i; | |
if (mb->count == 0) | |
continue; | |
int requiredcount = requestedAmount / mb->denomination; | |
if (requiredcount > 0) { | |
if (requiredcount > mb->count) { | |
requiredcount = mb->count; | |
} | |
servlist.push_back(new MoneyBundle(mb->denomination, requiredcount)); | |
requestedAmount = requestedAmount - requiredcount*mb->denomination; | |
} | |
} | |
if (CalculateTotal(servlist) != amount){ | |
Purge(servlist); | |
requestedAmount = amount; | |
for (list<MoneyBundle*>::reverse_iterator it = money.rbegin(); it != money.rend(); ++it){ | |
MoneyBundle* mb = *it; | |
if (mb->count == 0) | |
continue; | |
int requiredcount = requestedAmount / mb->denomination; | |
if (requiredcount > 0) { | |
if (requiredcount > mb->count) { | |
requiredcount = mb->count; | |
} | |
servlist.push_back(new MoneyBundle(mb->denomination, requiredcount)); | |
requestedAmount = requestedAmount - requiredcount*mb->denomination; | |
} | |
} | |
} | |
if (CalculateTotal(servlist) != amount){ | |
//int index = 0; | |
//for (list<MoneyBundle*>::iterator k = money.begin(); k != money.end(); ++k) { | |
// if ((*k)->count > 0){ | |
// ++index; | |
// } | |
//} | |
//int lastIndex = index - 1; | |
int count = count_if(money.begin(), money.end(), [](const MoneyBundle* mb){return (mb->count > 0); }); | |
int lastIndex = count - 1; | |
cout << "***CANNOT PROCESS REQUEST***" << endl; | |
if (count > 0) { | |
cout << "Try multiples of "; | |
int index = 0; | |
for (list<MoneyBundle*>::iterator k = money.begin(); k != money.end(); ++k) { | |
MoneyBundle* mb = *k; | |
if (mb->count > 0) { | |
cout << mb->denomination; | |
if (index < lastIndex - 1) | |
cout << ", "; | |
else if (index == lastIndex - 1) | |
cout << " and "; | |
else | |
cout << "."; | |
++index; | |
} | |
} | |
cout << endl; | |
} | |
else{ | |
cout << "No Cash" << endl; | |
} | |
} | |
else{ | |
cout << "***TAKE CASH***" << endl; | |
ServRequest(servlist); | |
} | |
Purge(servlist); | |
} | |
void AutomatedTellerMachine::ServRequest(list<MoneyBundle*> & servlist) | |
{ | |
long sum = 0; | |
list<MoneyBundle*>::iterator i = servlist.begin(); | |
while (i != servlist.end()){ | |
MoneyBundle* rm = *i; | |
cout << "Droppping " << rm->count << "nos. of $" << rm->denomination << " = " << rm->count * rm->denomination << endl; | |
for (list<MoneyBundle*>::iterator j = money.begin(); j != money.end(); ++j) { | |
MoneyBundle* mb = *j; | |
if (mb->denomination == rm->denomination) { | |
mb->count -= rm->count; | |
} | |
} | |
long bundleAmount = rm->count * rm->denomination; | |
sum += bundleAmount; | |
++i; | |
} | |
totalMoney -= sum; | |
} | |
int main() | |
{ | |
AutomatedTellerMachine atm; | |
char choice = 'y'; | |
int amount = 0; | |
while (choice == 'y' || choice == 'Y') | |
{ | |
atm.AdminShowStatus(); | |
cout << "\nEnter withdrawal amount: "; | |
amount = 0; | |
cin >> amount; | |
cin.get(); | |
atm.UserWithDraw(amount); | |
cout << "\nDo you want to continue: y/n "; | |
choice = cin.get(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment