How to Create a Simple Bank Operation Program Using Python Class and Member Functions?
Share
Condition for Bank Operation Program Using Python Class and Member Functions
Description:
A bank operation program in Python using a class defines a BankAccount class, which simulates basic
banking operations such as depositing, withdrawing, and checking the balance.
Step-by-Step Process
Define the BankAccount Class:
Create a class to represent a bank account, which includes attributes such as the account holder’s name and balance.
Initialize the Account:
Define an __init__ method to initialize the account with the owner's name and an initial balance.
Add Methods for Operations:
Define methods such as deposit(), withdraw(), and check_balance() to perform the respective banking operations.
Create an Object:
Create an object of the BankAccount class and perform operations like deposit and withdrawal using the object's methods.
Display the Results:
Print the results of each operation, like the updated balance after a transaction.
Sample Code
# Program for Bank operation
class BankAccount:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amount):
if amount > 0:
self.balance += amount
print(f"{amount} deposited. New balance: {self.balance}")
else:
print("Deposit amount must be positive.")
def withdraw(self, amount):
if 0 < amount <= self.balance:
self.balance -= amount
print(f"{amount} withdrawn. New balance: {self.balance}")
else:
print("Invalid withdrawal amount or insufficient funds.")
def check_balance(self):
print(f"{self.owner}'s Current balance: {self.balance}")