📅  最后修改于: 2023-12-03 14:51:15.118000             🧑  作者: Mango
在C++中使用文件处理可以为ATM机的数据存储和读取提供很好的解决方案。这种方法不仅能够提高程序的可读性和可维护性,还能实现数据的长期保存,使得ATM机可以随时访问以前的交易记录。
在C++中,可以用fstream
头文件中的类实现对文件的读写操作。该类具有open()
和close()
成员函数,可以打开和关闭文件。open()
函数需要传入文件名和打开方式,打开方式可以是输入、输出或输入输出等。下面是一个例子:
#include <fstream>
using namespace std;
int main() {
fstream file;
file.open("file.txt", ios::in | ios::out | ios::app);
// some code
file.close();
return 0;
}
下面是一个使用了文件处理的ATM程序样例,使用了account
类表示用户账户,transaction
类表示交易记录,ATM
类表示ATM机。其中,ATM
类中利用了文件处理来读取和写入用户信息和交易记录。
account
类下面是account
类的头文件和实现:
// account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <string>
class account {
public:
account(const std::string& username, const std::string& password, double balance);
std::string getUsername() const;
std::string getPassword() const;
double getBalance() const;
void changePassword(const std::string& newPassword);
void changeBalance(double amount);
private:
std::string username_;
std::string password_;
double balance_;
};
#endif
// account.cpp
#include "account.h"
using namespace std;
account::account(const string& username, const string& password, double balance)
: username_(username), password_(password), balance_(balance) {}
string account::getUsername() const { return username_; }
string account::getPassword() const { return password_; }
double account::getBalance() const { return balance_; }
void account::changePassword(const string& newPassword) { password_ = newPassword; }
void account::changeBalance(double amount) { balance_ += amount; }
transaction
类下面是transaction
类的头文件和实现:
// transaction.h
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <string>
class transaction {
public:
transaction(const std::string& username, double amount, const std::string& type);
std::string getUsername() const;
double getAmount() const;
std::string getType() const;
private:
std::string username_;
double amount_;
std::string type_;
};
#endif
// transaction.cpp
#include "transaction.h"
using namespace std;
transaction::transaction(const string& username, double amount, const string& type)
: username_(username), amount_(amount), type_(type) {}
string transaction::getUsername() const { return username_; }
double transaction::getAmount() const { return amount_; }
string transaction::getType() const { return type_; }
ATM
类下面是ATM
类的头文件和实现:
// atm.h
#ifndef ATM_H
#define ATM_H
#include "account.h"
#include "transaction.h"
#include <string>
#include <vector>
class ATM {
public:
ATM();
bool login(const std::string& username, const std::string& password);
void logout();
double checkBalance() const;
void deposit(double amount);
void withdraw(double amount);
private:
std::vector<account> accounts_;
std::vector<transaction> transactions_;
int currentUser_;
bool loggedIn_;
void readData();
void writeData() const;
};
#endif
// atm.cpp
#include "atm.h"
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
ATM::ATM() : accounts_(), transactions_(), currentUser_(-1), loggedIn_(false) {
readData();
}
bool ATM::login(const string& username, const string& password) {
for (size_t i = 0; i < accounts_.size(); ++i) {
if (accounts_[i].getUsername() == username && accounts_[i].getPassword() == password) {
currentUser_ = i;
loggedIn_ = true;
transactions_.emplace_back(username, 0, "Log in");
return true;
}
}
return false;
}
void ATM::logout() {
currentUser_ = -1;
loggedIn_ = false;
transactions_.emplace_back(accounts_[currentUser_].getUsername(), 0, "Log out");
}
double ATM::checkBalance() const {
if (loggedIn_) {
return accounts_[currentUser_].getBalance();
}
else {
return 0.0;
}
}
void ATM::deposit(double amount) {
if (loggedIn_) {
accounts_[currentUser_].changeBalance(amount);
transactions_.emplace_back(accounts_[currentUser_].getUsername(), amount, "Deposit");
writeData();
}
}
void ATM::withdraw(double amount) {
if (loggedIn_ && amount <= accounts_[currentUser_].getBalance()) {
accounts_[currentUser_].changeBalance(-amount);
transactions_.emplace_back(accounts_[currentUser_].getUsername(), -amount, "Withdraw");
writeData();
}
}
void ATM::readData() {
ifstream accountFile("accounts.dat");
if (!accountFile) {
cerr << "Could not open account file" << endl;
exit(EXIT_FAILURE);
}
string username, password;
double balance;
while (accountFile >> username >> password >> balance) {
accounts_.emplace_back(username, password, balance);
}
accountFile.close();
ifstream transactionFile("transactions.dat");
if (!transactionFile) {
cerr << "Could not open transaction file" << endl;
exit(EXIT_FAILURE);
}
string type;
while (transactionFile >> username >> balance >> type) {
transactions_.emplace_back(username, balance, type);
}
transactionFile.close();
}
void ATM::writeData() const {
ofstream accountFile("accounts.dat");
if (!accountFile) {
cerr << "Could not open account file" << endl;
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < accounts_.size(); ++i) {
accountFile << accounts_[i].getUsername() << ' ' << accounts_[i].getPassword() << ' '
<< fixed << setprecision(2) << accounts_[i].getBalance() << endl;
}
accountFile.close();
ofstream transactionFile("transactions.dat");
if (!transactionFile) {
cerr << "Could not open transaction file" << endl;
exit(EXIT_FAILURE);
}
for (size_t i = 0; i < transactions_.size(); ++i) {
transactionFile << transactions_[i].getUsername() << ' ' << fixed << setprecision(2)
<< transactions_[i].getAmount() << ' ' << transactions_[i].getType() << endl;
}
transactionFile.close();
}
下面是主程序的内容:
#include "atm.h"
#include <iostream>
using namespace std;
int main() {
ATM atm;
string username, password;
double amount;
int choice;
bool done = false;
do {
cout << "1. Login\n2. Check balance\n3. Deposit\n4. Withdraw\n5. Logout\n6. Exit\nYour choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Username: ";
cin >> username;
cout << "Password: ";
cin >> password;
if (atm.login(username, password)) {
cout << "Login successful." << endl;
}
else {
cout << "Login failed." << endl;
}
break;
case 2:
if (atm.checkBalance() > 0) {
cout << "Your balance is: " << atm.checkBalance() << endl;
}
else {
cout << "Please log in first." << endl;
}
break;
case 3:
if (atm.checkBalance() > 0) {
cout << "Amount to deposit: ";
cin >> amount;
atm.deposit(amount);
cout << "Deposit successful." << endl;
}
else {
cout << "Please log in first." << endl;
}
break;
case 4:
if (atm.checkBalance() > 0) {
cout << "Amount to withdraw: ";
cin >> amount;
atm.withdraw(amount);
cout << "Withdrawal successful." << endl;
}
else {
cout << "Please log in first." << endl;
}
break;
case 5:
atm.logout();
cout << "Logout successful." << endl;
break;
case 6:
done = true;
break;
default:
cout << "Invalid choice." << endl;
break;
}
} while (!done);
return 0;
}
使用文件处理可以为C++程序提供数据存储和读取的解决方案。在ATM程序中,我们可以将用户信息和交易记录保存在文件中,实现交易记录的长期保存和随时访问。