Question

In: Computer Science

In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...

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.

  1. Extend the BankAccount class. The member fields of the class are: Account Name, Account Id, Account Number and Account Balance. The field Account Id (is like a private social security number of type int) Convert the variables MIN_BALANCE=9.99, REWARDS_AMOUNT=1000.00, REWARDS_RATE=0.04 to static class constants Here is the UML for the class:

                                                        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

  1. Create a file called BankAccount.cpp which implements the BankAccount class as given in the UML diagram above. The class will have member variables( attributes/data) and instance methods(behaviours/functions that initialize, access and process data)
  2. Create a driver class to do the following:
    1. 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

  1. Print the array (without secret id) similar to Homeowork2 EXCEPT the account balance must show only 2 decimal digits. You will need to do some string processing.
  2. Find the account with largest balance and print it
  3. Find the account with the smallest balance and print it.
  4. 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.

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

Solutions

Expert Solution

// 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:


Related Solutions

Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you...
answer in c++ First – take your Box02.cpp file and rename the file Box03.cpp Did you ever wonder how an object gets instantiated (created)? What really happens when you coded Box b1; or Date d1; or Coord c1; I know you have lost sleep over this. So here is the answer………. When an object is created, a constructor is run that creates the data items, gets a copy of the methods, and may or may not initialize the data items....
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are...
C++ question. Need all cpp and header files. Part 1 - Polymorphism problem 3-1 You are going to build a C++ program which runs a single game of Rock, Paper, Scissors. Two players (a human player and a computer player) will compete and individually choose Rock, Paper, or Scissors. They will then simultaneously declare their choices and the winner is determined by comparing the players’ choices. Rock beats Scissors. Scissors beats Paper. Paper beats Rock. The learning objectives of this...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers...
Write C++ program (submit the .cpp,.h, .sln and .vcxproj files) Problem 1. Generate 100 random numbers of the values 1-20 in an input.txt. Now create a binary search tree using the numbers of the sequence. The tree set should not have any nodes with same values and all repeated numbers of the random sequence must be stored in the node as a counter variable. For example, if there are five 20s’ in the random sequence then the tree node having...
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT