Python 5: Object-Oriented Programming (OOP) — Retail Sales Edition

🎯 Goal:

By the end of this lesson, you’ll be able to:

  • Create Python classes for retail operations
  • Represent inventory, employees, and transactions
  • Use inheritance for role-based systems
  • Apply OOP to real retail workflows

🛍️ 1. Why OOP for Retail?

Imagine managing a retail business: you have products, employees, and sales records. Instead of juggling a mess of unrelated variables and lists, OOP lets you model everything in real-world terms.


📦 2. Building a Product Class

Let’s define a basic product:


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

    def sell(self, quantity):
        if quantity <= self.stock:
            self.stock -= quantity
            return f"Sold {quantity} x {self.name}"
        else:
            return f"Not enough stock for {self.name}"

    def restock(self, quantity):
        self.stock += quantity
        return f"Restocked {quantity} x {self.name}"

🛒 3. Selling and Managing Inventory


item1 = Product("Diamond Ring", 2500, 10)
print(item1.sell(2))      # Sold 2 x Diamond Ring
print(item1.restock(5))   # Restocked 5 x Diamond Ring
print(item1.stock)        # 13

This gives us full control over inventory operations.


👥 4. Creating an Employee Class


class Employee:
    def __init__(self, name, role):
        self.name = name
        self.role = role

    def process_sale(self, product, quantity):
        return f"{self.name} processed sale: {product.sell(quantity)}"
emp1 = Employee("Prince", "Sales Associate")
print(emp1.process_sale(item1, 3))

🧬 5. Inheritance for Specialized Roles

Let’s create a Manager who can also approve discounts:

class Manager(Employee):
    def approve_discount(self, product, percent):
        discounted_price = product.price * (1 - percent / 100)
        return f"Discount approved: {product.name} now ${discounted_price:.2f}"
mgr = Manager("Neha", "Store Manager")
print(mgr.approve_discount(item1, 10))

📊 6. Adding Sales Records (Composition)

class Sale:
    def __init__(self, product, employee, quantity):
        self.product = product
        self.employee = employee
        self.quantity = quantity
        self.total = product.price * quantity

    def summary(self):
        return f"{self.employee.name} sold {self.quantity} x {self.product.name} for ${self.total}"
pythonCopyEditsale1 = Sale(item1, emp1, 2)
print(sale1.summary())

✅ 7. Practice Time

Try these in your blog_env notebook:

  • Create 3 products and simulate stock depletion
  • Create 2 employees and assign them sales
  • Add a return_item() method that refunds stock
  • Track daily sales totals using a list of Sale objects

📘 Summary

  • OOP helps manage retail workflows like stock tracking and employee roles
  • Classes keep data and behavior bundled together
  • Inheritance helps you add features like manager privileges
  • OOP is ideal for modeling inventory, sales, and team structure

Comments

Leave a comment