C++程序设计实践:银行前台服务系统(三)

来源:互联网 发布:app产品优化 编辑:程序博客网 时间:2024/05/16 14:23

deposit.h

#pragma once

#include "login.h"

#include"mainMenu.h"

 

void deposit()

{

         system("cls");

         cout<<"mainmenu << deposit"<<endl<<endl;

         cout<<"--------------------------"<<endl;

         cout<<"|  Please login at first |"<<endl;

         cout<<"--------------------------"<<endl;

         Sleep(1000);

         //修改金额部分

 

         ifstream deposit1;

         ofstream deposit2;

         //定义读取文件的变量

         int number;

         char name[20];

         char password[20];

         double money;

         int type;

         char time[10];

         //定义错误次数

         int errorTime = 0;

         //调用登录函数login(),用变量enterNumber接受登录函数login()的返回值

         int enterNumber = login();

         //如果登录失败但未超过三次

         while((enterNumber == 0) &&(errorTime <= 2))

         {

                   Sleep(2000);

                   errorTime ++;

                   enterNumber = login();

         }

         //如果登录失败达到三次

         if(enterNumber == 0)

         {

                   cout<<"---------------------------------------------"<<endl;

                   cout<<"▏Sorry,login failed,will back to main menu▕"<<endl;

                   cout<<"---------------------------------------------"<<endl;

                   Sleep(2000);

                   outputMainMenu();

         }

         //登录成功,开始文件操作

         deposit1.open("account.txt",ios::in);

         deposit2.open("account1.txt",ios::out);

         double depositMoney;

         cout<<"--------------------------"<<endl;

         cout<<"Please enter deposit money: "<<endl;

         cout<<"--------------------------"<<endl;

         cin>>depositMoney;

         //对比新旧数据,创建新数据库

         deposit1>>number;

         deposit1>>name;

         deposit1>>password;

         deposit1>>money;

         deposit1>>type;

         deposit1>>time;

                           

         while(!deposit1.eof())

         {

                   //如果number == enterNumber,即找到该账户,则不将该账户的信息写入新文件

                   if(number == enterNumber)

             {        

                            if(depositMoney <= money)

                            {

                           

                            money -= depositMoney;

                            deposit2<<number<<" ";

                            deposit2<<name<<" ";                                 

                            deposit2<<password<<" ";

                            deposit2<<money<<" ";

                            deposit2<<type<<" ";

                            deposit2<<time<<endl;

                            }

                            else

                            {

                                     cout<<"-------------------------------------------"<<endl;

                                     cout<<" There is no enough money in your account! "<<endl;

                                     cout<<"-------------------------------------------"<<endl<<endl;

                                     system("PAUSE");

                                     deposit();

                            }

                   }

                   //如果不是用户所找的账户,则不加改动的写入新文件

                   else

                   {

                            deposit2<<number<<" ";

                            deposit2<<name<<" ";

                            deposit2<<password<<" ";

                            deposit2<<money<<" ";

                            deposit2<<type<<" ";

                            deposit2<<time<<endl;

                                              

                   }

                   //读取下一个用户的数据,直到找到该用户的账户                           

                   deposit1>>number;

                   deposit1>>name;

                   deposit1>>password;

                   deposit1>>money;

                   deposit1>>type;

                   deposit1>>time;

         }                         

                            //新文件名为account1.txt,原文件名为account.txt

                            deposit1.close();

                            deposit2.close();

                            char fileName[] = "account.txt";

                            char fileName1[] = "account1.txt";

                            remove(fileName);

                            rename(fileName1,fileName);

                            //将原文件删除,同时把新文件名字由account1.txt改为account.txt

                            cout<<"*******************"<<endl;

                            cout<<"* Deposit success! *"<<endl;

                            cout<<"*******************"<<endl;

                            //注销成功,让用户选择下一步操作

                            cout<<"================================"<<endl<<endl;

                            cout<<" ▎Please choose what to do:"<<endl<<endl;

                            cout<<"●1.Back to main menu"<<endl<<endl;

                            cout<<"●2.Continue to deposit"<<endl<<endl;

                            cout<<"●3.Exit the game"<<endl;

                            cout<<"================================"<<endl<<endl;

                            int choose;

                            cin>>choose;

                            //若选择1,则返回主菜单,选择2,继续注销账户,选择3就退出程序

                            if(choose == 1)

                            {

                                     outputMainMenu();

                            }

                            else if(choose == 2)

                            {

                                     deposit();

                            }

                            else if(choose == 3)

                            {}

}

 

withdrawals.h

#pragma once

#include "login.h"

#include"mainMenu.h"

 

void withdrawals()

{

         system("cls");

         cout<<"mainmenu << withdrawals"<<endl<<endl;

         cout<<"----------------------"<<endl;

         cout<<"Please login at first"<<endl;

         cout<<"----------------------"<<endl;

         system("pause");

         ifstream withdrawals1;

         ofstream withdrawals2;

         //定义读取文件的变量

         int number;

         char name[20];

         char password[20];

         double money;

         int type;

         char time[10];

         //定义错误次数

         int errorTime = 0;

         //调用登录函数login(),用变量enterNumber接受登录函数login()的返回值

         int enterNumber = login();

         //如果登录失败但未超过三次

         while((enterNumber == 0) &&(errorTime <= 2))

         {

                   cout<<"login failed!"<<endl;

                   Sleep(4000);

                   errorTime ++;

                   enterNumber = login();

         }

         //如果登录失败达到三次

         if(enterNumber == 0)

         {

                   cout<<"Sorry,login failed,will back to main menu"<<endl;

                   Sleep(3000);

                   outputMainMenu();

         }

         //登录成功,开始文件操作

         withdrawals1.open("account.txt",ios::in);

         withdrawals2.open("account1.txt",ios::out|ios::app);

         double withdrawalsMoney;

         cout<<"withdrawals money: ";

         cin>>withdrawalsMoney;

        

         withdrawals1>>number;

         withdrawals1>>name;

         withdrawals1>>password;

         withdrawals1>>money;

         withdrawals1>>type;

         withdrawals1>>time;

                           

         while(!withdrawals1.eof())

         {

                   //如果number == enterNumber,即找到该账户,则不将该账户的信息写入新文件

                   if(number == enterNumber)

             {        

                            money += withdrawalsMoney;

                            withdrawals2<<number<<" ";

                            withdrawals2<<name<<" ";                                  

                            withdrawals2<<password<<" ";

                            withdrawals2<<money<<" ";

                            withdrawals2<<type<<" ";

                            withdrawals2<<time<<endl;

                   }

                   else

                   {

                            //如果不是用户所找的账户,则不加改动的写入新文件

                            withdrawals2<<number<<" ";

                            withdrawals2<<name<<" ";

                            withdrawals2<<password<<" ";

                            withdrawals2<<money<<" ";

                            withdrawals2<<type<<" ";

                            withdrawals2<<time<<endl;

                                              

                   }

                   //读取下一个用户的数据,直到找到该用户的账户                           

                   withdrawals1>>number;

                   withdrawals1>>name;

                   withdrawals1>>password;

                   withdrawals1>>money;

                   withdrawals1>>type;

                   withdrawals1>>time;

         }                         

                   //新文件名为account1.txt,原文件名为account.txt

                            char fileName[] = "account.txt";

                            char fileName1[] = "account1.txt";

                            withdrawals1.close();

                            withdrawals2.close();

                            //将原文件删除,同时把新文件名字由account1.txt改为account.txt

                            remove(fileName);

                            rename(fileName1,fileName);

                            //注销成功,让用户选择下一步操作

                            cout<<"*******************"<<endl;

                            cout<<"* Withdrawals success! *"<<endl;

                            cout<<"*******************"<<endl;

                            cout<<"================================"<<endl<<endl;

                            cout<<" ▎Please choose what to do:"<<endl;

                            cout<<"●1.Back to main menu"<<endl;

                            cout<<"●2.Continue to withdrawal"<<endl;

                            cout<<"●3.Exit the game"<<endl;

                            cout<<"================================"<<endl<<endl;

                            int choose;

                            cin>>choose;

                            //若选择1,则返回主菜单,选择2,继续注销账户,选择3就退出程序

                            if(choose == 1)

                            {

                                     outputMainMenu();

                            }

                            else if(choose == 2)

                            {

                                     withdrawals();

                            }

                  

}

 

statistics.h

#pragma once

#include"mainMenu.h"

 

void statistics()

{

         //定义读取文件的变量

         int number;

         char name[20];

         char password[20];

         double money;

         int type;

         char time[10];

         system("cls");

        

         cout<<"main menu << statistic <<output";

         cout<<"Please choose what to do"<<endl<<endl;

         cout<<"====================================="<<endl<<endl;

         cout<<"● 1.View the order by account number"<<endl<<endl;

         cout<<"● 2.View the order by account balances"<<endl<<endl;

         cout<<"====================================="<<endl<<endl;

         //让用户选择按账号顺序输出还是按余额大小输出

         int choose;

         cin>>choose;

        

         //按账号顺序输出

         if(choose == 1)

         {

         ifstream statistic;

         //定义账户数量,总金额

         int numberOfAccount = 0;

         double totalMoney = 0;

         system("cls");

         //输出表头

         cout<<"*********************";

         cout<<" All account information ********************"<<endl<<endl;

         cout<<left;

         cout<<setw(10)<<"number"<<setw(15)<<"name"<<setw(15)<<"password";

         cout<<setw(10)<<"money"<<setw(10)<<"type"<<setw(15)<<"time"<<endl;

         statistic.open("account.txt",ios::in);

         //读取文件

         statistic>>number;

         statistic>>name;

         statistic>>password;

         statistic>>money;

         statistic>>type;

         statistic>>time;

         while(!statistic.eof())

         {

                   cout<<left;

             //输出用户的数据

                   cout<<setw(10)<<number<<setw(15)<<name<<setw(15)<<password;

                   cout<<setw(10)<<money<<setw(10)<<type<<setw(15)<<time<<endl;

                   //计算账户数量与余额总量

                   numberOfAccount ++;

                   totalMoney += money;

                   //读取下一个用户的数据

                   statistic>>number;

                   statistic>>name;

                   statistic>>password;

                   statistic>>money;

                   statistic>>type;

                   statistic>>time;

         }

         statistic.close();

         //计算账户数量与余额总量

         cout<<endl<<endl<<"--------------------------"<<endl;

         cout<<numberOfAccount<<" accounts in total"<<endl;

         cout<<"--------------------------"<<endl;

         cout<<"--------------------------"<<endl;

         cout<<"total money is "<<totalMoney<<endl;

         cout<<"--------------------------"<<endl;

         system("PAUSE");

         //让用户选择下一步操作

         cout<<"=============================="<<endl<<endl;

         cout<<"▏Please choose what to do:"<<endl<<endl;

         cout<<"●1.Back to main menu"<<endl<<endl;

         cout<<"●2.Exit the system"<<endl;

         cout<<"=============================="<<endl<<endl;

         int choose;

         cin>>choose;

         if(choose == 1)

         {

                   outputMainMenu();

         }

         else

         {}

         }

         //以上为按账号顺序输出

 

         //按账户余额排序

         if(choose == 2)

         {

                   ifstream statistic1;

                   statistic1.open("account.txt",ios::in);

                   //读取文件,获取账户数量

                   int i = 0;

                   //计算账户数量i以确定数组大小

                   while(!statistic1.eof())

                   {

                            statistic1>>number;

                            statistic1>>name;

                            statistic1>>password;

                            statistic1>>money;

                            statistic1>>type;

                            statistic1>>time;

                            i++;

                   }

                   //因为eof()多读取的一次文件

                   i--;

                  

                   statistic1.close();

                   int size = i;

                   //分别定义存放账号和金额的两个数组

                   int *numberList = new int [i];

                   double *moneyList = new double [i];

                  

                   statistic1.open("account.txt",ios::in);

                   //为两个数组赋值,将文件中的账号和金额数据分别放在这两个数组中

                   int index = 0;

                   while(index <= size - 1)

                   {

                            statistic1>>number;

                            statistic1>>name;

                            statistic1>>password;

                            statistic1>>money;

                            statistic1>>type;

                            statistic1>>time;

                            numberList[index] = number;

                            moneyList[index] = money;

                            index++;

                   }

                  

                   statistic1.close();

                   //对moneyList中的元素进行排序,在对moneyList进行操作时,同步的对numberList进行完全相同的操作

                   for(i = size - 1;i >= 1;i--)

                   {

                            double currentMax = moneyList[0];

                            int currentMaxIndex = 0;

                           

                            for(int j = 1;j <= i;j++)

                            {

                                     if(currentMax < moneyList[j])

                                     {

                                               currentMax = moneyList[j];

                                               currentMaxIndex = j;

                                     }

                            }

                            if(currentMaxIndex != i)

                            {

                                     moneyList[currentMaxIndex] = moneyList[i];

                                     moneyList[i] = currentMax;

                                    

                                     //对numberlist数组进行相同操作

                                     int temp;

                                     temp = numberList[currentMaxIndex];

                                     numberList[currentMaxIndex] = numberList[i];

                                     numberList[i] = temp;

                            }

                           

                   }

                  

                   //将排好序的数组存贮的信息按顺序输出

                   system("cls");

                   //输出表头

                   cout<<"*********************";

                   cout<<" All account information ********************"<<endl<<endl;

                   cout<<left;

                   cout<<setw(10)<<"number"<<setw(15)<<"name"<<setw(15)<<"password";

                   cout<<setw(10)<<"money"<<setw(10)<<"type"<<setw(15)<<"time"<<endl;

                   //numberList数组中储存的元素是按照金额大小排好顺序的账户对应的账号

                   for(i = 0;i <= size -1;i++)

                   {

                            statistic1.open("account.txt",ios::in);

                            //寻找已知账号的账户信息并输出

                            while(!statistic1.eof())

                            {

                                     statistic1>>number;

                                     statistic1>>name;

                                     statistic1>>password;

                                     statistic1>>money;

                                     statistic1>>type;

                                     statistic1>>time;

                                    

                                     if(number == numberList[i])

                                     {

                                               cout<<left;

                                               cout<<setw(10)<<number<<setw(15)<<name<<setw(15)<<password;

                                               cout<<setw(10)<<money<<setw(10)<<type<<setw(15)<<time<<endl;

                                               break;

                                     }

                            }

                            statistic1.close();

                   }

                   system("PAUSE");

                   //选择下一步操作

                   cout<<"=============================="<<endl<<endl;

                   cout<<"▏Please choose what to do:"<<endl<<endl;

                   cout<<"●1.Back to main menu"<<endl<<endl;

                   cout<<"●2.Exit the system"<<endl;

                   cout<<"=============================="<<endl<<endl;

                   int choose;

                   cin>>choose;

                   if(choose == 1)

                   {

                            outputMainMenu();

                   }

                   else

                   {}

         }

}

 

cancellation.h

#pragma once

#include "login.h"

#include"mainMenu.h"

 

void cancellation()

{

         system("cls");

         cout<<"mainmenu << cancellation"<<endl<<endl;

         cout<<"----------------------"<<endl;

         cout<<"Please login at first"<<endl;

         cout<<"----------------------"<<endl;

         system("pause");

        

         ifstream cancellation1;

         ofstream cancellation2;

         //定义读取文件的变量

         int number;

         char name[20];

         char password[20];

         double money;

         int type;

         char time[10];

         //定义错误次数

         int errorTime = 0;

         //调用登录函数login(),用变量enterNumber接受登录函数login()的返回值

         int enterNumber = login();

        

         //如果登录失败但未超过三次

         while((enterNumber == 0) &&(errorTime <= 2))

         {

                   cout<<"-----------"<<endl;

                   cout<<"login failed!"<<endl;

                   cout<<"-----------"<<endl;

                   Sleep(4000);

                   errorTime ++;

                   enterNumber = login();

         }

         //如果登录失败达到三次

         if(enterNumber == 0)

         {

                   cout<<"--------------------------------------------"<<endl;

                   cout<<"Sorry,login failed,will back to main menu"<<endl;

                   cout<<"--------------------------------------------"<<endl;

                   Sleep(3000);

                   outputMainMenu();

         }

         //登录成功,开始文件操作

         cancellation1.open("account.txt",ios::in);

         cancellation2.open("account1.txt",ios::out|ios::app);

        

         cancellation1>>number;

         cancellation1>>name;

         cancellation1>>password;

         cancellation1>>money;

         cancellation1>>type;

         cancellation1>>time;

                           

         while(!cancellation1.eof())

         {

                   //如果number == enterNumber,即找到该账户,则不将该账户的信息写入新文件

                   if(number == enterNumber)

                   {}    

                   else

                   {

                            //如果不是用户所找的账户,则不加改动的写入新文件

                            cancellation2<<number<<" ";

                            cancellation2<<name<<" ";

                            cancellation2<<password<<" ";

                            cancellation2<<money<<" ";

                            cancellation2<<type<<" ";

                            cancellation2<<time<<endl;

                                              

                   }

                   //读取下一个用户的数据,直到找到该用户的账户                 

                   cancellation1>>number;

                   cancellation1>>name;

                   cancellation1>>password;

                   cancellation1>>money;

                   cancellation1>>type;

                   cancellation1>>time;

         }                         

                            //新文件名为account1.txt,原文件名为account.txt

                            char fileName[] = "account.txt";

                            char fileName1[] = "account1.txt";

                            cancellation1.close();

                            cancellation2.close();

                            //将原文件删除,同时把新文件名字由account1.txt改为account.txt

                            remove(fileName);

                            rename(fileName1,fileName);

                            //注销成功,让用户选择下一步操作

                            cout<<"***********************"<<endl;

                            cout<<"* Cancellation success! *"<<endl;

                            cout<<"***********************"<<endl;

                            cout<<"================================"<<endl<<endl;

                            cout<<" ▎Please choose what to do:"<<endl;

                            cout<<"●1.Back to main menu"<<endl;

                            cout<<"●2.Continue to cancellation"<<endl;

                            cout<<"●3.Exit the game"<<endl;

                            cout<<"================================"<<endl<<endl;

                            int choose;

                            cin>>choose;

                            //若选择1,则返回主菜单,选择2,继续注销账户,选择3就退出程序

                            if(choose == 1)

                            {

                                     outputMainMenu();

                            }

                            else if(choose == 2)

                            {

                                     cancellation();

                            }

                  

}

原创粉丝点击