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 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...
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...
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...
Add the following method below to the CardDeck class, and create a test driver to show...
Add the following method below to the CardDeck class, and create a test driver to show that they work correctly. int cardsRemaining() //returns a count of the number of undealt cards remaining in the deck. Complete in Java programming language. // Models a deck of cards. Includes shuffling and dealing. //---------------------------------------------------------------------- package Homework4; import java.util.Random; import java.util.Iterator; import javax.swing.ImageIcon; public class CardDeck { public static final int NUMCARDS = 52; protected ABList<Card> deck; protected Iterator<Card> deal; public CardDeck() { deck...
Write a C program that will read different data types from the following file and store...
Write a C program that will read different data types from the following file and store it in the array of structures. Given file: (This file have more than 1000 lines of similar data): time latitude longitude depth mag magType nst gap dmin 2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021 2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757 2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205 2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049 2020-10-19T22:42:26.224Z...
Implement an application that will read data from an input file, interpret the data, and generate...
Implement an application that will read data from an input file, interpret the data, and generate the output to the screen. - The application will have two classes, Rectangle and Lab2ADriver. - Name your Eclipse project, Lab2A. Implement the Rectangle class with the following description. Rectangle Data fields -numRows: int -numCols: int -filled: Boolean Store the number of rows in the Rectangle Store the number of columns in the Rectangle Will define either a filled or unfilled rectangle True =...
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
Write a java program that can create, read, and append a file. Assume that all data...
Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods. The program should have three methods as follows: CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully. ReadingFile - It reads a contents of the file. This method only allow to read a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT