SCD Lab3
SCD Lab3
1. Employee Class
class Employee:
def __init__(self, role, department, salary):
self.role = role
self.department = department
self.salary = salary
def show_details(self):
return f"Role: {self.role}, Department: {self.department}, Salary:
{self.salary}"
2. Book Class
class Book:
def __init__(self, title, author, price):
self.title = title
self.author = author
self.price = price
def display_details(self):
return f"Title: {self.title}, Author: {self.author}, Price: $
{self.price:.2f}"
def display_details(self):
return f"{self.year} {self.make} {self.model}"
class Car(Vehicle):
def __init__(self, make, model, year, number_of_doors):
super().__init__(make, model, year)
self.number_of_doors = number_of_doors
def display_details(self):
return f"{super().display_details()}, Doors:
{self.number_of_doors}"
class Motorcycle(Vehicle):
def __init__(self, make, model, year, type_of_handlebar):
super().__init__(make, model, year)
self.type_of_handlebar = type_of_handlebar
def display_details(self):
return f"{super().display_details()}, Handlebar:
{self.type_of_handlebar}"
class Shape:
def area(self):
pass
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
# Demonstrating Polymorphism
shapes = [Circle(5), Rectangle(4, 6)]
for shape in shapes:
print(f"Area: {shape.area():.2f}")
5. BankAccount Class
class BankAccount:
def __init__(self, balance=0):
self.__balance = balance
def check_balance(self):
return f"Current Balance: ${self.__balance:.2f}"
6. Abstract Employee Class
from abc import ABC, abstractmethod
class Employee(ABC):
@abstractmethod
def calculate_salary(self):
pass
class FullTimeEmployee(Employee):
def __init__(self, monthly_salary):
self.monthly_salary = monthly_salary
def calculate_salary(self):
return f"Monthly Salary: ${self.monthly_salary:.2f}"
class PartTimeEmployee(Employee):
def __init__(self, hourly_rate, hours_worked):
self.hourly_rate = hourly_rate
self.hours_worked = hours_worked
def calculate_salary(self):
return f"Total Salary: ${self.hourly_rate * self.hours_worked:.2f}"
7. E-Commerce System
class Product:
def __init__(self, name, price):
self.name = name
self.price = price
class Customer:
def __init__(self, name):
self.name = name
class Order:
def __init__(self, customer):
self.customer = customer
self.products = []
def calculate_total(self):
return sum(product.price for product in self.products)
def display_order_details(self):
details = f"Customer: {self.customer.name}\nOrder Details:\n"
for product in self.products:
details += f"- {product.name}: ${product.price:.2f}\n"
details += f"Total Price: ${self.calculate_total():.2f}"
return details