#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
void showcode(void);
void openaccount(int sequence[10][15]);
struct Account{
char name[100];
char id[15];
char address[200];
char ccode[4];
char bcode[3];
char Dob[10];
int balance;
struct tm *Doo;
char accountnumber[11];
};
void main(void){
char option='\0', ch='\0', in='\0';
int count=0;
int sequence[10][15];
FILE *fp;
if((fp=fopen("Sequence.txt","r"))==NULL){
if((fp=fopen("Sequence.txt","w"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
fwrite(sequence, 150*sizeof(int), 1, fp);
}
else{
fread(sequence, 150*sizeof(int), 1, fp);
}
fclose(fp);
do{
printf("1) Show city code and branch code\n");
printf("2) Open an account\n");
printf("3) Show account details\n");
printf("4) Show list of accounts\n");
printf("5) Deposit in an account\n");
printf("6) Withdraw from an account\n");
printf("7) Transfer money\n");
printf("8) Transaction details\n");
printf("9) Close an account\n");
printf("10)Quit\n");
printf("\n");
printf("Option: ");
count=0;
option='\0', ch='\0', in='\0';
while((in=getchar())!='\n'){
count++;
option=ch;
ch=in;
}
if((count==1)&&(ch=='0')){
ch='a';
}
if(count>2){
ch='a';
}
if(count==2){
if((option=='1')&&(ch=='0')){
ch='0';
}
else{
ch='a';
}
}
printf("\n");
switch(ch){
case '1': showcode(); break;
case '2': openaccount(sequence); break;
case '3': printf("3\n"); break;
case '4': printf("4\n"); break;
case '5': printf("5\n"); break;
case '6': printf("6\n");break;
case '7': printf("7\n");break;
case '8': printf("8\n");break;
case '9': printf("9\n");break;
case '0': printf("break\n");break;
default: printf("Unknown option!\n");
printf("\n");
}
}while(ch!='0');
exit(0);
}
void showcode(void){
FILE *fp;
char name1[10], name2[10], name3[10];
char code1[4], code2[4], code3[4];
char buffer[20], citycode[4], choice;
char *name, *code;
int len, rmd;
if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
printf("City\t\t\tcode\t\t\tCity\t\t\tcode\t\t\tCity\t\t\tcode\n");
printf("_____________________________________________________________________________________________________________________________\n");
while(fgets(buffer, 20, fp)!=NULL){
len=strlen(buffer);
buffer[len-1]='\0';
code=strtok(buffer, "\t");
name=strtok(NULL, "\t");
if(strlen(code)==3){
rmd=(atoi(code))%3;
switch(rmd){
case 1:
strcpy(name1, name);
strcpy(code1, code);
break;
case 2:
strcpy(name2, name);
strcpy(code2, code);
break;
case 0: strcpy(name3, name);
strcpy(code3, code);
printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
break;
}
}
}
printf("%-10s\t\t%-10s\n", name1, code1);
printf("\n");
fseek(fp, 0, SEEK_SET);
printf("Enter the city code: ");
scanf("%s", citycode);
getchar();
printf("\n");
if((fp=fopen("CityBranchCodes_Leyang_Hu_20215416.txt", "r"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
while(fgets(buffer, 20, fp)!=NULL){
len=strlen(buffer);
buffer[len-1]='\0';
code=strtok(buffer, "\t");
name=strtok(NULL, "\t");
if((strcmp(code, citycode))==0){
break;
}
}
printf("Branch name\t\tcode\t\t\tBranch name\t\tcode\t\t\tBranch name\t\tcode\n");
printf("_____________________________________________________________________________________________________________________________\n");
while(fgets(buffer, 20, fp)!=NULL){
len=strlen(buffer);
buffer[len-1]='\0';
code=strtok(buffer, "\t");
name=strtok(NULL, "\t");
if(strlen(code)==3){
break;
}
rmd=(atoi(code))%3;
switch(rmd){
case 1:
strcpy(name1, name);
strcpy(code1, code);
break;
case 2:
strcpy(name2, name);
strcpy(code2, code);
break;
case 0: strcpy(name3, name);
strcpy(code3, code);
printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2, name3, code3);
break;
}
}
switch(rmd){
case 1:
printf("%-10s\t\t%-10s\n", name1, code1);
break;
case 2:
printf("%-10s\t\t%-10s\t\t%-10s\t\t%-10s\n", name1, code1, name2, code2);
break;
case 0:
break;
}
fclose(fp);
printf("\n");
printf("Do you want to see more city and branch codes (Y/N): ");
scanf("%c", &choice);
getchar();
if(choice=='Y'){
showcode();
}
printf("\n");
}
void openaccount(int sequence[10][15]){
struct Account *account=(struct Account*)malloc(sizeof(struct Account));
int blc,i,j,number,len;
char ch, orig[5], seq[5];
FILE *fp, *ptr;
time_t t;
printf("Enter the name of the account holder: ");
while((ch=getchar())!='\n'){
account->name[i]=ch;
i++;
}
printf("Enter the Id number of the account holder: ");
scanf("%s", account->id);
getchar();
printf("Enter the address of the account holder: ");
i=0;
while((ch=getchar())!='\n'){
account->address[i]=ch;
i++;
}
printf("Enter city code: ");
scanf("%s", account->ccode);
getchar();
printf("Enter branch code: ");
scanf("%s", account->bcode);
getchar();
printf("Enter the Dob of the account holder: ");
scanf("%s", account->Dob);
getchar();
while(blc<1000){
printf("Enter the initial balance of the account (not less that 1000 RMB): ");
scanf("%d", &blc);
if(blc<1000){
printf("Initial deposit can't be less than 1000 RMB.\n");
}
}
account->balance=blc;
time(&t);
account->Doo=localtime(&t);
if((fp=fopen("Account.txt", "a"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
i=atoi(account->ccode);
j=atoi(account->bcode);
number=(sequence[i][j])+1;
sequence[i][j]+=1;
if((ptr=fopen("Sequence.txt", "w"))==NULL){
printf("Fail to open the file!\n");
exit(EXIT_FAILURE);
}
fwrite(sequence, 150*sizeof(int), 1, ptr);
fclose(ptr);
sprintf(orig, "%d", number);
len=strlen(orig);
i=0;
for(j=0;j<5;j++){
if(j>=(5-len)){
seq[j]=orig[i];
i++;
}
else{
seq[j]='0';
}
}
strcpy(account->accountnumber, account->ccode);
strcat(account->accountnumber, account->bcode);
strcat(account->accountnumber, seq);
account->accountnumber[10]='\0';
printf("The account number is: %s\n", account->accountnumber);
fwrite(account, sizeof(struct Account), 1, fp);
free(account);
fclose(fp);
printf("\n");
printf("Do you want to open more accounts?(Y/N): ");
scanf("%c", &ch);
getchar();
if(ch=='Y'){
printf("\n");
openaccount(sequence);
}
printf("\n");
}