Write a python Bank account class with withdraw/deposit features

Write a python Bank account class with withdraw/deposit features:

In this post, we will write one bank account class with different basic features like view the current balance, withdraw, and deposit money. We will not use any storage like a database. So, once the program will be closed, the data will be removed. If you want to save the data permanently, you can write the data into a file. In a production banking application, a database system with Authentication, authorization, security on accessing account data etc. are used.

With this program, you will learn how to use class in python and how to use methods in python classes.

Python program:

Below is the complete python program:

class Bank:
	def __init__(self):
		self.total_amount = 0
		self.name = ''

	def welcome(self):
		self.name = input('Welcome to your Bank Account. Please Enter your name : ')

	def print_current_balance(self):
		print('Your Current balance : {}'.format(self.total_amount))

	def deposit(self):
		self.total_amount += float(input('Hello {}, please enter amount to deposit : '.format(self.name)))
		self.print_current_balance()

	def withdraw(self):
		amount_to_withdraw = float(input('Enter amount to withdraw : '))

		if amount_to_withdraw > self.total_amount:
			print('Insufficient Balance !!')
		else:
			self.total_amount -= amount_to_withdraw

		self.print_current_balance()


if __name__=="__main__":
	bank = Bank()
	bank.welcome()

	while True:
		input_value = int(input('Enter 1 to see your balance,\n2 to deposit\n3 to withdraw\n'))

		if input_value == 1:
			bank.print_current_balance()
		elif input_value == 2:
			bank.deposit()
		elif input_value == 3:
			bank.withdraw()
		else:
			print('Please enter a valid input.')

Explanation:

In this program,

  • Bank is a class used to do all banking related tasks. We can create one class with all methods related to banking. This class holds two values. One is the total money amount for the user and user name.
  • Once any object for this class is created, it will call the init(self) method, which is also known as the constructor. Inside this method, we are setting the total amount as 0 and user name as empty string.
  • This class has four methods:

    • welcome is used to greet the user and it also records the user name and keep it in the name variable.
    • printcurrentbalance is used to print the current balance i.e. total_amount
    • deposit method is used to deposit money. It takes the amount from the user and adds it to the total_amount.
    • withdraw method is used to withdraw money. It takes the amount and compares it with total_amount. If the amount is greater than total_amount, it shows one insufficient balance message. Else, it decrements that amount from total_amount.
  • If you run this program, it creates one Bank object in the beginning, which is bank. It calls welcome method to show a greet message and records the name.
  • It runs one infinite loop. It keeps taking user inputs and based on the value, it calls different methods of bank.

Output:

If you run this program, it will print the below output:

Welcome to your Bank Account. Please Enter your name : Alex
Enter 1 to see your balance,
2 to deposit
3 to withdraw
1
Your Current balance : 0
Enter 1 to see your balance,
2 to deposit
3 to withdraw
2
Hello Alex, please enter amount to deposit : 100
Your Current balance : 100.0
Enter 1 to see your balance,
2 to deposit
3 to withdraw
1
Your Current balance : 100.0
Enter 1 to see your balance,
2 to deposit
3 to withdraw
2
Hello Alex, please enter amount to deposit : 100
Your Current balance : 200.0
Enter 1 to see your balance,
2 to deposit
3 to withdraw
3
Enter amount to withdraw : 300
Insufficient Balance !! You have 200.0
Your Current balance : 200.0
Enter 1 to see your balance,
2 to deposit
3 to withdraw
3
Enter amount to withdraw : 400
Insufficient Balance !! You have 200.0
Your Current balance : 200.0
Enter 1 to see your balance,
2 to deposit
3 to withdraw
3
Enter amount to withdraw : 200
Your Current balance : 0.0
Enter 1 to see your balance,
2 to deposit
3 to withdraw

You might also like: