bank account using classes



this is what I have so far
could you guys help me and see what's wrong with it?

objective
1. The class should keep track of a bank account, recording deposits and withdrawals, plus a count of the total number of transactions.
2. It should have to constructors to set the initial balance. One a default, using 1000, the other have an argument for the initial balance. Each constructor should set the number of transactions to zero.
3. If a withdrawal is for more than the balance, the transaction will not occur and an error message will be written to the cout (or cerr). Each withdrawal or deposit will add one to the number of transactions.
4. For each line, the input will be d(deposit), or w for withdrawal, plus the amount for each. The output will be the current operation plus the amount, echoed to the output file. If the withdrawal is greater than the balance, write an error message in the member function. At the end of out output file, write the current balance and the number of transactions with appropriate messages.
5. Read and write to disk files. The main should use an end_of_file loop. The algorithm will be discussed in more detail in class.
6. Libraries - iostream, fstream, iomanip

input:

2000 Initial data for constructor with argument to be used in the actual code not in the data file.

data4b.txt
w 2500 will create error message
w 500
d 1500
d 700
w 1200
d 190
w 70
w 85
d 600
d 800
w 1100

Specifications for the class

Private members
Current_Balance, number_of_transactions (of type int)
public members
two constructors, one a default, to set the balance to 1000,
to other constructors should have an argument for the balance.
A transformer deposit
A transformer withdrawal
// both update the balance and increment the count
// if the withdraw is greater than the balance
// write an error message to the cout
an observer for the balance
an observer for the transactions

each of the above functions should be documented in the .h file (specification file)


This is what I have so far, could you guys test it out and see if there's anything wrong with it?




bankaccountdemo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "BackAccount.h"
#include <fstream>
#include <iostream>
#include <string.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    std::string line;
    char temp[1024];
    int initialCurrentBalance;
    std::ifstream data4db;
    std::ofstream output;

    data4db.open("data4b.txt");
    output.open("output.txt");  
 
    std::cin >> initialCurrentBalance;

    BackAccount backAccount(initialCurrentBalance);

    char * pch;
    
    while (!data4db.eof())
    {
        getline (data4db, line);
        memset(temp, 0, sizeof(temp));
        strncpy(temp, line.c_str(), sizeof(temp));
        output << temp << std::endl;
        pch = strtok(temp, " ");
        while ( pch != NULL)
        {
            // withraw or deposit
            if (strcmp(pch, "w") == 0)
            {
                pch = strtok(NULL, " ");
                backAccount.withdraw(atoi(pch));
            }
            else if (strcmp(pch, "d") == 0)
            {
                pch = strtok(NULL, " ");
                backAccount.deposit(atoi(pch));
            }
            else
            {
                pch = strtok(NULL, " ");
                // do nothing
            }
        }
    }

    data4db.close();
    output << "Current Balance is $" << backAccount.getCurrentBalance() << std::endl;
    output << "Number Of Transactions is " << backAccount.getNumberOfTransactions() << std::endl;
    output.close();   
}


bankaccount.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "BackAccount.h"
#include <iostream>

BackAccount::BackAccount()
{
    currentBalance = 1000;
    numberOfTransactions = 0;
}
        
BackAccount::BackAccount(int pCurrentBalance)
{
    currentBalance = pCurrentBalance;
    numberOfTransactions = 0;
}

void BackAccount::deposit(int amount)
{
    currentBalance += amount;
    numberOfTransactions++;
}

void BackAccount::withdraw(int amount)
{
    if (currentBalance >= amount)
    {
        currentBalance -= amount;  
    }
    else
    {
        std::cout << "ERROR: Cannot withdraw " << amount << " since current balance is " << currentBalance << std::endl;
    }
    numberOfTransactions++;
}

int BackAccount::getCurrentBalance()
{
    return currentBalance;
}

int BackAccount::getNumberOfTransactions()
{
    return numberOfTransactions;
}


bankaccount.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef BACKACCOUNT_DEFINED
#define BACKACCOUNT_DEFINED 1

class BackAccount
{
    public:
        BackAccount();
        virtual ~BackAccount(){}
        BackAccount(int currentBalance);
        // transformer methods
        void deposit(int amount);
        void withdraw(int amount);
        // observer methods
        int getCurrentBalance();
        int getNumberOfTransactions();
    protected:
    private:
        int currentBalance;
        int numberOfTransactions;
};
#endif 


data4b.txt
w 2500
w 500
d 1500
d 700
w 1200
d 190
w 70
w 85
d 600
d 800
w 1100
Use of direct memory manipulation is strongly discouraged in C++. Use of c-strings (char arrays) is discouraged too. There is variant of getline() function which takes delimeter different from default '\n', there is stoi() function which is atoi() for std::string.
In your case use of stream operations is preferred.
Also use of eof() usually leads to errors.
Modified while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
std::ifstream data4db("data4b.txt");//Open file where it is needed.
{ //Additional code block, to destroy op and amount when they are not needed
    char op;
    int amount;
    while (data4db >> op >> amount) { //If both values extracted successfully, go with the loop
        switch(op) {
            case 'w':
                backAccount.withdraw(amount);
                break;
            case 'd':
                backAccount.deposit(amount);
                break;
            default:
                std::cout << "Unknown operation: '" << op << "'" << std::endl;
                break;
        }
    }
}


DO you need to increase number of transactions if withdraw failed due to insufficient amount of money?
Last edited on
Topic archived. No new replies allowed.