我目前正在完成我的银行账户程序,但我在完成途中遇到了一些问题。这个问题似乎很容易解决,但我不能很好地解决我应该如何解决这个问题。简单的银行账户程序不能正确存储余额
我首先包括如下分配:
实现一个类账户。一个帐户有一个余额,用于添加和取款的功能,以及查询当前余额的功能。将值传递给构造函数以设置初始余额。 如果没有值传递,则应将初始余额设置为$ 0。 如果企图提取比帐户中可用的更多的钱,则会收取5美元的罚款。 增强帐户类以计算当前余额的利息。
实现一个班级银行。这家银行有两个对象,检查和节省上述练习中开发的类型账户。
实现四个实例方法:
存款(双量,字符串账户)
撤回(双量,字符串账户)
转移(双量,字符串账户)
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;
}
在动作函数中,您可以创建一对夫妇账户:'账户节省,检查',调用方法然后返回,确保账户得到注销。 – 2014-10-20 04:17:14
对不起,如果我没有正确地选择你的术语,但你是否建议我将这些方法从void类型更改为返回类型?这对我来说很重要,但我只想验证一下! – Gooeybanana 2014-10-20 04:20:55
哦等 - 动作函数不是Account的成员函数! – 2014-10-20 04:22:16