2014-10-20 68 views
1

我目前正在完成我的银行账户程序,但我在完成途中遇到了一些问题。这个问题似乎很容易解决,但我不能很好地解决我应该如何解决这个问题。简单的银行账户程序不能正确存储余额

我首先包括如下分配:

  1. 实现一个类账户。一个帐户有一个余额,用于添加和取款的功能,以及查询当前余额的功能。将值传递给构造函数以设置初始余额。 如果没有值传递,则应将初始余额设置为$ 0。 如果企图提取比帐户中可用的更多的钱,则会收取5美元的罚款。 增强帐户类以计算当前余额的利息。

  2. 实现一个班级银行。这家银行有两个对象,检查和节省上述练习中开发的类型账户。

实现四个实例方法:

存款(双量,字符串账户)
撤回(双量,字符串账户)
转移(双量,字符串账户)
printBalances()

这里帐户字符串是“S”或“C”。对于存款或提款,它指示哪个账户受到影响。对于转账,它表示从中取款的账户;钱会自动转移到其他账户。

唯一的问题似乎是在Account.cpp文件中为每个帐户实际存储余额变量中的信息。它只停留在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; 
} 
+0

在动作函数中,您可以创建一对夫妇账户:'账户节省,检查',调用方法然后返回,确保账户得到注销。 – 2014-10-20 04:17:14

+0

对不起,如果我没有正确地选择你的术语,但你是否建议我将这些方法从void类型更改为返回类型?这对我来说很重要,但我只想验证一下! – Gooeybanana 2014-10-20 04:20:55

+0

哦等 - 动作函数不是Account的成员函数! – 2014-10-20 04:22:16

回答

1

我想可能是,如果你更清晰的整体申报另一个班级'客户',然后分别给他们一个姓名,客户号码和一个检查和储蓄账户。客户应该在过程生命周期的某个地方实例化,以便它们不被删除,例如。在一个静态容器中,例如std :: map。

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

+0

谢谢先生!我想我直接认为它与Account.cpp有直接关系,因为我完全忽略了在非成员函数中声明类对象时发生的类似这样的事情。我不认为我可以根据教师的规则创建一个额外的课程,但我们还没有被告知std :: map,所以我怀疑我可以使用它们,尽管这种情况可能会有多大用处。但是,我会为了确保它而运行它,因为我觉得你的解决方案更有意义,我不明白为什么我们没有一个客户类。 – Gooeybanana 2014-10-20 04:43:45

0

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

而是当应用程序启动时,作为用户标识其帐户或创建相应的帐户实例。在用户操作它的整个过程中,实例都需要在那里。

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