Python 7: Practice OOP with 5 Real-World Problems

🎯 Goal:

In this blog, you’ll build real Python classes to solve practical problems. Each one will stretch your understanding of:

  • Class creation and methods
  • Object interaction
  • Inheritance
  • Retail sales logic

If you’ve followed the previous posts, this is your moment to apply it all!


🛒 1. Build a Product Class

Every retail system starts with a product. Let’s model one!

Problem:
Create a class Product with the following:

  • name, price, and stock as attributes
  • A method sell(quantity) that reduces the stock
  • A method get_info() that prints product name and remaining stock

Example:

p1 = Product("Ring", 1000, 10)
p1.sell(2)      # reduces stock to 8
p1.get_info()   # shows name + updated stock

💸 2. Add Discount Logic

Retail isn’t just about selling — it’s about pricing smartly.

Problem:
Add a method apply_discount(percent) to your Product class that updates the price after reducing it by the given percentage.

Update get_info() to also show the current price.

Example:

p1.apply_discount(10)  # price drops to 900
p1.get_info()          # shows updated price and stock

👥 3. Model an Employee Who Processes Sales

Let’s bring employees into the scene.

Problem:
Create a class Employee with:

  • name and role
  • A method process_sale(product, quantity)
    This method should call product.sell() and print a message showing who sold what.

Example:

e1 = Employee("Prince", "Sales Associate")
e1.process_sale(p1, 3)
# Prince sold 3 x Ring for $2700

📊 4. Track Each Sale as a Sale Object

We don’t just want to sell — we want to track each transaction.

Problem:
Create a Sale class that stores:

  • The product, employee, and quantity
  • Automatically calculates the total
  • A summary() method that prints the full transaction

Example:

s1 = Sale(p1, e1, 2)
s1.summary()
# Prince sold 2 x Ring for $1800

🏪 5. Build a Store That Manages Inventory

Now let’s manage multiple products in a single place.

Problem:
Create a class Store with:

  • A list of products
  • A method add_product(product)
  • A method list_inventory() that prints each product’s name and stock

Bonus: Add a method to find a product by name.

Example:

store = Store()
store.add_product(p1)
store.list_inventory()
# Ring - 5 in stock

✅ Summary

With these 5 problems, you’ve touched on:

  • Object creation and method chaining
  • Collaboration between classes
  • Real-world use of OOP in retail
  • The foundation for dashboards, APIs, and full apps

Comments

Leave a comment