In: Computer Science
Please do this in C++ only. Please Show your code and your output too. Would you focus on number 2 in the Required functions:? Please use all the variables in the program.
The assignment problem:
You are asked to develop an application that prints a bank’s customers' names, IDs, and accounts balances in different ways. In this program, you need to read a file of customer’s data into different arrays, pass these arrays to functions as (array parameter), calculate the total balance for all customers, then print some information. The input file “input.txt” contains the following information (ID, first name, last name, savings account balance, checking account balance). You have to store these different columns in different arrays where each index represents customer data.
Preliminaries
1. We will consider the file contains 10 rows maximum. So all your arrays size must be at least 10.
2. Since the size of all the arrays is 10, so it is better to create a constant variable with the value 10 than hard coding the arrays size.
3. Create five different arrays with different types based on the data in the file (for ID, First, Last, saving, and checking)
4. Create a “switch” statement with four options and the output should be generated based on the chose option. Check the options below:
1. Print customer information
2. Print all customer Names ordered by their Last Name.
3. Print Bank’s total amount.
4. Enter q/Q to quit
Required functions:
1. A function “printCustomersData” should be called when the user chooses the first option. This function is of type void (it does not return anything). “printCustomersData” function should print a table of the customers data with the following order format: “LastName , First Name , ID , Saving Account, Checking Account.”
2. The function “printNames” will print the Names (first and last) of customers in alphabetic order by their last name.
3. The function “printBankTotal” that prints the summation of the balance of all customers saving account ($17410.31) and the summation of the balance of all customers checking accounts ($27430.01).
4. If the user entered something not in the options such as the
letter ‘a’, the application should not be terminated. Instead, the
application should print a message that tells the user that his/her
input was incorrect, then the options should be displayed again,
and the application should ask the user to enter another
option.
5. The Menu options will be printed after each selection. Even when the user enters an invalid value. The only way to terminate the program is to enter ‘q’ or ‘Q’ which is the fourth option.
Hints:
1. Create an “if” statement to check if the file reading process went well. You can use many expressions such as: (!fin) or (fin.fail()), etc.
2. Use the function “setw(number)” that helps you to print the data in a shape of table. This function sets the number of spaces for each output. The function is in the <iomanip> library.
3. Save the whole project and zip it before uploading it to Canvas. (Do not submit only the ‘.cpp’ file and do not copy and paste your code to a ‘.txt’ file or Word file).
This what you will have in the “input.txt”
10 Homer Smith 810.2 101.10
20 Jack Stanely 100.0 1394.90
30 Daniel Hackson 333.90 7483.77
40 Sara Thomson 1930.02 4473.20
50 Thomas Elu 932.0 2334.30
60 Sam Carol 33.0 0.0
70 Tina Jefferson 334.90 777.5
80 Wael Lion 8843.2 88.90
90 Carol Smith 3994.09 2343.30
100 Jack Carlton 99.0 8433.04
The program is given below: that open file, read file into array, then print menu to user, take choice from user and continue until user enters Q/q, if user enter invalid option then print invalid and ask again, if user enter 1 then call printCustomersData() function that print customer data, if user enter 2 then call printNames() function that print first and lastname of customer in alphabetic order by last name, if user enter 3 then call printBankTotal() function that print sum of the balance of all customers saving account and sum of the balance of all customers checking accounts.
#include <iostream>
#include<bits/stdc++.h>
#include<iomanip>
#include<fstream>
using namespace std;
const int SIZE=10;
int ID[SIZE];
string First[SIZE],Last[SIZE];
float saving[SIZE],checking[SIZE];
//printCustomersData() function print customers data
void printCustomersData(int count)
{
int i;
cout<<left<<setw(10)<<"LastName"<<left<<setw(10)<<"First
Name"<<left<<setw(10)<<"ID";
cout<<left<<setw(10)<<"Saving
Account"<<left<<setw(10)<<"Checking
Account.";
//execute for loop from 0 to count
for(i=0;i<count;i++)
{ //print data
cout<<"\n"<<left<<setw(10)<<Last[i]<<left<<setw(10)<<First[i]<<left<<setw(10)<<ID[i];
cout<<left<<setw(10)<<saving[i]<<left<<setw(10)<<checking[i];
}
}
//printNames() function print first and lastname of customer in
alphabetic order by last name
void printNames(int c)
{
string val;
int i,min_Index;
string temp,temp1;
string new_lastname[10],new_fname[10];
cout<<"Print customers in alphabetic order by their last
name.";
//execute for loop from 0 to c
for(i=0;i<c;i++)
{ //store values from last[i] into new_lastname[i] and First[i]
into new_fname[i]
new_lastname[i]=Last[i];
new_fname[i]=First[i];
}
//sort data in alphabetic order by last name
for(i=0;i<c-1;i++)
{
for (int j=i+1; j<c; j++)
{
if (new_lastname[i]>new_lastname[j])
{
temp=new_lastname[i];
new_lastname[i]=new_lastname[j];
new_lastname[j]=temp;
temp1=new_fname[i];
new_fname[i]=new_fname[j];
new_fname[j]=temp1;
}
}
}
//print first and last name of sorted data in alphabetic order by
last name
for(i=0;i<c;i++)
{
cout<<"\n"<<left<<setw(10)<<new_fname[i]<<left<<setw(10)<<new_lastname[i];
}
}
//printBankTotal() function print sum of the balance of all
customers saving account
//and sum of the balance of all customers checking accounts.
void printBankTotal(int c)
{
int i;
float total_saving_acc=0,total_checking_acc=0;
//execute for loop from 0 to c
for(i=0;i<c;i++)
{ //perform sum
total_saving_acc=total_saving_acc+saving[i];
total_checking_acc=total_checking_acc+checking[i];
}
//print sum of the balance of all customers saving and checking
account
printf("\nbalance of all customers saving account:
$%.2f",total_saving_acc);
printf("\nbalance of all customers checking account:
$%.2f",total_checking_acc);
}
int main()
{
ifstream fin;
char ch='a';
int count=0;
//open file
fin.open("input.txt");
//if file not opened
if(fin.fail())
{ //then print file not opened.
cout<<"\ncould not open file";
exit(1);
}
//read into arrays
while(fin>>ID[count]>>First[count]>>Last[count]>>saving[count]>>checking[count])
{ //increment count by 1
count=count+1;
}
//close file
fin.close( );
//print menu
cout<<"\n1. Print customer information";
cout<<"\n2. Print all customer Names ordered by their Last
Name.";
cout<<"\n3. Print Bank’s total amount.";
cout<<"\n4. Enter q/Q to quit\n";
//take choice from user
cin>>ch;
//continue until user enters q or Q
while(!(ch=='Q' || ch=='q'))
{
switch(ch)
{
//if ch is 1
case '1'://call printCustomersData() function
printCustomersData(count);
break;
//if ch is 2 the call printNames() function
case '2':printNames(count);
break;
//if ch is 3 then call printBankTotal() function
case '3':printBankTotal(count);
break;
//if ch is 4 then simply break
case '4':break;
//for any other choice print Inavalid option
default: cout<<"\nInvalid option ";
break;
}
//print menu
cout<<"\n1. Print customer information";
cout<<"\n2. Print all customer Names ordered by their Last
Name.";
cout<<"\n3. Print Bank’s total amount.";
cout<<"\n4. Enter q/Q to quit\n";
//take choice from user
cin>>ch;
}
return 0;
}
The screenshot of code is given below:
Output: