简单的银行账户程序无法正确存储余额

  • 本文关键字:存储 余额 程序 简单 c++
  • 更新时间 :
  • 英文 :


我目前正在完成我的银行账户程序,但在完成过程中遇到了一些问题。这个问题似乎很容易解决,但我无法完全理解如何真正解决它

我将首先包括以下任务:

  1. 实现一个类Account。账户具有余额、添加和提取资金的功能以及查询当前余额的功能。将值传递给构造函数以设置初始平衡。如果没有传递任何值,则初始余额应设置为$0。如果试图提取的金额超过账户中的可用金额,则收取5美元的罚款。增强Account类以计算当前余额的利息。

  2. 实现一个类Bank。这家银行有两个对象,支票和储蓄,属于上一次开发的账户类型。

实现四种实例方法:

存款(双倍金额,字符串帐户)
提款(双倍金额,字符串帐户)
转账(双倍金额,字符串帐户)
printBalances()

这里的帐户字符串是"S"或"C"。对于存款或取款,它表示哪个帐户受到影响。对于转账,它表示从中提取资金的账户;钱会自动转入另一个账户。

唯一的问题似乎是实际将每个帐户的信息存储在account.cpp文件的余额变量中。它只是停留在0,这就是为什么我觉得这个问题应该很容易解决。我想我只是忘记了关于类实现的一些非常基本的东西,但这就是我来到这里的原因!现在我想一想,我的困惑部分来自于我以前实现过类似的程序,但只使用了数组而不是变量,而且我没有遇到同样的问题。数据似乎被存储到了数组中,所以这可能是我的问题?代码如下:

账户:

#include <iostream>
#include <string>
#include <iomanip>      
using namespace std;
class Account
{
public:
    Account();
    Account(double balance);
    void Add(double money);
    void Withdraw(double money);
    double GetBalance();
private:
    double balance; 
};

账户.cpp:

#include "Account.h"
//  Penalty Fee Constant
const double PENALTY_FEE = 5.00;
Account::Account()
{
    balance = 0.00;
}
Account::Account(double money)
{
    balance = money;
}
void Account::Add(double money)
{
    balance += money;
}
void Account::Withdraw(double money)
{
    if(money > balance)
        balance += PENALTY_FEE;
    else
        balance -= money;
}
double Account::GetBalance()
{
    return balance;
}

Bank.cpp:

#include "Account.h"
void deposit(double, string);
void withdraw(double, string);
void transfer(double, string);
void printBalances();
int main()
{
    string accountChoice;
    int selection;
    double transaction = 0;
    //  !!!!!!!!!!!!!!!!!!HAVE TO STILL COMPUTE INTEREST!!!!!!!!!!!!!!!!
    cout << fixed << showpoint << setprecision(2);
    do
    {
        cout << "Please make a selection:" << endl;
        cout << "1.) Deposit" << endl;
        cout << "2.) Withdraw" << endl;
        cout << "3.) Transfer" << endl;
        cout << "4.) Print balances" << endl;
        cout << "5.) Quit" << endl;
        cin >> selection;
        if(selection == 1)
        {
            cout << endl << "Please select the account you would like to perform operations on(S or C):" << endl;
            cin >> accountChoice;
            cout << endl << "Please enter the amount to be deposited:" << endl;
            cin >> transaction;
            cout << endl;
            deposit(transaction, accountChoice);
        }
        else if(selection == 2)
        {
            cout << endl << "Please select the account you would like to perform operations on(S or C):" << endl;
            cin >> accountChoice;
            cout << endl << "Please enter the amount to be withdrawn:" << endl;
            cin >> transaction;
            cout << endl;
            withdraw(transaction, accountChoice);
        }
        else if(selection == 3)
        {
            cout << endl << "Please select the account you would like to perform operations on(S or C):" << endl;
            cin >> accountChoice;
            cout << endl << "Please enter the amount to be transferred:" << endl;
            cin >> transaction;
            cout << endl;
            transfer(transaction, accountChoice);
        }
        else if(selection == 4)
            printBalances();
        else
            cout << "Closing program -- Thank you for using the ATM teller!" << endl;
    }while(selection != 5);

    system("pause");
    return 0;
}
void deposit(double amount, string account)
{
    Account savings, checking;
    if(account == "S" || account == "s")
        savings.Add(amount);
    else
        checking.Add(amount);
}
void withdraw(double amount, string account)
{
    Account savings, checking;
    if(account == "S" || account == "s")
        savings.Withdraw(amount);
    else
        checking.Withdraw(amount);
}
void transfer(double amount, string account)
{
    Account savings, checking;
    if(account == "S" || account == "s")
    {
        savings.Withdraw(amount);
        checking.Add(amount);
    }
    else
    {
        checking.Withdraw(amount);
        savings.Add(amount);
    }
}
void printBalances()
{
    Account savings, checking;
    cout << "The current balance in savings is: " << savings.GetBalance() << endl;
    cout << "The current balance in checking is: " << checking.GetBalance() << endl << endl;
}

我认为,如果您声明另一个类"Customer",并分别给它们一个名称、客户编号和一个支票储蓄账户,总体上可能会更清楚。客户应该在有进程生存期的地方实例化,这样它们就不会被删除,例如在静态容器中,例如std::map。

ATM,(对不起!),你似乎有一些非成员函数,它们实例化帐户,用它们做一些事情,然后在函数返回时删除它们。

每次需要时都会创建一个新的Account对象。当然,当您打印它时,它将为0,因为默认构造函数将余额初始化为0。

相反,当应用程序启动时,作为用户来识别他的帐户或其他内容,并创建相应的帐户实例。该实例需要在用户操作它的整个时间内都在那里

因此,实例化不是在方法中,而是在主函数中。并将实例传递给方法,作为根据需要修改实例的一种方式。

最新更新