40. Problem to create a customer class in Python

In this Python oops program, we will create a customer class in Python. Class called Customer with attributes name and balance.

Customer class in Python

Steps to solve the program
  1. The Customer class is defined with a constructor method __init__ that takes name and balance as parameters and initializes the instance variables self.name and self.balance with the provided values.
  2. The class also has two methods, deposit and withdraw, to perform deposit and withdrawal operations on the customer’s account.
  3. The deposit method takes an amount as a parameter and adds it to the balance of the customer.
  4. The withdraw method takes an amount as a parameter and checks if the requested amount is less than or equal to the current balance. If so, it subtracts the amount from the balance. Otherwise, it prints “Insufficient balance”.
  5. The code creates an object customer of the Customer class by calling its constructor and passing the name “Jason Roy” and an initial balance of 1000 as arguments.
  6. The deposit method is called on the customer object, which adds 500 to the current balance.
  7. The withdraw method is then called on the customer object, which subtracts 200 from the current balance.
  8. Finally, the remaining balance is printed using the print statement.
  9. Thus, we have create a customer class in Python.
				
					class Customer:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance
    
    def deposit(self, amount):
        self.balance += amount
    
    def withdraw(self, amount):
        if amount <= self.balance:
            self.balance -= amount
        else:
            print("Insufficient balance")

# Create an object of the Customer class
customer = Customer("Jason Roy", 1000)
customer.deposit(500)
customer.withdraw(200)
print("Balance:", customer.balance)

				
			

Output:

				
					Balance: 1300
				
			

Related Articles

Create a Python class called Manager that inherits from the Employee class.

Create a Python class called VIPCustomer that inherits from the Customer class.

Leave a Comment