Skip to content

Instantly share code, notes, and snippets.

@hackerdem
Created May 6, 2016 05:53
Show Gist options
  • Save hackerdem/696d532d4c240d88197376f8e7e0f232 to your computer and use it in GitHub Desktop.
Save hackerdem/696d532d4c240d88197376f8e7e0f232 to your computer and use it in GitHub Desktop.
You are required to develop the BankAccount structure, and develop sub- functions will be called by the provided main program to track transactions of a bank account using the BankAccount structure. The solution should have the following features: - Declares three structures for Customer, Transaction and Account information; - Implements functio…
*******************************************************************************
/*account header file*/
*******************************************************************************
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
#include <iomanip>
#include <string>
#include "transaction.h"
#include "Customer.h"
using namespace std;
#define DEFAULT_NUMBER "06-3121-1021-0001"
class Account
{
//containing seven members: Account Holder, AccountNo, Balance, Total Deposit, Total Withdrawal,
//Transaction TransactionList dynamic array of maximum 50 records and Transaction count on the existing transaction records
protected:
Customer *_Holder;
string _AccountNo;
double _Balance, _TotalDeposit, _TotalWithdrawal;
Transaction * _TransactionList;
int _TransactionCount;
public:
Account( Customer* holder,
string number = DEFAULT_NUMBER,
string date = DATE,
double balance = EMPTY,
double deposit = EMPTY,
double withdraw = EMPTY );
Account(Account& account);
~Account();
Customer * GetHolder() const;
string GetAccountNo() const;
double GetBalance() const;
double GetTotalDeposit() const;
double GetWithdrawal() const;
Transaction* GetTransactionList() const;
int GetTransactionCount() const;
void SetHolder(Customer* holder);
void SetBalance(double amount);
void SetTotalDeposit(double amount);
void SetTotalWithdrawal(double amount);
void SetTransactionList(Transaction* list, int size);
void RecordDeposit(Transaction* transaction);
//pure abstract functions
virtual void RecordWithdraw(Transaction* transaction) = 0;
virtual void RecordPayment(Transaction* transaction) = 0;
virtual void RecordTransaction(Transaction* transaction) = 0;
virtual void PrintReport() = 0;
Account& operator+= (Transaction& transaction);
};
#endif
******************************************************************************************************
account.cpp
******************************************************************************************************
#include "account.h"
//contructor
Account::Account( Customer *holder, string number, string date, double balance, double deposit, double withdraw )
{
_TransactionList = new Transaction[50];
_Holder = holder;
_AccountNo = number;
Transaction t(date, DEFAULT_DESC, balance);
_TransactionList[0] = t;
_TransactionCount = 1;
_Balance = balance;
_TotalDeposit = deposit;
_TotalWithdrawal = withdraw;
}
Account::Account(Account& account)
{
_TransactionList = new Transaction[50];
_Holder = account.GetHolder();
_AccountNo = account.GetAccountNo();
SetTransactionList( account.GetTransactionList(), account.GetTransactionCount() );
}
//Detructor
Account::~Account()
{
delete []_TransactionList;
}
//Member functions
Customer * Account::GetHolder() const
{
return _Holder;
}
string Account::GetAccountNo() const
{
return _AccountNo;
}
double Account::GetBalance() const
{
return _Balance;
}
double Account::GetTotalDeposit() const
{
return _TotalDeposit;
}
double Account::GetWithdrawal() const
{
return _TotalWithdrawal;
}
Transaction* Account::GetTransactionList() const
{
return &_TransactionList[0];
}
int Account::GetTransactionCount() const
{
return _TransactionCount;
}
void Account::SetHolder(Customer* holder)
{
Customer* tmp = _Holder;
_Holder = holder;
delete tmp;
}
void Account::SetBalance(double amount)
{
_Balance = amount;
}
void Account::SetTotalDeposit(double amount)
{
_TotalDeposit = amount;
}
void Account::SetTotalWithdrawal(double amount)
{
_TotalWithdrawal = amount;
}
void Account::SetTransactionList(Transaction* list, int size)
{
Transaction* tmp = _TransactionList;
_TransactionList = list;
_TransactionCount = size;
delete tmp; //delete the old
//update balance, Deposit, withdraw value
_Balance = 0;
_TotalDeposit = 0;
_TotalWithdrawal = 0;
for( int i = 0; i < size ; i++ )
{
_Balance += _TransactionList[i].GetTransactionAmount();
if( _TransactionList[i].GetTransactionAmount() > 0 )
{
_TotalDeposit += _TransactionList[i].GetTransactionAmount();
}
else if( _TransactionList[i].GetTransactionAmount() < 0
&& ( _TransactionList[i].GetTransactionAmount() + _Balance ) >= 0 )
{
_TotalWithdrawal -= _TransactionList[i].GetTransactionAmount();
}
}
}
//- A function RecordDeposit() that store the Transaction parameter into the Transaction TransactionList array of the
//parameter Account and increasing the Transaction count by one. Make sure to check the transaction parameter
//must have the amount higher than zero. Otherwise, display an error message. There is no requiring for the
//default parameter values.
void Account::RecordDeposit(Transaction* transaction)
{
if( transaction->GetTransactionAmount() > 0 )
{
_TransactionList[_TransactionCount] = *transaction;
_Balance += transaction->GetTransactionAmount();
_TotalDeposit += transaction->GetTransactionAmount();
_TransactionCount++;
}
else
{
string err = "ERROR! Invalid deposit value";
throw err;
}
}
// A function RecordWithdraw() that store the Transaction parameter into the Transaction TransactionList array of
//the parameter Account and increasing the Transaction count by one. Make sure to check the transaction
//parameter must have the amount higher than zero and less than or equal to the parameter account balance
//. Otherwise, display an error message. There is no requiring for the default parameter values.
void Account::RecordWithdraw(Transaction* transaction)
{
_Balance += transaction->GetTransactionAmount();
_TotalWithdrawal += transaction->GetTransactionAmount();
transaction->setTransactionAmount(-transaction->GetTransactionAmount());
_TransactionList[_TransactionCount] = *transaction;
_TransactionCount++;
}
//method apply the deep copy to add the parameter Transaction to the
//TransactionList array and increase the TransactionCount by 1.
Account& Account::operator+= (Transaction& transaction)
{
_TransactionList[_TransactionCount] = transaction;
_TransactionCount++;
return *this;
}
*******************************************************************************************************
CreditAccount header
******************************************************************************************************
#ifndef CREDITACCOUNT_H
#define CREDITACCOUNT_H
#include "account.h"
class CreditAccount : public Account
{
private:
double _limit;
public:
CreditAccount(Customer* holder,
string number = DEFAULT_NUMBER,
string date = DATE,
double limit = EMPTY,
double balance = EMPTY,
double deposit = EMPTY,
double withdraw = EMPTY)
: Account(holder, number, date, balance, deposit, withdraw){
_limit = limit;
}
CreditAccount(CreditAccount &creditAccount) : Account(creditAccount){}
double GetLimit();
void SetLimit(double limit);
void RecordWithdraw(Transaction* transaction);
void RecordPayment(Transaction* transaction);
void RecordTransaction(Transaction* transaction);
void PrintReport();
friend ostream& operator<<(ostream& out, CreditAccount& temp);
};
#endif
***********************************************************************************************************
CreditAccount cpp
***********************************************************************************************************
#include "CreditAccount.h"
double CreditAccount::GetLimit()
{
return _limit;
}
void CreditAccount::SetLimit(double limit)
{
_limit = limit;
}
void CreditAccount::RecordWithdraw(Transaction* transaction)
{
if (transaction->GetTransactionAmount() <= _limit - _Balance)
{
_Balance += transaction->GetTransactionAmount();
SetTotalWithdrawal(_TotalWithdrawal + transaction->GetTransactionAmount());
transaction->setTransactionAmount(-transaction->GetTransactionAmount());
*this += *transaction;
}
else
{
string err = "ERROR! Invalid withdraw value";
throw err;
}
}
void CreditAccount::RecordPayment(Transaction* transaction)
{
_Balance -= 2 * transaction->GetTransactionAmount();
Account::RecordDeposit(transaction);
}
void CreditAccount::RecordTransaction(Transaction* transaction)
{
if (transaction->GetTransactionAmount() <= _limit - _Balance)
{
Account::RecordWithdraw(transaction);
}
else
{
string err = "ERROR! Invalid withdraw value";
throw err;
}
}
//Print all detail of this account
void CreditAccount::PrintReport()
{
cout << endl;
cout << *this;
double bal = 0;
cout << "DATE" << "\t\t" << "DESCRIPTION" << "\t\t" << "DEPOSIT" << "\t\t" << "WITHDRAWAL" << "\t" << "BALANCE" << endl;
cout << "------------------------------------------------------------------------------" << endl;
for (int i = 0; i< _TransactionCount; i++)
{
Transaction t = _TransactionList[i];
cout << fixed;
bal -= t.GetTransactionAmount();
cout << t << bal << endl;
}
cout << endl << endl;
}
ostream& operator<<(ostream& out, CreditAccount& temp)
{
out << "ACCOUNT SUMMARY :: " << temp._AccountNo << " - ";
out << *temp._Holder << " - Limit: $" << temp._limit << endl;
out << "\tTotal Deposits : $" << fixed << right << setprecision(2) << setw(8) << temp._TotalDeposit << endl;
out << "\tTotal Withdrawal : $" << fixed << right << setprecision(2) << setw(8) << temp._TotalWithdrawal << endl;
out << "\tFinal Balance : $" << fixed << right << setprecision(2) << setw(8) << temp._Balance << endl << endl;
return out;
}
***************************************************************************************************************
Customer header
************************************************************************************************************
#include <iostream>
#include <iomanip>
#include <string>
#ifndef Custome_h
#define Custome_h
using namespace std;
#define BLANK ""
#define DEFAULT_ID "000001"
#define DEFAULT_PIN "0000"
class Customer
{
private:
string _CustomerName;
string _UserID;
string _Pin;
public:
Customer(string name = BLANK, string id = DEFAULT_ID, string pin = DEFAULT_PIN);
Customer(Customer& cus);
string GetCustomerName() const;
string GetUserID() const;
string GetPin() const;
void SetCustomerName(string name);
void SetPin(string pin);
bool FindCustomer(string id, string pin) const;
friend ostream& operator<<(ostream& out, const Customer& temp);
};
#endif
***********************************************************************************************************
Customer cpp
***********************************************************************************************************
#include "Customer.h"
//Contructors
Customer::Customer(string name, string id, string pin)
{
_CustomerName = name;
_UserID = id;
_Pin = pin;
}
Customer::Customer(Customer& cus)
{
_CustomerName = cus._CustomerName;
_UserID = cus._UserID;
_Pin = cus._Pin;
}
//Member functions
string Customer::GetCustomerName() const
{
return _CustomerName;
}
string Customer::GetUserID() const
{
return _UserID;
}
string Customer::GetPin() const
{
return _Pin;
}
void Customer::SetCustomerName(string name)
{
_CustomerName = name;
}
void Customer::SetPin(string pin)
{
_Pin = pin;
}
bool Customer::FindCustomer(string id, string pin) const
{
return ( (id == _UserID) && (pin == _Pin)) ? true : false;
}
ostream& operator<<(ostream& out,const Customer& temp)
{
out << temp.GetCustomerName();
return out;
}
*************************************************************************************************************
SavingAccount header
************************************************************************************************************
#ifndef SAVINGACCOUNT_H
#define SAVINGACCOUNT_H
#include "account.h"
class SavingAccount : public Account
{
public:
SavingAccount(Customer* holder,
string number = DEFAULT_NUMBER,
string date = DATE,
double balance = EMPTY,
double deposit = EMPTY,
double withdraw = EMPTY)
: Account(holder, number, date, balance, deposit, withdraw){}
SavingAccount(SavingAccount &savingAccount) : Account(savingAccount){}
void RecordWithdraw(Transaction* transaction);
void RecordPayment(Transaction* transaction);
void RecordTransaction(Transaction* transaction);
void PrintReport();
friend ostream& operator<<(ostream& out, SavingAccount& temp);
};
#endif
*************************************************************************************************************
SavingAccount cpp
*************************************************************************************************************
#include "SavingAccount.h"
void SavingAccount::RecordWithdraw(Transaction* transaction)
{
if (transaction->GetTransactionAmount() <= _Balance )
{
_Balance -= transaction->GetTransactionAmount();
SetTotalWithdrawal(_TotalWithdrawal + transaction->GetTransactionAmount());
transaction->setTransactionAmount(-transaction->GetTransactionAmount());
*this += *transaction;
}
else
{
string err = "ERROR! Invalid withdraw value";
throw err;
}
}
void SavingAccount::RecordPayment(Transaction* transaction )
{
Account::RecordDeposit(transaction);
}
void SavingAccount::RecordTransaction(Transaction* transaction)
{
if (transaction->GetTransactionAmount() <= _Balance)
{
_Balance -= 2 * transaction->GetTransactionAmount();
Account::RecordWithdraw(transaction);
}
else
{
string err = "ERROR! Invalid withdraw value";
throw err;
}
}
//Print all detail of this account
void SavingAccount::PrintReport()
{
cout << endl;
cout << *this;
double bal = 0;
cout << "DATE" << "\t\t" << "DESCRIPTION" << "\t\t" << "DEPOSIT" << "\t\t" << "WITHDRAWAL" << "\t" << "BALANCE" << endl;
cout << "------------------------------------------------------------------------------" << endl;
for (int i = 0; i< _TransactionCount; i++)
{
Transaction t = _TransactionList[i];
cout << fixed;
bal += t.GetTransactionAmount();
cout << t << bal << endl;
}
cout << endl << endl;
}
ostream& operator<<(ostream& out, SavingAccount& temp)
{
out << "ACCOUNT SUMMARY :: " << temp._AccountNo << " - ";
out << *temp._Holder << endl;
out << "\tTotal Deposits : $" << fixed << right << setprecision(2) << setw(8) << temp._TotalDeposit << endl;
out << "\tTotal Withdrawal : $" << fixed << right << setprecision(2) << setw(8) << temp._TotalWithdrawal << endl;
out << "\tFinal Balance : $" << fixed << right << setprecision(2) << setw(8) << temp._Balance << endl << endl;
return out;
}
*************************************************************************************************************
transaction header
*************************************************************************************************************
#include <iostream>
#include <iomanip>
#include <string>
#ifndef transaction_h
#define transaction_h
#define EMPTY 0
#define BLANK ""
#define DATE "01/01/2014"
#define DEFAULT_DESC "Opening Balance"
using namespace std;
class Transaction
{
private:
string _TransactionDate, _Description;
double _TransactionAmount;
public:
Transaction( string date = DATE, string description = DEFAULT_DESC, double amount = EMPTY );
Transaction(Transaction& trans);
string GetTransactionDate() const;
string GetTransactionDescription() const;
double GetTransactionAmount() const;
void setTransactionAmount(double amount);
friend ostream& operator<<(ostream& out, const Transaction& temp);
};
#endif
*************************************************************************************************************
transaction cpp
*************************************************************************************************************
#include "transaction.h"
//Contructors
Transaction::Transaction( string date, string description, double amount )
{
_TransactionDate = date;
_Description = description;
_TransactionAmount = amount;
}
Transaction::Transaction(Transaction& trans)
{
_TransactionDate = trans._TransactionDate;
_Description = trans._Description;
_TransactionAmount = trans._TransactionAmount;
}
//Member functions
string Transaction::GetTransactionDate() const
{
return _TransactionDate;
}
string Transaction::GetTransactionDescription() const
{
return _Description;
}
double Transaction::GetTransactionAmount() const
{
return _TransactionAmount;
}
void Transaction::setTransactionAmount(double amount)
{
_TransactionAmount = amount;
}
ostream& operator<<(ostream& out, const Transaction& temp)
{
if( temp.GetTransactionAmount() >= 0 )
{
out <<left<<setw(12)<<temp.GetTransactionDate() << " " <<left<<setw(20)<< setprecision(2)<< temp.GetTransactionDescription() << " $"
<<fixed<<right<<setprecision(2)<<setw(8)<< temp.GetTransactionAmount() << " $"
<<fixed<<right<<setprecision(2)<<setw(8);
}
else
{
cout <<left<<setw(12)<< temp.GetTransactionDate() << " " << left << setw(20)<< temp.GetTransactionDescription() << " $"
<<fixed<<right<<setprecision(2)<<setw(10)<< (-temp.GetTransactionAmount()) <<" $"
<<fixed<<right<<setprecision(2)<<setw(8);
}
return out;
}
**********************************************************************************************************
main cpp
**********************************************************************************************************
#include "SavingAccount.h"
#include "CreditAccount.h"
using namespace std;
int main()
{
Customer* Mary = new Customer("Mary Jones", "235718", "5074");
Customer* John = new Customer("John Smith", "375864", "3251");
Account* MaryAccount = new SavingAccount(Mary, "06-3121-10212357", "01/03/2014", 100);
Account* MaryCreditAccount = new CreditAccount(Mary, "06-4521-4351-4486", "05/03/2014", 500);
Account* JohnAccount = new SavingAccount(John, "06-3121-10213758", "10/03/2014");
try { MaryAccount->RecordWithdraw(new Transaction("01/03/2014", "ATM withdrawal", 50)); }
catch (string message) { cout << message << "\n"; }
try { MaryAccount->RecordDeposit(new Transaction("02/03/2014", "Deposit", 90)); }
catch (string message) { cout << message << "\n"; }
try { MaryAccount->RecordWithdraw(new Transaction("04/03/2014", "ATM withdrawal", 150)); }
catch (string message) { cout << message << "\n"; }
try { MaryAccount->RecordDeposit(new Transaction("05/03/2014", "Deposit", 20)); }
catch (string message) { cout << message << "\n"; }
try { MaryAccount->RecordTransaction(new Transaction("05/03/2014", "withdraw", 100)); }
catch (string message) { cout << message << "\n"; }
try { MaryAccount->RecordTransaction(new Transaction("05/03/2014", "withdraw", 50)); }
catch (string message) { cout << message << "\n"; }
try { MaryCreditAccount->RecordTransaction(new Transaction("05/03/2014", "Purchase", 400)); }
catch (string message) { cout << message << "\n"; }
try { MaryCreditAccount->RecordTransaction(new Transaction("06/03/2014", "Purchase", 200)); }
catch (string message) { cout << message << "\n"; }
try { MaryCreditAccount->RecordPayment(new Transaction("06/03/2014", "Payment", 200)); }
catch (string message) { cout << message << "\n"; }
try { MaryCreditAccount->RecordTransaction(new Transaction("07/03/2014", "Purchase", 200)); }
catch (string message) { cout << message << "\n"; }
try { JohnAccount->RecordDeposit(new Transaction("11/03/2014", "Deposit", 20)); }
catch (string message) { cout << message << "\n"; }
try { JohnAccount->RecordDeposit(new Transaction("12/03/2014", "Deposit", 80)); }
catch (string message) { cout << message << "\n"; }
try { JohnAccount->RecordTransaction(new Transaction("12/03/2014", "withdraw", 50)); } // parent method
catch (string message) { cout << message << "\n"; }
MaryAccount->PrintReport();
MaryCreditAccount->PrintReport();
JohnAccount->PrintReport();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment