怎么用c++编写ATM程序?
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
#include <iostream>
using namespace std;
///////////////////////////////用户类//////////////////////////////////////
class User
{
private:
int money;
string userName;
string password;
public:
User();
~User();
User(string userName, string password);
int saveMoney(int money);// 存款
int getMoney(int money); // 取款
int checkMoney(); // 查询余额
string getUserName(); // 取得用户名
string getPassword(); // 取得密码
};
User::User()
{
money = 0;
}
User::User(string userName, string password)
{
money = 0;
this->userName = userName;
this->password = password;
}
int User::saveMoney(int money)
{
this->money += money;
return this->money;
}
int User::getMoney(int money)
{
this->money -= money;
return this->money;
}
int User::checkMoney()
{
return this->money;
}
string User::getUserName()
{
return this->userName;
}
string User::getPassword()
{
return this->password;
}
/////////////////////////////////////////////////////////////////////////
///////////////////////////////////数据库类,用来存储用户//////////////////////
class DataBase
{
private:
User *users[40]; // 默认数据可最多能存放40个用户
int userNumber; // 已注册用户的数量
public:
DataBase();
~DataBase();
bool add(User *user); // 添加新用户
User * getUser(int i); // 按照索引取得用户
User * getUser(string name); // 按照用户名取得用户
int getUserNumber(); // 取得数据库中用户的数量
};
DataBase::DataBase()
{
userNumber = 0;
}
DataBase::~DataBase()
{
delete users;
}
bool DataBase::add(User *user)
{
if(userNumber < 40 && getUser(user->getUserName()) == NULL) // 如果当前用户名在数据库中不存在
{
users[userNumber] = user;
userNumber++;
return true; // 返回true表示新用户添加成功
}
return false; // 返回false表示新用户添加失败
}
User * DataBase::getUser(int i)
{
if(i > 40 || i < 0) return NULL;
return users[i];
}
User * DataBase::getUser(string userName)
{
for(int i = 0; i < userNumber; i++)
{
User *user = getUser(i);
if(user->getUserName() == userName) return user;
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////
DataBase database;
int welcom();
// 注册
User * user_register();
User * user_login();
int exit();
int user_process();
int main()
{
int state;
int user_state;
User *user;
do
{
state = welcom();
switch(state)
{
case 1:
user = user_register(); // 用户注册
if(NULL != user)
{
cout<<"\n注册成功\n";
do
{
user_state = user_process();
switch(user_state)
{
case 1:// 存钱
{
cout<<"输入存钱金额:";
int num;
cin>>num;
cout<<"余额为:"<<user->saveMoney(num)<<endl;;
}
break;
case 2:// 取钱
{
cout<<"输入取钱金额:";
int num;
cin>>num;
cout<<"余额为:"<<user->getMoney(num)<<endl;
}
break;
case 3:// 查看余额
{
cout<<"余额为:"<<user->checkMoney()<<endl;
}
break;
case 4://返回
user_state = 0;
default: break;
}
} while(user_state);
user = NULL;
}
else cout<<"\n注册失败\n";
break;
case 2:
user = user_login(); // 用户登陆
if(NULL != user)
{
cout<<"登陆成功"<<endl;
do
{
user_state = user_process();
switch(user_state)
{
case 1:// 存钱
{
cout<<"输入存钱金额:";
int num;
cin>>num;
cout<<"余额为:"<<user->saveMoney(num)<<endl;;
}
break;
case 2:// 取钱
{
cout<<"输入取钱金额:";
int num;
cin>>num;
cout<<"余额为:"<<user->getMoney(num)<<endl;
}
break;
case 3:// 查看余额
{
cout<<"余额为:"<<user->checkMoney()<<endl;
}
break;
case 4://返回
user_state = 0;
default: break;
}
} while(user_state);
user = NULL;
}
else cout<<"\n登陆失败\n";
break;
case 3:
state = exit();
break;
default: break;
}
}
while(state);
return 0;
}
int welcom()
{
cout<<"\t\t**********欢迎使用ATM系统**********\n";
cout<<"\t\t* 1.用户注册 *\n";
cout<<"\t\t* 2.用户登录 *\n";
cout<<"\t\t* 3.退出系统 *\n";
cout<<"\t\t***********************************\n";
int num;
cin>>num;
return num;
}
// 注册
User * user_register()
{
string userName;
string password;
cout<<"请输入账号:";
cin>>userName;
if(NULL != database.getUser(userName)) // 如果用户名存在
return NULL; // 注册失败
cout<<"请输入密码:";
cin>>password;
User *user = new User(userName, password);
database.add(user);
return user; // 注册成功
}
User * user_login()
{
string userName;
string password;
cout<<"请输入账号:";
cin>>userName;
cout<<"请输入密码:";
cin>>password;
User *user = database.getUser(userName);
if(NULL != user) // 如果用户存在
if(user->getPassword() == password)
return user; // 如果密码匹配则登陆成功
return NULL; // 如果用户不存在
}
int exit()
{
return 0; // 退出系统
}
int user_process()
{
cout<<"\t\t******************************\n";
cout<<"\t\t* 请选择操作类型 *\n";
cout<<"\t\t* 1.存 钱 *\n";
cout<<"\t\t* 2.取 钱 *\n";
cout<<"\t\t* 3.查 看 余 额 *\n";
cout<<"\t\t* 4.返 回 *\n";
cout<<"\t\t******************************\n";
int num;
cin>>num;
return num;
}
using namespace std;
///////////////////////////////用户类//////////////////////////////////////
class User
{
private:
int money;
string userName;
string password;
public:
User();
~User();
User(string userName, string password);
int saveMoney(int money);// 存款
int getMoney(int money); // 取款
int checkMoney(); // 查询余额
string getUserName(); // 取得用户名
string getPassword(); // 取得密码
};
User::User()
{
money = 0;
}
User::User(string userName, string password)
{
money = 0;
this->userName = userName;
this->password = password;
}
int User::saveMoney(int money)
{
this->money += money;
return this->money;
}
int User::getMoney(int money)
{
this->money -= money;
return this->money;
}
int User::checkMoney()
{
return this->money;
}
string User::getUserName()
{
return this->userName;
}
string User::getPassword()
{
return this->password;
}
/////////////////////////////////////////////////////////////////////////
///////////////////////////////////数据库类,用来存储用户//////////////////////
class DataBase
{
private:
User *users[40]; // 默认数据可最多能存放40个用户
int userNumber; // 已注册用户的数量
public:
DataBase();
~DataBase();
bool add(User *user); // 添加新用户
User * getUser(int i); // 按照索引取得用户
User * getUser(string name); // 按照用户名取得用户
int getUserNumber(); // 取得数据库中用户的数量
};
DataBase::DataBase()
{
userNumber = 0;
}
DataBase::~DataBase()
{
delete users;
}
bool DataBase::add(User *user)
{
if(userNumber < 40 && getUser(user->getUserName()) == NULL) // 如果当前用户名在数据库中不存在
{
users[userNumber] = user;
userNumber++;
return true; // 返回true表示新用户添加成功
}
return false; // 返回false表示新用户添加失败
}
User * DataBase::getUser(int i)
{
if(i > 40 || i < 0) return NULL;
return users[i];
}
User * DataBase::getUser(string userName)
{
for(int i = 0; i < userNumber; i++)
{
User *user = getUser(i);
if(user->getUserName() == userName) return user;
}
return NULL;
}
/////////////////////////////////////////////////////////////////////////
DataBase database;
int welcom();
// 注册
User * user_register();
User * user_login();
int exit();
int user_process();
int main()
{
int state;
int user_state;
User *user;
do
{
state = welcom();
switch(state)
{
case 1:
user = user_register(); // 用户注册
if(NULL != user)
{
cout<<"\n注册成功\n";
do
{
user_state = user_process();
switch(user_state)
{
case 1:// 存钱
{
cout<<"输入存钱金额:";
int num;
cin>>num;
cout<<"余额为:"<<user->saveMoney(num)<<endl;;
}
break;
case 2:// 取钱
{
cout<<"输入取钱金额:";
int num;
cin>>num;
cout<<"余额为:"<<user->getMoney(num)<<endl;
}
break;
case 3:// 查看余额
{
cout<<"余额为:"<<user->checkMoney()<<endl;
}
break;
case 4://返回
user_state = 0;
default: break;
}
} while(user_state);
user = NULL;
}
else cout<<"\n注册失败\n";
break;
case 2:
user = user_login(); // 用户登陆
if(NULL != user)
{
cout<<"登陆成功"<<endl;
do
{
user_state = user_process();
switch(user_state)
{
case 1:// 存钱
{
cout<<"输入存钱金额:";
int num;
cin>>num;
cout<<"余额为:"<<user->saveMoney(num)<<endl;;
}
break;
case 2:// 取钱
{
cout<<"输入取钱金额:";
int num;
cin>>num;
cout<<"余额为:"<<user->getMoney(num)<<endl;
}
break;
case 3:// 查看余额
{
cout<<"余额为:"<<user->checkMoney()<<endl;
}
break;
case 4://返回
user_state = 0;
default: break;
}
} while(user_state);
user = NULL;
}
else cout<<"\n登陆失败\n";
break;
case 3:
state = exit();
break;
default: break;
}
}
while(state);
return 0;
}
int welcom()
{
cout<<"\t\t**********欢迎使用ATM系统**********\n";
cout<<"\t\t* 1.用户注册 *\n";
cout<<"\t\t* 2.用户登录 *\n";
cout<<"\t\t* 3.退出系统 *\n";
cout<<"\t\t***********************************\n";
int num;
cin>>num;
return num;
}
// 注册
User * user_register()
{
string userName;
string password;
cout<<"请输入账号:";
cin>>userName;
if(NULL != database.getUser(userName)) // 如果用户名存在
return NULL; // 注册失败
cout<<"请输入密码:";
cin>>password;
User *user = new User(userName, password);
database.add(user);
return user; // 注册成功
}
User * user_login()
{
string userName;
string password;
cout<<"请输入账号:";
cin>>userName;
cout<<"请输入密码:";
cin>>password;
User *user = database.getUser(userName);
if(NULL != user) // 如果用户存在
if(user->getPassword() == password)
return user; // 如果密码匹配则登陆成功
return NULL; // 如果用户不存在
}
int exit()
{
return 0; // 退出系统
}
int user_process()
{
cout<<"\t\t******************************\n";
cout<<"\t\t* 请选择操作类型 *\n";
cout<<"\t\t* 1.存 钱 *\n";
cout<<"\t\t* 2.取 钱 *\n";
cout<<"\t\t* 3.查 看 余 额 *\n";
cout<<"\t\t* 4.返 回 *\n";
cout<<"\t\t******************************\n";
int num;
cin>>num;
return num;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询