21xrx.com
2025-03-27 17:00:25 Thursday
文章检索 我的文章 写文章
C++ ATM机存取款系统源代码
2023-07-05 01:35:52 深夜i     --     --
C++ ATM机 存取款系统 源代码 编程

ATM机是一种方便快捷的自动取款机,通过它可以进行现金存取、转账、查询余额等操作。如果你想学习如何构建一个ATM机存取款系统,那么可以使用C++来实现。下面就是一个简单的ATM机存取款系统源代码。

首先,我们需要定义一些基本的变量和函数来帮助我们完成这个程序。例如,我们需要定义一个账户类来保存用户的信息,还需要定义一个银行类来管理所有的账户。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Account {
public:
  int accountNumber;
  string name;
  double balance;
  Account()
    accountNumber = 0;
    name = "";
    balance = 0.0;
  
  Account(int num, string n, double bal)
    accountNumber = num;
    name = n;
    balance = bal;
  
};
class Bank {
public:
  Account accounts[10];
  Bank() {
    accounts[0] = Account(12345, "Alice", 1000.0);
    accounts[1] = Account(23456, "Bob", 2000.0);
    accounts[2] = Account(34567, "Charlie", 3000.0);
    accounts[3] = Account(45678, "David", 4000.0);
    accounts[4] = Account(56789, "Eve", 5000.0);
    accounts[5] = Account(67890, "Frank", 6000.0);
    accounts[6] = Account(78901, "Grace", 7000.0);
    accounts[7] = Account(89012, "Henry", 8000.0);
    accounts[8] = Account(90123, "Iris", 9000.0);
    accounts[9] = Account(12309, "Jack", 10000.0);
  }
  Account* findAccount(int accountNumber) {
    for (int i = 0; i < 10; i++) {
      if (accounts[i].accountNumber == accountNumber) {
        return &accounts[i];
      }
    }
    return NULL;
  }
};

在银行类中,我们为了方便起见使用了假数据来模拟账户信息。当然,在实现时需要连接数据库或文件存储真实的账户数据。

然后,我们需要定义一个ATM类来处理用户的操作。它应该包括现金存取、转账和查询余额这些常见的功能。

class ATM {
public:
  Bank* bank;
  ATM(Bank* b)
    bank = b;
  
  void deposit(int accountNumber, double amount) {
    Account* account = bank->findAccount(accountNumber);
    if (account == NULL)
      cout << "Account not found." << endl;
      return;
    
    account->balance += amount;
    cout << "Deposit of $" << fixed << setprecision(2) << amount << " to account " << accountNumber << " completed." << endl;
  }
  void withdraw(int accountNumber, double amount) {
    Account* account = bank->findAccount(accountNumber);
    if (account == NULL)
      cout << "Account not found." << endl;
      return;
    
    if (account->balance < amount)
      cout << "Insufficient funds." << endl;
      return;
    
    account->balance -= amount;
    cout << "Withdrawal of $" << fixed << setprecision(2) << amount << " from account " << accountNumber << " completed." << endl;
  }
  void transfer(int fromAccount, int toAccount, double amount) {
    Account* from = bank->findAccount(fromAccount);
    Account* to = bank->findAccount(toAccount);
    if (from == NULL || to == NULL || from == to)
      cout << "Invalid transfer." << endl;
      return;
    
    if (from->balance < amount)
      cout << "Insufficient funds." << endl;
      return;
    
    from->balance -= amount;
    to->balance += amount;
    cout << "Transfer of $" << fixed << setprecision(2) << amount << " from account " << fromAccount << " to account " << toAccount << " completed." << endl;
  }
  void balanceInquiry(int accountNumber) {
    Account* account = bank->findAccount(accountNumber);
    if (account == NULL)
      cout << "Account not found." << endl;
      return;
    
    cout << "Balance of account " << accountNumber << ": $" << fixed << setprecision(2) << account->balance << endl;
  }
};

现在我们已经实现了银行和ATM机两个类,可以组合在一起来使用了。我们可以在main函数中创建一个ATM对象并让用户进行操作。

int main() {
  Bank bank;
  ATM atm(&bank);
  int choice;
  int accountNumber;
  double amount;
  int fromAccount, toAccount;
  do {
    cout << "Choose an option: " << endl;
    cout << "1. Deposit" << endl;
    cout << "2. Withdraw" << endl;
    cout << "3. Transfer" << endl;
    cout << "4. Balance inquiry" << endl;
    cout << "5. Exit" << endl;
    cin >> choice;
    switch (choice) {
    case 1:
      cout << "Enter account number: ";
      cin >> accountNumber;
      cout << "Enter amount to deposit: $";
      cin >> amount;
      atm.deposit(accountNumber, amount);
      break;
    case 2:
      cout << "Enter account number: ";
      cin >> accountNumber;
      cout << "Enter amount to withdraw: $";
      cin >> amount;
      atm.withdraw(accountNumber, amount);
      break;
    case 3:
      cout << "Enter account to transfer from: ";
      cin >> fromAccount;
      cout << "Enter account to transfer to: ";
      cin >> toAccount;
      cout << "Enter amount to transfer: $";
      cin >> amount;
      atm.transfer(fromAccount, toAccount, amount);
      break;
    case 4:
      cout << "Enter account number: ";
      cin >> accountNumber;
      atm.balanceInquiry(accountNumber);
      break;
    case 5:
      break;
    default:
      cout << "Invalid option." << endl;
      break;
    }
  } while (choice != 5);
  return 0;
}

好了,通过这段简短的代码,我们已经成功地创建了一个ATM机存取款系统。当然,这只是一个简单的实现。在真实的应用中,我们需要考虑更多的因素,例如账户的安全、多线程的问题等。

  
  

评论区