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
- 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.
- The class also has two methods, deposit and withdraw, to perform deposit and withdrawal operations on the customer’s account.
- The deposit method takes an amount as a parameter and adds it to the balance of the customer.
- 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”.
- 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.
- The deposit method is called on the customer object, which adds 500 to the current balance.
- The withdraw method is then called on the customer object, which subtracts 200 from the current balance.
- Finally, the remaining balance is printed using the print statement.
- 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 VIPCustomer that inherits from the Customer class.
Create a Python class called Phone with attributes brand, model, and storage.
Create a Python class called Laptop with attributes brand, model, and storage.
Create a Python class called Book with attributes title, author, and pages.
Create a Python class called EBook that inherits from the Book class.
Create a Python class called ShoppingCart with attributes items and total_cost.