In: Computer Science
In C++
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 arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help you with this work. The data file is also uploaded.
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
Create BankAccount.h.
This is given for BankAccount.cpp:
#include <string>
#include <iostream>
#include "BankAccount.h"
// BankAccount::BankAccount();
BankAccount::BankAccount(std::string accountName, int id, int
accountNumber, double accountBalance){
//
}
//getters
std::string BankAccount::getAccountName(){
//
}
int BankAccount::getAccountNumber(){
//
}
double BankAccount::getAccountBalance(){
//
}
void BankAccount::setAccountBalance(double accountBalance){
//
}
std::string fixPoint(std::string number){
//adjust the two decimal places
}
std::string BankAccount::toString(){
}
bool BankAccount::withdraw(double amount){
//
}
void BankAccount::deposit(double amount){
//
}
This is given for main.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include //your header file
using namespace std;
const int SIZE = 8;
void fillArray (ifstream &input,BankAccount
accountsArray[]);
int largest(BankAccount accountsArray[]);
int smallest(BankAccount accountsArray[]);
void printArray(BankAccount accountsArray[]);
int main() {
//open file
//fill accounts array
//print array
//find largest
//find smallest
//find duplicates and print if necessary
BankAccount accountsArray[SIZE];
fillArray(input,accountsArray);
printArray(accountsArray);
cout<<"Largest Balance: "<<endl;
cout<<accountsArray[largest(accountsArray)].toString()<<endl;
cout<<"Smallest Balance :"<<endl;
cout<<accountsArray[smallest(accountsArray)].toString()<<endl;
}
}
void fillArray (ifstream &input,BankAccount
accountsArray[]){
}
int largest(BankAccount accountsArray[]){
//returns index of largest account balance
}
int smallest(BankAccount accountsArray[]){
//returns index of smallest
}
BankAccount removeDuplicate(BankAccount account1, BankAccount
account2){
return (account1.equals(account2));
}
void printArray(BankAccount accountsArray[]){
cout<<" FAVORITE BANK - CUSTOMER DETAILS "<<endl;
cout<<" --------------------------------"<<endl;
//print array using to string method
}
file "BankData.dat" contains:
Matilda Patel 453456 1232 -4.00
Fernando Diaz 323468 1234 250.0
Vai vu 432657 1240 987.56
Howard Chen 234129 1236 194.56
Vai vu 432657 1240 -888987.56
Sugata Misra 987654 1238 10004.8
Fernando Diaz 323468 1234 8474.0
Lily Zhaou 786534 1242 001.98
// BankAccount.h
#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_
#include <iostream>
using namespace std;
class BankAccount
{
private:
string accountName; // First and Last name of Account holder
int accountId; // secret social security number
int accountNumber; // integer
double accountBalance; // current balance amount
static double MIN_BALANCE;
static double REWARDS_AMOUNT;
static double REWARDS_RATE;
int getId();
void addReward(double amount); // adds rewards rate * amount to balance
public:
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
double getAccountBalance(); // returns the balance
string getAccountName(); // returns name
int getAccountNumber();
void setAccountBalance(double amount) ;
bool equals(BankAccount other) ; // returns true if this equals other. False otherwise
bool withdraw(double amount); //deducts from balance and returns true if resulting balance is less than minimum balance
void deposit(double amount); //adds amount to balance. If amount is greater than rewards amount, calls addReward method
string toString(); // return the account information as a string with three lines.
// “Account Name: “ name
// “Account Number:” number
// “Account Balance:” balance
};
#endif /* BANKACCOUNT_H_ */
//end of BankAccount.h
// BankAccount.cpp
#include "BankAccount.h"
#include <string>
#include <sstream>
#include <iomanip>
double BankAccount::MIN_BALANCE = 9.99;
double BankAccount::REWARDS_AMOUNT = 1000.00;
double BankAccount::REWARDS_RATE = 0.04;
BankAccount::BankAccount()
{
accountName = "";
accountId= 0;
accountNumber = 0;
accountBalance = 0;
}
BankAccount::BankAccount(string accountName,int id, int accountNumber, double accountBalance)
{
this->accountName = accountName;
this->accountId = id;
this->accountNumber = accountNumber;
this->accountBalance = accountBalance;
}
double BankAccount:: getAccountBalance()
{
return accountBalance;
}
string BankAccount:: getAccountName()
{
return accountName;
}
int BankAccount:: getAccountNumber()
{
return accountNumber;
}
void BankAccount:: setAccountBalance(double amount)
{
accountBalance = amount;
}
bool BankAccount:: equals(BankAccount other)
{
return(getId() == other.getId());
}
int BankAccount::getId()
{
return accountId;
}
bool BankAccount:: withdraw(double amount)
{
accountBalance -= amount;
if(accountBalance < MIN_BALANCE)
return true;
return false;
}
void BankAccount:: deposit(double amount)
{
accountBalance += amount;
if(amount > REWARDS_AMOUNT)
addReward(amount);
}
// addReward method
void BankAccount::addReward(double amount)
{
accountBalance += (amount*REWARDS_RATE);
}
string BankAccount::toString()
{
ostringstream ssBalance;
ssBalance<<fixed<<setprecision(2);
ssBalance<<accountBalance;
return "Account Name : "+accountName+"\nAccount Number : "+to_string(accountNumber)+"\nAccount Balance : "+ssBalance.str();
}
//end of BankAccount.cpp
// main.cpp : C++ driver program to test the BankAccount class using array of accounts
#include <iostream>
#include <fstream>
#include "BankAccount.h"
using namespace std;
const int SIZE = 8;
// function declaration for array
void fillArray (ifstream &input,BankAccount accountsArray[]);
int largest(BankAccount accountsArray[]);
int smallest(BankAccount accountsArray[]);
void printArray(BankAccount accountsArray[]);
BankAccount removeDuplicate(BankAccount account1, BankAccount account2);
int main()
{
//open file
ifstream input;
input.exceptions(ifstream::failbit | ifstream::badbit);
//string line;
try{
input.open("BankData.dat"); // provide full path to file
BankAccount accountsArray[SIZE];
//fill accounts array
fillArray(input,accountsArray);
input.close();
//print array
printArray(accountsArray);
//find largest
cout<<"Largest Balance: "<<endl;
cout<<accountsArray[largest(accountsArray)].toString()<<endl;
//find smallest
cout<<"Smallest Balance :"<<endl;
cout<<accountsArray[smallest(accountsArray)].toString()<<endl;
//find duplicates and print if necessary
bool duplicates = false;
for(int i=0;i<SIZE-1;i++)
{
for(int j=i+1;j<SIZE;j++)
{
accountsArray[j] = removeDuplicate(accountsArray[i],accountsArray[j]);
if(accountsArray[j].getAccountName() == "XXXX XXXX")
duplicates = true;
}
}
if(duplicates)
{
cout<<"\nDuplicate Account Found : Reprinting List"<<endl;
printArray(accountsArray);
}
}catch(ifstream::failure &e)
{
cerr<<"Error while opening/reading the input file"<<endl;
}
return 0;
}
void fillArray (ifstream &input,BankAccount accountsArray[]){
string firstName, lastName;
int accountId;
int accountNumber;
double accountBalance;
int count = 0;
string line;
while((!input.eof()) && (count < SIZE))
{
input>>firstName>>lastName>>accountId>>accountNumber>>accountBalance;
BankAccount account(firstName+" "+lastName,accountId,accountNumber,accountBalance);
accountsArray[count] = account;
count++;
}
}
int largest(BankAccount accountsArray[]){
int max = 0;
for(int i=1;i<SIZE;i++)
{
if(accountsArray[i].getAccountBalance() > accountsArray[max].getAccountBalance())
max = i;
}
return max;
}
int smallest(BankAccount accountsArray[]){
int min = 0;
for(int i=1;i<SIZE;i++)
{
if(accountsArray[i].getAccountBalance() < accountsArray[min].getAccountBalance())
min = i;
}
return min;
}
BankAccount removeDuplicate(BankAccount account1, BankAccount account2){
if(account1.equals(account2))
{
return(BankAccount("XXXX XXXX",0,0,0));
}else
return account2;
}
void printArray(BankAccount accountsArray[]){
cout<<" FAVORITE BANK - CUSTOMER DETAILS "<<endl;
cout<<" --------------------------------"<<endl;
for(unsigned int i=0;i<SIZE;i++)
{
cout<<accountsArray[i].toString()<<endl<<endl;
}
}
//end of program
Output:
Input file:
Output: