Write a menu driven/interactive program for managing bank accounts. All account
ID: 3741672 • Letter: W
Question
Write a menu driven/interactive program for managing bank accounts. All account information should be stored in a binary file accounts.dat. Your data file accounts.dat should contain at least 5 different customers. You can limit the maximum number of accounts in the bank by 40. Store all accounts in memory as an array, but any change on any account should be immediately reflected in the data file (accounts.dat) not only on exit.
For each customer the data file should store the following fields:
• First name
• Middle initial
• Last name
• Account number (any 6-digit number)
• Account balance
The menu should provide you with the following options:
0. Exit
1. Deposit
2. Withdrawal
3. Add account
4. Remove account
5. Balance inquiry
6. View accounts
The program proceeds based on the user’s selection as follows:
• user selects 0, the program terminates (for any other choices, the program does something and then returns to the menu).
• user selects 1, the program asks about the account number and the amount to deposit, and performs the requested update in the data file. After performing the file update, the program should report the new balance on the affected account. If a wrong account number is provided, the program reports this and doesn’t make any changes.
• user selects 2, the program first asks for the account number and the amount to withdraw. If the current balance is insufficient to cover the withdrawal, the program reports this information and doesn’t make any changes in the account. Otherwise, the program updates the file accordingly and prints out the new balance on the affected account. If a wrong account number is provided, the program reports this and doesn’t make any changes.
• user selects 3, the program asks for information about a new customer, and then creates an account for this customer. The new account number should also be provided on input. If the user provides an account number already used, then the program complains and doesn’t perform the requested update (nothing is changed).
• user selects 4, the program asks for an account number and it then removes the requested account from the data file. If a wrong account number is provided, the program reports this and doesn’t make any changes.
• user selects 5, the program asks for an account number and it then prints out all information about that account stored in the data file. If a wrong account number is provided, the program reports this and doesn’t make any changes.
• user selects 6, the program prints out all information about all accounts in the system one by one.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
struct bank
{
char fnm[25];
char mnm[25];
char lnm[25];
long int acctno;
float abal;
};
void main()
{
int choice=-1, i=0,num=0;
long int ano;
float amt;
FILE *fp;
struct bank acct[40];
/* Open the data file */
fp=fopen("accounts.dat", "ab+");
if(fp==NULL)
{printf("File cannot be opened");
exit(1);
}
fseek(fp,0,SEEK_SET); /* move record pointer at the beginning of file */
while(fread(acct+i,sizeof(struct bank),1, fp)==1) /* Read & store the content of file in array */
i++;
num=i; /* num variable will keep track of number of record in file */
do
{
printf("0. Exit ");
printf("1. Deposit ");
printf("2. Withdrawal ");
printf("3. Add account ");
printf("4. Remove Account ");
printf("5. Balance Inquiry ");
printf("6. View Account ");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 0: /* Exit from program */
fclose(fp);
break;
case 1: /* Code for Deposit */
fseek(fp,0,SEEK_SET);
printf("Enter the account number for deposit:");
scanf("%ld",&ano);
printf("Enter the amount to be deposited");
scanf("%f",&amt);
for(i=0;i<num;i++)
{if(acct[i].acctno==ano)
{ acct[i].abal=acct[i].abal+amt;
fseek(fp,i*sizeof(struct bank),SEEK_SET); /* move pointer at desired record in file */
fwrite(acct+i,sizeof(struct bank),1, fp); /* write modified record into file*/
break;
}
}
if(i==num)
printf("Account number does not exits ");
break;
case 2: /* Code for Withdrawal */
fseek(fp,0,SEEK_SET);
printf("Enter the account number for withdrawal:");
scanf("%ld",&ano);
printf("Enter the amount to be withdrawal");
scanf("%f",&amt);
for(i=0;i<num;i++)
{if(acct[i].acctno==ano)
{if(acct[i].abal<amt) /* check for required balance */
{printf("Not a sufficient balance ");
break;}
acct[i].abal=acct[i].abal-amt;
fseek(fp,i*sizeof(struct bank),SEEK_SET);
fwrite(acct+i,sizeof(struct bank),1, fp);
break;
}
}
if(i==num)
printf("Account number does not exits ");
break;
case 3: /* Code to add new customer */
printf("Enter the 6-digit account number for new customer:");
scanf("%ld",&ano);
for(i=0;i<num;i++)
{if(acct[i].acctno==ano)
{printf("Account already exist ");
break;
}
}
if(i==num)
{printf("Enter the First-Name:");
scanf("%s",acct[i].fnm);
printf("Enter the Middle-Name:");
scanf("%s",acct[i].mnm);
printf("Enter the Last-Name:");
scanf("%s",acct[i].lnm);
acct[i].acctno=ano;
printf("Enter the Amount:");
scanf("%f",&acct[i].abal);
fseek(fp,0,SEEK_END); /* Move the record pointer at the end of file */
fwrite(acct+i,sizeof(struct bank),1, fp); /* write record at the end of file */
num++;
}
break;
case 4: /* Code for delete record */
fseek(fp,0,SEEK_SET);
printf("Enter the account number to be deleted:");
scanf("%ld",&ano);
for(i=0;i<num;i++)
{if(acct[i].acctno==ano)
break;
}
if(i==num)
printf("Account number does not exits ");
else
{while(i<num)
{strcpy(acct[i].fnm,acct[i+1].fnm);
strcpy(acct[i].mnm,acct[i+1].mnm);
strcpy(acct[i].lnm,acct[i+1].lnm);
acct[i].acctno=acct[i+1].acctno;
acct[i].abal=acct[i+1].abal;
}
num--;
fp=fopen("accounts.dat", "wb"); /* open file in write mode to remove the all records */
for(i=0;i<num;i++)
fwrite(acct+i,sizeof(struct bank),1, fp); /* write records without deleted record */
fclose(fp);
/* reopen file */
fopen("accounts.dat", "ab+");
}
break;
case 5: /* Code to print the required customer detail */
printf("Enter the account number for getting the detail:");
scanf("%ld",&ano);
for(i=0;i<num;i++)
{if(acct[i].acctno==ano)
{printf("First-Name:%s ",acct[i].fnm);
printf("Middle-Name:%s ",acct[i].mnm);
printf("Last-Name:%s ",acct[i].lnm);
printf("Account-No:%ld ",acct[i].acctno);
printf("Balance-Amount%.2f ",acct[i].abal);
break;
}
}
if(i==num)
printf("Account number does not exits ");
break;
case 6: /* Code to print deatils of all the customers */
for(i=0;i<num;i++)
{
printf("First-Name:%s ",acct[i].fnm);
printf("Middle-Name:%s ",acct[i].mnm);
printf("Last-Name:%s ",acct[i].lnm);
printf("Account-No:%ld ",acct[i].acctno);
printf("Balance-Amount%.2f ",acct[i].abal);
}
} /* end of switch-case */
}while(choice!=0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.