c++ - 帮助处理类和派生类

标签 c++

我正在处理一个使用基类“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;
}

最佳答案

我实际上在一家银行工作,所以我不能离开这个。 :-)

增加你的问题:

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

尽管将文本写入 cout,但交易确实已经发生,因为 = 为 Balance 分配了一个新值!也许您应该只比较余额和金额?

然后您在 else 部分再次重复无效利息计算。

关于c++ - 帮助处理类和派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768270/

相关文章:

c++ - 检测到 glibc;内存损坏

c++ - 如果我们在增加指针后删除一个数组会发生什么?

java - 强化 : vsprintf: prevented 33-byte write into 32-byte buffer

c++ - 下面的C++语句是什么意思

c++ - 将文本和占位符、变量复制到剪贴板

对象数据成员的 C++ 连续内存访问

c++ - 无限 while 循环 C++

c++ - 用于检测内存泄漏的应用程序

c++ - VS2010 中不同大小的 lambda 表达式?

c++ - 控制符号可见性的标准方法