Print Friendly and PDF
(Account Class) Create an Account class that a bank might use to represent customers’ bank accounts. Include a data member of type int to represent the account balance. [Note: In subsequent chapters, we’ll use numbers that contain decimal points (e.g., 2.75)—called floating­ point values— to represent dollar amounts.] Provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it’s greater than or equal to 0. If not, set the balance to 0 and display an error message indicating that the initial balance was invalid. Provide three member
(Account Class) Create an Account class that a bank might use to represent customers’ bank accounts. Include a data member of type int to represent the account balance. [Note: In subsequent chapters, we’ll use numbers that contain decimal points (e.g., 2.75)—called
floating­ point values— to represent dollar amounts.]


Provide a constructor that receives an initial balance and uses it to initialize the data member. The constructor should validate the initial balance to ensure that it’s greater than or equal to 0. If not, set the balance to 0 and display an error message indicating that the initial balance was invalid. Provide three member functions. Member function credit should add an amount to the current balance.

Member function debit should withdraw money from the Account and ensure that the debit amount does not exceed the Account’s balance. If it does, the balance should be left unchanged and the function should print a message indicating "Debit amount exceeded account balance." Member function get Balance should return the current balance. Create a program that creates two Account objects and tests the member functions of class Account.

Solution

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#include "Account. h"
int main()
{
Account account1( 150 ) ;
Account account2( ‐200 ) ;
int choise;

int accountNo;
int debAmount;
int credAmount;
//display initial balances
cout << "Initial balance of account1 is " << account1. getBalance() << endl;
cout << "Initial balance of account2 is " << account2. getBalance() << endl;
menu:
cout << "\nPlease select the transaction (enter the number) : " << endl;
cout << "1. Balance Inquiry" << endl;
cout << "2. Debit" << endl;
cout << "3. Credit" << endl;
cin >> choise;
//balance inquiry
switch (choise)
{
case 1:
cout << "Balance Inquiry is selected" << endl;
cout << "please select the account (enter the number) : " << endl;
cout << "1. account1\n2. account2" << endl;
cin >> accountNo;
cout << "option selected: " << accountNo << endl;
switch ( accountNo )
{
case 1 :
cout << "account1 balance: " << account1. getBalance() << endl;
goto menu;
//break;
case 2 :
cout << "account2 balance: " << account2. getBalance() << endl;
goto menu;
//break;

default :
cout << "incorrect option! " << endl;
goto menu;
}
//break;
goto menu;
case 2 :
cout << "Debit is selected" << endl;
cout << "please select the account (enter the number) : " << endl;
cout << "1. account1\n2. account2" << endl;
cin >> accountNo;
cout << "option selected: " << accountNo << endl;
switch ( accountNo )
{
case 1 :
cout << "account1 balance: " << account1. getBalance() << endl;
cout << "enter debit amount: " << endl;
cin >> debAmount;
cout << "entered amount is " << debAmount << endl;
account1. debitBalance(debAmount) ;
cout << "current balance is " << account1. getBalance() << endl;
goto menu;
//break;
case 2 :
cout << "account2 balance: " << account2. getBalance() << endl;
cout << "enter debit amount: " << endl;
cin >> debAmount;
cout << "entered amount is " << debAmount << endl;
account2. debitBalance(debAmount) ;
cout << "current balance is " << account2. getBalance() << endl;
goto menu;
//break;
default :
cout << "incorrect option! ! ! " << endl;
goto menu;

}
break;
case 3 :
cout << "Credit is selected" << endl;
cout << "please select the account (enter the number) : " << endl;
cout << "1. account1\n2. account2" << endl;
cin >> accountNo;
cout << "option selected: " << accountNo << endl;
switch ( accountNo )
{
case 1 :
cout << "account1 balance: " << account1. getBalance() << endl;
cout << "enter debit amount: " << endl;
cin >> credAmount;
cout << "entered amount is " << credAmount << endl;
account1. creditBalance(credAmount) ;
cout << "current balance is " << account1. getBalance() << endl;
goto menu;
//break;
case 2 :
cout << "account2 balance: " << account2. getBalance() << endl;
cout << "enter debit amount: " << endl;
cin >> credAmount;
cout << "entered amount is " << credAmount << endl;
account2. creditBalance(credAmount) ;
cout << "current balance is " << account2. getBalance() << endl;
goto menu;
//break;
default :
cout << "incorrect option! ! ! " << endl;
goto menu;
}
break;
default:
cout << "incorrect option! " << endl;
}

system("PAUSE") ;
return 0; 
}

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
//creating account class
class Account
{p
ublic:
Account ( int ) ;
void setBalance( int ) ;
int getBalance() ;
int debitBalance( int ) ;
int creditBalance( int ) ;
private:
int balance; // bank account balance name
};

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#include "Account. h"
//initialization with constructor, with supplied args
Account: : Account ( int initialBalance )
{

if (initialBalance >= 0)
setBalance ( initialBalance ) ;
cout << "Account balance set without any problem" << endl;
if (initialBalance < 0)
{
balance = 0;
cout << "Account balance cannot be negative. Account amount set as 0" << endl;
}
}v
oid Account: : setBalance(int initialBalance)
{
balance = initialBalance;
}i
nt Account: : debitBalance( int debitAmount )
{
if (debitAmount > balance)
cout << "Your balance is not enough! \n";
else
{
balance = balance ‐ debitAmount;
}
}i
nt Account: : creditBalance( int creditAmount )
{
balance += creditAmount;
}i
nt Account: : getBalance()
{
return balance;
}

zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: