Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
#include <string>

// FUNCTIONS
void menu();
void account(int option);

using namespace std;
string AccountType[] = {"", "CHECKINGS", "SAVINGS"};
string response;

int AccountDetails[] = {
	1234,		// pin number
	10000,		// checking account balance
	55000,		// savings account balance
};

bool validatePin(int pin) {
	if (pin == AccountDetails[0]) {
		return true;
	} else {
		return false;
	}
}

bool proceed(string response) {
	if(response =="y" || response == "Y"){
		return true;
	} else if(response == "n" || response == "N"){
		string exit;
		cout << "\n\n\n\t Thank you for banking with us.";
		cin >> exit;
		return false;
	}

}

class AccountSettings {
	private:
		int type;		// account type
		int balance;	// account balance
	public:
		AccountSettings(int type) {
			// type 1 = checkings
			// type 2 = savings
			this->type = type;
			this->balance = AccountDetails[this->type];
		}
		int getWithdraw() {
			int withdrawAmount;
			cout << "Please enter amount to withdrawn:\n " << endl;
			cin >> withdrawAmount;

			// get account type
			if(withdrawAmount <= this->balance){
				int AccountBalance = this->balance -= withdrawAmount;
				cout << "Dispensing... ";
				cout << "₱"<< withdrawAmount << endl;
				
				// update the account balance
				AccountDetails[this->type] = AccountBalance;
				getBalance();

			} else {
				cout << "Insufficent funds" << endl;
				getBalance();
			}

			return 0;
		}

		int getDeposit() {
			int depositAmount;
			cout << "Please enter an amount to deposit:\n" << endl;
			cin >> depositAmount;

			int AccountBalance = this->balance += depositAmount;

			// update the account balance
			AccountDetails[this->type] = AccountBalance;

			cout << "\t₱" << depositAmount << " was deposited into your account";
			getBalance();
			 
			return 0;
		}

		int getTransfer() {
			int AmountTransfer;
			int TransferTo = this->type== 1 ? 2 : 1;

			cout << "Enter amount to transfer to your "<< AccountType[TransferTo] << " account."<< endl;
			cin >>  AmountTransfer;

			if(AmountTransfer <= AccountDetails[this->type]) {
				// update the current account balance in the selected account
				int NewBalance = this->balance -= AmountTransfer;
				AccountDetails[this->type] = NewBalance;

				// Set the new transfered amount to transfered account
				int TransferedAmount = AccountDetails[TransferTo] += AmountTransfer;
				AccountDetails[TransferTo] = TransferedAmount;

				cout << "₱" << AmountTransfer << " has been transfered to your "<< AccountType[TransferTo] << " account." << endl;
				getBalance();

			} else {
				cout << "Insuffient funds." << endl;
				getBalance();
			}

			return 0;
		}

		int getBalance() {
			string confirmBalance;
			// get the account type, and return balance
			cout << "Would you like to check your "<< AccountType[this->type] << " account balance? (y/n)\n" << endl;
			cin >> confirmBalance;
			if(confirmBalance=="y" || confirmBalance=="Y"){
				cout << "Your account balance is: ₱" << this->balance << endl;
			} 
			
			cout << "\n\nWould you like to continue (y/n)?\n";
			cin >> response;

			if (proceed(response)) {
				account(this->type); // return to account menu
			}

			return 0;
		}
};

void account(int option) {
		// account option = 1 (checkings)
		// account option = 2 (savings)
			cout << "\n\n" <<AccountType[option] << "--\n\t1. Check balance"
				<<"\n\t2. Withdraw from " << AccountType[option] 
				<<"\n\t3. Deposit to " << AccountType[option] 
				<<"\n\t4. Transfer " 
				<<"\n\t5. Return to Menu." << endl;

		// Pass in account type
		AccountSettings Account(option); 

		int selectMenu;
		cin >> selectMenu;
		
		switch(selectMenu){
			case 1:
				cout << Account.getBalance();
				break;
			case 2: 
				cout << Account.getWithdraw();
				break;
			case 3: 
				cout << Account.getDeposit();
				break;
			case 4:
				cout << Account.getTransfer();
				break;
			case 5:
				menu(); // return to main menu
				break;
			default:
				cout << "Would you like to continue? (y/n)\n";
				cin >> response;
				proceed(response);
			
				if (proceed(response)) {
					menu(); // return to main menu
				} 
				break;
		}
}

void menu() {
	int option;
	cout << "\n\nMain Menu" <<endl;
	cout << "\tPlease select a trasaction " << endl;
	cout << "\t1. Checkings \n\t2. Savings \n\t3. Exit" << endl;
	cin >> option;
	switch(option){
		case 1: account(option); // checkings
			break;
		case 2: account(option); // savings
			break;
		default:
			cout << "Would you like to continue? (y/n)\n";
			cin >> response;
			proceed(response);
			
			if (proceed(response)) {
				menu();
			} 
			break;
	}
}


// Begin MAIN
int main() {
	int pin;
	int count=1;
	
	cout << "Welcome to Bank of BSIE.\n\tPlease enter your pin number to access your account:" << endl;
	do {
		cin >> pin;

		if(validatePin(pin)) {
			menu(); // continue to main menu
		} else {
			cout << "Invalid pin. Please enter pin number:" << endl; 
		}
	} while (++count<=3); 									//given 3 attempts for the wrong pin
    
    if(count==4)
        cout<<"\tExceed  the limit, please try again after 24 hours";
	    cout<<"\nThank you for visiting\n"<<endl;
	    
	return 0;
}


What I have tried:

I dont know if this is oop, i hope someone could explain it to me thankyou in advance.
Posted
Updated 22-Feb-22 3:28am
Comments
_Asif_ 31-Jan-22 2:04am    
There are entire books, written on OOP, and you want us to explain it to you in a paragraph? Secondly, who has written the above code? Have you copy-pasted it from somewhere?

No, it isn't. Not even close. it looks like C code that has been "converted to C++" by changing printf to cout, and adding an irrelevant "class" is all.

Go back to your recent tutorials and read your notes over again: you don't seem to have grasped the concepts of OOPs design at all and it's far too big a subject for us to cover in a little text box!
 
Share this answer
 
In my personal opinion, it is an unfortunate example of OOP (the AccountSettings class acts on a global variables, like AccountDetails, AccountType).
 
Share this answer
 
Hello,
As i see by checking whole code:
That code declares a class and defines (the body) that class with private and public attributes and methods. That is all. That would be very nice to define classes in a seperate header file or source file. Just in that case, those classes can be reused in some others programs and applications easily.

Main routine does not create any objects with those classes! Please try to create objects in terms of classes defined in code then use these objects attributes and methods to perform algorithm-scenario. In that case one can say "ohh that is some OOP".
Please go,step by step.
Regards,
IM
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900