Python PBL Converted
Python PBL Converted
E-Commerce Cart
Modules:
Ans:
product.py
class Product:
self.name = name
self.price = price
self.stock = stock
cart.py
class Cart:
self.items = {}
product.stock -= quantity
print(f"Added {quantity} x {product.name} to cart.")
else:
self.items[product] -= quantity
product.stock += quantity
if self.items[product] == 0:
del self.items[product]
else:
def total_price(self):
def view_cart(self):
print(f"Total: ₹{self.total_price()}")
discounts.py
class Discount:
@staticmethod
if coupon_code in Discount.coupons:
payment.py
class Payment:
@staticmethod
def process_payment(amount):
print(f"Processing payment of ₹
main.py
p1 = Product("Laptop", 50000, 5)
cart = Cart()
cart.add_product(p1, 1)
cart.add_product(p2, 2)
cart.view_cart()
total = cart.total_price()
Payment.process_payment(total_after_discount)
Library Management System
Use Case:
Modules:
Ans:
book.py
class Book:
self.title = title
self.author = author
self.book_id = book_id
self.copies = copies
class Library:
self.books = {}
def add_book(self, book):
self.books[book.book_id] = book
if book_id in self.books:
del self.books[book_id]
print("Book removed.")
else:
if book.title.lower() == title.lower():
return book
member.py
class Member:
self.member_id = member_id
self.name = name
self.borrowed_books = []
self.max_books = max_books
def can_borrow(self):
class Transaction:
issue_date = datetime.now()
book.copies -= 1
member.borrowed_books.append(book)
else:
if book.book_id in self.issued_books:
issue_date = self.issued_books.pop(book.book_id)[1]
return_date = datetime.now()
book.copies += 1
member.borrowed_books.remove(book)
else:
class Reports:
@staticmethod
book = library.books.get(book_id)
@staticmethod
def calculate_fines(transaction):
if overdue_days > 0:
fine = overdue_days * 10
main.py
library = Library()
transaction = Transaction()
library.add_book(book1)
library.add_book(book2)
transaction.issue_book(book1, member1)
transaction.issue_book(book2, member2)
Reports.issued_books(transaction, library)
transaction.return_book(book1, member1)
Reports.calculate_fines(transaction)
Use Case:
A hotel booking application where users book rooms, check availability, and manage
payments.
Modules:
rooms.py
class Room:
self.room_number = room_number
self.room_type = room_type
self.price = price
self.available = available
class Hotel:
self.rooms = {}
self.rooms[room.room_number] =
room
def check_availability(self):
booking.py
class Booking:
hotel.rooms[room_number].available = False
self.bookings[room_number] = customer_id
else:
if room_number in self.bookings:
del self.bookings[room_number]
hotel.rooms[room_number].available = True
else:
billing.py
class Billing:
if room_number in hotel.rooms:
self.bills[customer_id] = total_cost
else:
customer.py
class Customer:
self.name = name
self.contact = contact
main.py
hotel = Hotel()
booking =
Booking() billing =
Billing()
nAvailable Rooms:")
print(room)
available_rooms = hotel.check_availability()
print(room)
Use Case:
A teacher needs an app to calculate grades, store student info, and generate reports.
Modules:
Ans:
student.py
class Student:
self.roll_no = roll_no
self.name = name
class Marks:
self.student_marks[roll_no] = marks
grading.py
class Grading:
@staticmethod
def assign_grade(total):
return "A+"
return "A"
return "B"
60:
return "C"
50:
return "D"
else:
return "F (Fail)"
report.py
class Report:
@staticmethod
total = marks_module.calculate_total(student.roll_no)
average = marks_module.calculate_average(student.roll_no)
grade = Grading.assign_grade(average)
print("\nStudent Report:")
print(f"{student}")
print(f"Grade: {grade}")
main.py
marks_module = Marks()
Report.generate_report(student2, marks_module)