Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using c++ 1 Goal The purpose of this assignment is to get you familiar with Stre

ID: 3756863 • Letter: U

Question

Using c++

1 Goal The purpose of this assignment is to get you familiar with Streams and File I/O. Your goal is to create a simple bank database (file) where all the individual accounts are stored in a file.

2 Requirements The following data should be stored corresponding to each account:

>Account Number: an integer number (must be unique for every account)

>Name: two words corresponding to the first and last name

>Balance: a float point number

2. Your program should provide a menu of options that allow performing any of the following operations:

> Add an account

>Delete an account

>Search for an account by account number and display the account info

>Make a withdrawal Make a deposit

>Transfer money from one account to another

>List all accounts

Your program should be coded in a way such that all previous changes are written to the file before any new operations are performed.

3 Testing Your program must be able to handle the following abnormal situations:

For add an account option, if an existing account number is entered, the program should respond with a message and no new account should be created

For delete an account option, if the account number could not be found, a message should be displayed and no account should be deleted

For withdrawal operation, if the account number does not exist or the balance is not sufficient, a message should be displayed and no changes should be made to the database For deposit operation, if the account number does not exist, a message should be displayed and no changes should be made to the database

For transfer operation, if the source account number or the destination account number does not exist, or the balance of the source account is not sufficient, a message should be displayed and no changes should be made to the database

Explanation / Answer

C++ with details

#include<iostream>
#include <fstream>
#include <string>
using namespace std;
struct bank
{
int accNumber;
string Fname, Lname;
float balance;
};
static int j = 0;
struct bank B[5];
void read_from_file(const char* filename)
{
   /*Variable declarations*/
ifstream infile;
/*Opening file*/
infile.open (filename);
int i=0;
if (infile.is_open())
{
    /*Reading all details from file*/
while(!infile.eof()) // To get you all the lines.
{
  
   infile>>B[i].accNumber;
   infile>>B[i].Fname; // Saves the line in STRING.
   infile>>B[i].Lname;
   infile>>B[i].balance;
   /*To prevent read last line twice*/
if( infile.eof() )
       {
          break;
       }
   j++;
   i++;
  
  
}
/*Printing all account details after reading*/
for (i = 0; i<j; i++){
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << ' ';
}
infile.close();
}
}
int addAccount()
{
int accNum,i;
string fname, lname;
float balance;
cout << " --- Provide details --- ";
cout << "Acc Number: "; cin >> accNum;
for (i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
j = j + 1;
B[i].accNumber = accNum;
cout << "first name: "; cin >> B[i].Fname;
cout << "last name: "; cin >> B[i].Lname;
cout << "Balance:"; cin >> B[i].balance;
}
void deleteAccount()
{
int accNum;
cout << "Enter account number: "; cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum){
for (int k = i; k<j - 1; k++)
B[k] = B[k + 1];
}
}
}
void searchAccount()
{
int accNum;
cout << "Enter account number: "; cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum){
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << " ";
}
}
}
int withdraw()
{
float withd;
int accNum,flag=0;
cout << "Enter account number from you want to withdraw:";
cin >> accNum;
for (int i = 0; i<j; i++)
{
if (B[i].accNumber == accNum)
{
    flag=0;
cout << "Enter withdraw amount : ";
cin >> withd;
if (B[i].balance >= withd){
B[i].balance = B[i].balance - withd;
cout << "Total :" << B[i].balance << " ";
return 0;
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
else{
flag=1;
}
}
if(flag==1){
    cout << "Error: No changes should be made to the database ";
return 0;
}
}
int deposit()
{
float dep;
int accNum,flag=0;
cout << "Enter account number you want to Deposit:";
cin >> accNum;
for (int i = 0; i<j; i++)
{
    flag=0;
if (B[i].accNumber == accNum)
{
cout << "Enter deposit amount : ";
cin >> dep;
B[i].balance = B[i].balance + dep;
cout << "Total :" << B[i].balance << " ";
return 0;
}
else{
flag=1;
}
}
if(flag==1){
    cout << "Error: No changes should be made to the database ";
return 0;
}
}
int transfer()
{
float tran;
int fromAcc, toAcc, fr, to, i, f1, f2;
cout << "Enter account number you want to transfer from "; cin >> fromAcc;
cout << "to "; cin >> toAcc;
for (i = 0; i<j; i++)
if (B[i].accNumber == fromAcc){
f1 = 1;
fr = i;
}
for (i = 0; i<j; i++)
if (B[i].accNumber == toAcc){
f2 = 1;
to = i;
}
if (f1 == 1 && f2 == 1)
{
cout << "Enter amount to transfer : ";
cin >> tran;
if (B[fr].balance >= tran){
B[to].balance = B[to].balance + tran;
B[fr].balance = B[fr].balance - tran;
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
else{
cout << "Error: No changes should be made to the database ";
return 0;
}
}
void list()
{
for (int i = 0; i<j; i++)
cout << B[i].accNumber << " " << B[i].Fname << " " << B[i].Lname << " " << B[i].balance << " ";
}
/*Write into file*/
void write_into_file(const char* filename){
   int i;
   ofstream myfile (filename);
   if (myfile.is_open()) {
      
   for(i=0;i<j;i++){
       myfile<<B[i].accNumber<<" ";
   myfile<<B[i].Fname<<" "; // Saves the line in STRING.
   myfile<<B[i].Lname<<" ";
   myfile<<B[i].balance<<" ";
  
   }
  
}
}
int main()
{
int op,i;
char ch;
read_from_file("account.txt");
do{
cout << "-- Operations menu --- ";
cout << " 1. Add an Account "
"2. Delete an Account "
"3. Search for an account by account number and display the account info "
"4. Make a Withdraw "
"5. Make a Deposit "
"6. Transfer money from one account to another "
"7. List all accounts ";
cout << "Choose the operation : ";
cin >> op;
switch (op){
case 1: addAccount();
break;
case 2: deleteAccount();
break;
case 3: searchAccount();
break;
case 4: withdraw();
break;
case 5: deposit();
break;
case 6: transfer();
break;
case 7: list();
break;
default: cout << "Invalid operation ";
exit(0);
}
cout << "Do you wants more operation (Y/y) by default No : ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
/*Writing into File at the End of all operations*/
write_into_file("account.txt");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote