Help handling classes and derived classes

I am working on an assignment that uses the base class "bankAccount" and the two derived classes "checkAccount" and "savingAccount". I am currently confused on the output that I get. all ending residues end in negative. Can someone take a look at my code and see if they notice why this could be? I believe that I am doing something wrong with the process function of the derived class "checkAccount". The "processAccount" function of the process will be similar, I just have not done it first, because the first does not work. thanks!

Title:

#ifndef HEADER_H_INCLUDED #define HEADER_H_INCLUDED #include <iostream> #include <fstream> using namespace std; class bankAccount { public: bankAccount(); void setAccountInfo(int accountNumTemp, double balanceTemp); void prePrint(char accountType); void process(char accountType, char transactionTypeTemp, int amountTemp, int j); void postPrint(); private: int accountNumber; double balance; }; class checkingAccount: public bankAccount { public: void prePrint(int accountNumber, char accountType, double checkingBalance); checkingAccount(); double checkingAccount:: process(char transactionTypeTemp, int amountTemp, int j, double checkingBalance); /*applyTansaction(); applyInterest();*/ private: float interestRate; int minimumBalance; float serviceCharge; }; class savingsAccount: public bankAccount { public: void prePrint(int savingsAccountNumber, char accountType, double savingsBalance); savingsAccount(); /* applyTansaction(); applyInterest();*/ private: float interestRate; }; #endif // HEADER_H_INCLUDED 

class implementation:

 #include "header.h" bankAccount:: bankAccount() { accountNumber = 0; balance = 0; } void bankAccount:: setAccountInfo(int accountNumTemp, double balanceTemp) { accountNumber = accountNumTemp; balance = balanceTemp; } void bankAccount:: prePrint(char accountType) { if(accountType == 'C') { int checkingAccountNumber = accountNumber; double checkingBalance = balance; checkingAccount ca; ca.prePrint(checkingAccountNumber, accountType, checkingBalance); } else if (accountType == 'S') { int savingsAccountNumber = accountNumber; double savingsBalance = balance; savingsAccount sa; sa.prePrint(savingsAccountNumber, accountType, savingsBalance); } } void bankAccount:: process(char accountType, char transactionTypeTemp, int amountTemp, int j) { double checkingBalance; checkingAccount ca; //savingsAccount sa; if (accountType == 'C') { checkingBalance = balance; balance = ca.process(transactionTypeTemp, amountTemp, j, checkingBalance); } /*else if (accountType == 'S') { savingsBalance = balance; sa.process(transactionTypeTemp, amountTemp, j, savingsBalance) }*/ } void bankAccount:: postPrint() { cout << "Balance after processing: " << balance << endl; } checkingAccount:: checkingAccount() { interestRate = .02; minimumBalance = 500; serviceCharge = 20; } void checkingAccount:: prePrint(int checkingAccountNumber, char accountType, double checkingBalance) { cout << "Account Number:" << checkingAccountNumber << " account type:" << accountType << " Starting Balance:" << checkingBalance << endl; } double checkingAccount:: process(char transactionTypeTemp, int amountTemp, int j, double checkingBalance) { if (transactionTypeTemp == 'D') { checkingBalance = checkingBalance + amountTemp; checkingBalance = (checkingBalance * interestRate); } else if (transactionTypeTemp == 'W') { if ((checkingBalance = checkingBalance - amountTemp) < 0) { cout << "error: transaction number" << j + 1 << " never occured due to insufficent funds." << endl; } else { checkingBalance = checkingBalance - amountTemp; if(checkingBalance < minimumBalance) //if last transaction brought the balance below minimum balance { checkingBalance = (checkingBalance - serviceCharge); //apply service charge checkingBalance = (checkingBalance * interestRate); //apply interest } else // if last transaction did not bring the balance below minimum balance { checkingBalance = (checkingBalance * interestRate); //apply interest without service charge } } } return checkingBalance; } savingsAccount:: savingsAccount() { interestRate = .04; } void savingsAccount:: prePrint(int savingsAccountNumber, char accountType, double savingsBalance) { cout << "Account Number:" << savingsAccountNumber << " account type:" << accountType << " Starting Balance:" << savingsBalance << endl; } 

Main:

 #include "header.h" int main() { ifstream inFile; int numberOfAccounts, accountNumTemp, transactionNum, amountTemp; double balanceTemp; char discard, accountType, transactionTypeTemp; bankAccount ba; cout << "Processing account data..." << endl; inFile.open("Bank.txt"); if (!inFile) { for (int a = 0; a < 20; a++) cout << endl; cout << "Cannot open the input file." << endl; return 1; } inFile >> numberOfAccounts; inFile.get(discard); for (int i = 0; i < numberOfAccounts; i++) { inFile >> accountNumTemp >> accountType >> balanceTemp >> transactionNum; inFile.get(discard); ba.setAccountInfo(accountNumTemp, balanceTemp); ba.prePrint(accountType); for (int j = 0; j < transactionNum; j++) { inFile >> transactionTypeTemp >> amountTemp; inFile.get(discard); ba.process(accountType, transactionTypeTemp, amountTemp, j); } ba.postPrint(); } inFile.close(); return 0; } 
+4
source share
2 answers

I really work in a bank, so I could not leave it. :-)

Adding to your problems:

 if (transactionTypeTemp == 'D') { checkingBalance = checkingBalance + amountTemp; checkingBalance = (checkingBalance * interestRate); } 

This actually only leaves interest in the account!

In addition, the real bank does not calculate interest when you make a deposit, but on fixed days, just once a month or once a year. The interest you receive (or pay) also depends on the number of days during which the account has a certain balance.

 if ((checkingBalance = checkingBalance - amountTemp) < 0) { cout << "error: transaction number" << j + 1 << " never occured due to insufficent funds." << endl; } 

Despite the text written in cout, the transaction really did , since = assigns a new value to Balance! Perhaps you should compare the balance and the amount?

Then you repeat the invalid percent calculation in the else part again.

+2
source

There are numerous problems. I will talk about it. One base class (bank account) and two derived classes (bank account and savings account). Then you have methods (process method) in the base class that create objects from derived classes. This is bad. I think re-looking at the classes creating the classes will help you solve your problem.

+1
source

All Articles