1. 程式人生 > >專案實踐: 銀行儲蓄系統的設計和實現

專案實踐: 銀行儲蓄系統的設計和實現

8.14 銀行儲蓄系統的設計和實現

一、問題描述:

模擬銀行櫃檯業務的要求,實現一個小型的“銀行儲蓄系統”軟體的開發,其中包括開戶、存款、取款、轉帳、改密、掛失、解掛、銷戶等功能。
在開發過程中,請按照問題求解過程的要求,體驗開發過程中需要做的工作。除了下面的系統基本功能的描述外,鼓勵開展調研,使你開發的軟體儘量符合實際的銀行儲蓄系統的實際業務內容。可以參考8.1節中關於選用合適的資料結構的討論,確定資料儲存方案。
要求在程式執行過程中或程式結束執行之前,將業務發生的資料儲存到檔案中,並在下一次執行時,能從檔案中讀出資料,使業務能夠繼續在原先的基礎上開展。可以使用文字檔案,也可以使用二進位制檔案。
根據模組化程式設計的要求,將各功能設計成獨立的函式實現。必要時,提取出公共的功能設計專門的函式,作為支援各函式中的子功能要呼叫的模組。建議設計“選單”式的操作介面,方便使用者的使用。
各功能的要求分別為:
(1)開戶:增加一個銀行賬戶,輸入賬號、姓名、密碼、金額,狀態自動置為0(正常)。建議輸入密碼的過程中,以星號(*)代替實際的輸入的符號顯示出來(實現方法請利用搜索引擎獲得幫助)。作為對密碼的設定,在輸入一次密碼後,需要再次輸入密碼,兩次輸入一致後,才接受並儲存。由於設定了密碼,其他的業務必須在輸入的賬號、密碼均正確時才能繼續。
(2)存款:輸入賬號、金額,增加該賬號的餘額。
(3)取款:輸入賬號、金額,減少取款後的餘額。要求取款額不能超過原餘額。
(4)查詢:輸入賬號,顯示賬戶資訊。
(5)轉賬:輸入轉出的賬號、金額以及轉入的賬戶,減少轉出賬號的餘額,增加轉入賬號的餘額。要求轉出賬戶的金額不能超過該賬號的餘額,轉出減少的金額,與轉入賬戶增加的金額相同。
(6)掛失:輸入賬號,將其狀態改變為1(掛失)。處於掛失狀態的賬號,不能執行除解掛以外的其他任何操作。
(7)解掛:輸入賬號,將狀態為1(掛失)的賬戶的狀態改為0(正常)。
(8)銷戶:輸入賬號,確認後,提示將餘額全部取完,將餘額置0,並將狀態state置為2(銷戶)。辦理銷戶後的賬號,不能再執行除查詢以外的功能。

(9)改密:用新密碼替代舊密碼。新密碼要求輸入兩次,一致後才確認改密成功。

二、程式碼實現:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
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

  第一列到第五列分別對應這使用者的:卡號、姓名、密碼、餘額、狀態。  測試的時候可以用這個檔案來測試。