Question

In: Computer Science

Create a driver class to do the following: a. Read data from the given file BankData.data...

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

Solutions

Expert Solution

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


Related Solutions

Create a c file to read from an existing file one character at a time and...
Create a c file to read from an existing file one character at a time and print that character to the console Create a c file to read a character from the standard input and write it to a file. please help me
Add The following methods to the LinkedQueue class and create a test driver for each to...
Add The following methods to the LinkedQueue class and create a test driver for each to show they work correctly. In order to practice your linked list coding skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously defined public methods of the class. a. String toString () creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging the class...
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
Write a python program to read from a file the names and grades of a class...
Write a python program to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a gradesInput() function that reads data from a file and stores it and returns it as a dictionary....
Write a pyhton program to read from a file the names and grades of a class...
Write a pyhton program to read from a file the names and grades of a class of students, to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a dataInput() function that reads data from a file and stores it and returns it as a dictionary....
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Create a Java class file for an Account class. In the File menu select New File......
Create a Java class file for an Account class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Account. For Package: select csci1011.lab8. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab8; /** * * @author Your Name */ public class Account { } Implement the Account...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT