项目实践: 银行储蓄系统的设计和实现

8.14 银行储蓄系统的设计和实现



一、问题描述:



模拟银行柜台业务的要求,实现一个小型的“银行储蓄系统”软件的开发,其中包括开户、存款、取款、转帐、改密、挂失、解挂、销户等功能。
在开发过程中,请按照问题求解过程的要求,体验开发过程中需要做的工作。除了下面的系统基本功能的描述外,鼓励开展调研,使你开发的软件尽量符合实际的银行储蓄系统的实际业务内容。可以参考8.1节中关于选用合适的数据结构的讨论,确定数据存储方案。
要求在程序运行过程中或程序结束运行之前,将业务发生的数据保存到文件中,并在下一次运行时,能从文件中读出数据,使业务能够继续在原先的基础上开展。可以使用文本文件,也可以使用二进制文件。
根据模块化程序设计的要求,将各功能设计成独立的函数实现。必要时,提取出公共的功能设计专门的函数,作为支持各函数中的子功能要调用的模块。建议设计“菜单”式的操作界面,方便用户的使用。
各功能的要求分别为:
(1)开户:增加一个银行账户,输入账号、姓名、密码、金额,状态自动置为0(正常)。建议输入密码的过程中,以星号(*)代替实际的输入的符号显示出来(实现方法请利用搜索引擎获得帮助)。作为对密码的设置,在输入一次密码后,需要再次输入密码,两次输入一致后,才接受并保存。由于设置了密码,其他的业务必须在输入的账号、密码均正确时才能继续。
(2)存款:输入账号、金额,增加该账号的余额。
(3)取款:输入账号、金额,减少取款后的余额。要求取款额不能超过原余额。
(4)查询:输入账号,显示账户信息。
(5)转账:输入转出的账号、金额以及转入的账户,减少转出账号的余额,增加转入账号的余额。要求转出账户的金额不能超过该账号的余额,转出减少的金额,与转入账户增加的金额相同。
(6)挂失:输入账号,将其状态改变为1(挂失)。处于挂失状态的账号,不能执行除解挂以外的其他任何操作。
(7)解挂:输入账号,将状态为1(挂失)的账户的状态改为0(正常)。
(8)销户:输入账号,确认后,提示将余额全部取完,将余额置0,并将状态state置为2(销户)。办理销户后的账号,不能再执行除查询以外的功能。

(9)改密:用新密码替代旧密码。新密码要求输入两次,一致后才确认改密成功。


二、代码实现:



#include
#include
#include
int i=0;
int n=-1;
int k;
int id1;
FILE *p;
struct score{
	int  num;
	char name[20];
	int secret;
	int money;
	int state;
}rec[20];
void open()//开户
{
     n=n+1;
    printf("	please input your name: \n");
	scanf("%s",rec[n].name);
    rec[n].num=n;
	printf("please input your secret: \n");
	scanf("%d",&rec[n].secret);
	 printf("how much do you want to saving?\n");
	 scanf("%d",&rec[n].money);
	 rec[n].state=0;
	 printf("Open account successful,your account is %d!",n);
	 system("pause");
}
void savings()//存款
{
	int id;
	int dl;
    printf("input num \n");
	scanf("%d",&id);
	printf("input your money\n");
	scanf("%d",&dl);
	if(rec[id].state==0)
	{
	rec[id].money+=dl;
	printf("Saving money successful!\n");


	//用于测试

        // printf("%d %s %d %d %d\n",rec[id].num,rec[id].name,rec[id].secret,rec[id].money,rec[id].state);

    }
	else
	{
		printf("your account is report you can't saving money\n");
	}
	 system("pause");

}
void draw()//取款
{
    int id1;
	int dl;
	int k;
	int i=0;
     printf("input num \n");
	scanf("%d",&id1);
	//验证密码,有三次机会
	do
    {
        printf("input your key\n");
        scanf("%d",&k);
        i++;
    }while((k!=rec[id1].secret)&&(i<=2));
    if(k==rec[id1].secret)
    {
	   printf("How much do you want to draw\n");
	   scanf("%d",&dl);
      if(rec[id1].state==0)
      {
          //如果总钱数超过了余额会提示重新输入
          if(dl>rec[id1].money)
	      {
	        do
	        {
		      printf("your money is only %d,you can't tansfer,please resume load: \n",rec[id1].money);
		       scanf("%d",&dl);
	         } while(dl>rec[id1].money);
	          rec[id1].money=rec[id1].money-dl;
	         printf("Draw money successful!\n");
	      }
	      else
	     {
	         rec[id1].money=rec[id1].money-dl;
	         printf("Draw money successful!\n");
	   }
      }
      else
	  {
		printf("your account is report\n");
	  }
    }
    else{printf("your key is wrong you can't do account\n");}
    system("pause");
}
void refer()//查询
{
	int id;
	int k;
	int i=0;
	printf("Input your num:\n");
	scanf("%d",&id);
	//验证密码,有三次机会
	do
    {
        printf("input your key\n");
        scanf("%d",&k);
        i++;
    }while((k!=rec[id].secret)&&(i<=2));
    if(k==rec[id].secret)
    {
        printf("Your account information:\n");
	   if(rec[id].state==0)
	   {
	      printf("name:%5s\n money:%5d\n your account is normal\n",rec[id].name,rec[id].money);
	   }
	   else if(rec[id].state==0){
		 printf("name:%5s\n money:%5d\n your account is report\n",rec[id].name,rec[id].money);
	    }
	    else
        {
            printf("name:%5s\n money:%5d\n your account is closed\n",rec[id].name,rec[id].money);
        }
    }
    else{printf("your key is wrong you can't do account\n");}
    system("pause");
}
void transfer()//转账
{
    int id1;
	int id2;
	int s;
	int k;
	int i=0;
	printf("what is your account?\n");
    scanf("%d",&id1);
    printf("which account do you want to transfer?\n");
	scanf("%d",&id2);
    do
    {
        printf("Please input your key: \n");
        scanf("%d",&k);
        i++;
    }while((k!=rec[id1].secret)&&(i<=2));
    if(k==rec[id1].secret)
    {
	  if(rec[id1].state==1||rec[id1].state==1)
	  {
		printf("your can't transfer\n");
	  }
	  else
	  {
        printf("how much do you want to tansfer?\n");
        scanf("%d",&s);
        if(s>rec[id1].money)
	   {
	   do
	   {
		printf("your money is only %d,you can't tansfer,please resume load\n",rec[id1].money);
		scanf("%d",&s);
	   }while(s>rec[id1].money);
	    rec[id1].money=rec[id1].money-s;
	     rec[id2].money=rec[id2].money+s;
	     printf("transfer success!\n");
	  }
	   else
	  {
	     rec[id1].money=rec[id1].money-s;
	     rec[id2].money=rec[id2].money+s;
	     printf("transfer success!\n");
	   }
	   }
    }
    else{printf("your key is wrong you can't do account\n");}
    system("pause");
}
void report()//挂失
{
    int i=0;
    int k;
    int id1;
     printf("what is your account?\n");
	 scanf("%d",&id1);
    do
    {
        printf("Please input your key: \n");
        scanf("%d",&k);
        i++;
    }while((k!=rec[id1].secret)&&(i<=2));
    if(k==rec[id1].secret)
    {
        if(rec[id1].state==0)
        {
	    rec[id1].state=1;
	    printf("Report successful!\n");
        }
        else
        {
            printf("The account already reported,you needn't repetition report\n");
        }
    }
    else{
        printf("your key is wrong you can't do account");
    }
    system("pause");
}
void cancel_report()//解挂
{
    int id1;
    int i=0;
    int k;
	printf("what is your account?\n");
   	scanf("%d",&id1);
     do
    {
        printf("Please input your key: \n");
        scanf("%d",&k);
        i++;
    }while((k!=rec[id1].secret)&&(i<=2));
    if(k==rec[id1].secret)
    {
        if(rec[id1].state==1)
        {
            rec[id1].state=0;
            printf("Cancel report successful!\n");
        }
        else
        {
            printf("The account is not report,you can't cancel report\n");
        }
    }
     else{
        printf("your key is wrong you can't do account");
    }
system("pause");
}
void cancel_account()//销户
{
    int id1;
    int i=0;
    int k;
	printf("what is your account?\n");
   	scanf("%d",&id1);
     do
    {
        printf("Please input your key: \n");
        scanf("%d",&k);
        i++;
    }while((k!=rec[id1].secret)&&(i<=2));
    if(k==rec[id1].secret)
    {
        rec[id1].state=2;
      printf("We will draw all the money in your account\n");
      printf("Give you your money %d yuan\n",rec[id1].money);
      rec[id1].money=0;
      printf("Close account successful!\n");
    }
     else{
        printf("your key is wrong you can't do account");
    }
    system("pause");

}
void change_security()//改密
{
    int s;
    int i=0;
    int k;
    int id1;
    int k1,k2;
    printf("what is your account?\n");
   	scanf("%d",&id1);
    do
    {
        printf("Please input your key: \n");
        scanf("%d",&k);
        i++;
    }while((k!=rec[id1].secret)&&(i<=2));
    if(k==rec[id1].secret)
    {
        printf("what key do you want to change to?");
        scanf("%d",&k1);
         printf("Input your key one more time");
         scanf("%d",&k2);
         if(k1==k2)
         {
             rec[id1].secret=k1;
             printf("Change security successful!\n");
         }
         else {printf("Change security FAIL,the two key isn't same!\n");}

    }
     else{
        printf("your key is wrong you can't do account");
    }
system("pause");
}
void readDate()
{

	int i=0;
	FILE *fp;
    if((fp=fopen("date.txt","r+"))==NULL)
	{
		printf("Can't open the date\n");
		exit(0);
	}

		while(fscanf(fp,"%d %s %d %d %d\n",&rec[i].num,rec[i].name,&rec[i].secret,&rec[i].money,&rec[i].state ) != EOF)
		{
        i++;
        n++;
		}
	fclose(fp);

}
void writeDate()
{
    int i;
	FILE *fp;
	if((fp=fopen("date.txt","r+"))==NULL)
	{
		printf("Can't open the date\n");
		exit(0);
	}

	for(i=0;i<=n;i++)
	{
		fprintf(fp,"\n");
      	fprintf(fp,"%d %s %d %d %d\n",rec[i].num,rec[i].name,rec[i].secret,rec[i].money,rec[i].state);
	}
   fclose(fp);
   printf("saving date successful!\n");
   system("pause");
}

int main()
{
	int s;
    readDate();

	 while(1)
	{
		system("cls");
		printf("******************************MENU*********************************************\n\n");
		printf("                      0:Open a new account\n");
        printf("                      1:Savings money\n");
		printf("                      2:Draw money \n");
		printf("                      3:Refer account\n");
		printf("                      4:Transfer account\n");
		printf("                      5:Report account\n");
		printf("                      6:Cancel report\n");
		printf("                      7:cancel_account\n");
		printf("                      8:change_security\n");
		printf("                      9:Save date\n");
		printf("                      10:Quit\n");
		printf("*******************************************************************************\n\n");
		do{
			printf("\n            Input your choice(0 ~ 10):");
			scanf("%d",&s);
		}while(s<0||s>10);
		system("cls");
		switch(s)
		{
		    case 0:open();break;
		    case 1:savings();break;
		    case 2:draw();break;
		    case 3:refer();break;
		    case 4:transfer();break;
		    case 5:report();break;
		    case 6:cancel_report();break;
		    case 7:cancel_account();break;
		    case 8:change_security();break;
		    case 9:writeDate();break;
		    case 10:exit(0);

		}
	}
    return 0;
}


此银行管理系统设计的比较简单,一些功能如隐藏密码等功能也没有实现,还有对于密码的输入没有对其进行长度的限制当超出长度是程序就出错了,大家可以试着完善一些,我日后也会做出改进的。


三、程序测试:

     在“data.txt"中存储了银行的数据,程序运行时就会读到内存中进行处理,处理完成之后,就会再写入文件中。程序运行之前,文件中的数据如下:

               0 king 1 1 0

               1 ding 1 1 0

               2 vkj 1 1 0

               3 k 1 1 0

                4 j 1 2 0

  第一列到第五列分别对应这用户的:卡号、姓名、密码、余额、状态。  测试的时候可以用这个文件来测试。


你可能感兴趣的:(C/C++项目实践)