*****PLEASE TEST AND MAKE SURE CODE WORKS***** Write a C program for managing ba
ID: 3702608 • Letter: #
Question
*****PLEASE TEST AND MAKE SURE CODE WORKS*****
Write a C program for managing bank accounts. All account information should be stored in a binary file info.dat. The file info.dat should contain at least 2 different customers. The max number of accounts in the bank is 10. Store all accounts in memory as an array but any change on any account should be reflected in the data file (info.dat) not only on exit.
For each customer the data file should store the following fields:
• First name
• Last name
• Account number (any 5 digit number)
• Account balance
The menu should have the following options:
1. Exit
2. Deposit
3. Withdrawal
4. Add account
5. Remove account
6. Balance inquiry
7. View accounts
The program continues based on the user’s selection as follows --
• user input 1, the program terminates (for any other choices, the program does something and then returns to the menu).
• user input 2, 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 input 3, 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 input 4, 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 input 5, 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 input 6, 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 input 7, the program prints out all information about all accounts in the system one by one.
Explanation / Answer
# include <stdio.h>
typedef struct
{
char firstName[25];
char lastName[25];
long long int accountNumber;
long long int accountBalance;
} addressbook;
#define ARRAYLEN 10
void addAccount();
void viewAccounts();
void balance();
void deposit(long long int depo);
void Withdrawal(long long int draw);
void deleteAccount(long long int delete);
addressbook a[0];
FILE *fp;
FILE *fp_tmp;
int count = 0;
int main()
{
int option;
long long int depo, draw, delete;
do
{
printf("1) Exit ");
printf("2) Deposit ");
printf("3) Withdrawal ");
printf("4) Add account ");
printf("5) Remove account ");
printf("6) Balance inquiry ");
printf("7) View accounts ");
printf("Choose the option: ");
scanf("%d", &option);
switch (option)
{
case 1: printf("Exit");
break;
case 2: printf("Enter account number to deposit: ");
scanf("%lld", &depo);
deposit(depo);
break;
case 3: printf("Enter account number to Withdrawal: ");
scanf("%lld", &draw);
Withdrawal(draw);
break;
case 4: addAccount();
break;
case 5: printf("Enter account number to remove: ");
scanf("%lld", &delete);
deleteAccount(delete);
break;
case 6: balance();
break;
case 7: viewAccounts();
break;
default: printf("Choose correct option: ");
break;
}
}while(option != 1);
return 0;
}
void addAccount()
{
fp = fopen("addressbook.dat","a+");
for( int i=0; i<1 ; i++)
{
int digiCount = 0, number;
printf("Enter Details ");
printf("Enter firstname: ");
scanf("%s", a[count].firstName);
printf("Enter lastname: ");
scanf("%s", a[count].lastName);
// do{
printf("Enter account number(Should be 5 digits): ");
scanf("%lld", &a[count].accountNumber);
printf("enter accountBalance: ");
scanf("%lld", &a[count].accountBalance);
fwrite(&a[count], sizeof(a), 1, fp); /* notice, array indexed */
}
fclose(fp);
count = count + 1;
}
void viewAccounts()
{
fopen("addressbook.dat", "r");
for( int i=0; i<count; i++)
{
fread(&a[i], sizeof(a), 1, fp );
printf("lastname: %s ", a[i].firstName);
printf("firstname: %s ", a[i].lastName);
printf("accountNumber: %lld ", a[i].accountNumber);
printf("accountBalance: %lld ", a[i].accountBalance);
printf(" ");
}
fclose(fp);
}
void deposit(long long int depo)
{
long long int new, past;
int total = 0;
int c;
fopen("addressbook.dat", "r+");
for(int i = 0; i < count; i++)
{
fread(&a[i], sizeof(a), 1, fp );
if(a[i].accountNumber == depo)
{
past = a[i].accountBalance;
printf("Enter money to deposit: ");
scanf("%lld", &new);
new += past;
a[i].accountBalance = new;
c = fwrite(&a[i].accountBalance, 25, 1, fp);
}
}
if(c == 1)
{
printf("Success. ");
}
else
{
printf("Fail. ");
printf("Enter registered Account Number ");
}
fclose(fp);
}
void Withdrawal(long long int draw)
{
long long int new, past;
int total = 0;
int c;
fopen("addressbook.dat", "r+");
for(int i = 0; i < count; i++)
{
fread(&a[i], sizeof(a), 1, fp );
if(a[i].accountNumber == draw)
{
past = a[i].accountBalance;
printf("Enter money to Withdrawal: ");
scanf("%lld", &new);
past -= new;
a[i].accountBalance = past;
c = fwrite(&a[i].accountBalance, 25, 1, fp);
}
}
if(c == 1)
{
printf("Success ");
}
else
{
printf("Fail ");
printf("Enter registered Account Number ");
}
fclose(fp);
}
void deleteAccount(long long int delete)
{
int c=0;
fopen("addressbook.dat", "r+");
for(int i = 0; i < count; i++)
{
fp_tmp=fopen("tmp.dat", "a+");
fread(&a[i], sizeof(a), 1, fp );
if(a[i].accountNumber == delete)
{
printf("Account removed ");
c = 1;
}
else
{
fwrite(&a[i], sizeof(a), 1, fp_tmp);
}
}
if(c == 0)
{
printf("No records found ");
}
fclose(fp);
fclose(fp_tmp);
remove("addressbook.dat");
rename("tmp.dat", "addressbook.dat");
count--;
}
void balance()
{
long long int new, acc;
int c=0;
printf("Enter account number: ");
scanf("%lld", &acc);
fopen("addressbook.dat", "r");
for( int i=0; i<count; i++)
{
fread(&a[i], sizeof(a), 1, fp );
new = a[i].accountNumber;
if(new == acc)
{
printf("accountBalance: %lld ", a[i].accountBalance);
c=1;
}
printf(" ");
if(c == 0)
{
printf("Enter registered Account Number ");
}
}
fclose(fp);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.