Question

In: Computer Science

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

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.

  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.


    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

Solutions

Expert Solution

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


Related Solutions

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...
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...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class representing Polynomials #include <iostream> #include <cmath> using namespace std; class Poly { private: int degree; int *coefficients; public: Poly(); Poly(int coeff, int degree); Poly(int coeff); Poly(const Poly& p); ~Poly(); void resize(int new_degree); void setCoefficient(int exp, int coeff); int getCoefficient(int exp); void display(); }; // default constructor to create a polynomial with constant 0 Poly::Poly() { // set degree to 0 degree = 0; //...
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...
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make...
Data Structures Use good style. Make sure that you properly separate code into .h/.cpp files. Make sure that you add preprocessor guards to the .h files to allow multiple #includes. Overview You will be writing the classes Grade and GradeCollection. You will be writing testing code for the Grade and GradeCollection classes. Part 1 – Create a Grade Class Write a class named Grade to store grade information. Grade Class Specifications Include member variables for name (string), score (double). Write...
Using the 3 program files below to read the sample students.txt file and create a singly...
Using the 3 program files below to read the sample students.txt file and create a singly linked-list of students. The code in sll_list.h and mainDriver.c shouldn't be changed, only sll_list.c. The finished program should print out a list that initializes the sampled 5 students and allows the user to use the functions found in mainDriver.c. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // mainDriver.c // #include "sll_list.h" int main() {    int choice,list_size,id;    do    {        printf("1. initialize list of students\n2. add additional student...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT