2011-04-24 6 views
4

기본 클래스 "bankAccount"와 파생 클래스 "checkingAccount"및 "savingsAccount"를 사용하는 할당 작업을하고 있습니다. 나는 현재 내가 얻고있는 결과에 당황하고있다. 모든 결산 잔액이 부정적으로 끝나고 있습니다. 누구든지 내 코드를 살펴보고 왜 이것이있을 수 있는지 알아볼 수 있습니까? 나는 파생 클래스 "checkingAccount"의 프로세스 함수에서 뭔가 잘못하고 있다고 가정합니다. "savingsAccount"프로세스 기능이 유사합니다. 아직 시작하지 않았으므로 첫 번째 기능이 작동하지 않습니다. 감사!클래스 및 파생 클래스 처리 도움말

헤더 :

#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 

클래스 구현 :

#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; 
} 

주 :

#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; 
} 
+2

숙제를 도와 주는데 지독한 많은 코드가 있습니다. 코드를 디버깅 해 보셨습니까? 어떤 시점에서 일어날 것으로 예상되는 일이 어떤 일에서 벗어나는가? 그 행과 변수에 사용 된 변수를 다시 확인하십시오. 또한, 코드에서'int checkingAccountNumber = accountNumber;'와'double checkingBalance = balance;와 같이 불필요하게 줄을 반복하는 장소가 많이 있습니다 -이 줄은 if 문 위로 이동하여 코드를 줄이거 나 만들 수 있습니다 유지 관리가 더 쉽다. [DRY] (http://en.wikipedia.org/wiki/DRY)에 대해 알아보십시오 – Basic

+0

@basiclife, 네, 저는 그 스트레치를 알고 있습니다. 누군가가 모든 코드를 파헤 치고 문제를 파악할 것입니다. 누군가가 지루해하고 내일 다시 작업하기 전에 몇 가지 조언을 해줄 수 있기 때문에 나는 오늘 밤 모든 것을 올릴 것이라고 생각했습니다. : P – darko

+1

1)이 코드는 컴파일되지 않으므로 실제로 문제가되는 코드가 아닙니다. 2) 오류가 발생하는 데이터 ('Bank.txt')가 포함되어 있지 않습니다. 모두 3)이 코드는 너무 복잡합니다. 둘 모두 파손 된 * 파생 클래스가 두 개 있습니다.오류를 발견하기 전이나 후에 두 번째 수업을 작성 했습니까? 작동하지 않는 코드에는 절대로 추가하지 마십시오. 이제이 코드를 간소화하고 오류를 계속 가져올 수 있는지 확인한 다음 문제의 원인이 당신에게로 흘러 나오는지 확인한 다음 질문을 편집하십시오. – Beta

답변

2

저는 실제로 은행에서 일하고 있습니다. 그래서 저는 이것을 남겨 둘 수 없었습니다. :-)

문제에 추가 :

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

이 실제로 에만 계정에 대한이자를 남긴다!

또한 실제 은행은 예금을 할 때이자를 계산하지 않지만, 한 달에 한 번 또는 일년에 한 번 같은 고정 일에 계산합니다. 당신이 얻는 (혹은 지불하는)이자 또한 계정이 일정한 균형을 유지 한 날 수에 달려 있습니다. =이 균형을 새로운 값을 할당로를 법원에 기록 텍스트에도 불구하고

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

는 트랜잭션이 정말 가 발생했습니다! 아마도 균형과 금액을 비교해야할까요?

그런 다음 else 부분에서 유효하지 않은이자 계산을 다시 반복하십시오.

+0

+1 거기에 몇 가지 다른 문제가 있다고 생각하지만, 아키텍처는 조정이 필요하지만 위의 내용을 잘 살펴 보았습니다. – Basic

1

는 NUM있다 어리석은 문제. 내가 언급 할께. 당신은 하나의 기본 클래스 (은행 accounnt)와 두 개의 파생 클래스 (당좌 계좌와 예금 계좌). 그런 다음 파생 클래스에서 객체를 인스턴스화하는 기본 클래스 내에 메소드 (process 메소드)가 있습니다. 이것은 좋지 않다. 클래스 파생을 재검토하면 문제를 해결하는 데 도움이 될 것입니다.