我应该编写一个主程序,提示用户输入名称和初始余额。您的程序应该创建两个对象,一个使用默认构造函数,另一个使用允许用户输入的名称和余额用于初始化其对象的构造函数。然后提示用户将一笔金额记入账户和一笔金额借记。使用 creditAccount 将钱贷给使用参数化构造函数创建的对象,使用 debitAccount 从使用默认构造函数创建的对象中减去借方金额。使用 displayBalance 显示两个对象的余额。重复上述循环,直到用户输入退出程序的选项。我只是不明白从这里到哪里去让它实际构建和使用这个类。主文件
//9/17/2013
//
//I have read and understand the Lab Submittal Policy document on BB.
#include <iostream>
#include <sstream>
#include <string>
#include "Account.h"
using namespace std;
int main()
{
char Redo;
string CustomerName;
do
{
float InitialBalance = -1;
float balance1 = 0;
float balance2 = 0;
Account Account;
Account.CreditAccount (balance1, InitialBalance);
Account.DebitAccount (balance2, InitialBalance);
Account.DisplayBalance (CustomerName, balance1, balance2);
//Asks user if they want redo the program
cout << "Would you like to redo the program?\n";
cout << "Please enter Y or N: \n \n";
cin >> Redo;
}while(Redo == 'Y' || Redo == 'y');
char exitchar; //Exit's the program.
cout << "\nPress any key and <enter> to exit the program.\n";
cin >> exitchar;
return 0;
}
帐户.h
using namespace std;
class Account {
public:
float balance1;
float balance2;
string CustomerName;
float InitialBalance;
float CreditAccount(float& balance1, float InitialBalance);
float DebitAccount(float& balance2, float InitialBalance);
float DisplayBalance(string CustomerName, float balance1, float balance2);
Account ();
Account(float balance)
{
SetInitialBalance(balance);
}
void Account::SetInitialBalance(float balance)
{
if(balance >= 0)
{
InitialBalance = balance;
}
else
cout << "Error! Initial Balance cannot be less than 0." << endl;
}
};
Account::Account(void)
{
string CustomerName;
cout << "Your Account Machine" << endl;
cout << "Please enter your last name." << endl;
cin >> CustomerName;
while(InitialBalance < 0)
{
cout << "Please enter your account balance. No Commas." << endl;
cin >> InitialBalance;
if(InitialBalance < 0)
cout << "Error account balance must be positive." << endl;
}
}
float Account::CreditAccount(float& balance1, float InitialBalance)
{
float CreditInput = -1;
while(CreditInput<0){
cout << "Would you like to credit the account? Enter the amount you would like to credit." << endl;
cin >> CreditInput;
if (CreditInput<0)
cout << "Credit must be positive." << endl;
}
balance1 = (CreditInput + InitialBalance);
return balance1;
}
float Account::DebitAccount(float& balance2, float InitialBalance)
{
float DebitInput = 0;
while((InitialBalance - DebitInput)<0){
cout << "Would you like to debit the account? Enter the amount you would like to debit." << endl;
cin >> DebitInput;
if((InitialBalance-DebitInput)<0)
cout << "You cannot debit more than you have avalaible." << endl;
}
balance2 = (InitialBalance - DebitInput);
if( DebitInput > InitialBalance)
{
cout << "Debit amount exceeds account balance." << endl;
return 0;
}
else
return balance2;
}
float Account::DisplayBalance(string CustomerName, float balance1, float balance2)
{
cout << "Customer Name: " << CustomerName << endl;
cout << "Account Balance for credited account: " << balance1 << endl;
cout << "Account Balance for debited account: " << balance2 << endl;
return 0;
}
更新我更新了我的代码,我只有一个问题。我想知道为什么我的程序没有读取任何 CustomerName 或 InitialBalance。其他一切都符合我的喜好。
再次感谢你!