c++声明一个银行账号类Account,求大神大佬啊

该类有账号(id)、余额(balance)两个数据成员,有获取账号、获取余额、存款和取款的函数,以及必要的构造函数。请按上述要求声明该银行账号类并在main函数中定义该类的多个对象,然后对它们进行存取款和查询余额的操作。

代码

/**
 * c++声明一个银行账号类Account
 * 该类有账号(id)、余额(balance)两个数据成员,
 * 有获取账号、获取余额、存款和取款的函数,以及必要的构造函数。
 * 请按上述要求:
 * 声明该银行账号类并在main函数中定义该类的多个对象,
 * 然后对它们进行存取款和查询余额的操作。
 */

#include <iostream>
#include <mutex>
#include <sstream>
#include <string>
#include <vector>

class account_t {
 public:
  account_t(const std::string& id) {
    _id      = id;
    _balance = 0;

    _writer << "================================================" << std::endl
            << "[  create  ] " << _id;
    _logs.push_back(_writer.str());
    _writer.str("");
  }
  ~account_t() {
    _writer << "[  delete  ] " << _id << std::endl
            << "------------------------------------------------" << std::endl
            << "final balance : " << _balance << std::endl
            << "================================================" << std::endl;
    _logs.push_back(_writer.str());
    _writer.str("");
    dump();
  }
  account_t(const account_t& rhs) = delete;
  account_t& operator=(const account_t& rhs) = delete;

  // getters
 public:
  const std::string& id() const { return _id; }

  void dump() {
    for (const auto& log : _logs) { std::cout << log << std::endl; }
  }

 public:
  double balance() {
    std::lock_guard<std::mutex> lock(_mutex);
    return _balance;
  }

  void deposit(double amount) {
    std::lock_guard<std::mutex> lock(_mutex);
    amount = trim(amount);
    if (amount <= 0) return;
    _balance = _balance + amount;
    _writer << "[ deposit  ] " << amount << ", with balance of " << _balance;
    _logs.push_back(_writer.str());
    _writer.str("");
  }

  void withdraw(double amount) {
    std::lock_guard<std::mutex> lock(_mutex);
    amount = trim(amount);
    if (amount <= 0 || amount > _balance) return;
    _balance = _balance - amount;
    _writer << "[ withdraw ] " << amount << ", with balance of " << _balance;
    _logs.push_back(_writer.str());
    _writer.str("");
  }

 protected:
  double trim(double amount) {
    int _amount = amount * 1000;
    return _amount / 1000.0f;
  }

 private:
  std::string              _id;
  double                   _balance;
  std::mutex               _mutex;
  std::vector<std::string> _logs;
  std::stringstream        _writer;
};

int main(int argc, char const* argv[]) {
  std::vector<std::string> ids = {
      "402674 8234 8239 384", "622892 8234 8239 384"};

  for (const auto& id : ids) {
    auto account = new account_t(id);
    for (int it = 0; it < 5; it++) {
      account->deposit(std::rand() * 1000.0f / RAND_MAX);
      account->withdraw(std::rand() * 1000.0f / RAND_MAX);
    }
    delete account;
  }

  return system("pause");
}

结果

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-05-10
#include <iostream>
#include <string>
 
using namespace std;
 
class BankAccount
{
public:
    BankAccount(string holder,long id)
    {
        strHolder = holder;
        nID = id;
        dBalance = 0.0;
    }
 
    ~BankAccount()
    {
 
    }
 
public:
    double QueryBalance()
    {
        return dBalance;
    }
 
    double SaveMoney(double dSaveNum)
    {
        dBalance += dSaveNum;
        return dBalance;
    }
 
    double DrawMoney(double dDrawNum)
    {
        if (dBalance < dDrawNum)
        {
            cout << "余额不足。" << endl;
        }
        else
        {
            dBalance -= dDrawNum;
        }
 
        return dBalance;
    }
 
private:
    string strHolder;
    long nID;
    double dBalance;
};
 
int main() 
{
    BankAccount A = BankAccount("A",100000001);
    BankAccount B = BankAccount("B",100000002);
 
    A.SaveMoney(100.1);
    cout << "A账户余额:" << A.QueryBalance() << endl;
    B.DrawMoney(10);
    B.SaveMoney(9999.99);
    B.DrawMoney(1234);
    cout << "B账户余额:" << B.QueryBalance() << endl;
 
    system("pause");
    return 0;
}