main.cpp
#include
#include
#include
#include
#include "Bank.hpp"
using namespace std;
int read_accts(Bank& bank, int max_accts)
{
int i = 0;
//ifstream infile("C:UsersSmart PCDesktopAssignment 3 (3110)myinput.txt");
ifstream infile("myinput");
string whiteSpace;
getline(infile, whiteSpace);
// check is file can be opened
if (infile)
{
// read only first max_accts accounts,
// in order to avoid overflow
for (i = 0; i> firstName;
// check is end of file reached
if (!infile.eof())
{
infile >> lastName;
infile >> ssn;
infile >> accountNumber;
infile >> accountType;
infile >> balance;
infile >> status;
infile >> transactions;
bank.addAccount(firstName, lastName, ssn, accountNumber, accountType, balance,
status);
int index = bank.findAccount(accountNumber);
Account* acc = bank.getAccount(index);
for(int i=0; i> transactionType;
infile >> amount;
if (acc)
acc->addTransaction(Transaction(transactionType, amount));
}
}
else {
break;
}
}
infile.close();
}
else {
cout << "cannot open inpout file" << endl;
}
return i;
}
*/
void menu()
{
cout << "Select one of the following:" << endl << endl;
cout << " W - Withdrawal" << endl;
cout << " D - Deposit" << endl;
cout << " N - New account" << endl;
cout << " B - Balance" << endl;
cout << " I - Account Info" << endl;
cout << " H - Account Info plus Account Transaction History" << endl;
cout << " C - Close Account (close but do not delete the account)" << endl;
cout << " R - Reopen a closed account" << endl;
cout << " X - Delete Account (close and delete the account from the database))" << endl;
cout << " Q - Quit" << endl;
}
void withdrawal(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
if (index >= 0)
{
double amount;
cout << "Enter amount to withdraw: ";
cin >> amount;
// check is there possible to withdraw
if (amount>0)
{
// check is sufficient balance at account
if (bank.getAccount(index)->makeWithdrawal(amount))
{
cout << "Withdraw is completed." << endl;
}
else {
cout << "Error. Insuffucient funds." << endl;
}
}
else {
cout << "Error. Invalid amount, needed to entyer positiove number." << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void deposit(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
if (index >= 0)
{
double amount;
cout << "Enter amount to deposit: ";
cin >> amount;
// check is there possible to withdraw
if (amount>0)
{
bank.getAccount(index)->makeDeposit(amount); //deposit operation
cout << "Deposit is completed." << endl;
}
else {
cout << "Error. Invalid amount, needed to entyer positiove number." << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void new_acct(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
if (index == -1)
{
double amount;
string firstName;
string lastName;
string SSN;
string accountType;
cout << "Enter first name: ";
cin >> firstName;
cout << "Enter last name: ";
cin >> lastName;
do
{
cout << "Enter Social Security Number: ";
cin >> SSN;
// SSN is 9 digits, check for validity
if (SSN.length() == 9)
{
break;
}
else {
cout << "Error. Invalid SSN, needed to enter 9 digits. Pleasy try again." << endl;
}
} while (true);
do
{
cout << "Enter Account type (C - Checking, S - Saving, D - CD) : ";
cin >> accountType;
// check for validity
if (accountType == "C" || accountType == "S" || accountType == "D")
{
break;
}
else {
cout << "Error. Invalid Account type. Please try again" << endl;
}
} while (true);
do
{
cout << "Enter amount to deposit: ";
cin >> amount;
// check for validity
if (amount>0)
{
break;
}
else {
cout << "Error. Invalid amount, needed to entyer positiove number." << endl;
}
} while (true);
if (bank.openAccount(firstName, lastName, SSN, requested_account, accountType,
amount))
cout << "Account is opened." << endl;
else
cout << "Error. Cannot open account." << endl;
}
else {
cout << "Error. Account number "" << requested_account << "" already exists."
<< endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void close_acct(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
if (!acc->isClosed())
{
acc->close();
cout << "Account number "" << requested_account << "" has been closed. "<<
endl;
} else {
cout << "Error. Account number "" << requested_account << "" is already
closed." << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void reopen_acct(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
if (acc->isClosed())
{
acc->open();
cout << "Account number "" << requested_account << "" has been reopened.
"<< endl;
} else {
cout << "Error. Account number "" << requested_account << "" is already
opened." << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void delete_acct(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
if (index >= 0)
{
if (bank.deleteAccount(index))
cout << "Account is closed." << endl;
else
cout << "Error. Cannot close account." << endl;
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void balance(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
acc->addTransaction(Transaction("banance"));
cout << "Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl;
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void account_info(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
acc->addTransaction(Transaction("getinfo"));
if (acc->getAccountType() == "S")
cout << "Saving ";
else if (acc->getAccountType() == "C")
cout << "Checking ";
else if (acc->getAccountType() == "D")
cout << "CD ";
cout << " Account #" << acc->getAccountNumber() << endl;
cout << " Depositor info:" << endl;
cout << " First name: " << acc->getDepositor().getName().getFirstname() << endl;
cout << " Last name: " << acc->getDepositor().getName().getLastname() << endl;
cout << " SSN: " << acc->getDepositor().getSSN() << endl;
cout << " Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl;
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void account_info_tr(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
acc->addTransaction(Transaction("getinfo_tr"));
if (acc->getAccountType() == "S")
cout << "Saving ";
else if (acc->getAccountType() == "C")
cout << "Checking ";
else if (acc->getAccountType() == "D")
cout << "CD ";
cout << " Account #" << acc->getAccountNumber() << endl;
cout << " Depositor info:" << endl;
cout << " First name: " << acc->getDepositor().getName().getFirstname() << endl;
cout << " Last name: " << acc->getDepositor().getName().getLastname() << endl;
cout << " SSN: " << acc->getDepositor().getSSN() << endl;
cout << " Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl;
int trCount = acc->getTransactionCount();
cout << endl << "Transaction List: [" << trCount << " trsnsaction(s)]" << endl;
for(int j=0; jgetTransaction(j).getTransactionType();
cout << "t";
if (acc->getTransaction(j).getAmount()>0)
{
cout << acc->getTransaction(j).getAmount();
}
cout << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
/*
This function prints a table of the complete account information for every active account.
*/
void print_accts(Bank& bank)
{
ofstream outfile("myoutput.txt");
if (outfile)
{
for (int i = 0; i < bank.getAccounsNumber(); i++)
{
Account* acc = bank.getAccount(i);
outfile << acc->getDepositor().getName().getFirstname();
outfile << "t";
outfile << acc->getDepositor().getName().getLastname();
outfile << "t";
outfile << acc->getDepositor().getSSN();
outfile << "t";
outfile << acc->getAccountNumber();
outfile << "t";
// save as char
outfile << acc->getAccountType();
outfile << "t";
outfile << fixed << setprecision(2) << acc->getBalance();
outfile << "t";
outfile << fixed << setprecision(2) << (acc->isClosed() ? 0 : 1);
outfile << "t";
outfile << fixed << setprecision(2) << acc->getTransactionCount();
outfile << endl;
for(int j=0; jgetTransactionCount(); j++)
{
Transaction tr = acc->getTransaction(j);
outfile << tr.getTransactionType();
outfile << "t";
outfile << tr.getAmount();
outfile << endl;
}
}
outfile.close();
}
else {
cout << "Cannot open output file";
}
}
int main()
{
string choice;
Bank bank;
int accountsNumber = 0;
bool stop = false;
accountsNumber = read_accts(bank, MAX_NUM);
print_accts(bank);
do
{
cout << endl;
menu();
cout << "Your choice: ";
cin >> choice;
if (choice == "Q" || choice == "q")
{
stop = true;
}
else if (choice == "W" || choice == "w")
{
withdrawal(bank);
}
else if (choice == "D" || choice == "d")
{
deposit(bank);
}
else if (choice == "N" || choice == "n")
{
new_acct(bank);
}
else if (choice == "B" || choice == "b")
{
balance(bank);
}
else if (choice == "I" || choice == "i")
{
account_info(bank);
}
else if (choice == "H" || choice == "h")
{
account_info_tr(bank);
}
else if (choice == "C" || choice == "c")
{
close_acct(bank);
}
else if (choice == "R" || choice == "r")
{
reopen_acct(bank);
}
else if (choice == "X" || choice == "x")
{
delete_acct(bank);
}
else
{
cout << "Invalid choice, please try again." << endl;
}
} while (!stop);
print_accts(bank);
system("pause");
return 0;
}
Account.cpp
#include "Account.hpp"
#include "Transaction.hpp"
Account::Account(void)
{
transactionCount = 0;
capacity = 101;
transactions = new Transaction[capacity];
}
Account::~Account(void)
{
delete[] transactions;
}
Account::Account(Depositor& depositor, double balance, int accountNumber, string
accountType, int status)
{
_depositor = depositor;
_balance = balance;
_accountNumber = accountNumber;
_accountType = accountType;
_status = status;
transactionCount = 0;
capacity = 101;
transactions = new Transaction[capacity];
}
Account::Account(const Account& account)
{
_depositor = account._depositor;
_balance = account._balance;
_accountNumber = account._accountNumber;
_accountType = account._accountType;
_status = account._status;
capacity = account.capacity;
transactionCount = account.transactionCount;
transactions = new Transaction[capacity];
for(int i=0; i=capacity-1)
{
capacity = capacity * 2;
Transaction* newTransaction = new Transaction[capacity];
for(int i=0; i_balance)
{
return false;
}
else
{
addTransaction(Transaction("withdraw", amount));
_balance -= amount;
return true;
}
}
void Account::open()
{
addTransaction(Transaction("open"));
_status = 1;
}
void Account::close()
{
addTransaction(Transaction("close"));
_status = 0;
}
bool Account::isClosed()
{
return _status==0;
}
Account.hpp
#ifndef Account_hpp
#define Account_hpp
#include
#include "Depositor.hpp"
#include "Transaction.hpp"
class Account
{
Depositor _depositor;
double _balance;
int _accountNumber;
string _accountType;
int _status;
int capacity;
int transactionCount;
Transaction* transactions;
public:
Account(void);
Account(Depositor& depositor, double balance, int accountNumber, string accountType, int
status);
Account(const Account& account);
~Account(void);
void addTransaction(Transaction transaction);
int getTransactionCount();
Transaction getTransaction(int index);
void open();
void close();
bool isClosed();
double getBalance();
Depositor& getDepositor();
int getAccountNumber();
string getAccountType();
void makeDeposit(double amount);
bool makeWithdrawal(double amount);
};
#endif /* Account_hpp */
Bank.cpp
#include "Bank.hpp"
Bank::Bank(void)
{
accountsNumber = 0;
}
Bank::~Bank(void)
{
}
int Bank::getAccounsNumber()
{
return accountsNumber;
}
bool Bank::openAccount(string firstName, string lastName, string ssn, int accountNumber, string
accountType, double balance)
{
if (addAccount(firstName, lastName, ssn, accountNumber, accountType, balance, 1))
{
int index = findAccount(accountNumber);
if (index>=0)
{
_accounts[index]->open();
}
return true;
}
return false;
}
bool Bank::addAccount(string firstName, string lastName, string ssn, int accountNumber, string
accountType, double balance, int status)
{
if (accountsNumbergetAccountNumber() == accountNumber)
return i;
}
return -1;
}
int Bank::findAccountSSN(string ssn)
{
for (int i = 0; igetDepositor().getSSN() == ssn)
return i;
}
return -1;
}
Account* Bank::getAccount(int index)
{
return _accounts[index];
}
bool Bank::deleteAccount(int index)
{
if (index >= 0 && index
#define MAX_NUM 100
#include "Account.hpp"
class Bank
{
Account* _accounts[MAX_NUM];
int accountsNumber;
public:
Bank(void);
~Bank(void);
bool openAccount(string firstName, string lastName, string ssn, int accountNumber, string
accountType, double balance);
bool addAccount(string firstName, string lastName, string ssn, int accountNumber, string
accountType, double balance, int status);
int findAccount(int accountNumber);
int findAccountSSN(string ssn);
Account* getAccount(int index);
bool deleteAccount(int index);
int getAccounsNumber();
};
#endif /* Bank_hpp */
Depositor.cpp
#include "Depositor.hpp"
Depositor::Depositor(void)
{
}
Depositor::~Depositor(void)
{
}
Depositor::Depositor(const Depositor& depositor)
{
_ssn = depositor._ssn;
_name = depositor._name;
}
Depositor::Depositor(Name& name, string ssn)
{
_ssn = ssn;
_name = name;
}
Name& Depositor::getName()
{
return _name;
}
string Depositor::getSSN()
{
return _ssn;
}
Depositor.hpp
#ifndef Depositor_hpp
#define Depositor_hpp
#include
#include "Name.hpp"
class Depositor
{
string _ssn;
Name _name;
public:
Depositor(void);
Depositor(const Depositor& depositor);
Depositor(Name& name, string ssn);
~Depositor(void);
Name& getName();
string getSSN();
};
#endif /* Depositor_hpp */
Name.cpp
#include "Name.hpp"
Name::Name(void)
{
}
Name::~Name(void)
{
}
Name::Name(const Name& name)
{
_firstName = name._firstName;
_lastName = name._lastName;
}
Name::Name(string firstName, string lastName)
{
_firstName = firstName;
_lastName = lastName;
}
string Name::getFirstname()
{
return _firstName;
}
string Name::getLastname()
{
return _lastName;
}
Name.hpp
#ifndef Name_hpp
#define Name_hpp
#include
#include
using namespace std;
class Name
{
string _firstName;
string _lastName;
public:
Name(void);
Name(const Name& name);
Name(string firstName, string lastName);
~Name(void);
string getFirstname();
string getLastname();
};
#endif /* Name_hpp */
Transaction.cpp
#include "Transaction.hpp"
Transaction::Transaction()
{
}
Transaction::Transaction(string transactionType, double amount)
{
_transactionType = transactionType;
_amount = amount;
}
Transaction::Transaction(string transactionType)
{
_transactionType = transactionType;
_amount = 0.0;
}
Transaction::Transaction(const Transaction& transaction)
{
_transactionType = transaction._transactionType;
_amount = transaction._amount;
}
Transaction::~Transaction(void)
{
}
string Transaction::getTransactionType()
{
return _transactionType;
}
double Transaction::getAmount()
{
return _amount;
}
Transaction.hpp
#include "Transaction.hpp"
Transaction::Transaction()
{
}
Transaction::Transaction(string transactionType, double amount)
{
_transactionType = transactionType;
_amount = amount;
}
Transaction::Transaction(string transactionType)
{
_transactionType = transactionType;
_amount = 0.0;
}
Transaction::Transaction(const Transaction& transaction)
{
_transactionType = transaction._transactionType;
_amount = transaction._amount;
}
Transaction::~Transaction(void)
{
}
string Transaction::getTransactionType()
{
return _transactionType;
}
double Transaction::getAmount()
{
return _amount;
}
Solution
main.cpp
#include
#include
#include
#include
#include "Bank.hpp"
using namespace std;
int read_accts(Bank& bank, int max_accts)
{
int i = 0;
//ifstream infile("C:UsersSmart PCDesktopAssignment 3 (3110)myinput.txt");
ifstream infile("myinput");
string whiteSpace;
getline(infile, whiteSpace);
// check is file can be opened
if (infile)
{
// read only first max_accts accounts,
// in order to avoid overflow
for (i = 0; i> firstName;
// check is end of file reached
if (!infile.eof())
{
infile >> lastName;
infile >> ssn;
infile >> accountNumber;
infile >> accountType;
infile >> balance;
infile >> status;
infile >> transactions;
bank.addAccount(firstName, lastName, ssn, accountNumber, accountType, balance,
status);
int index = bank.findAccount(accountNumber);
Account* acc = bank.getAccount(index);
for(int i=0; i> transactionType;
infile >> amount;
if (acc)
acc->addTransaction(Transaction(transactionType, amount));
}
}
else {
break;
}
}
infile.close();
}
else {
cout << "cannot open inpout file" << endl;
}
return i;
}
*/
void menu()
{
cout << "Select one of the following:" << endl << endl;
cout << " W - Withdrawal" << endl;
cout << " D - Deposit" << endl;
cout << " N - New account" << endl;
cout << " B - Balance" << endl;
cout << " I - Account Info" << endl;
cout << " H - Account Info plus Account Transaction History" << endl;
cout << " C - Close Account (close but do not delete the account)" << endl;
cout << " R - Reopen a closed account" << endl;
cout << " X - Delete Account (close and delete the account from the database))" << endl;
cout << " Q - Quit" << endl;
}
void withdrawal(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
if (index >= 0)
{
double amount;
cout << "Enter amount to withdraw: ";
cin >> amount;
// check is there possible to withdraw
if (amount>0)
{
// check is sufficient balance at account
if (bank.getAccount(index)->makeWithdrawal(amount))
{
cout << "Withdraw is completed." << endl;
}
else {
cout << "Error. Insuffucient funds." << endl;
}
}
else {
cout << "Error. Invalid amount, needed to entyer positiove number." << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void deposit(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
if (index >= 0)
{
double amount;
cout << "Enter amount to deposit: ";
cin >> amount;
// check is there possible to withdraw
if (amount>0)
{
bank.getAccount(index)->makeDeposit(amount); //deposit operation
cout << "Deposit is completed." << endl;
}
else {
cout << "Error. Invalid amount, needed to entyer positiove number." << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void new_acct(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
if (index == -1)
{
double amount;
string firstName;
string lastName;
string SSN;
string accountType;
cout << "Enter first name: ";
cin >> firstName;
cout << "Enter last name: ";
cin >> lastName;
do
{
cout << "Enter Social Security Number: ";
cin >> SSN;
// SSN is 9 digits, check for validity
if (SSN.length() == 9)
{
break;
}
else {
cout << "Error. Invalid SSN, needed to enter 9 digits. Pleasy try again." << endl;
}
} while (true);
do
{
cout << "Enter Account type (C - Checking, S - Saving, D - CD) : ";
cin >> accountType;
// check for validity
if (accountType == "C" || accountType == "S" || accountType == "D")
{
break;
}
else {
cout << "Error. Invalid Account type. Please try again" << endl;
}
} while (true);
do
{
cout << "Enter amount to deposit: ";
cin >> amount;
// check for validity
if (amount>0)
{
break;
}
else {
cout << "Error. Invalid amount, needed to entyer positiove number." << endl;
}
} while (true);
if (bank.openAccount(firstName, lastName, SSN, requested_account, accountType,
amount))
cout << "Account is opened." << endl;
else
cout << "Error. Cannot open account." << endl;
}
else {
cout << "Error. Account number "" << requested_account << "" already exists."
<< endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void close_acct(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
if (!acc->isClosed())
{
acc->close();
cout << "Account number "" << requested_account << "" has been closed. "<<
endl;
} else {
cout << "Error. Account number "" << requested_account << "" is already
closed." << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void reopen_acct(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
if (acc->isClosed())
{
acc->open();
cout << "Account number "" << requested_account << "" has been reopened.
"<< endl;
} else {
cout << "Error. Account number "" << requested_account << "" is already
opened." << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void delete_acct(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
if (index >= 0)
{
if (bank.deleteAccount(index))
cout << "Account is closed." << endl;
else
cout << "Error. Cannot close account." << endl;
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void balance(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
acc->addTransaction(Transaction("banance"));
cout << "Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl;
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void account_info(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
acc->addTransaction(Transaction("getinfo"));
if (acc->getAccountType() == "S")
cout << "Saving ";
else if (acc->getAccountType() == "C")
cout << "Checking ";
else if (acc->getAccountType() == "D")
cout << "CD ";
cout << " Account #" << acc->getAccountNumber() << endl;
cout << " Depositor info:" << endl;
cout << " First name: " << acc->getDepositor().getName().getFirstname() << endl;
cout << " Last name: " << acc->getDepositor().getName().getLastname() << endl;
cout << " SSN: " << acc->getDepositor().getSSN() << endl;
cout << " Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl;
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
void account_info_tr(Bank& bank)
{
int index, requested_account;
//prompt for account number
cout << endl << "Enter the account number: ";
cin >> requested_account;
// account #0 is not valid
if (requested_account != 0)
{
index = bank.findAccount(requested_account);
Account* acc = bank.getAccount(index);
if (index >= 0)
{
acc->addTransaction(Transaction("getinfo_tr"));
if (acc->getAccountType() == "S")
cout << "Saving ";
else if (acc->getAccountType() == "C")
cout << "Checking ";
else if (acc->getAccountType() == "D")
cout << "CD ";
cout << " Account #" << acc->getAccountNumber() << endl;
cout << " Depositor info:" << endl;
cout << " First name: " << acc->getDepositor().getName().getFirstname() << endl;
cout << " Last name: " << acc->getDepositor().getName().getLastname() << endl;
cout << " SSN: " << acc->getDepositor().getSSN() << endl;
cout << " Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl;
int trCount = acc->getTransactionCount();
cout << endl << "Transaction List: [" << trCount << " trsnsaction(s)]" << endl;
for(int j=0; jgetTransaction(j).getTransactionType();
cout << "t";
if (acc->getTransaction(j).getAmount()>0)
{
cout << acc->getTransaction(j).getAmount();
}
cout << endl;
}
}
else {
cout << "Error. Account number "" << requested_account << "" is not found." <<
endl;
}
}
else {
cout << "Error. Invalid account number." << endl;
}
}
/*
This function prints a table of the complete account information for every active account.
*/
void print_accts(Bank& bank)
{
ofstream outfile("myoutput.txt");
if (outfile)
{
for (int i = 0; i < bank.getAccounsNumber(); i++)
{
Account* acc = bank.getAccount(i);
outfile << acc->getDepositor().getName().getFirstname();
outfile << "t";
outfile << acc->getDepositor().getName().getLastname();
outfile << "t";
outfile << acc->getDepositor().getSSN();
outfile << "t";
outfile << acc->getAccountNumber();
outfile << "t";
// save as char
outfile << acc->getAccountType();
outfile << "t";
outfile << fixed << setprecision(2) << acc->getBalance();
outfile << "t";
outfile << fixed << setprecision(2) << (acc->isClosed() ? 0 : 1);
outfile << "t";
outfile << fixed << setprecision(2) << acc->getTransactionCount();
outfile << endl;
for(int j=0; jgetTransactionCount(); j++)
{
Transaction tr = acc->getTransaction(j);
outfile << tr.getTransactionType();
outfile << "t";
outfile << tr.getAmount();
outfile << endl;
}
}
outfile.close();
}
else {
cout << "Cannot open output file";
}
}
int main()
{
string choice;
Bank bank;
int accountsNumber = 0;
bool stop = false;
accountsNumber = read_accts(bank, MAX_NUM);
print_accts(bank);
do
{
cout << endl;
menu();
cout << "Your choice: ";
cin >> choice;
if (choice == "Q" || choice == "q")
{
stop = true;
}
else if (choice == "W" || choice == "w")
{
withdrawal(bank);
}
else if (choice == "D" || choice == "d")
{
deposit(bank);
}
else if (choice == "N" || choice == "n")
{
new_acct(bank);
}
else if (choice == "B" || choice == "b")
{
balance(bank);
}
else if (choice == "I" || choice == "i")
{
account_info(bank);
}
else if (choice == "H" || choice == "h")
{
account_info_tr(bank);
}
else if (choice == "C" || choice == "c")
{
close_acct(bank);
}
else if (choice == "R" || choice == "r")
{
reopen_acct(bank);
}
else if (choice == "X" || choice == "x")
{
delete_acct(bank);
}
else
{
cout << "Invalid choice, please try again." << endl;
}
} while (!stop);
print_accts(bank);
system("pause");
return 0;
}
Account.cpp
#include "Account.hpp"
#include "Transaction.hpp"
Account::Account(void)
{
transactionCount = 0;
capacity = 101;
transactions = new Transaction[capacity];
}
Account::~Account(void)
{
delete[] transactions;
}
Account::Account(Depositor& depositor, double balance, int accountNumber, string
accountType, int status)
{
_depositor = depositor;
_balance = balance;
_accountNumber = accountNumber;
_accountType = accountType;
_status = status;
transactionCount = 0;
capacity = 101;
transactions = new Transaction[capacity];
}
Account::Account(const Account& account)
{
_depositor = account._depositor;
_balance = account._balance;
_accountNumber = account._accountNumber;
_accountType = account._accountType;
_status = account._status;
capacity = account.capacity;
transactionCount = account.transactionCount;
transactions = new Transaction[capacity];
for(int i=0; i=capacity-1)
{
capacity = capacity * 2;
Transaction* newTransaction = new Transaction[capacity];
for(int i=0; i_balance)
{
return false;
}
else
{
addTransaction(Transaction("withdraw", amount));
_balance -= amount;
return true;
}
}
void Account::open()
{
addTransaction(Transaction("open"));
_status = 1;
}
void Account::close()
{
addTransaction(Transaction("close"));
_status = 0;
}
bool Account::isClosed()
{
return _status==0;
}
Account.hpp
#ifndef Account_hpp
#define Account_hpp
#include
#include "Depositor.hpp"
#include "Transaction.hpp"
class Account
{
Depositor _depositor;
double _balance;
int _accountNumber;
string _accountType;
int _status;
int capacity;
int transactionCount;
Transaction* transactions;
public:
Account(void);
Account(Depositor& depositor, double balance, int accountNumber, string accountType, int
status);
Account(const Account& account);
~Account(void);
void addTransaction(Transaction transaction);
int getTransactionCount();
Transaction getTransaction(int index);
void open();
void close();
bool isClosed();
double getBalance();
Depositor& getDepositor();
int getAccountNumber();
string getAccountType();
void makeDeposit(double amount);
bool makeWithdrawal(double amount);
};
#endif /* Account_hpp */
Bank.cpp
#include "Bank.hpp"
Bank::Bank(void)
{
accountsNumber = 0;
}
Bank::~Bank(void)
{
}
int Bank::getAccounsNumber()
{
return accountsNumber;
}
bool Bank::openAccount(string firstName, string lastName, string ssn, int accountNumber, string
accountType, double balance)
{
if (addAccount(firstName, lastName, ssn, accountNumber, accountType, balance, 1))
{
int index = findAccount(accountNumber);
if (index>=0)
{
_accounts[index]->open();
}
return true;
}
return false;
}
bool Bank::addAccount(string firstName, string lastName, string ssn, int accountNumber, string
accountType, double balance, int status)
{
if (accountsNumbergetAccountNumber() == accountNumber)
return i;
}
return -1;
}
int Bank::findAccountSSN(string ssn)
{
for (int i = 0; igetDepositor().getSSN() == ssn)
return i;
}
return -1;
}
Account* Bank::getAccount(int index)
{
return _accounts[index];
}
bool Bank::deleteAccount(int index)
{
if (index >= 0 && index
#define MAX_NUM 100
#include "Account.hpp"
class Bank
{
Account* _accounts[MAX_NUM];
int accountsNumber;
public:
Bank(void);
~Bank(void);
bool openAccount(string firstName, string lastName, string ssn, int accountNumber, string
accountType, double balance);
bool addAccount(string firstName, string lastName, string ssn, int accountNumber, string
accountType, double balance, int status);
int findAccount(int accountNumber);
int findAccountSSN(string ssn);
Account* getAccount(int index);
bool deleteAccount(int index);
int getAccounsNumber();
};
#endif /* Bank_hpp */
Depositor.cpp
#include "Depositor.hpp"
Depositor::Depositor(void)
{
}
Depositor::~Depositor(void)
{
}
Depositor::Depositor(const Depositor& depositor)
{
_ssn = depositor._ssn;
_name = depositor._name;
}
Depositor::Depositor(Name& name, string ssn)
{
_ssn = ssn;
_name = name;
}
Name& Depositor::getName()
{
return _name;
}
string Depositor::getSSN()
{
return _ssn;
}
Depositor.hpp
#ifndef Depositor_hpp
#define Depositor_hpp
#include
#include "Name.hpp"
class Depositor
{
string _ssn;
Name _name;
public:
Depositor(void);
Depositor(const Depositor& depositor);
Depositor(Name& name, string ssn);
~Depositor(void);
Name& getName();
string getSSN();
};
#endif /* Depositor_hpp */
Name.cpp
#include "Name.hpp"
Name::Name(void)
{
}
Name::~Name(void)
{
}
Name::Name(const Name& name)
{
_firstName = name._firstName;
_lastName = name._lastName;
}
Name::Name(string firstName, string lastName)
{
_firstName = firstName;
_lastName = lastName;
}
string Name::getFirstname()
{
return _firstName;
}
string Name::getLastname()
{
return _lastName;
}
Name.hpp
#ifndef Name_hpp
#define Name_hpp
#include
#include
using namespace std;
class Name
{
string _firstName;
string _lastName;
public:
Name(void);
Name(const Name& name);
Name(string firstName, string lastName);
~Name(void);
string getFirstname();
string getLastname();
};
#endif /* Name_hpp */
Transaction.cpp
#include "Transaction.hpp"
Transaction::Transaction()
{
}
Transaction::Transaction(string transactionType, double amount)
{
_transactionType = transactionType;
_amount = amount;
}
Transaction::Transaction(string transactionType)
{
_transactionType = transactionType;
_amount = 0.0;
}
Transaction::Transaction(const Transaction& transaction)
{
_transactionType = transaction._transactionType;
_amount = transaction._amount;
}
Transaction::~Transaction(void)
{
}
string Transaction::getTransactionType()
{
return _transactionType;
}
double Transaction::getAmount()
{
return _amount;
}
Transaction.hpp
#include "Transaction.hpp"
Transaction::Transaction()
{
}
Transaction::Transaction(string transactionType, double amount)
{
_transactionType = transactionType;
_amount = amount;
}
Transaction::Transaction(string transactionType)
{
_transactionType = transactionType;
_amount = 0.0;
}
Transaction::Transaction(const Transaction& transaction)
{
_transactionType = transaction._transactionType;
_amount = transaction._amount;
}
Transaction::~Transaction(void)
{
}
string Transaction::getTransactionType()
{
return _transactionType;
}
double Transaction::getAmount()
{
return _amount;
}

main.cpp #include iostream #include iomanip #include fs.pdf

  • 1.
    main.cpp #include #include #include #include #include "Bank.hpp" using namespacestd; int read_accts(Bank& bank, int max_accts) { int i = 0; //ifstream infile("C:UsersSmart PCDesktopAssignment 3 (3110)myinput.txt"); ifstream infile("myinput"); string whiteSpace; getline(infile, whiteSpace); // check is file can be opened if (infile) { // read only first max_accts accounts, // in order to avoid overflow for (i = 0; i> firstName; // check is end of file reached if (!infile.eof()) { infile >> lastName; infile >> ssn; infile >> accountNumber; infile >> accountType; infile >> balance; infile >> status; infile >> transactions;
  • 2.
    bank.addAccount(firstName, lastName, ssn,accountNumber, accountType, balance, status); int index = bank.findAccount(accountNumber); Account* acc = bank.getAccount(index); for(int i=0; i> transactionType; infile >> amount; if (acc) acc->addTransaction(Transaction(transactionType, amount)); } } else { break; } } infile.close(); } else { cout << "cannot open inpout file" << endl; } return i; } */ void menu() { cout << "Select one of the following:" << endl << endl; cout << " W - Withdrawal" << endl; cout << " D - Deposit" << endl; cout << " N - New account" << endl; cout << " B - Balance" << endl; cout << " I - Account Info" << endl; cout << " H - Account Info plus Account Transaction History" << endl; cout << " C - Close Account (close but do not delete the account)" << endl;
  • 3.
    cout << "R - Reopen a closed account" << endl; cout << " X - Delete Account (close and delete the account from the database))" << endl; cout << " Q - Quit" << endl; } void withdrawal(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); if (index >= 0) { double amount; cout << "Enter amount to withdraw: "; cin >> amount; // check is there possible to withdraw if (amount>0) { // check is sufficient balance at account if (bank.getAccount(index)->makeWithdrawal(amount)) { cout << "Withdraw is completed." << endl; } else { cout << "Error. Insuffucient funds." << endl; } } else {
  • 4.
    cout << "Error.Invalid amount, needed to entyer positiove number." << endl; } } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void deposit(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); if (index >= 0) { double amount; cout << "Enter amount to deposit: "; cin >> amount; // check is there possible to withdraw if (amount>0) { bank.getAccount(index)->makeDeposit(amount); //deposit operation cout << "Deposit is completed." << endl;
  • 5.
    } else { cout <<"Error. Invalid amount, needed to entyer positiove number." << endl; } } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void new_acct(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); if (index == -1) { double amount; string firstName; string lastName; string SSN; string accountType; cout << "Enter first name: "; cin >> firstName;
  • 6.
    cout << "Enterlast name: "; cin >> lastName; do { cout << "Enter Social Security Number: "; cin >> SSN; // SSN is 9 digits, check for validity if (SSN.length() == 9) { break; } else { cout << "Error. Invalid SSN, needed to enter 9 digits. Pleasy try again." << endl; } } while (true); do { cout << "Enter Account type (C - Checking, S - Saving, D - CD) : "; cin >> accountType; // check for validity if (accountType == "C" || accountType == "S" || accountType == "D") { break; } else { cout << "Error. Invalid Account type. Please try again" << endl; } } while (true); do { cout << "Enter amount to deposit: "; cin >> amount;
  • 7.
    // check forvalidity if (amount>0) { break; } else { cout << "Error. Invalid amount, needed to entyer positiove number." << endl; } } while (true); if (bank.openAccount(firstName, lastName, SSN, requested_account, accountType, amount)) cout << "Account is opened." << endl; else cout << "Error. Cannot open account." << endl; } else { cout << "Error. Account number "" << requested_account << "" already exists." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void close_acct(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) {
  • 8.
    index = bank.findAccount(requested_account); Account*acc = bank.getAccount(index); if (index >= 0) { if (!acc->isClosed()) { acc->close(); cout << "Account number "" << requested_account << "" has been closed. "<< endl; } else { cout << "Error. Account number "" << requested_account << "" is already closed." << endl; } } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void reopen_acct(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account);
  • 9.
    Account* acc =bank.getAccount(index); if (index >= 0) { if (acc->isClosed()) { acc->open(); cout << "Account number "" << requested_account << "" has been reopened. "<< endl; } else { cout << "Error. Account number "" << requested_account << "" is already opened." << endl; } } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void delete_acct(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); if (index >= 0)
  • 10.
    { if (bank.deleteAccount(index)) cout <<"Account is closed." << endl; else cout << "Error. Cannot close account." << endl; } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void balance(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); Account* acc = bank.getAccount(index); if (index >= 0) { acc->addTransaction(Transaction("banance")); cout << "Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl; } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl;
  • 11.
    } } else { cout <<"Error. Invalid account number." << endl; } } void account_info(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); Account* acc = bank.getAccount(index); if (index >= 0) { acc->addTransaction(Transaction("getinfo")); if (acc->getAccountType() == "S") cout << "Saving "; else if (acc->getAccountType() == "C") cout << "Checking "; else if (acc->getAccountType() == "D") cout << "CD "; cout << " Account #" << acc->getAccountNumber() << endl; cout << " Depositor info:" << endl; cout << " First name: " << acc->getDepositor().getName().getFirstname() << endl; cout << " Last name: " << acc->getDepositor().getName().getLastname() << endl; cout << " SSN: " << acc->getDepositor().getSSN() << endl;
  • 12.
    cout << "Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl; } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void account_info_tr(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); Account* acc = bank.getAccount(index); if (index >= 0) { acc->addTransaction(Transaction("getinfo_tr")); if (acc->getAccountType() == "S") cout << "Saving "; else if (acc->getAccountType() == "C") cout << "Checking "; else if (acc->getAccountType() == "D") cout << "CD ";
  • 13.
    cout << "Account #" << acc->getAccountNumber() << endl; cout << " Depositor info:" << endl; cout << " First name: " << acc->getDepositor().getName().getFirstname() << endl; cout << " Last name: " << acc->getDepositor().getName().getLastname() << endl; cout << " SSN: " << acc->getDepositor().getSSN() << endl; cout << " Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl; int trCount = acc->getTransactionCount(); cout << endl << "Transaction List: [" << trCount << " trsnsaction(s)]" << endl; for(int j=0; jgetTransaction(j).getTransactionType(); cout << "t"; if (acc->getTransaction(j).getAmount()>0) { cout << acc->getTransaction(j).getAmount(); } cout << endl; } } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } /* This function prints a table of the complete account information for every active account. */ void print_accts(Bank& bank) { ofstream outfile("myoutput.txt"); if (outfile)
  • 14.
    { for (int i= 0; i < bank.getAccounsNumber(); i++) { Account* acc = bank.getAccount(i); outfile << acc->getDepositor().getName().getFirstname(); outfile << "t"; outfile << acc->getDepositor().getName().getLastname(); outfile << "t"; outfile << acc->getDepositor().getSSN(); outfile << "t"; outfile << acc->getAccountNumber(); outfile << "t"; // save as char outfile << acc->getAccountType(); outfile << "t"; outfile << fixed << setprecision(2) << acc->getBalance(); outfile << "t"; outfile << fixed << setprecision(2) << (acc->isClosed() ? 0 : 1); outfile << "t"; outfile << fixed << setprecision(2) << acc->getTransactionCount(); outfile << endl; for(int j=0; jgetTransactionCount(); j++) { Transaction tr = acc->getTransaction(j); outfile << tr.getTransactionType(); outfile << "t"; outfile << tr.getAmount(); outfile << endl; } } outfile.close(); } else { cout << "Cannot open output file";
  • 15.
    } } int main() { string choice; Bankbank; int accountsNumber = 0; bool stop = false; accountsNumber = read_accts(bank, MAX_NUM); print_accts(bank); do { cout << endl; menu(); cout << "Your choice: "; cin >> choice; if (choice == "Q" || choice == "q") { stop = true; } else if (choice == "W" || choice == "w") { withdrawal(bank); } else if (choice == "D" || choice == "d") { deposit(bank); } else if (choice == "N" || choice == "n") { new_acct(bank); } else if (choice == "B" || choice == "b")
  • 16.
    { balance(bank); } else if (choice== "I" || choice == "i") { account_info(bank); } else if (choice == "H" || choice == "h") { account_info_tr(bank); } else if (choice == "C" || choice == "c") { close_acct(bank); } else if (choice == "R" || choice == "r") { reopen_acct(bank); } else if (choice == "X" || choice == "x") { delete_acct(bank); } else { cout << "Invalid choice, please try again." << endl; } } while (!stop); print_accts(bank); system("pause"); return 0; } Account.cpp
  • 17.
    #include "Account.hpp" #include "Transaction.hpp" Account::Account(void) { transactionCount= 0; capacity = 101; transactions = new Transaction[capacity]; } Account::~Account(void) { delete[] transactions; } Account::Account(Depositor& depositor, double balance, int accountNumber, string accountType, int status) { _depositor = depositor; _balance = balance; _accountNumber = accountNumber; _accountType = accountType; _status = status; transactionCount = 0; capacity = 101; transactions = new Transaction[capacity]; } Account::Account(const Account& account) { _depositor = account._depositor; _balance = account._balance; _accountNumber = account._accountNumber; _accountType = account._accountType; _status = account._status; capacity = account.capacity; transactionCount = account.transactionCount;
  • 18.
    transactions = newTransaction[capacity]; for(int i=0; i=capacity-1) { capacity = capacity * 2; Transaction* newTransaction = new Transaction[capacity]; for(int i=0; i_balance) { return false; } else { addTransaction(Transaction("withdraw", amount)); _balance -= amount; return true; } } void Account::open() { addTransaction(Transaction("open")); _status = 1; } void Account::close() { addTransaction(Transaction("close")); _status = 0; } bool Account::isClosed() { return _status==0; } Account.hpp #ifndef Account_hpp #define Account_hpp #include
  • 19.
    #include "Depositor.hpp" #include "Transaction.hpp" classAccount { Depositor _depositor; double _balance; int _accountNumber; string _accountType; int _status; int capacity; int transactionCount; Transaction* transactions; public: Account(void); Account(Depositor& depositor, double balance, int accountNumber, string accountType, int status); Account(const Account& account); ~Account(void); void addTransaction(Transaction transaction); int getTransactionCount(); Transaction getTransaction(int index); void open(); void close(); bool isClosed(); double getBalance(); Depositor& getDepositor(); int getAccountNumber(); string getAccountType(); void makeDeposit(double amount); bool makeWithdrawal(double amount); }; #endif /* Account_hpp */
  • 20.
    Bank.cpp #include "Bank.hpp" Bank::Bank(void) { accountsNumber =0; } Bank::~Bank(void) { } int Bank::getAccounsNumber() { return accountsNumber; } bool Bank::openAccount(string firstName, string lastName, string ssn, int accountNumber, string accountType, double balance) { if (addAccount(firstName, lastName, ssn, accountNumber, accountType, balance, 1)) { int index = findAccount(accountNumber); if (index>=0) { _accounts[index]->open(); } return true; } return false; } bool Bank::addAccount(string firstName, string lastName, string ssn, int accountNumber, string accountType, double balance, int status) { if (accountsNumbergetAccountNumber() == accountNumber) return i; } return -1;
  • 21.
    } int Bank::findAccountSSN(string ssn) { for(int i = 0; igetDepositor().getSSN() == ssn) return i; } return -1; } Account* Bank::getAccount(int index) { return _accounts[index]; } bool Bank::deleteAccount(int index) { if (index >= 0 && index #define MAX_NUM 100 #include "Account.hpp" class Bank { Account* _accounts[MAX_NUM]; int accountsNumber; public: Bank(void); ~Bank(void); bool openAccount(string firstName, string lastName, string ssn, int accountNumber, string accountType, double balance); bool addAccount(string firstName, string lastName, string ssn, int accountNumber, string accountType, double balance, int status); int findAccount(int accountNumber); int findAccountSSN(string ssn); Account* getAccount(int index); bool deleteAccount(int index); int getAccounsNumber();
  • 22.
    }; #endif /* Bank_hpp*/ Depositor.cpp #include "Depositor.hpp" Depositor::Depositor(void) { } Depositor::~Depositor(void) { } Depositor::Depositor(const Depositor& depositor) { _ssn = depositor._ssn; _name = depositor._name; } Depositor::Depositor(Name& name, string ssn) { _ssn = ssn; _name = name; } Name& Depositor::getName() { return _name; } string Depositor::getSSN() { return _ssn; } Depositor.hpp #ifndef Depositor_hpp #define Depositor_hpp #include #include "Name.hpp"
  • 23.
    class Depositor { string _ssn; Name_name; public: Depositor(void); Depositor(const Depositor& depositor); Depositor(Name& name, string ssn); ~Depositor(void); Name& getName(); string getSSN(); }; #endif /* Depositor_hpp */ Name.cpp #include "Name.hpp" Name::Name(void) { } Name::~Name(void) { } Name::Name(const Name& name) { _firstName = name._firstName; _lastName = name._lastName; } Name::Name(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; } string Name::getFirstname() {
  • 24.
    return _firstName; } string Name::getLastname() { return_lastName; } Name.hpp #ifndef Name_hpp #define Name_hpp #include #include using namespace std; class Name { string _firstName; string _lastName; public: Name(void); Name(const Name& name); Name(string firstName, string lastName); ~Name(void); string getFirstname(); string getLastname(); }; #endif /* Name_hpp */ Transaction.cpp #include "Transaction.hpp" Transaction::Transaction() { } Transaction::Transaction(string transactionType, double amount) {
  • 25.
    _transactionType = transactionType; _amount= amount; } Transaction::Transaction(string transactionType) { _transactionType = transactionType; _amount = 0.0; } Transaction::Transaction(const Transaction& transaction) { _transactionType = transaction._transactionType; _amount = transaction._amount; } Transaction::~Transaction(void) { } string Transaction::getTransactionType() { return _transactionType; } double Transaction::getAmount() { return _amount; } Transaction.hpp #include "Transaction.hpp" Transaction::Transaction() { } Transaction::Transaction(string transactionType, double amount) { _transactionType = transactionType; _amount = amount; } Transaction::Transaction(string transactionType) {
  • 26.
    _transactionType = transactionType; _amount= 0.0; } Transaction::Transaction(const Transaction& transaction) { _transactionType = transaction._transactionType; _amount = transaction._amount; } Transaction::~Transaction(void) { } string Transaction::getTransactionType() { return _transactionType; } double Transaction::getAmount() { return _amount; } Solution main.cpp #include #include #include #include #include "Bank.hpp" using namespace std; int read_accts(Bank& bank, int max_accts) { int i = 0; //ifstream infile("C:UsersSmart PCDesktopAssignment 3 (3110)myinput.txt"); ifstream infile("myinput");
  • 27.
    string whiteSpace; getline(infile, whiteSpace); //check is file can be opened if (infile) { // read only first max_accts accounts, // in order to avoid overflow for (i = 0; i> firstName; // check is end of file reached if (!infile.eof()) { infile >> lastName; infile >> ssn; infile >> accountNumber; infile >> accountType; infile >> balance; infile >> status; infile >> transactions; bank.addAccount(firstName, lastName, ssn, accountNumber, accountType, balance, status); int index = bank.findAccount(accountNumber); Account* acc = bank.getAccount(index); for(int i=0; i> transactionType; infile >> amount; if (acc) acc->addTransaction(Transaction(transactionType, amount)); } } else { break;
  • 28.
    } } infile.close(); } else { cout <<"cannot open inpout file" << endl; } return i; } */ void menu() { cout << "Select one of the following:" << endl << endl; cout << " W - Withdrawal" << endl; cout << " D - Deposit" << endl; cout << " N - New account" << endl; cout << " B - Balance" << endl; cout << " I - Account Info" << endl; cout << " H - Account Info plus Account Transaction History" << endl; cout << " C - Close Account (close but do not delete the account)" << endl; cout << " R - Reopen a closed account" << endl; cout << " X - Delete Account (close and delete the account from the database))" << endl; cout << " Q - Quit" << endl; } void withdrawal(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) {
  • 29.
    index = bank.findAccount(requested_account); if(index >= 0) { double amount; cout << "Enter amount to withdraw: "; cin >> amount; // check is there possible to withdraw if (amount>0) { // check is sufficient balance at account if (bank.getAccount(index)->makeWithdrawal(amount)) { cout << "Withdraw is completed." << endl; } else { cout << "Error. Insuffucient funds." << endl; } } else { cout << "Error. Invalid amount, needed to entyer positiove number." << endl; } } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void deposit(Bank& bank) { int index, requested_account;
  • 30.
    //prompt for accountnumber cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); if (index >= 0) { double amount; cout << "Enter amount to deposit: "; cin >> amount; // check is there possible to withdraw if (amount>0) { bank.getAccount(index)->makeDeposit(amount); //deposit operation cout << "Deposit is completed." << endl; } else { cout << "Error. Invalid amount, needed to entyer positiove number." << endl; } } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void new_acct(Bank& bank)
  • 31.
    { int index, requested_account; //promptfor account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); if (index == -1) { double amount; string firstName; string lastName; string SSN; string accountType; cout << "Enter first name: "; cin >> firstName; cout << "Enter last name: "; cin >> lastName; do { cout << "Enter Social Security Number: "; cin >> SSN; // SSN is 9 digits, check for validity if (SSN.length() == 9) { break; } else { cout << "Error. Invalid SSN, needed to enter 9 digits. Pleasy try again." << endl; } } while (true);
  • 32.
    do { cout << "EnterAccount type (C - Checking, S - Saving, D - CD) : "; cin >> accountType; // check for validity if (accountType == "C" || accountType == "S" || accountType == "D") { break; } else { cout << "Error. Invalid Account type. Please try again" << endl; } } while (true); do { cout << "Enter amount to deposit: "; cin >> amount; // check for validity if (amount>0) { break; } else { cout << "Error. Invalid amount, needed to entyer positiove number." << endl; } } while (true); if (bank.openAccount(firstName, lastName, SSN, requested_account, accountType, amount)) cout << "Account is opened." << endl; else cout << "Error. Cannot open account." << endl;
  • 33.
    } else { cout <<"Error. Account number "" << requested_account << "" already exists." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void close_acct(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); Account* acc = bank.getAccount(index); if (index >= 0) { if (!acc->isClosed()) { acc->close(); cout << "Account number "" << requested_account << "" has been closed. "<< endl; } else { cout << "Error. Account number "" << requested_account << "" is already closed." << endl; } }
  • 34.
    else { cout <<"Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void reopen_acct(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); Account* acc = bank.getAccount(index); if (index >= 0) { if (acc->isClosed()) { acc->open(); cout << "Account number "" << requested_account << "" has been reopened. "<< endl; } else { cout << "Error. Account number "" << requested_account << "" is already opened." << endl; } } else {
  • 35.
    cout << "Error.Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void delete_acct(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); if (index >= 0) { if (bank.deleteAccount(index)) cout << "Account is closed." << endl; else cout << "Error. Cannot close account." << endl; } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } }
  • 36.
    void balance(Bank& bank) { intindex, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); Account* acc = bank.getAccount(index); if (index >= 0) { acc->addTransaction(Transaction("banance")); cout << "Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl; } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void account_info(Bank& bank) { int index, requested_account; //prompt for account number cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid
  • 37.
    if (requested_account !=0) { index = bank.findAccount(requested_account); Account* acc = bank.getAccount(index); if (index >= 0) { acc->addTransaction(Transaction("getinfo")); if (acc->getAccountType() == "S") cout << "Saving "; else if (acc->getAccountType() == "C") cout << "Checking "; else if (acc->getAccountType() == "D") cout << "CD "; cout << " Account #" << acc->getAccountNumber() << endl; cout << " Depositor info:" << endl; cout << " First name: " << acc->getDepositor().getName().getFirstname() << endl; cout << " Last name: " << acc->getDepositor().getName().getLastname() << endl; cout << " SSN: " << acc->getDepositor().getSSN() << endl; cout << " Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl; } else { cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } void account_info_tr(Bank& bank) { int index, requested_account;
  • 38.
    //prompt for accountnumber cout << endl << "Enter the account number: "; cin >> requested_account; // account #0 is not valid if (requested_account != 0) { index = bank.findAccount(requested_account); Account* acc = bank.getAccount(index); if (index >= 0) { acc->addTransaction(Transaction("getinfo_tr")); if (acc->getAccountType() == "S") cout << "Saving "; else if (acc->getAccountType() == "C") cout << "Checking "; else if (acc->getAccountType() == "D") cout << "CD "; cout << " Account #" << acc->getAccountNumber() << endl; cout << " Depositor info:" << endl; cout << " First name: " << acc->getDepositor().getName().getFirstname() << endl; cout << " Last name: " << acc->getDepositor().getName().getLastname() << endl; cout << " SSN: " << acc->getDepositor().getSSN() << endl; cout << " Balance is: " << fixed << setprecision(2) << acc->getBalance() << endl; int trCount = acc->getTransactionCount(); cout << endl << "Transaction List: [" << trCount << " trsnsaction(s)]" << endl; for(int j=0; jgetTransaction(j).getTransactionType(); cout << "t"; if (acc->getTransaction(j).getAmount()>0) { cout << acc->getTransaction(j).getAmount(); }
  • 39.
    cout << endl; } } else{ cout << "Error. Account number "" << requested_account << "" is not found." << endl; } } else { cout << "Error. Invalid account number." << endl; } } /* This function prints a table of the complete account information for every active account. */ void print_accts(Bank& bank) { ofstream outfile("myoutput.txt"); if (outfile) { for (int i = 0; i < bank.getAccounsNumber(); i++) { Account* acc = bank.getAccount(i); outfile << acc->getDepositor().getName().getFirstname(); outfile << "t"; outfile << acc->getDepositor().getName().getLastname(); outfile << "t"; outfile << acc->getDepositor().getSSN(); outfile << "t"; outfile << acc->getAccountNumber(); outfile << "t"; // save as char outfile << acc->getAccountType();
  • 40.
    outfile << "t"; outfile<< fixed << setprecision(2) << acc->getBalance(); outfile << "t"; outfile << fixed << setprecision(2) << (acc->isClosed() ? 0 : 1); outfile << "t"; outfile << fixed << setprecision(2) << acc->getTransactionCount(); outfile << endl; for(int j=0; jgetTransactionCount(); j++) { Transaction tr = acc->getTransaction(j); outfile << tr.getTransactionType(); outfile << "t"; outfile << tr.getAmount(); outfile << endl; } } outfile.close(); } else { cout << "Cannot open output file"; } } int main() { string choice; Bank bank; int accountsNumber = 0; bool stop = false; accountsNumber = read_accts(bank, MAX_NUM); print_accts(bank); do { cout << endl;
  • 41.
    menu(); cout << "Yourchoice: "; cin >> choice; if (choice == "Q" || choice == "q") { stop = true; } else if (choice == "W" || choice == "w") { withdrawal(bank); } else if (choice == "D" || choice == "d") { deposit(bank); } else if (choice == "N" || choice == "n") { new_acct(bank); } else if (choice == "B" || choice == "b") { balance(bank); } else if (choice == "I" || choice == "i") { account_info(bank); } else if (choice == "H" || choice == "h") { account_info_tr(bank); } else if (choice == "C" || choice == "c") { close_acct(bank); }
  • 42.
    else if (choice== "R" || choice == "r") { reopen_acct(bank); } else if (choice == "X" || choice == "x") { delete_acct(bank); } else { cout << "Invalid choice, please try again." << endl; } } while (!stop); print_accts(bank); system("pause"); return 0; } Account.cpp #include "Account.hpp" #include "Transaction.hpp" Account::Account(void) { transactionCount = 0; capacity = 101; transactions = new Transaction[capacity]; } Account::~Account(void) { delete[] transactions; } Account::Account(Depositor& depositor, double balance, int accountNumber, string accountType, int status)
  • 43.
    { _depositor = depositor; _balance= balance; _accountNumber = accountNumber; _accountType = accountType; _status = status; transactionCount = 0; capacity = 101; transactions = new Transaction[capacity]; } Account::Account(const Account& account) { _depositor = account._depositor; _balance = account._balance; _accountNumber = account._accountNumber; _accountType = account._accountType; _status = account._status; capacity = account.capacity; transactionCount = account.transactionCount; transactions = new Transaction[capacity]; for(int i=0; i=capacity-1) { capacity = capacity * 2; Transaction* newTransaction = new Transaction[capacity]; for(int i=0; i_balance) { return false; } else { addTransaction(Transaction("withdraw", amount)); _balance -= amount; return true;
  • 44.
    } } void Account::open() { addTransaction(Transaction("open")); _status =1; } void Account::close() { addTransaction(Transaction("close")); _status = 0; } bool Account::isClosed() { return _status==0; } Account.hpp #ifndef Account_hpp #define Account_hpp #include #include "Depositor.hpp" #include "Transaction.hpp" class Account { Depositor _depositor; double _balance; int _accountNumber; string _accountType; int _status; int capacity; int transactionCount; Transaction* transactions; public: Account(void);
  • 45.
    Account(Depositor& depositor, doublebalance, int accountNumber, string accountType, int status); Account(const Account& account); ~Account(void); void addTransaction(Transaction transaction); int getTransactionCount(); Transaction getTransaction(int index); void open(); void close(); bool isClosed(); double getBalance(); Depositor& getDepositor(); int getAccountNumber(); string getAccountType(); void makeDeposit(double amount); bool makeWithdrawal(double amount); }; #endif /* Account_hpp */ Bank.cpp #include "Bank.hpp" Bank::Bank(void) { accountsNumber = 0; } Bank::~Bank(void) { } int Bank::getAccounsNumber() { return accountsNumber; }
  • 46.
    bool Bank::openAccount(string firstName,string lastName, string ssn, int accountNumber, string accountType, double balance) { if (addAccount(firstName, lastName, ssn, accountNumber, accountType, balance, 1)) { int index = findAccount(accountNumber); if (index>=0) { _accounts[index]->open(); } return true; } return false; } bool Bank::addAccount(string firstName, string lastName, string ssn, int accountNumber, string accountType, double balance, int status) { if (accountsNumbergetAccountNumber() == accountNumber) return i; } return -1; } int Bank::findAccountSSN(string ssn) { for (int i = 0; igetDepositor().getSSN() == ssn) return i; } return -1; } Account* Bank::getAccount(int index) { return _accounts[index]; } bool Bank::deleteAccount(int index) { if (index >= 0 && index
  • 47.
    #define MAX_NUM 100 #include"Account.hpp" class Bank { Account* _accounts[MAX_NUM]; int accountsNumber; public: Bank(void); ~Bank(void); bool openAccount(string firstName, string lastName, string ssn, int accountNumber, string accountType, double balance); bool addAccount(string firstName, string lastName, string ssn, int accountNumber, string accountType, double balance, int status); int findAccount(int accountNumber); int findAccountSSN(string ssn); Account* getAccount(int index); bool deleteAccount(int index); int getAccounsNumber(); }; #endif /* Bank_hpp */ Depositor.cpp #include "Depositor.hpp" Depositor::Depositor(void) { } Depositor::~Depositor(void) { } Depositor::Depositor(const Depositor& depositor) { _ssn = depositor._ssn; _name = depositor._name;
  • 48.
    } Depositor::Depositor(Name& name, stringssn) { _ssn = ssn; _name = name; } Name& Depositor::getName() { return _name; } string Depositor::getSSN() { return _ssn; } Depositor.hpp #ifndef Depositor_hpp #define Depositor_hpp #include #include "Name.hpp" class Depositor { string _ssn; Name _name; public: Depositor(void); Depositor(const Depositor& depositor); Depositor(Name& name, string ssn); ~Depositor(void); Name& getName(); string getSSN(); }; #endif /* Depositor_hpp */
  • 49.
    Name.cpp #include "Name.hpp" Name::Name(void) { } Name::~Name(void) { } Name::Name(const Name&name) { _firstName = name._firstName; _lastName = name._lastName; } Name::Name(string firstName, string lastName) { _firstName = firstName; _lastName = lastName; } string Name::getFirstname() { return _firstName; } string Name::getLastname() { return _lastName; } Name.hpp #ifndef Name_hpp #define Name_hpp #include #include using namespace std; class Name {
  • 50.
    string _firstName; string _lastName; public: Name(void); Name(constName& name); Name(string firstName, string lastName); ~Name(void); string getFirstname(); string getLastname(); }; #endif /* Name_hpp */ Transaction.cpp #include "Transaction.hpp" Transaction::Transaction() { } Transaction::Transaction(string transactionType, double amount) { _transactionType = transactionType; _amount = amount; } Transaction::Transaction(string transactionType) { _transactionType = transactionType; _amount = 0.0; } Transaction::Transaction(const Transaction& transaction) { _transactionType = transaction._transactionType; _amount = transaction._amount; } Transaction::~Transaction(void) {
  • 51.
    } string Transaction::getTransactionType() { return _transactionType; } doubleTransaction::getAmount() { return _amount; } Transaction.hpp #include "Transaction.hpp" Transaction::Transaction() { } Transaction::Transaction(string transactionType, double amount) { _transactionType = transactionType; _amount = amount; } Transaction::Transaction(string transactionType) { _transactionType = transactionType; _amount = 0.0; } Transaction::Transaction(const Transaction& transaction) { _transactionType = transaction._transactionType; _amount = transaction._amount; } Transaction::~Transaction(void) { } string Transaction::getTransactionType() { return _transactionType; }
  • 52.