📅  最后修改于: 2023-12-03 14:49:48.053000             🧑  作者: Mango
本文将为您介绍如何使用C++编写一个简单的ATM管理系统,包括系统流程、实现功能以及代码示例等方面的内容。希望本文可以为C++程序员提供一些学习和编程上的帮助和启示。
ATM管理系统主要分为以下几个流程:
根据ATM管理系统的流程,我们需要实现以下功能:
下面是ATM管理系统的基本代码实现,你可以以此为基础进行更加复杂的拓展。
#include <iostream>
#include <string>
namespace ATM {
class User {
private:
std::string cardNumber;
std::string password;
double balance;
std::string name;
std::string phone;
public:
User(std::string cardNumber, std::string password, double balance, std::string name, std::string phone) :
cardNumber(cardNumber), password(password), balance(balance), name(name), phone(phone) {}
~User() {};
std::string getCardNumber() const { return cardNumber; }
std::string getPassword() const { return password; }
double getBalance() const { return balance; }
std::string getName() const { return name; }
std::string getPhone() const { return phone; }
void setCardNumber(const std::string &cardNumber) { this->cardNumber = cardNumber; }
void setPassword(const std::string &password) { this->password = password; }
void setBalance(double balance) { this->balance = balance; }
void setName(const std::string &name) { this->name = name; }
void setPhone(const std::string &phone) { this->phone = phone; }
bool validatePassword(std::string password) {
return password == this->password;
}
bool transfer(User &target, double amount) {
if (balance < amount) {
return false;
} else {
balance -= amount;
target.balance += amount;
return true;
}
}
bool withdraw(double amount) {
if (balance < amount) {
return false;
} else {
balance -= amount;
return true;
}
}
bool deposit(double amount) {
balance += amount;
return true;
}
};
class ATM {
private:
User* user;
void menu() {
std::cout << "=====ATM管理系统=====" << std::endl;
std::cout << "1. 查询余额" << std::endl;
std::cout << "2. 转账" << std::endl;
std::cout << "3. 取款" << std::endl;
std::cout << "4. 存款" << std::endl;
std::cout << "5. 修改密码" << std::endl;
std::cout << "6. 退出系统" << std::endl;
std::cout << "请输入您的选项:";
}
public:
ATM() {
user = nullptr;
}
bool login(std::string cardNumber, std::string password) {
User u = getUserInfo(cardNumber);
if (u.validatePassword(password)) {
user = new User(u.getCardNumber(), u.getPassword(), u.getBalance(), u.getName(), u.getPhone());
return true;
} else {
return false;
}
}
void logout() {
if (user != nullptr) {
saveUserInfo(*user);
delete user;
user = nullptr;
}
}
void queryBalance() {
std::cout << "当前余额:" << user->getBalance() << std::endl;
}
bool withdraw(double amount) {
bool result = user->withdraw(amount);
if (result) {
std::cout << "取款成功,当前余额:" << user->getBalance() << std::endl;
} else {
std::cout << "余额不足,取款失败" << std::endl;
}
return result;
}
bool transfer(std::string cardNumber, double amount) {
User target = getUserInfo(cardNumber);
bool result = user->transfer(target, amount);
if (result) {
std::cout << "转账成功,当前余额:" << user->getBalance() << std::endl;
} else {
std::cout << "余额不足,转账失败" << std::endl;
}
return result;
}
bool changePassword(std::string oldPassword, std::string newPassword) {
if (user->validatePassword(oldPassword)) {
user->setPassword(newPassword);
std::cout << "修改密码成功" << std::endl;
return true;
} else {
std::cout << "旧密码错误,修改密码失败" << std::endl;
return false;
}
}
bool deposit(double amount) {
bool result = user->deposit(amount);
if (result) {
std::cout << "存款成功,当前余额:" << user->getBalance() << std::endl;
} else {
std::cout << "存款失败" << std::endl;
}
return result;
}
User getUserInfo(std::string cardNumber) {
// TODO: 实现从数据库中获取用户信息
return User("0000000000000000", "123456", 1000.0, "张三", "13300000000");
}
void saveUserInfo(const User &user) {
// TODO: 实现将用户信息保存到数据库
}
void run() {
std::string cardNumber, password;
std::cout << "请输入您的银行卡号:";
std::cin >> cardNumber;
std::cout << "请输入您的密码:";
std::cin >> password;
bool success = login(cardNumber, password);
if (!success) {
std::cout << "登录失败" << std::endl;
return;
} else {
std::cout << "登录成功,欢迎您," << user->getName() << std::endl;
}
int option;
do {
menu();
std::cin >> option;
switch (option) {
case 1: {
queryBalance();
break;
}
case 2: {
std::string cardNumberTo;
double amount;
std::cout << "请输入对方银行卡号:";
std::cin >> cardNumberTo;
std::cout << "请输入转账金额:";
std::cin >> amount;
transfer(cardNumberTo, amount);
break;
}
case 3: {
double amount;
std::cout << "请输入取款金额:";
std::cin >> amount;
withdraw(amount);
break;
}
case 4: {
double amount;
std::cout << "请输入存款金额:";
std::cin >> amount;
deposit(amount);
break;
}
case 5: {
std::string oldPassword, newPassword;
std::cout << "请输入旧密码:";
std::cin >> oldPassword;
std::cout << "请输入新密码:";
std::cin >> newPassword;
changePassword(oldPassword, newPassword);
break;
}
case 6: {
std::cout << "感谢使用本系统,再见!" << std::endl;
break;
}
default: {
std::cout << "输入错误,请重新输入" << std::endl;
break;
}
}
} while (option != 6);
logout();
}
};
}
int main() {
ATM::ATM atm;
atm.run();
return 0;
}
ATM管理系统是一个比较典型的程序,涉及到了用户管理、文件操作、字符串处理等多个方面,是学习C++程序的一个比较好的练手项目。在编写代码的过程中,要注重代码规范和注释规范,提高代码的可读性和可维护性。希望大家能够通过本文学习到有用的知识和技能,不断探索和深入学习。