You are on page 1of 3

9/21/23, 9:14 AM 210090107120_assignment3

Practical 3 : Classes and Objects


Name : Priyansh Jain
Enrollment No : 210090107120
Class : III-A Batch - A3
Dataset Used : Online Retail 2
Dataset description : A real online retail transaction data set of two years.

a. Write a Python program to create a Bank class where deposits and withdrawals
can be handled by using instance methods.

In [ ]: class Bank:
def __init__(self, balance=0):
self.balance = balance

def deposit(self, amount):


if amount > 0:
self.balance += amount
return f"Deposited ${amount}. New balance: ${self.balance}"
else:
return "Invalid deposit amount. Please deposit a positive amount."

def withdraw(self, amount):


if 0 < amount <= self.balance:
self.balance -= amount
return f"Withdrew ${amount}. New balance: ${self.balance}"
elif amount > self.balance:
return "Insufficient funds. Cannot withdraw."
else:
return "Invalid withdrawal amount. Please withdraw a positive amount

def get_balance(self):
return f"Current balance: ${self.balance}"

# Example usage:
my_account = Bank(1000) # Create a Bank instance with an initial balance of $10

print(my_account.get_balance()) # Print current balance


print(my_account.deposit(500)) # Deposit $500
print(my_account.withdraw(200)) # Withdraw $200
print(my_account.withdraw(1500)) # Attempt to withdraw more than the balance

Current balance: $1000


Deposited $500. New balance: $1500
Withdrew $200. New balance: $1300
Insufficient funds. Cannot withdraw.

b. Write a program to make a module. Implement (a)method overloading


(b)method overriding (c)subclass (d)multilevel inheritance (e)multiple inheritance.
Use the classes, properties and methods according to your data set. Import it in
other program

file:///C:/Users/Priyansh/Desktop/python assignments/Priyansh/210090107120_assignment3.html 1/3


9/21/23, 9:14 AM 210090107120_assignment3

In [ ]: import numpy
import pandas

df = pandas.read_csv("C:\\Users\\Priyansh\\Desktop\\py_assignments\\online_retai

df.shape

Out[ ]: (541910, 8)

In [ ]: import numpy
import pandas

df = pandas.read_csv("C:\\Users\\Priyansh\\Desktop\\py_assignments\\online_retai

class Product:
def __init__(self, product_id, name, price):
self.product_id = product_id
self.name = name
self.price = price

def display_product_info(self):
return f"Product ID: {self.product_id}, Name: {self.name}, Price: {self.

class Customer:
def __init__(self, customer_id, name):
self.customer_id = customer_id
self.name = name

def display_customer_info(self):
return f"Customer ID: {self.customer_id}, Name: {self.name}"

class Order(Product, Customer):


def __init__(self, order_id, product_id, customer_id, quantity):
Product.__init__(self, product_id, "Product Name", 0) # Default product
Customer.__init__(self, customer_id, "Customer Name") # Default custome
self.order_id = order_id
self.quantity = quantity

def display_order_info(self):
product_info = self.display_product_info()
customer_info = self.display_customer_info()
return f"Order ID: {self.order_id}, Quantity: {self.quantity}\n{product_

# Method Overloading
def calculate_total(order):
if isinstance(order, Order):
return order.quantity * order.price
elif isinstance(order, list):
total = 0
for o in order:
if isinstance(o, Order):
total += o.quantity * o.price
return total

# Method Overriding
class DiscountedOrder(Order):
def __init__(self, order_id, product_id, customer_id, quantity, discount):
super().__init__(order_id, product_id, customer_id, quantity)
self.discount = discount

file:///C:/Users/Priyansh/Desktop/python assignments/Priyansh/210090107120_assignment3.html 2/3


9/21/23, 9:14 AM 210090107120_assignment3

def display_order_info(self):
original_info = super().display_order_info()
return f"{original_info}\nDiscount: {self.discount}%"

# Subclass (Subclassing Customer)


class VIPCustomer(Customer):
def __init__(self, customer_id, name, membership_level):
super().__init__(customer_id, name)
self.membership_level = membership_level

# Multilevel Inheritance (Subclassing VIPCustomer)


class PremiumVIPCustomer(VIPCustomer):
def __init__(self, customer_id, name, membership_level, exclusive_benefits):
super().__init__(customer_id, name, membership_level)
self.exclusive_benefits = exclusive_benefits

# Multiple Inheritance (Subclassing Product and VIPCustomer)


class SpecialProduct(Product, VIPCustomer):
def __init__(self, product_id, name, price, customer_id, membership_level):
Product.__init__(self, product_id, name, price)
VIPCustomer.__init__(self, customer_id, "Customer Name", membership_leve

file:///C:/Users/Priyansh/Desktop/python assignments/Priyansh/210090107120_assignment3.html 3/3

You might also like