|
-
March 8th, 2011, 01:50 PM
#1
Help with C++ project
Hey I have a project for school where I have to make a bank class program using classes. I have to make a base class named Account and then 3 derived classes, Checking, Savings, and Loan. The problem I have is this program is read from a file our teacher created, so in main I have a while loop going through the file and if it reads an s it makes a savings object, if it is l it makes a loan object, etc. This is working fine but this objects also take in an account number, so transactions can be processed later. Here is the code for main:
Code:
main()
{
ifstream in;
char transactionType;
int accountNumber;
string accountName;
double initialBalance;
double theInterestRate;
double theCheckFee;
double theTransactionAmount;
string theAccountType;
in.open("hello.txt");
if (in.fail())
cout << "Error reading file";
Account account[5];
while (!in.eof())
{
in >> transactionType;
if (transactionType == 's')
{
in >> accountNumber;
in >> accountName;
in >> initialBalance;
in >> theInterestRate;
Savings savingAccount(accountNumber, accountName, initialBalance,
theInterestRate);
cout << "new account..." << endl << accountName << " (savings)";
cout << savingAccount;
account[accountNumber] = savingAccount;
}
else if (transactionType == 'c')
{
in >> accountNumber;
in >> accountName;
in >> initialBalance;
in >> theCheckFee;
Checkings checkingAccount(accountNumber, accountName, initialBalance,
theCheckFee);
cout << "new account..." << endl << accountName << " (checking)";
cout << checkingAccount;
account[accountNumber] = checkingAccount;
}
else if (transactionType == 'l')
{
theAccountType = "loan";
in >> accountNumber;
in >> accountName;
in >> initialBalance;
in >> theInterestRate;
initialBalance = -(initialBalance);
Loan loanAccount(accountNumber, accountName, initialBalance,
theInterestRate, theAccountType);
cout << "new account..." << endl << accountName << " (loan)";
cout << loanAccount;
account[accountNumber] = loanAccount;
}
else if (transactionType == 't')
{
in >> accountNumber;
in >> theTransactionAmount;
account[accountNumber].addTransaction(theTransactionAmount);
cout << account[accountNumber];
}
else if (transactionType == 'u')
{
in >> accountNumber;
}
else
cout << "Invalid Transaction Type";
}
So I save the loan, savings, or checking objects back into an account array so I can sort through the accounts in the transaction part of the function. But then I am changing the Account object and the loan, checkings, or savings, aren't even taken in at all. And since transactions are different depending on if it is a loan, savings, or checking, it doesn't work. So how would I make this work? Virtual functions? Or is there a way to point to my loan, savings, or checking account with an account number without actually changing the values. Idk. I am confused so I'm sure I'm not making any sense.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|