Created
October 1, 2018 12:15
-
-
Save abranhe/99f37da52055649b5bd7780bed1f3bda to your computer and use it in GitHub Desktop.
Bank Account in C++ https://repl.it/@abranhe/bank-cpp
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 <string> | |
#include "account.h" | |
using namespace std; | |
Account::Account(void):balance(0) | |
{ | |
} | |
vector<string> Account::report() | |
{ | |
vector<string> r; | |
r.push_back("Balance is " + to_string(balance)); | |
r.push_back("Transaction"); | |
for(auto t:log) | |
{ | |
r.push_back(t.report()); | |
} | |
r.push_back("----------------------"); | |
return r; | |
} | |
bool Account::deposit(int amt) | |
{ | |
if(amt >= 0) | |
{ | |
balance += amt; | |
log.push_back(Transaction(amt, "Deposit")); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
bool Account::withdraw(int amt) | |
{ | |
if(amt >= 0) | |
{ | |
if(balance >= amt) | |
{ | |
balance -= amt; | |
log.push_back(Transaction(amt, "Withdraw")); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
else | |
{ | |
return false; | |
} | |
} |
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 <vector> | |
#include <string> | |
#include "transaction.h" | |
class Account | |
{ | |
private: | |
int balance; | |
std::vector<Transaction> log; | |
public: | |
Account(); | |
std::vector<std::string> report(); | |
bool deposit(int amt); | |
bool withdraw(int amt); | |
}; |
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 "account.h" | |
using namespace std; | |
int main() | |
{ | |
Account a1; | |
a1.deposit(100); | |
cout << "After deposition $10" << endl; | |
for(auto s:a1.report()) | |
{ | |
cout << s << endl; | |
} | |
a1.withdraw(50); | |
if(a1.withdraw(100)) | |
{ | |
cout << "second withdraw succeeds" << endl; | |
} | |
cout << "after withdrawing $50 then $100" << endl; | |
for(auto s:a1.report()) | |
{ | |
cout << s << endl; | |
} | |
return 0; | |
} |
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 "transaction.h" | |
using namespace std; | |
Transaction::Transaction(int amt, string kind):amount(amt), type(kind) | |
{ | |
} | |
string Transaction::report() | |
{ | |
string r; | |
r += ""; | |
r += type; | |
r += " "; | |
r += to_string(amount); | |
return r; | |
} |
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 <string> | |
class Transaction | |
{ | |
private: | |
int amount; | |
std::string type; | |
public: | |
Transaction(int amt, std::string kind); | |
std::string report(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment