In: Computer Science
BankAccount:
You will create 3 files: The .h (specification file), .cpp
(implementation file) and main file. You will practice writing
class constants, using data files. You will add methods public and
private to your BankAccount class, as well as helper methods in
your main class. You will create an array of objects and practice
passing objects to and return objects from functions. String
functions practice has also been included.
BankAccount |
-string accountName // First and Last name of Account holder -int accountId // secret social security number -int accountNumber // integer -double accountBalance // current balance amount |
+ BankAccount() //default constructor that sets name to “”, account number to 0 and balance to 0 +BankAccount(string accountName,int id, int accountNumber, double accountBalance) // regular constructor +getAccountBalance(): double // returns the balance +getAccountName: string // returns name +getAccountNumber: int +setAccountBalance(double amount) : void +equals(BankAccount other) : BankAccount // returns BankAccount object ** -getId():int ** +withdraw(double amount) : bool //deducts from balance and returns true if resulting balance is less than minimum balance +deposit(double amount): void //adds amount to balance. If amount is greater than rewards amount, calls // addReward method -addReward(double amount) void // adds rewards rate * amount to balance +toString(): String // return the account information as a string with three lines. “Account Name: “ name “Account Number:” number “Account Balance:” balance |
The order in the file is first name, last name, id, account number, balance. Note that account name consists of both first and last name
Here is the code:
BankAccount.h
/* Gaurd defination */
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <iostream>
#include <sstream>
using namespace std;
class BankAccount
{
private:
/* private member fields*/
string accountName;
int accountId;
int accountNumber;
double accountBalance;
/* private functions */
int getId();
void addReward(double amount);
public:
/*public constant values */
static const double REWARDS_AMOUNT;
static const double REWARDS_RATE;
/* public constructors */
BankAccount();
BankAccount(string accountName, int id, int
accountNumber, double accountBalance);
/* public functions */
double getAccountBalance();
string getAccountName();
int getAccountNumber();
void setAccountBalance(double amount);
BankAccount equals(BankAccount other);
bool withdraw(double amount);
void deposit(double amount);
string toString();
};
#endif // BANKACCOUNT_H
BankAccount.cpp
#include "BankAccount.h"
/* implementing every constant values, functions */
const double BankAccount::REWARDS_AMOUNT = 1000.0;
const double BankAccount::REWARDS_RATE = 0.04;
/* default constructor */
BankAccount::BankAccount() {
accountName = "";
accountBalance = 0.0;
accountNumber = 0;
}
/* constructor with initialization */
BankAccount::BankAccount(string accountName, int id, int
accountNumber, double accountBalance) {
this->accountName = accountName;
this->accountId = id;
this->accountNumber = accountNumber;
this->accountBalance = accountBalance;
}
/* accessor */
double BankAccount::getAccountBalance()
{
return this->accountBalance;
}
string BankAccount::getAccountName() {
return accountName;
}
int BankAccount::getAccountNumber() {
return accountNumber;
}
int BankAccount::getId() {
return this->accountId;
}
/* mutator */
void BankAccount::setAccountBalance(double amount) {
this->accountBalance = amount;
}
/* functions */
//Check for available amount is bigger */
bool BankAccount::withdraw(double amount) {
if (accountBalance < amount)
{
return false;
}
accountBalance -= amount;
return true;
}
//add the given amount and check for rewards
void BankAccount::deposit(double amount) {
accountBalance += amount;
if (amount > REWARDS_AMOUNT)
{
addReward(amount);
}
}
void BankAccount::addReward(double amount) {
double reward = amount * REWARDS_RATE;
accountBalance += reward;
}
string BankAccount::toString() {
std::stringstream stm;
stm.precision(2);
stm << "Account Name: " << accountName
<<
"\nAccount Number: " <<
accountNumber <<
"\nAccount Balance: " <<
fixed << accountBalance;
return stm.str();
}
Driver
Class(main)
#include <iostream>
#include <fstream>
#include "BankAccount.h"
using namespace std;
// max account that array handles
const int MAX_ACCOUNT = 8;
int main()
{ //array of accounts
BankAccount accounts[MAX_ACCOUNT];
//start reading file
fstream infile("BankData.dat");
//read file line by line.
string line;
int count = 0;
while (getline(infile, line)) {
//converting line to stream for reading tokens
istringstream iss(line);
string fname, lname;
int id;
int accNo;
double balance;
//Reading stream for tokens
iss >> fname >> lname
>> id >> accNo >> balance;
string name = fname + " " +
lname;
//creating new account and
increment the count
accounts[count++] =
BankAccount(name,id,accNo,balance);
}
/* Printing accounts */
for(int i=0;i<MAX_ACCOUNT;i++){
cout << accounts[i].toString() << endl <<
endl;
}
cout << endl;
/*Finding largest account balance in accounts*/
//Assume first account with index zero is largest
//And compare it with other account using loop.
double largestBalance = accounts[0].getAccountBalance();
int largestAccountIndex = 0;
for(int i=1; i<MAX_ACCOUNT; i++){
if(accounts[i].getAccountBalance() > largestBalance){
largestAccountIndex = i;
largestBalance = accounts[i].getAccountBalance();
}
}
cout << "ACCOUNT WITH SMALLEST BALANCE:" <<
endl;
cout << accounts[largestAccountIndex].toString();
cout << endl << endl;
/*Finding smallest account balance in accounts*/
//Assume first account with index zero is
smallest
//And compare it with other account using loop.
double smallestBalance = accounts[0].getAccountBalance();
int smallestAccountIndex = 0;
for(int i=1; i<MAX_ACCOUNT; i++){
if(accounts[i].getAccountBalance() < smallestBalance){
smallestAccountIndex = i;
smallestBalance = accounts[i].getAccountBalance();
}
}
cout << "ACCOUNT WITH SMALLEST BALANCE:" <<
endl;
cout << accounts[smallestAccountIndex].toString();
cout << endl << endl;
/*Finding duplicate accounts */
//Loop through account using two loops to find the
duplicates.
for(int i=0;i< MAX_ACCOUNT;i++){
for(int j=i+1; j < MAX_ACCOUNT;j++){
//Check for duplicate and make sure there is no account
//already marked as duplicate.
if(accounts[i].getAccountNumber() == accounts[j].getAccountNumber()
&&
accounts[i].getAccountNumber() != 0){
//Found duplicate account, print it.
cout << endl << "Duplicate account found: " <<
endl;
cout << accounts[i].toString() << endl <<
endl;
//change the jth account
accounts[j] = BankAccount("XXXX XXXX",0,0,0);
}
}
}
return 0;
}
Ouput