Question

I was wondering if you have members of a class in C++ with the same arguments, if there a way to simplify this? For example:

#include <iostream>
using namespace std:

class bankAccount
{
public:
        bankAccount()
        {
           privAcct = "MyAccount";
           privPin = "MyPin";
        {
        void changeBalance(string acct, string pin)
        {
           if(acct == privAcct && pin == privPin)
           {
               cout << "YAY! You can do this!" << endl;
           }
        }
        void otherMethod(string acct, string pin)
        {
           if(acct == privAcct && pin == privPin)
           {
               cout << "YAY! You can do this!" << endl;
           }
        }
private:
        string privAcct, privPin;
};

As you can see they both take the same arguments and both need the same conditions to be true to access the "meat" of the method.

Although I was thinking it might be a good idea to potentially create one method and then have a switch statement in order to access different parts of the method, I want to be able to access a "changeBalance" portion or a "otherMethod" portion of the class. I just wasn't sure if there was a way to make this a little bit "neater", or simplify it?

My entire code is this:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;
void accountInfo();

class account{
public:
    //constructor
    account()
    {
        balance = 0.00;
        accountNum = "0303";
        pin = "2222";
    }
    //default for wrong entry response
    void wrongEntry()
    {
        cout << "Wrong account number or PIN. Please try again.\n" << endl;
    }
    //change pin number
    void changePin(string actNum, string oldPin)
    {
        if(actNum == accountNum && oldPin == pin)
        {
            string newPin;
            cout << "Please enter in a new pin: ";
            cin >> newPin;
            pin = newPin;
            cout << "Thank you." << endl;
        }
        else
        {
            wrongEntry();
        }
    }
    //change balance
    void changeBalance(string actNum, string pinnum)
    {
        if(actNum == accountNum && pinnum == pin)
        {
            double newAdd;
            cout << "Your current balance is " << balance << "\nPlease enter the additional amount you would like to deposit: ";
            cin >> newAdd;
            balance += newAdd;
            cout << "Your new balance is " << balance << ".\nThank you.\n" << endl;

        }
        else
        {
            wrongEntry();
        }
    }
    //print balance and account #
    void printBalance(string actNum, string pinnum)
    {
        if(actNum == accountNum && pinnum == pin)
        {
            cout << "For account #"<< accountNum << endl;
            cout << "Your current balance is $" << balance << ".\nThank you.\n";
        }
        else
        {
            wrongEntry();
        }
    }

private:
    string accountNum, pin;
    double balance;
};

int main ()
{
    int input;
    string aN, pin;
    account bo;

    while(1) 
    {
        cout << "Please enter account number: ";
        cin >> aN;
        cout << "Please enter corresponding PIN: ";
        cin >> pin;

        ///options
        cout << "Please choose from the following options:\n";
        cout << " 1. View Balance\n 2. Deposit\n 3. Change pin\nChoice: ";
        cin >> input;
        cout << endl;
        switch(input)
        {
        case 1:
            bo.printBalance(aN, pin);
            break;
        case 2:
            bo.changeBalance(aN, pin);
            break;
        case 3:
            bo.changePin(aN, pin);
            break;
        default:
            cout << "The information you entered does not seem to match our records.\nPlease try again.\n";
            break;
        }
        char response;
        cout << "\nAre you done with your transaction?: Y / N";
        cin >> response;
        cout << endl;
        if(response == 'Y' || response == 'y')
        {
            return 0;
        } 
    }
}

I realize I should put my class in separate header files, along with declaring a constructor a properly, but I'm just looking to simplify the code.

Thank you.

Was it helpful?

Solution 2

You could encapsulate the "restricted" interface in another type, and only allow that type to be created with valid credentials. Something like

class Account {
public:
    struct Accessor {
        Accessor(Account & a, string acct, string pin) : account(&a) {
            if (acct != a.privAcct || pin != a.privPin) {
                throw BadCredentials();
            }
        }

        void changePin() {account->changePin();}
        // and other functions

        Account * account;
    };
private:
    string privAcct, privPin;

    // Only accessible through an Accessor
    void changePin();
    // and other functions
};

Usage:

Accessor(account, acct, pin).changePin();

Or, if the entire interface is restricted, you might simply put the validation in the constructor of Account itself.

OTHER TIPS

You can create a new class LoginCredentials which contains acct and pin as private members, and a member function that will check for equality.

You could think about doing this as transactions.

(pseudo-code/design)

transactionValid  = BeginTransation(account, pin);
if ( transactionValid )
{
  changePin(...);
  chackBalance(...);
}
EndTransaction();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top