Skip to content

Instantly share code, notes, and snippets.

@abranhe
Created October 1, 2018 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abranhe/99f37da52055649b5bd7780bed1f3bda to your computer and use it in GitHub Desktop.
Save abranhe/99f37da52055649b5bd7780bed1f3bda to your computer and use it in GitHub Desktop.
Bank Account in C++ https://repl.it/@abranhe/bank-cpp
#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;
}
}
#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);
};
#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;
}
#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;
}
#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