In: Computer Science
Create a driver class to do the following:
a.
Read data from the given file BankData.data and create and array of BankAccount Objects
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
b.
Print the array (without account id) the account balance must
show only 2 decimal digits. You will need to do some string processing.
c.
Find the account with largest balance and print it
d.
Find the account with the smallest balance and print it.
e.
Determine if there are duplicate accounts in the array. If there are then set the duplicate account
name to XXXX XXXX and rest of the fields to 0. Note it’s hard to “delete” elements from an array.
Or add new accounts if there is no room in the array. If
duplicate(s) are found, print the array.
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
}
BankData.data
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 __BANK_ACCOUNT_H
#define __BANK_ACCOUNT_H
#include<iostream>
#include<sstream>
using namespace std;
//class BankAccount
class BankAccount
{
private:
//private fields
string name;
int id;
int accountNumber;
double balance;
public:
//default constructor.
BankAccount()
{
}
//parameterized constructor
BankAccount(string name,int id,int
accountNumber,double balance)
{
this->name =
name;
this->id =
id;
this->accountNumber = accountNumber;
this->balance
= balance;
}
//getters for each field
string getName()
{
return
name;
}
int getId()
{
return id;
}
int getAccountNumber()
{
return
accountNumber;
}
double getBalance()
{
return
balance;
}
//tostring method
string toString()
{
stringstream
ss;
ss <<"Name
: "<<name<<endl;
ss <<"Id :
"<<id<<endl;
ss<<"account Num : "<<accountNumber<<endl;
ss<<"Balance : "<<balance<<endl;
return
ss.str();
}
//compares the current account with
passed account
//and returns account with null
fields like below if both are equal.
BankAccount equals(BankAccount
other)
{
if(other.getAccountNumber() == accountNumber)
{
BankAccount account("XXXX XXXX",0,0,0);
return account;
}
else
{
return other;
}
}
};
#endif
//---------- main.cpp----------
#include <iostream>
#include <fstream>
#include <string>
#include "BankAccount.h"
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[]);
BankAccount removeDuplicate(BankAccount account1, BankAccount
account2);
int main() {
//open file
//fill accounts array
//print array
//find largest
//find smallest
//find duplicates and print if necessary
BankAccount accountsArray[SIZE];
//create input stream object
ifstream input("BankData.data");
cout<<"After filling\n\n";
//fill array
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;
//remove duplicate accounts
for(int i =0;i<SIZE;i++)
{
for(int j =
i+1;j<SIZE;j++)
{
//if i and j are
equal j contents will be replaced by null fields
//if not j will
be returned by removeDuplicate() method
accountsArray[j]
= removeDuplicate(accountsArray[i],accountsArray[j]);
}
}
cout<<"\nAfter removing duplicates\n\n";
printArray(accountsArray);
}
void fillArray (ifstream &input,BankAccount
accountsArray[])
{
string line;
int i =0;
string fname ="";
string lname ="";
int id =-1;
int accountNumber=-1;
double balance=0;
while(getline(input,line) && i <
SIZE)
{
//create stringstream object for
read line
stringstream ss(line);
//load data into each
variable
ss>>
fname>>lname>>id>>accountNumber>>balance;
//if any of the values are not read
from file
if(fname=="" || lname == "" ||
id==-1 || accountNumber == -1)
{
//set default
values.
cout<<"\nError: Some values are missed in line:
"<<line<<endl;
cout<<"Creating Null BankAccount account(\"XXXX XXXX\",0,0,0)
to fill the array.\n";
accountsArray[i]
= BankAccount("XXXX XXXX",0,0,0);
}
else
{
//if values are
read create new object and store.
accountsArray[i]
= BankAccount(fname+" "+lname,id,accountNumber,balance);
}
fname="";
lname="";
id=-1;
accountNumber = -1;
balance = 0;
i++;
}
}
int largest(BankAccount accountsArray[])
{
int i;
//assume max balance as 0
double maxBalance = 0;
double curBalance;
int ind=0;
for(i =0 ;i<SIZE;i++)
{
//get i_th account balance
curBalance =
accountsArray[i].getBalance();
//compare and modify if
necessary
if(curBalance >
maxBalance)
{
maxBalance =
curBalance;
ind = i;
}
}
return ind;
}
int smallest(BankAccount accountsArray[])
{
int i;
//assume 0_th account has min balance
double minBalance =
accountsArray[0].getBalance();
double curBalance;
int ind=0;
for(i =1 ;i<SIZE;i++)
{
//compare i_th account balance and
modify if necessary.
curBalance =
accountsArray[i].getBalance();
if(curBalance <
minBalance)
{
minBalance =
curBalance;
ind = i;
}
}
return ind;
}
BankAccount removeDuplicate(BankAccount account1, BankAccount
account2)
{
return (account1.equals(account2));
}
void printArray(BankAccount accountsArray[]){
cout<<" FAVORITE BANK - CUSTOMER DETAILS "<<endl;
cout<<" --------------------------------"<<endl;
printf("\n %5s %18s %12s\n\n","Name","Ac Number","Balance");
BankAccount cur;
for(int i = 0;i<SIZE;i++)
{
cur = accountsArray[i];
printf(" %12s %8d
%12.2lf\n\n",cur.getName().c_str(),cur.getAccountNumber(),cur.getBalance());
}
cout<<endl;
}
//BankData.dat
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
//PLEASE COMMENT IF YOU HAVE DOUBTS AND PLEASE LIKE THE ANSWER.