Create an application that searches an array of objects.The application should l
ID: 3773101 • Letter: C
Question
Create an application that searches an array of objects.The application should lookup name and balance based on the account number according to the list shown.
Account Number
Name
Balance
7221379
6853921
4215534
5551298
6224649
6502286
8448936
2883903
6752376
3564698
6373150
6375372
8736368
7218536
6681985
3909086
5221192
5901163
2427821
6331655
Abula
Allen
Austin
Bailey
Balovich
Bates
Bettencourt
Blackburn
Boucher
Brown
Duncan
Estrada
Fernandez
Gallagher
Gonzales
Ha
Hernandez
Howard
Johnston
Nguyen
970.12
268.01
3821.94
3193.97
444.21
4253.79
2916.99
906.31
1291.99
2001.18
2963.20
1152.60
1113.05
3703.46
2667.88
3042.25
72.04
2229.66
3130.92
3004.99
The data is to be stored as an array of objects (using a class definition) or an array of structured records. A sequential search should be used to locate the account number entered by the user. If the user enters an account number that is not in the array, the program should display a message indicating the account number is not in the list. If the user enters an account number that is in the list, the program should save the index (position) that the account number was found, and use that index to lookup the customer name and balance. The program is to display the customer account number, name and balance. Also Give the user the option of looking up the customer's name and finding the account number.
Account Number
Name
Balance
7221379
6853921
4215534
5551298
6224649
6502286
8448936
2883903
6752376
3564698
6373150
6375372
8736368
7218536
6681985
3909086
5221192
5901163
2427821
6331655
Abula
Allen
Austin
Bailey
Balovich
Bates
Bettencourt
Blackburn
Boucher
Brown
Duncan
Estrada
Fernandez
Gallagher
Gonzales
Ha
Hernandez
Howard
Johnston
Nguyen
970.12
268.01
3821.94
3193.97
444.21
4253.79
2916.99
906.31
1291.99
2001.18
2963.20
1152.60
1113.05
3703.46
2667.88
3042.25
72.04
2229.66
3130.92
3004.99
Explanation / Answer
Here is the cpp code. I have used a file named Accounts.txt to store the account details and I am fetching records from this file.
#include<iostream>
#include <fstream>
#include <string>
#include <time.h>
#include<ctime>
using namespace std;
class Account
{
private:
int account_no;
string name;
float balance;
public:
Account();
int getAccountNo();
void setAccountNo(int val);
string getName();
float getBalance();
void setBalance(float bal);
void setName(string name);
};
Account::Account()
{
this->account_no=0;
this->name="";
}
//accessor methods of class Account
int Account::getAccountNo()
{
return this->account_no;
}
string Account::getName()
{
return this->name;
}
//mutator methods of class Account
void Account::setAccountNo(int account_no)
{
this->account_no = account_no;
}
void Account::setName(string name)
{
this->name = name;
}
float Account::getBalance()
{
return this->balance;
}
void Account::setBalance(float balance)
{
this->balance=balance;;
}
void sortNames(Account *fnames,int count);
int linearSearch (Account *fnames, int searchAcc, int count);
int binarySearch(Account *fnames, string searchKey, int low, int high);
int getAccountFromUser();
void searchAccount(int searchKey, Account *fnames,int count);
string toUpper(string name);
const int SIZE=1000;
int main()
{
Account fnames[SIZE];
string name;
int value;
float bal;
int count=0;
string searchKey;
int searchAcc;
ifstream infile("Accounts.txt");
//infile.open("CommonAccount.txt");
if(infile.is_open()) // Correct way of opening a file
{
infile>>value;
infile>>name;
infile>>bal;
fnames[count].setName(name);
fnames[count].setAccountNo(value);
fnames[count].setBalance(bal);
while(!infile.eof())
{
count++;
infile>>value;
infile>>name;
infile>>bal;
fnames[count].setName(name);
fnames[count].setAccountNo(value);
fnames[count].setBalance(bal);
}
}
else cout << "out";
sortNames(fnames, count);
for(int i=0;i<=count;i++)
cout<<fnames[i].getName()<<" "<<fnames[i].getAccountNo()<<" "<<fnames[i].getBalance()<<endl;
searchAcc=getAccountFromUser();
//searchKey=toUpper(searchKey);
cout << searchKey<<endl;
searchAccount(searchAcc, fnames,count);
char repeat='Y';
while (repeat!='N'){
cout<<"Would You Like To Find Another Account? (y/n): ";
cin >> repeat;
if(repeat=='Y' || repeat=='y') {
searchAcc=getAccountFromUser(); // do all the console read in a function to call it anytime
searchAccount(searchAcc, fnames,count);
char repeat='Y';
}
else {
break;
}
}
system("pause");
infile.close();
return 0;
}
int getAccountFromUser()
{
int searchAcc;
cout << "Enter Account No. to find: " << endl;
cin >> searchAcc;
return searchAcc;
}
void searchAccount(int searchAcc, Account *fnames,int count)
{
clock_t startClock,finishClock;
double timeCount;
startClock = clock()/1000;
int pos1=linearSearch(fnames,searchAcc,count);
finishClock = clock()/1000;
timeCount=finishClock-startClock;
if(pos1!=-1)
cout << "Account Found:# "<< fnames[pos1].getName()<<" : Balance:- "<<fnames[pos1].getBalance()<< endl;
else
cout << "Account not found" << endl;
//cout<< "The time taken to find the Account using linear search is: " << timeCount << " nanoseconds." << endl;
timeCount=finishClock-startClock;
}
void sortNames(Account *fnames, int count)
{
Account temp;
for (int i = 0; i<count; i++)
{
for (int j = i + 1; j <= count; j++)
{
if (fnames[i].getName().compare(fnames[j].getName())>0)
{
temp = fnames[i];
fnames[i] = fnames[j];
fnames[j] = temp;
}
}
}
}
int linearSearch (Account *fnames, int searchKey, int count) {
for (int i = 0; i <=count; ++i)
{
if (fnames[i].getAccountNo()==searchKey)
return i;
}
return -1;
}
// Accounts.txt
7221379 Abula 970.12
6853921 Allen 268.01
4215534 Austin 3821.94
5551298 Bailey 3193.97
6224649 Balovich 444.21
6502286 Bates 4253.79
8448936 Bettencourt 2916.99
2883903 Blackburn 906.31
6752376 Boucher 1291.99
3564698 Brown 2001.18
6373150 Duncan 2963.20
6375372 Estrada 1152.60
8736368 Fernandez 1113.05
7218536 Gallagher 3703.46
6681985 Gonzales 2667.88
3909086 Ha 3042.25
5221192 Hernandes 72.04
5901163 Howard 2229.66
2427821 Johnston 3130.92
6331655 Nguyen 3004.99
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.