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

时间:2014-10-20 04:02:57

标签: c++

我目前正在结束我的银行帐户计划,但我在完成途中遇到了一些问题。问题似乎很容易解决,但我不能完全理解我应该如何解决这个问题。

我首先要包含以下作业:

  
      
  1. 实施班级帐户。帐户具有余额,添加和取款的功能,以及查询当前余额的功能。将值传递给构造函数以设置初始余额。   如果没有传递值,则初始余额应设置为$ 0。   如果试图提取的金额超过账户中的可用金额,则需要支付5美元的罚金。   增强Account类以计算当前余额的利息。

  2.   
  3. 实施班级银行。该银行有两个对象,即检查和储蓄,在上一个练习中开发的Account类型。

  4.         

    实施四种实例方法:

         

    存款(双倍金额,String帐户)
      取款(双倍金额,String帐户)
      转账(双倍金额,String账户)
      printBalances()

         

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

唯一的问题似乎是将每个帐户的信息实际存储在Account.cpp文件的balance变量中。它只是保持在0,这就是为什么我认为这个问题应该很容易解决的原因。我想象我只是忘记了关于类实现的非常基本的东西,但这就是我在这里的原因!现在我考虑一下,我认为我的困惑部分来自于我之前实现过类似程序但仅使用数组而不是变量的事实,我没有遇到同样的问题。数据似乎无论如何都存储在数组中,所以这可能是我的问题?代码如下:

Account.h:

#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; 
};

Account.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;
}

2 个答案:

答案 0 :(得分:1)

我认为,如果您宣布另一个班级“客户”,并且每个班级都给他们一个名字,客户编号和一个支票和储蓄帐户,那么整体可能会更清楚。应该在具有进程生存期的某个地方实例化客户,以便不删除它们,例如。在静态容器中,例如std :: map。

ATM,(抱歉!),您似乎有一些非成员函数可以实例化帐户,使用它们执行操作,然后在函数返回时删除它们。

答案 1 :(得分:0)

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

而是当应用程序启动时,作为用户识别他的帐户或某事并创建相应的帐户实例。在用户操作的整个过程中,实例需要存在。

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