简易银行卡系统C++版本
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<string> 3 #include <stdio.h> 4 #include<conio.h> 5 using namespace std; 6 int N=10000; //设置全局变量,这里N不是很安全,需要重新考虑 7 8 //顾客信息,包括顾客名字,身份证号,密码,账户余额! 9 struct customer 10 { 11 string name; //用户名,在注册的时候需要填写的 12 int IDcard; //用户身份证号,在注册的时候需要填写的 可以随便填写,或者是做格式检查,不管了~ 13 int password; //用户密码,六位数 14 double AmountOfMoney; //账户余额 15 int LoginNumber; //账户,登陆的时候用的 16 customer *Next; //指向下一位 17 string admin; //管理员账户登录账号 18 int Apassword; //管理员账户登录密码 19 bool flag; 20 customer() 21 { 22 admin=admin; 23 Apassword=10086; 24 } 25 }; 26 27 28 //customer *head=NULL; 29 30 class BankManager 31 { 32 private: 33 customer *head; //链表头指针 34 public: 35 BankManager();//构造函数 36 customer* login(); 37 void CreateAccount(); 38 bool isLoginNumber(int loginnumber,int password); //判断账号是否存在 39 int welcome2(); 40 int welcome1(); 41 void manager(); //管理员账户 42 void managerChangePassword(); 43 bool managerWelcome(); 44 int MainManagerWelcome(); 45 void savemoney(customer* L); 46 void takemoney(customer* L); 47 bool exchange(customer* p,double money); //只能从已登录账号p向其他账号转账 48 void destory(customer* p); 49 void changePassword(customer *p); 50 customer* freezeaccountByID(); 51 customer* freezeaccountByLoginNumber(); 52 void print(customer* p); 53 void freezeaccount(customer *p); 54 customer *lookforAccount(); 55 void reuse(customer *p); 56 void ReturnToWelcome1(); 57 bool CheckOut(string name,int Idcard); //检查账号是否已经存在 58 }; //必须加分号 59 60 void BankManager::freezeaccount(customer *p) 61 { 62 p->flag=true; 63 cout<<"冻结成功!"<<endl; 64 } 65 66 customer *BankManager::lookforAccount() 67 { 68 cout<<"请输入要查找的账户和密码:"<<endl; 69 int loginnumber; 70 int password; 71 cin>>loginnumber; 72 cin>>password; 73 customer *p; 74 p=head; 75 while (p->LoginNumber!=loginnumber) 76 { 77 p=p->Next; 78 } 79 if (p->password==password) 80 { 81 return p; 82 } 83 else 84 { 85 cout<<"您输入的账户和密码不匹配!"<<endl; 86 exit(1); 87 } 88 } 89 90 bool BankManager::CheckOut(string name,int Idcard) //判断账号是否已经存在 91 { 92 customer *p; 93 p=head; 94 while(p->Next!=NULL) 95 { 96 p=p->Next; 97 if(p->IDcard==Idcard) 98 { 99 return false; 100 } 101 int len; 102 len=name.length(); 103 for (int i=0;i<len;i++) //i<=len 会出现字符越界!!! 104 { 105 if (p->name[i]==name[i]) 106 { 107 return false; 108 } 109 } 110 } 111 return true; 112 } 113 114 int BankManager::welcome2 () 115 { 116 cout<<"What can we do for you?"<<endl; 117 cout<<endl; 118 for(int i=0;i<80;i++) 119 cout<<"*"; 120 cout<<endl; 121 122 cout<<"1 管理员登陆"<<endl; 123 cout<<"2 用户登陆"<<endl; 124 cout<<"3 用户注册"<<endl; 125 cout<<"4 退出"<<endl; 126 for(int i=0;i<80;i++) 127 cout<<"*"; 128 cout<<endl; 129 int j; 130 cout<<"请输入1,2,3或者4"<<endl; 131 cin>>j; 132 return j; 133 } 134 135 bool BankManager::managerWelcome() 136 { 137 cout<<"请输入管理员账户和密码:"<<endl; 138 139 string administer="abcde"; 140 int Apassword1; 141 customer *p; 142 p=head; 143 /** 144 string administer; //如果没有初始化,则会出现string 类型数组越界,这个问题怎么解决?! 145 for(int i=0;i<5;i++) 146 { 147 cin>>administer[i]; 148 cout<<"TODO"<<endl; 149 if (administer[i]!=p->admin[i]) 150 { 151 return false; 152 } 153 } 154 **/ 155 for (int j=0;j<5;j++) 156 { 157 cin>>administer[j]; 158 } 159 while(p->admin!=administer) 160 return false; 161 cin>>Apassword1; 162 if (Apassword1!=p->Apassword) 163 { 164 return false; 165 } 166 return true; 167 } 168 169 void BankManager::managerChangePassword() 170 { 171 cout<<"请输入要重置的5位的用户名和5位的密码:"<<endl; 172 customer *p; 173 p=head; 174 string ad="admin"; 175 int apassword; 176 for (int i=0;i<5;i++) 177 { 178 cin>>ad[i]; 179 } 180 cin>>apassword; 181 p->admin=ad; 182 p->Apassword=apassword; 183 cout<<"用户名和密码修改成功!"<<endl; 184 } 185 186 int BankManager::MainManagerWelcome() 187 { 188 cout<<"Welcome ! Admin!"<<endl; 189 cout<<"What can i do for you?"<<endl; 190 for (int i=0;i<80;i++) 191 { 192 cout<<"*"; 193 } 194 cout<<endl; 195 cout<<"1 修改管理员账户和密码"<<endl; 196 cout<<"2 冻结账户"<<endl; 197 cout<<"3 解冻结账户"<<endl; 198 cout<<"4 找回账号和密码"<<endl; 199 cout<<"5 退出管理员账户"<<endl; 200 for (int i=0;i<80;i++) 201 { 202 cout<<"*"; 203 } 204 cout<<endl; 205 cout<<"请输入你的选择!"<<endl; 206 int logo; 207 cin>>logo; 208 return logo; 209 } 210 211 void BankManager::manager() //管理员账户提供查询工作,可以通过用户名查询,可以通过身份证号查询,可以通过登陆账号查询,可以做找回账号和冻结的工作 212 { 213 //冻结和解冻结工作只能通过管理员来进行 214 } 215 216 BankManager::BankManager() 217 { 218 //customer *head; //构造函数的重复定义,导致执行不下去! 219 head=new customer; 220 head->flag=false; 221 head->Next=NULL; 222 head->AmountOfMoney=0; 223 head->admin="admin"; 224 head->Apassword=10086; 225 } 226 227 void BankManager::CreateAccount() 228 { 229 string s; 230 int Idcard; 231 int password,password1; //两次输入密码 232 customer *L,*p; 233 L=new customer; 234 p=new customer; 235 p=head; 236 //L=(customer *)malloc(sizeof(struct customer)); 237 cout<<"please input your name:"<<endl; 238 //for(int i=0;i<20;i++) //这里关于字符串输入可以改进,应该写成动态增长的!说不定有人的名字很臭很长 239 // cin>>s[i]; 240 cin>>s; 241 cout<<"please input your IDcard:"<<endl; //判断身份证是18位的 242 cin>>Idcard; 243 cout<<"please set a password:"<<endl; 244 cin>>password; 245 cout<<"please input the password again:"<<endl; 246 cin>>password1; 247 while(password!=password1) //两次输入的密码不相同 248 { 249 cout<<"The password are not same!"<<endl; 250 cout<<"please set a password:"<<endl; 251 cin>>password; 252 cout<<"please input the password again:"<<endl; 253 cin>>password1; 254 } 255 while(CheckOut(s,Idcard)==false) 256 { 257 cout<<"用户或者身份证号已经注册过,请换一个!"<<endl; //可以考虑如果一个用户或者身份证号可以开多个账号情况 258 cout<<"please input your name:"<<endl; 259 cin>>s; 260 cout<<"please input your Idcard:"<<endl; 261 cin>>Idcard; 262 } 263 if(password==password1) 264 { 265 cout<<"Congratulations! you have a bank account,your login number is :"<<N<<endl; 266 L->name=s; 267 L->IDcard=Idcard; 268 L->password=password; 269 L->flag=false; 270 L->AmountOfMoney=0; 271 L->LoginNumber=N; //随机分配卡号 272 while(p->Next!=NULL) 273 p=p->Next; 274 p->Next=L; 275 L->Next=NULL; 276 N++; 277 print(L); 278 } 279 /** 280 cout<<"按任意键返回主菜单"<<endl; 281 _getch(); 282 welcome1(); 283 **/ 284 } 285 286 customer* BankManager::login() //注册遇到了相同的账户,那么要返回的! 287 { 288 int loginnumber,password; 289 cout<<"Please input your login number and password:"<<endl; 290 cin>>loginnumber; 291 cin>>password; 292 customer *p; 293 p=head; 294 int k =0; 295 while(isLoginNumber(loginnumber,password)==false) 296 { 297 cout<<"The login number you input is not igsit!"; 298 cout<<"Please input your login number and password:"<<endl; 299 cin>>loginnumber; 300 cin>>password; 301 k++; 302 if(k>3) //当错误次数大于3次,让使用者推出 303 exit(1); 304 } 305 while(p->Next!=NULL&&p->LoginNumber!=loginnumber) 306 p=p->Next; //找到注册账号 307 cout<<"Welcome!"<<endl; 308 return p; 309 } 310 311 bool BankManager::isLoginNumber(int loginnumber,int password) //判断账户是否存在 312 { 313 customer *p; 314 p=head; 315 while(p->Next!=NULL) 316 { 317 p=p->Next; 318 if((p->LoginNumber==loginnumber)&&(p->password==password)) 319 return true; 320 } 321 return false; //不存在这个账户 322 } 323 324 int BankManager::welcome1() 325 { 326 cout<<"Please select your choice:"<<endl; 327 for(int i=0;i<80;i++) 328 { 329 cout<<"*"; 330 } 331 cout<<"1 存款"<<endl; 332 cout<<"2 取款"<<endl; 333 cout<<"3 转账"<<endl; 334 cout<<"4 销户"<<endl; 335 cout<<"5 修改密码"<<endl; 336 cout<<"6 查看"<<endl; 337 cout<<"7 退出"<<endl; 338 for(int i=0;i<80;i++) 339 { 340 cout<<"*"; 341 } 342 int j; 343 cout<<"Please input your choice:"<<endl; 344 cin>>j; 345 return j; 346 } 347 348 void BankManager::savemoney(customer* L) 349 { 350 cout<<"please input the amount of money you want to save:"<<endl; 351 double money; 352 cin>>money; 353 if(money>0) 354 { 355 L->AmountOfMoney=L->AmountOfMoney+money; 356 cout<<"存款完成!"<<endl; 357 } 358 else 359 { 360 cout<<"别逗了,你个穷鬼!"<<endl; 361 } 362 cout<<endl; 363 /** 364 cout<<"完成存款,按任意键返回主菜单:"<<endl; 365 _getch(); 366 welcome1(); 367 **/ 368 } 369 370 void BankManager::takemoney (customer* L) 371 { 372 cout<<"Please input the amount you want to take off:"<<endl; 373 int amount=0; 374 cin>>amount; 375 if(amount>=0) 376 { 377 L->AmountOfMoney=L->AmountOfMoney-amount; 378 cout<<"取款完成"<<endl; 379 } 380 else 381 { 382 cout<<"余额不足,不允许透支!"<<endl; 383 } 384 cout<<endl; 385 } 386 387 bool BankManager::exchange(customer* p,double money) //在子进程运行中,没有任何安全机制,感觉非常的怪异啊 388 { 389 customer *L; 390 L=head; 391 cout<<"请输入要转账的目标账户:"<<endl; 392 int loginnumber; 393 cin>>loginnumber; 394 //查找目标账户 395 while(L->LoginNumber!=loginnumber) 396 { 397 L=L->Next; 398 } 399 if(L->LoginNumber==loginnumber) 400 { 401 p->AmountOfMoney=p->AmountOfMoney-money; 402 L->AmountOfMoney=L->AmountOfMoney+money; 403 cout<<"转账成功!"<<endl; 404 return true; 405 } 406 else 407 { 408 cout<<"Can not find the goal account!"<<endl; 409 return false; 410 } 411 } 412 413 void BankManager::destory(customer *p) 414 { 415 customer *p1; 416 p1=head; 417 while(p1->Next!=p) 418 p1=p1->Next; 419 p1->Next=p->Next; 420 delete p; 421 } 422 423 void BankManager::changePassword(customer *p) 424 { 425 int newpassword,newpassword2; 426 cout<<"Please input the new password:"<<endl; 427 cin>>newpassword; 428 cout<<"Please input the new password again:"<<endl; 429 cin>>newpassword2; 430 while(newpassword!=newpassword2) 431 { 432 cout<<"The password you input is not same!"; 433 cout<<"Please input the new password:"<<endl; 434 cin>>newpassword; 435 cout<<"Please input the new password again:"<<endl; 436 cin>>newpassword2; 437 } 438 if(newpassword==newpassword2) 439 { 440 p->password=newpassword; 441 } 442 } 443 444 void BankManager::print(customer *p) 445 { 446 for(int i=0;i<80;i++) 447 cout<<"*"; 448 cout<<endl; 449 cout<<"The money in your account is: "<<p->AmountOfMoney<<endl; 450 cout<<"Your IDcard is: "<<p->IDcard<<endl; 451 cout<<"Your login number is: "<<p->LoginNumber<<endl; 452 cout<<"Your name is: "<<p->name<<endl; 453 for(int i=0;i<80;i++) 454 { 455 cout<<"*"; 456 } 457 cout<<endl; 458 } 459 460 customer* BankManager::freezeaccountByID() 461 { 462 cout<<"Please input your IDcard:"; 463 double IDcard; 464 cin>>IDcard; 465 customer *p; 466 p=head; 467 while (p->IDcard!=IDcard) 468 { 469 p=p->Next; 470 } 471 if (p->Next==NULL&&p->IDcard!=IDcard) 472 { 473 cout<<"您要冻结的账户不存在"<<endl; 474 exit(1); 475 } 476 return p; 477 } 478 479 customer* BankManager::freezeaccountByLoginNumber() 480 { 481 cout<<"请输入你的登陆账号"<<endl; 482 int loginnumber; 483 cin>>loginnumber; 484 customer *p; 485 p=head; 486 while(p->LoginNumber!=loginnumber) 487 { 488 p=p->Next; 489 } 490 if (p->LoginNumber!=loginnumber&&p->Next==NULL) 491 { 492 cout<<"你要查找的账号不存在!"<<endl; 493 exit(1); 494 } 495 return p; 496 } 497 498 void BankManager::reuse(customer *p) 499 { 500 p->flag=false; 501 cout<<"解冻结成功!"<<endl; 502 } 503 504 int main() 505 { 506 int n; 507 BankManager a; 508 cout<<"顾客您好,冻结账户和解冻结账户,请找管理员!"<<endl; 509 n=a.welcome2(); 510 while(1) 511 { 512 if(n==1) 513 { 514 int count=0; 515 while (a.managerWelcome()==false&&count<3) 516 { 517 a.managerWelcome(); 518 count++; 519 } 520 if (count>=3) 521 { 522 exit(1); //防止刺探账户! 523 } 524 int logo; 525 logo=a.MainManagerWelcome(); 526 while(1) 527 { 528 if (logo==1) 529 { 530 a.managerChangePassword(); 531 } 532 else if (logo==4) 533 { 534 customer *p1; 535 customer *p2; 536 537 p1=a.freezeaccountByID(); 538 p2=a.freezeaccountByLoginNumber(); 539 540 if (p1->IDcard==p2->IDcard&&p1->LoginNumber==p2->LoginNumber) 541 { 542 cout<<"Welcome!"<<endl; 543 for (int i=0;i<80;i++) 544 { 545 cout<<"*"; 546 } 547 cout<<endl; 548 a.print(p1); //把账户打印出来 549 for (int i=0;i<80;i++) 550 { 551 cout<<"*"; 552 } 553 cout<<endl; 554 } 555 } 556 else if (logo==2) 557 { 558 customer *p1; 559 p1=a.lookforAccount(); 560 a.freezeaccount(p1); 561 } 562 else if(logo==3) 563 { 564 customer *p1; 565 p1=a.lookforAccount(); 566 a.reuse(p1); 567 } 568 else if (logo==5) 569 { 570 break; 571 } 572 else 573 cout<<"输入的选择非法!"<<endl; 574 cout<<"按任意键返回选择界面"<<endl; 575 _getch(); 576 logo=a.MainManagerWelcome(); 577 } 578 } 579 else if(n==2) 580 { 581 customer *L; 582 L=(customer *)malloc(sizeof(struct customer)); //申请一个空间 583 int m=0; 584 L=a.login(); 585 if(L->flag == false) 586 { 587 m=a.welcome1(); 588 while(1) 589 { 590 if(m==1) 591 { 592 a.savemoney(L); 593 } 594 else if(m==2) 595 { 596 a.takemoney(L); 597 } 598 else if(m==3) 599 { 600 double money; 601 cout<<"请输入转账金额:"<<endl; 602 cin>>money; 603 if (L->AmountOfMoney<money) 604 { 605 cout<<"余额不足,不能转账!"<<endl; 606 continue; 607 } 608 else 609 a.exchange(L,money); 610 } 611 else if(m==4) 612 { 613 a.destory(L); 614 //continue; //会出现强退的错误!应该是退出了if循环进入了while循环中,但是该节点已经删除,所以会出现错误! 615 break; 616 } 617 else if(m==5) 618 { 619 a.changePassword(L); 620 } 621 else if(m==6) 622 { 623 a.print(L); 624 } 625 else if(m==7) 626 { 627 break; 628 } 629 cout<<"按任意键返回选择界面"<<endl; 630 _getch(); 631 m=a.welcome1(); 632 } 633 } 634 else 635 { 636 cout<<"你的账户处于冻结中,如果想要解冻结请找管理员!"<<endl; 637 } 638 } 639 else if(n==3) 640 a.CreateAccount(); 641 else if(n==4) 642 exit(1); 643 else 644 cout<<"your input is illegal!"<<endl; 645 cout<<"按任意键返回主菜单"<<endl; 646 _getch(); 647 n=a.welcome2(); 648 } 649 return 0; 650 }
这是自己写的简易银行卡系统,基本功能可以实现,也是比较简单的!!