源代码 mainBody.cpp
#include<iostream> #include<fstream> #include<stdio.h> #include<cstring> #include<cstdlib> #include<vector> #include<iomanip> #include<windows.h> using namespace std; #include"mainMenu.h" int main() { system("color 3A"); //输出欢迎界面 (此部分欢迎界面代码因word不支持格式未粘贴,请看cpp文件) system("PAUSE"); //输出主菜单 outputMainMenu();
return 0; }
|
mainMenu.h
#pragma once #include"createNewAccount.h" #include"cancellation.h" #include"changePassword.h" #include"checkBanlance.h" #include"deposit.h" #include"login.h" #include"statistics.h" #include"withdrawals.h" //声明函数签名 void selectMainMenu(char choose);
void outputMainMenu() { system("cls"); cout<<" *************************************************"<<endl; cout<<" * Welcome to the Self-Service Banking System *"<<endl; cout<<" *************************************************"<<endl<<endl;
cout<<" ====================================="<<endl<<endl; cout<<" ● 1.Create new account ●"<<endl<<endl; cout<<" ● 2.Change Your Password ●"<<endl<<endl; cout<<" ● 3.Check account banlance ●"<<endl<<endl; cout<<" ● 4.Deposit ●"<<endl<<endl; cout<<" ● 5.Withdrawals ●"<<endl<<endl; cout<<" ● 6.Statistics account information ●"<<endl<<endl; cout<<" ● 7.Cancellation accounts ●"<<endl<<endl; cout<<" ● 0.Exit the system ●"<<endl<<endl; cout<<" ====================================="<<endl<<endl; cout<<"Please choose what you want to operate by enter the corresponding number"<<endl<<endl; //让用户选择功能 char choose; cin>>choose; selectMainMenu(choose); }
void selectMainMenu(char choose) { //根据用户的选择调用不同的函数 if(choose == '1') createNewAccount(); else if(choose == '2') changePassword(); else if(choose == '3') checkBanlance(); else if(choose == '4') deposit(); else if(choose == '5') withdrawals(); else if(choose == '6') statistics(); else if(choose == '7') cancellation(); else if(choose == '0') {} else { cout<<"The character entered are illegal, please re-enter it"<<endl; Sleep(2000); outputMainMenu(); } } |
createNewAccount.h
#pragma once #include"mainMenu.h" void outputMainMenu(); int createAccount(int select);
//显示一级子菜单 void createNewAccount() { system("cls"); cout<<"mainMenu<<create new account"<<endl<<endl; cout<<"Please choose what to do"<<endl<<endl; cout<<" ====================================="<<endl<<endl; cout<<" ● 1.Create current account"<<endl<<endl; cout<<" ● 2.Create regular account"<<endl<<endl; cout<<" ● 3.Return to main menu"<<endl<<endl; cout<<" ● 4.Exit the system"<<endl<<endl; cout<<" ====================================="<<endl<<endl; //让用户选择不同的功能 int select; cin>>select; //检测文件是否存在,若不存在则建立文件 ifstream inf; ofstream ouf; inf.open("account.txt",ios::out); if(inf==0) { inf.close(); ouf.open("account.txt"); if(ouf) cout<<"Created successfully!"<<endl; } inf.close(); ouf.close(); if(select == 1 || select == 2) { createAccount(select); } else if(select == 3) { outputMainMenu(); } else if(select = 4) {}; }
//判断用户所选择的功能并录入信息 int createAccount(int select) { //编号,姓名,密码,金额,类型,时间
system("cls"); ifstream getNumber; ofstream add;//定义文件流对象 //获取新创建账户的编号 getNumber.open("account.txt",ios::in); //number为最终编号 int number = 0; //设置六个读取文件的变量 int number1 = 0; char name1[20]; char password[20]; double money; int type; char time[10]; //生成新的账号,即检查所有已存在账户的账号,找出其中的最大值,将其加一后作为新账户的账号 while(!getNumber.eof()) { getNumber>>number1; getNumber>>name1; getNumber>>password; getNumber>>money; getNumber>>type; getNumber>>time;
if(number1 > number) { number = number1; } } number++; getNumber.close();
//让用户输入信息 add.open("account.txt",ios::app); cout<<"-------------------------"<<endl; cout<<"| Please enter your name |"<<endl; cout<<"-------------------------"<<endl; //输入姓名 char name[30]; cin>>name;
//输入密码 char password1[25]; char password2[25];
cout<<"----------------------------"<<endl; cout<<"| Please enter the password|"<<endl; cout<<"----------------------------"<<endl; cin>>password1; cout<<"-----------------------------------"<<endl; cout<<"| Please enter the password again |"<<endl; cout<<"-----------------------------------"<<endl; cin>>password2; cout<<"---------------------------------------------"<<endl; cout<<"Please enter the date by style like 20091206"<<endl; cout<<"---------------------------------------------"<<endl; cin>>time; //判断两次输入的密码是否相同 if(strcmp(password1,password2) == 0) { //若相同则将其写入文件 int money = 0; add<<number<<" "; add<<name<<" "; add<<password1<<" "<<money<<" "; add<<select<<" "<<time<<endl;
add.close(); system("cls"); //显示创建成功 cout<<"**************"<<endl; cout<<" Add Success!"<<endl; cout<<"**************"<<endl<<endl; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔"<<endl; cout<<"●Your account number is "<<number<<" ,Please remeber it!!"<<endl; cout<<"▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁"<<endl<<endl; cout<<" Please choose what you want to do"<<endl<<endl; cout<<"●1.Continue to add"<<endl<<endl; cout<<"●2.Back to main menu"<<endl<<endl; cout<<"●3.Exit the system"<<endl; //选择下一步操作 int choose; cin>>choose; switch(choose) { case 1:createNewAccount();//继续添加 break; case 2:outputMainMenu();//返回主菜单 break; case 0:;//退出系统 break; } } //若两次输入的密码不同 else { cout<<"---------------------------------------------------------------------"<<endl; cout<<"The password you enter twice is diffrent,please enter it again"<<endl; cout<<"---------------------------------------------------------------------"<<endl; add.close(); system("PAUSE"); system("cls"); createAccount(select); } return 0; } |
login.h
#pragma once #include"mainMenu.h"
int login() { system("cls"); ifstream find; cout<<"mainMenu<<login"<<endl<<endl; cout<<"--------------------------------------------------"<<endl; cout<<"▏ Please enter your account number and password▕"<<endl; cout<<"--------------------------------------------------"<<endl; cout<<" number: "; //让用户输入账号与密码 int enterNumber; cin>>enterNumber; cout<<"Password:"; char enterPassword[20]; cin>>enterPassword;
//定义读取文件的变量 bool rightPassword = false; int number1; char name1[20]; char password1[20]; double money1; int type1; char time1[10]; //寻找用户的账户 find.open("account.txt",ios::in); while(!find.eof()) { find>>number1; find>>name1; find>>password1; find>>money1; find>>type1; find>>time1; //如果姓名和密码均相同 if(number1 == enterNumber && strcmp(password1,enterPassword) == 0) { rightPassword = true; break; } } find.close(); //登录成功 if(rightPassword == true) { system("cls"); cout<<endl<<"**************"<<endl; cout<<"login success!"<<endl; cout<<"**************"<<endl; return number1; } //登陆失败,用户名或密码错误 else { cout<<"--------------------------------------------"<<endl; cout<<"error name or password,Please enter it again!"<<endl; cout<<"--------------------------------------------"<<endl<<endl; return 0; } } |
checkBanlance.h
#pragma once #include "login.h" #include"mainMenu.h"
void checkBanlance() { system("cls"); cout<<"main menu << check banlance"<<endl<<endl; cout<<"▔▔▔▔▔▔▔▔▔▔▔▔▔"<<endl; cout<<"| Please login at first |"<<endl; cout<<"▁▁▁▁▁▁▁▁▁▁▁▁▁"<<endl; Sleep(1000);
int number; char name[20]; char password[20]; double money; int type; char time[10];
ifstream check;
//定义错误次数 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(); } check.open("account.txt",ios::in); //寻找已知账号的账户信息并输出 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;
while(!check.eof()) { check>>number; check>>name; check>>password; check>>money; check>>type; check>>time;
if(number == enterNumber) { cout<<left; cout<<setw(10)<<number<<setw(15)<<name<<setw(15)<<password; cout<<setw(10)<<money<<setw(10)<<type<<setw(15)<<time<<endl; break; } } check.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 {} } |
changePassword.h
#pragma once #include "login.h" #include"mainMenu.h"
void changePassword() { system("cls"); cout<<"mainmenu << change"<<endl<<endl; cout<<"-----------------------------"<<endl; cout<<"Please login at first"<<endl; cout<<"-----------------------------"<<endl; Sleep(1000);
ifstream change1; ofstream change2; //定义读取文件的变量 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(); } //登录成功,开始文件操作 change1.open("account.txt",ios::in); change2.open("account1.txt",ios::out); char newPassword[20]; cout<<"please enter the new password"<<endl; cin>>newPassword; change1>>number; change1>>name; change1>>password; change1>>money; change1>>type; change1>>time;
while(!change1.eof()) { //如果number == enterNumber,即找到该账户,则不将该账户的信息写入新文件 if(number == enterNumber) { change2<<number<<" "; change2<<name<<" "; change2<<newPassword<<" "; change2<<money<<" "; change2<<type<<" "; change2<<time<<endl; } else { //如果不是用户所找的账户,则不加改动的写入新文件 change2<<number<<" "; change2<<name<<" "; change2<<password<<" "; change2<<money<<" "; change2<<type<<" "; change2<<time<<endl;
} //读取下一个用户的数据,直到找到该用户的账户 change1>>number; change1>>name; change1>>password; change1>>money; change1>>type; change1>>time; } //新文件名为account1.txt,原文件名为account.txt char fileName[] = "account.txt"; char fileName1[] = "account1.txt"; change1.close(); change2.close(); //将原文件删除,同时把新文件名字由account1.txt改为account.txt remove(fileName); rename(fileName1,fileName); //注销成功,让用户选择下一步操作 cout<<"*******************"<<endl; cout<<"* Change 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 change password"<<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) { changePassword(); }
} |