Code:
#include<conio.h>
#include<stdio.h>
#include<string.h>
#define SIZE 100
/*definitions for the structures*/
struct date
{
int day;
int month;
int year;
};
typedef struct date DATE;
struct address
{
char town[50];
char street[50];
char county[50];
};
typedef struct address ADDRESS;
struct details
{
int acc_number;
char first_name[15];
char surname [20];
int age;
ADDRESS address;
DATE DOB;
float current_balance;
int overdraft;
float lodge;
float withdraw;
};
typedef struct details DETAILS;
/*prototypes for fucntions*/
void add_an_account(DETAILS []);
void remove_an_account(DETAILS []);
void edit_account(DETAILS []);
void display_an_account(DETAILS []);
void display_account_details(DETAILS []);
void overdraft(DETAILS []);
void lodge_money(DETAILS []);
void withdraw(DETAILS []);
void init_database(DETAILS []);
int search_database(DETAILS [], int);
int menu(void);
void main()
{
DETAILS persons[SIZE];
int menu_choice;
init_database(persons);
do
{
menu_choice = menu();
switch(menu_choice)
{
case 1 : add_an_account(persons);
break;
case 2 : remove_an_account(persons);
break;
case 3 : edit_account(persons);
break;
case 4 : display_an_account(persons);
break;
}
}
while (menu_choice !=0);
}
/*add an employee function*/
void add_an_account(DETAILS bank_account[])
{
int i=0; int count=0; int j;
while(bank_account[i].acc_number!=0 && i < SIZE)
i++;
if ( i == SIZE)
printf("/n The database is full, please delete an account to add a new one /n");
else
{
printf("\n\n Enter your desired account number between 1 and 100 : ");
do{
scanf("%d", &bank_account[i].acc_number);
fflush(stdin);
}
while(bank_account[i].acc_number <=0 );
for(j=0;j<SIZE;j++)
{
if(bank_account[j].acc_number == bank_account[i].acc_number)
count++;
}
if(count == 1)
{
/* name of the new account */
printf("\n please enter your first name : ");
scanf("%15s", &bank_account[i].first_name);
fflush(stdin);
printf("\n please enter your surname : ");
scanf("%20s", &bank_account[i].surname);
fflush(stdin);
/*date of birth for the new account*/
printf("\n please enter your birth date\n");
printf("\n Day: (Max 2 digits) : ");
scanf("%2d", &bank_account[i].DOB.day);
fflush(stdin);
printf("\n Month: (Max 2 digits) : ");
scanf("%2d", &bank_account[i].DOB.month);
fflush(stdin);
printf("\n Year: (Max 2 digits) : ");
scanf("%2d", &bank_account[i].DOB.year);
fflush(stdin);
printf("\n Enter your age : ");
scanf("%2d", &bank_account[i].age);
fflush(stdin);
/*the address of the new account*/
printf("\n please enter your address\n");
printf("\n Town: ");
scanf("%s", &bank_account[i].address.town);
fflush(stdin);
printf("\n Street: ");
scanf("%s", &bank_account[i].address.street);
fflush(stdin);
printf("\n county: ");
scanf("%s", &bank_account[i].address.county);
fflush(stdin);
/* the balance of the new account
printf("\n please enter your starting balance \n");
scanf("%d", &bank_account[i].current_balance);
fflush(stdin);*/
}
else
printf("sorry this account number is already being used");
}
}
/*delete an account*/
/* deleting an account is done by replacing the account with a 0 in the array*/
void remove_an_account(DETAILS bank_account[])
{
int account_number;
int pos;
printf("\nEnter account that you want to be removed(1-100): ");
do
scanf("%3d", &account_number);
while ( account_number <= 0 );
pos = search_database(bank_account, account_number);
if( pos == SIZE)
printf("\nThis account is not in the database\n");
else
{
printf("\naccount %3d deleted", account_number);
bank_account[pos].acc_number = 0;
bank_account[pos].current_balance = 0;
bank_account[pos].overdraft = 0;
bank_account[pos].lodge = 0;
bank_account[pos].withdraw = 0;
bank_account[pos].DOB.day = 0;
bank_account[pos].DOB.month = 0;
bank_account[pos].DOB.year = 0;
bank_account[pos].age = 0;
}
}
/*display a bank account*/
void display_an_account( DETAILS bank_account[] )
{
int account_number;
int pos;
printf("\nEnter account number desired(1 - 100): ");
do
scanf("%d", &account_number);
while(account_number <=0);
pos = search_database( bank_account, account_number);
if(pos == SIZE)
printf("This account is not in the database\n");
else
display_account_details( &bank_account[pos]);
}
/*edit an accounts details*/
/*to edit the account you have to find the position of the account in the memory and change it*/
void edit_account(DETAILS bank_account[])
{
int account_number;
int j;
printf("\nEnter the account number you wish to edit: ");
do
scanf("%3d", &account_number);
while(account_number <=0);
j = search_database(bank_account, account_number);
if(j == SIZE)
printf("\nThere is no account with this number\n");
else
{
/*Replace the account holders first name*/
printf("\nPlease enter the new first name desired: ");
scanf("%s", bank_account[j].first_name);
fflush(stdin);
printf("\nPlease enter the new surname desired: ");
scanf("%s", bank_account[j].surname);
fflush(stdin);
/*change the date of birth in the bank account*/
printf("\nPlease enter the new date of birth desired\n");
printf("Day:");
scanf("%s", bank_account[j].DOB.day);
fflush(stdin);
printf("Month:");
scanf("%s", bank_account[j].DOB.month);
fflush(stdin);
printf("Year:");
scanf("%s", bank_account[j].DOB.year);
fflush(stdin);
printf("New age: ");
scanf("%d", bank_account[j].age);
fflush(stdin);
/* change the address in the bank_account*/
printf("\nPlease enter the new town desired: \n");
scanf("%s", bank_account[j].address.town);
fflush(stdin);
printf("\nPlease enter the new street desired: \n");
scanf("%s", bank_account[j].address.street);
fflush(stdin);
printf("\nPlease enter the new county desired: \n");
scanf("%s", bank_account[j].address.county);
fflush(stdin);
}
}
/* search for an account*/
int search_database(DETAILS bank_account[], int emp_number)
{
int i = 0;
while(i<SIZE && bank_account[i].acc_number != emp_number)
i++;
return(i);
}
/*display the details of an account*/
void display_account_details(DETAILS *ptr)
{
printf("\n\n\n");
printf("Account number: %d\n", ptr->acc_number);
printf("First name: %s\n", ptr->first_name);
printf("Surname: %s\n", ptr->surname);
printf("Age: %d\n", ptr->age);
printf("Date of birth: %d/%d/%d\n", ptr->DOB.day, ptr->DOB.month, ptr->DOB.year);
printf("Town: %s\n street: %s\n County: %s\n",ptr->address.town,ptr->address.street,ptr->address.county);
printf("Balance: %d", ptr->current_balance);
}
/*initialise database*/
void init_database(DETAILS bank_account[])
{
int i;
for(i=0;i<SIZE;i++)
{
bank_account[i].acc_number = 0;
bank_account[i].current_balance = 0;
bank_account[i].overdraft = 0;
bank_account[i].lodge = 0;
bank_account[i].withdraw = 0;
bank_account[i].DOB.day = 0;
bank_account[i].DOB.month = 0;
bank_account[i].DOB.year = 0;
bank_account[i].age = 0;
}
}
/*menu function*/
int menu(void)
{
int choice;
/*display the menu to the user*/
printf("\n\n 1: Add an account\n");
printf("\n\n 2: delete an account\n");
printf("\n\n 3: Edit an account\n");
printf("\n\n 4: display an account\n");
printf("\n\n 5: Exit the program\n");
do
{
scanf("%d", &choice);
fflush(stdin);
}
while (choice <0|| choice >4);
return(choice);
}