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 methods like viewing the current balance, withdrawing, and depositing money. We will not use any database. So, once the program will be closed, the data will be lost. 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. should be used.

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

Python program for bank account class:

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.')

Download it on Github

Explanation:

In this program,

  • Bank is a class used to do all the 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 the user name. These values are assigned to the total_amount and name variables.

  • The __init(self)__ method is invoked if we create any object of the Bank class. This method is used to initialize the attributes of the object. Inside this method, we are resetting the values of the user name and total amount variables. It assigns 0 to the total_amount variable and an empty string to the name variable.

  • This class has four methods:

    • The welcome() method is used to greet the user and it also records the user name and assigns it to the name variable.
    • The print_current_balance() method is used to print the current balance i.e. total_amount.
    • The deposit() method is used to deposit money. It takes the amount from the user and adds it to the current value of total_amount and assigns the new value to this variable.
    • The withdraw() method is used to withdraw money. It takes the amount and compares it with the total_amount. If the amount is greater than total_amount, it shows one ‘insufficient balance’ message. Else, it deducts that amount from the total_amount*.
  • If you run this program, it creates one Bank object, bank, at the beginning of the program. It calls the welcome() method to show a greeting message to the user and records the name.

  • It runs one infinite while loop. It keeps reading user inputs and based on the input, it calls different methods of the bank object.

Python bank account class example

Output:

If you run this program, it will output as below:

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: