主要实现:1.开户 2.销户 3.存款 4.取款 5.查询 7.转账 主要就是用的是vector容器 还有实现一个银行登陆代码三次登陆失败,开始倒计时。
总结:理解也是比较的浅显因为就用了个迭代器在没啥了 然后就是加了一点点的文件操作,希望各路大佬前来指正!!!虽然这个小项目做完了但是还是没有很清晰的理解面向对象的编程,脑子里面还是原来的面向过程,继续努力吧
两个类 用户类
#pragma once
#include <iostream>
#include <vector>
#include <iostream>
#include <time.h>
#include <fstream>
using namespace std;
class Host {
public:
Host();
vector<Host>v1;
void WriteHost(vector<Host>& v1);
void ReadHost(vector<Host>& v1);
void SetHost(vector<Host>& v1);
void ShowHost(vector<Host>& v1);
void DisplayHost(vector<Host>& v1);
void DeletHost(vector<Host>& v1);
void TakeHost(vector<Host>& v1);
void SaveHost(vector<Host>& v1);
void Transfer(vector<Host>& v1);
~Host();
private:
long int id;
string name;
int money;
string idcard;
string ad;
};
菜单类
#pragma once
#include<iostream>
using namespace std;
#include <windows.h>
#define TOP 100
#define USERNAME "芜湖xatu"
#define PASSWD "20060723"
#define NUM 32
class MyMenu {
public:
MyMenu();
void Menu();
void BankMenu();
void CutDown(int times);
void Lock(int a);
int Login();
~MyMenu();
};
用户函数
#include"Host.h"
Host::Host()
{
id = id;
name = name;
money = money;
idcard = idcard;
ad = ad;
}
void Host::WriteHost(vector<Host>& v1)
{
ofstream f1;
f1.open("HostData.txt", ios::out);
if (!f1) {
cout << "文件打开失败" << endl;
exit(1);
}
f1 << " 账号 姓名 存款 ID 地址 " << endl;
vector<Host>::iterator it = v1.begin();
for (; it != v1.end(); it++)
{
f1 << (*it).id << " " << (*it).name << " " << (*it).money << " " << (*it).idcard << " " << (*it).ad << " " << endl;
}
f1.close();
}
void Host::ReadHost(vector<Host>& v1)
{
ifstream f1;
f1.open("HostData.txt", ios::in);
if (!f1) {
cout << "文件打开失败" << endl;
exit(1);
}
vector<Host>::iterator it = v1.begin();
for (; it != v1.end(); it++)
{
f1 >> (*it).id >> (*it).name >> (*it).money >> (*it).idcard >> (*it).ad;
}
f1.close();
}
void Host::SetHost(vector<Host>&v1)
{
Host h;
int a;
printf("你的银行账号:");
a = 10000 + (rand() % 100);
h.id = a;
cout << h.id<<endl;
printf("请输入账户姓名:");
cin >> h.name;
printf("请输入目前存款:");
cin >> h.money;
printf("请输入身份证号:");
cin >> h.idcard;
printf("请输入地址:");
cin >> h.ad;
v1.push_back(h);
}
void Host::ShowHost(vector<Host>& v1)
{
vector<Host>::iterator it = v1.begin();
cout << "账号 姓名 存款 ID 地址" << endl;
for (; it != v1.end(); it++)
{
cout << (*it).id << " " << (*it).name << " " << (*it).money << " " << (*it).idcard << " "<<(*it).ad << " " << endl;
}
}
void Host::DisplayHost(vector<Host>& v1)
{
long int id;
int flag = 0;
int falt = 0;
while (!flag) {
if (falt) { cout << "输入有误请重新输入" << endl; }
else if (!falt) { cout << "请输入你的账号:" << endl; }
cin >> id;
vector<Host>::iterator it = v1.begin();
for (; it != v1.end(); it++,falt++)
{
if ((*it).id == id)
{
cout << "账号 姓名 存款 ID 地址" << endl;
cout << (*it).id << " " << (*it).name << " " << (*it).money << " " << (*it).idcard << " " << (*it).ad << " " << endl;
flag = 1;
break;
}
}
}
}
void Host::DeletHost(vector<Host>& v1)
{
long int id;
int flag = 0;
int falt = 0;
while (!flag) {
if (falt) { cout << "输入有误请重新输入" << endl; }
else if (!falt) { cout << "请输入你的账号:" << endl; }
cin >> id;
vector<Host>::iterator it = v1.begin();
for (; it != v1.end(); it++,falt++)
{
if ((*it).id == id)
{
cout << "账号 姓名 存款 ID 地址" << endl;
v1.erase(it);
flag = 1;
break;
}
}
}
}
void Host::TakeHost(vector<Host>& v1)
{
long int id;
int flag1 = 0;
int falt = 0;
while (!flag1) {
if (falt) { cout << "输入有误请重新输入" << endl; }
else if (!falt) { cout << "请输入你的账号:" << endl; }
cin >> id;
vector<Host>::iterator it = v1.begin();
for (; it != v1.end(); it++,falt++)
{
if ((*it).id == id)
{
int temp = 0;
int flag = 0;
cout << "现在账户存款为:" << (*it).money << endl;
cout << "取多少钱呢:" << endl;
while (!flag) {
cin >> temp;
if (temp > (*it).money) {
cout << "不好意思你的账户没有这么多钱!" << endl;
cout << "取多少钱呢:" << endl;
}
else {
temp = (*it).money - temp;
(*it).money = temp;
cout << "现在账户存款为:" << (*it).money << endl;
flag = 1;
flag1 = 1;
}
}
}
}
}
}
void Host::SaveHost(vector<Host>& v1)
{
long int id;
int flag = 0;
int falt = 0;
while (!flag) {
if (falt) { cout << "输入有误请重新输入" << endl; }
else if (!falt) { cout << "请输入你的账号:" << endl; }
cin >> id;
vector<Host>::iterator it = v1.begin();
for (; it != v1.end(); it++,falt++)
{
if ((*it).id == id)
{
int temp = 0;
cout << "存多少钱呢:" << endl;
cin >> temp;
temp = (*it).money + temp;
(*it).money = temp;
cout << "现在账户存款为:" << (*it).money << endl;
flag = 1;
break;
}
}
}
}
void Host::Transfer(vector<Host>& v1)
{
long int id;
int flag1 = 0;
int falt = 0;
int falt1 = 0;
int MyTemp = 0;
while (!flag1) {
if (falt) { cout << "输入有误请重新输入" << endl; }
else if (!falt) { cout << "请输入你转出的账号:" << endl; }
cin >> id;
vector<Host>::iterator it = v1.begin();
for (; it != v1.end(); it++, falt++)
{
if ((*it).id == id)
{
int temp = 0;
int flag = 0;
cout << "现在账户存款为:" << (*it).money << endl;
cout << "转多少钱呢:" << endl;
while (!flag) {
cin >> temp;
MyTemp = temp;
if (temp > (*it).money) {
cout << "不好意思你的账户没有这么多钱!" << endl;
cout << "转账多少钱呢:" << endl;
}
else {
temp = (*it).money - temp;
(*it).money = temp;
cout << "现在账户存款为:" << (*it).money << endl;
flag = 1;
}
}
}
}
if (falt1) { cout << "输入有误请重新输入" << endl; }
else if (!falt1) { cout << "请输入你转入账号:" << endl; }
cin >> id;
vector<Host>::iterator Myit = v1.begin();
for (; Myit != v1.end(); Myit++, falt1++)
{
if ((*Myit).id == id)
{
MyTemp = (*Myit).money + MyTemp;
(*Myit).money = MyTemp;
cout << "现在账户存款为:" << (*Myit).money << endl;
flag1 = 1;
break;
}
}
}
}
Host::~Host(){}
菜单函数
#include"MyMenu.h"
MyMenu::MyMenu() {}
void MyMenu::Menu() {
cout << "**************************" << endl;
cout << "* 欢迎使用银行管理系统 *" << endl;
cout << "* *" << endl;
cout << "* 1. 用户登录 *" << endl;
cout << "* *" << endl;
cout << "* 2. 退出登陆 *" << endl;
cout << "* *" << endl;
cout << "**************************" << endl;
}
void MyMenu::BankMenu() {
cout << "**************************" << endl;
cout << "* 1. 开户 *" << endl;
cout << "* 2. 销户 *" << endl;
cout << "* 3. 存款 *" << endl;
cout << "* 4. 取款 *" << endl;
cout << "* 5. 查询 *" << endl;
cout << "* 6. 已有账户 *" << endl;
cout << "* 7. 转账 *" << endl;
cout << "* 8. 退出 *" << endl;
cout << "**************************" << endl;
}
void MyMenu::CutDown(int times) {
while (times) {
printf("你的系统已经被锁住,剩余------> %2dS \r", times);
times--;
Sleep(1000);
}
cout << "\n" << endl;
}
void MyMenu::Lock(int a) {
switch (a) {
case 1:
CutDown(30);
break;
case 2:
CutDown(60);
break;
case 3:
CutDown(120);
break;
default:
break;
}
}
int MyMenu::Login() {
int count = 3;
int lock_count = 0;
char name[NUM];
char pswd[NUM];
do {
name[0] = '\0';
pswd[0] = '\0';
cout << "请输入用户名-> " << endl;
cin >> name;
cout << "请输入你的密码->" << endl;
cin >> pswd;
if (0 == strcmp(name, USERNAME) && 0 == strcmp(pswd, PASSWD)) {
return 1;
}
count--;
if (count <= 0) {
lock_count++;
Lock(lock_count);
count = 3;
}
else {
cout << "认证失败,你还有" << count << "次机会!\n" << endl;
}
} while (1);
return 0;
}
MyMenu::~MyMenu(){}
主函数
#include "MyMenu.h"
#include"Host.h"
void Select() {
MyMenu mm;
Host h;
vector<Host>v1;
int choice = 0;
int flag = 0;
MyThis:
while (!flag) {
mm.BankMenu();
cout << "请用户输入选择->" << endl;
cin >> choice;
switch (choice) {
case 1:
h.SetHost(v1);
cout << "你创建的账户如下" << endl;
h.WriteHost(v1);
h.ShowHost(v1);
break;
case 2:
h.DeletHost(v1);
cout << "你创建的账户如下" << endl;
h.WriteHost(v1);
h.ShowHost(v1);
break;
case 3:
h.SaveHost(v1);
h.WriteHost(v1);
break;
case 4:
h.TakeHost(v1);
h.WriteHost(v1);
break;
case 5:
h.DisplayHost(v1);
break;
case 6:
h.ReadHost(v1);
h.ShowHost(v1);
break;
case 7:
h.Transfer(v1);
h.WriteHost(v1);
h.ShowHost(v1);
break;
case 8:
flag = 1;
break;
default:
cout << "输入非法!重新选择!" << endl;
goto MyThis;
}
}
}
int main(){
MyMenu mm;
mm.Menu();
int choice = 0;
int flag = 0;
cout << "请用户输入选择->" << endl;
MyThis:
cin >> choice;
while (!flag) {
switch (choice) {
case 1:
mm.Login();
system("cls");
Select();
flag = 1;
break;
case 2:
flag = 1;
break;
default:
cout << "输入非法!重新选择!" << endl;
goto MyThis;
}
}
cout << "再见" << endl;
}