Exercise 43. Bank Account class:

  1. Create a Python class called BankAccount which represents a bank account, having as attributes: accountNumber (numeric type), name (name of the account owner as string type), balance.
  2. Create a constructor with parameters: accountNumber, name, balance.
  3. Create a Deposit() method which manages the deposit actions.
  4. Create a Withdrawal() method  which manages withdrawals actions.
  5. Create an bankFees() method to apply the bank fees with a percentage of 5% of the balance account.
  6. Create a display() method to display account details.
  7. Give the complete code for the  BankAccount class.

Solution

class BankAccount:
# create the constuctor with parameters: accountNumber, name and balance
def __init__(self,accountNumber, name, balance):
self.accountNumber = accountNumber
self.name = name
self.balance = balance

# create Deposit() method
def Deposit(self , d ):
self.balance = self.balance + d

# create Withdrawal method
def Withdrawal(self , w):
if(self.balance < w):
print("impossible operation! Insufficient balance !")
else:
self.balance = self.balance - w
# create bankFees() method
def bankFees(self):
self.balance = (95/100)*self.balance

# create display() method
def display(self):
print("Account Number : " , self.accountNumber)
print("Account Name : " , self.name)
print("Account Balance : " , self.balance , " $")

# Testing the code :
newAccount = BankAccount(2178514584, "Albert" , 2700)
# Creating Withdrawal Test
newAccount.Withdrawal(300)
# Create deposit test
newAccount.Deposit(200)
# Display account informations
newAccount.display()

The output is :
Account Number :  2178514584
Account Name :  Albert
Account Balance :  2600  $

Younes Derfoufi
my-courses.net
One thought on “Solution Exercise 43 : class to manage bank account”

Leave a Reply