Oops Hands - On

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

OOPS HANDS ON

1. Bank Account System

● Concepts Covered: Classes, Objects, Encapsulation, Inheritance


● Description: Create a BankAccount class with attributes like account_number,
balance, and methods like deposit, withdraw, and check_balance. You can
extend it by creating a SavingsAccount and CurrentAccount with additional
features.

# Define the base class BankAccount


class BankAccount:
def __init__(self, account_number, balance=0):
# Initialize account number and balance
self.account_number = account_number
self.balance = balance

def deposit(self, amount):


# Method to deposit money into the account
self.balance += amount
print(f"Deposited {amount}. New balance: {self.balance}")

def withdraw(self, amount):


# Method to withdraw money from the account
if amount > self.balance:
print("Insufficient funds")
else:
self.balance -= amount
print(f"Withdrew {amount}. New balance: {self.balance}")

def check_balance(self):
# Method to check the current balance
print(f"Current balance: {self.balance}")

# Inheritance: Create a SavingsAccount class that inherits from BankAccount


class SavingsAccount(BankAccount):
def __init__(self, account_number, balance=0, interest_rate=0.02):
# Initialize base class attributes along with interest rate
super().__init__(account_number, balance)
self.interest_rate = interest_rate
def add_interest(self):
# Method to add interest to the balance
interest = self.balance * self.interest_rate
self.balance += interest
print(f"Added interest: {interest}. New balance: {self.balance}")

# Create objects and perform actions


account = SavingsAccount("123456")
account.deposit(1000)
account.add_interest()
account.withdraw(500)
account.check_balance()

2. Library Management System

● Concepts Covered: Inheritance, Polymorphism, Encapsulation


● Description: Design a Library class with methods to add_book, remove_book, and
lend_book. Create subclasses for different types of libraries like DigitalLibrary
and PhysicalLibrary with specialized methods.

# Base class Library


class Library:
def __init__(self):
# Initialize an empty dictionary for books
self.books = {}

def add_book(self, title, author):


# Method to add a book
self.books[title] = author
print(f"Added book: {title} by {author}")

def remove_book(self, title):


# Method to remove a book
if title in self.books:
del self.books[title]
print(f"Removed book: {title}")
else:
print("Book not found")
def lend_book(self, title, borrower):
# Method to lend a book
if title in self.books:
print(f"Lent {title} to {borrower}")
else:
print("Book not available")

# Inheritance: DigitalLibrary extends Library


class DigitalLibrary(Library):
def __init__(self):
# Initialize the base class
super().__init__()
self.digital_books = []

def add_digital_book(self, title):


# Method to add a digital book
self.digital_books.append(title)
print(f"Added digital book: {title}")

# Create objects and manage books


library = DigitalLibrary()
library.add_book("Python Basics", "John Doe")
library.add_digital_book("Advanced Python")
library.lend_book("Python Basics", "Alice")
library.remove_book("Python Basics")

3. Employee Management System

● Concepts Covered: Inheritance, Encapsulation, Polymorphism


● Description: Develop an Employee class with attributes like name, id, and salary.
Create subclasses like Manager and Developer with additional attributes and
methods. Implement a method to calculate the bonus for each type of employee.

# Base class Employee


class Employee:
def __init__(self, name, emp_id, salary):
# Initialize employee attributes
self.name = name
self.emp_id = emp_id
self.salary = salary

def display_info(self):
# Method to display employee information
print(f"Employee: {self.name}, ID: {self.emp_id}, Salary: {self.salary}")

# Inheritance: Manager class extends Employee


class Manager(Employee):
def __init__(self, name, emp_id, salary, bonus):
# Initialize base class attributes along with bonus
super().__init__(name, emp_id, salary)
self.bonus = bonus

def calculate_bonus(self):
# Method to calculate total salary with bonus
total_salary = self.salary + self.bonus
print(f"Total salary for Manager {self.name}: {total_salary}")

# Inheritance: Developer class extends Employee


class Developer(Employee):
def __init__(self, name, emp_id, salary, programming_language):
# Initialize base class attributes along with programming language
super().__init__(name, emp_id, salary)
self.programming_language = programming_language

def display_info(self):
# Override the display_info method to include programming language
super().display_info()
print(f"Programming Language: {self.programming_language}")

# Create objects and manage employees


manager = Manager("Alice", 101, 70000, 5000)
developer = Developer("Bob", 102, 50000, "Python")

manager.display_info()
manager.calculate_bonus()
developer.display_info()

4 . Online Shopping Cart

● Concepts Covered: Composition, Encapsulation, Polymorphism


● Description: Build a ShoppingCart class that can contain multiple Product objects.
Implement methods to add_product, remove_product, and calculate_total.
Use composition to associate products with the shopping cart.

# Class Product to represent an individual product


class Product:
def __init__(self, name, price):
# Initialize product name and price
self.name = name
self.price = price

def __str__(self):
# String representation of the product
return f"{self.name}: ${self.price}"

# Class ShoppingCart to manage a list of products


class ShoppingCart:
def __init__(self):
# Initialize an empty list of products
self.products = []

def add_product(self, product):


# Method to add a product to the cart
self.products.append(product)
print(f"Added {product.name} to cart")

def remove_product(self, product):


# Method to remove a product from the cart
if product in self.products:
self.products.remove(product)
print(f"Removed {product.name} from cart")
else:
print("Product not in cart")

def calculate_total(self):
# Method to calculate the total cost
total = sum(product.price for product in self.products)
print(f"Total cost: ${total}")

# Create products and add them to the cart


apple = Product("Apple", 1.5)
banana = Product("Banana", 0.8)

cart = ShoppingCart()
cart.add_product(apple)
cart.add_product(banana)
cart.calculate_total()
cart.remove_product(apple)
cart.calculate_total()
5. Student Management System

● Concepts Covered: Inheritance, Encapsulation, Polymorphism


● Description: Create a Student class with attributes like name, roll_number, and
marks. Add methods to calculate grade. Extend this with subclasses like
UndergraduateStudent and GraduateStudent with specific attributes and
methods.

# Base class Student


class Student:
def __init__(self, name, roll_number, marks):
# Initialize student attributes
self.name = name
self.roll_number = roll_number
self.marks = marks

def calculate_grade(self):
# Method to calculate grade based on marks
if self.marks >= 90:
return "A"
elif self.marks >= 75:
return "B"
elif self.marks >= 60:
return "C"
else:
return "D"

def display_info(self):
# Method to display student information
grade = self.calculate_grade()
print(f"Student: {self.name}, Roll No: {self.roll_number}, Grade: {grade}")

# Inheritance: UndergraduateStudent class extends Student


class UndergraduateStudent(Student):
def __init__(self, name, roll_number, marks, year):
# Initialize base class attributes along with year
super().__init__(name, roll_number, marks)
self.year = year

def display_info(self):
# Override the display_info method to include the year
super().display_info()
print(f"Year: {self.year}")
# Create objects and manage students
student1 = UndergraduateStudent("Alice", 101, 88, "2nd Year")
student2 = Student("Bob", 102, 55)

student1.display_info()
student2.display_info()

6. Vehicle Rental System

● Concepts Covered: Inheritance, Polymorphism, Encapsulation


● Description: Design a Vehicle class with attributes like vehicle_id, brand, and
rental_price. Create subclasses like Car, Bike, and Truck with specialized
attributes. Implement methods to rent_vehicle and return_vehicle.

# Base class Vehicle


class Vehicle:
def __init__(self, vehicle_id, brand, rental_price):
# Initialize vehicle attributes
self.vehicle_id = vehicle_id
self.brand = brand
self.rental_price = rental_price

def rent_vehicle(self):
# Method to rent the vehicle
print(f"{self.brand} with ID {self.vehicle_id} rented for ${self.rental_price} per day")

def return_vehicle(self):
# Method to return the vehicle
print(f"{self.brand} with ID {self.vehicle_id} returned")

# Inheritance: Car class extends Vehicle


class Car(Vehicle):
def __init__(self, vehicle_id, brand, rental_price, seats):
# Initialize base class attributes along with seats
super().__init__(vehicle_id, brand, rental_price)
self.seats = seats

def rent_vehicle(self):
# Override the rent_vehicle method to include the number of seats
super().rent_vehicle()
print(f"This car has {self.seats} seats")
# Create objects and manage vehicle rentals
car = Car("C123", "Toyota", 50, 4)
car.rent_vehicle()
car.return_vehicle()

7. Restaurant Management System

● Concepts Covered: Inheritance, Encapsulation, Composition


● Description: Develop a Restaurant class that manages MenuItems. Create classes
like Dish, Drink, and Dessert, each with specific attributes. Implement methods to
add_to_menu, remove_from_menu, and order_item.

# Base class MenuItem


class MenuItem:
def __init__(self, name, price):
# Initialize menu item name and price
self.name = name
self.price = price

def __str__(self):
# String representation of the menu item
return f"{self.name}: ${self.price}"

# Base class Restaurant


class Restaurant:
def __init__(self):
# Initialize an empty menu list
self.menu = []

def add_to_menu(self, item):


# Method to add an item to the menu
self.menu.append(item)
print(f"Added {item.name} to the menu")

def remove_from_menu(self, item_name):


# Method to remove an item from the menu by name
self.menu = [item for item in self.menu if item.name != item_name]
print(f"Removed {item_name} from the menu")

def display_menu(self):
# Method to display the current menu
if self.menu:
print("Menu:")
for item in self.menu:
print(item)
else:
print("The menu is empty")

# Derived class Dish from MenuItem


class Dish(MenuItem):
def __init__(self, name, price, calories):
# Initialize base class attributes and additional attribute
super().__init__(name, price)
self.calories = calories

def __str__(self):
# Override string representation to include calories
return f"{self.name} (${self.price}) - {self.calories} calories"

# Derived class Drink from MenuItem


class Drink(MenuItem):
def __init__(self, name, price, size):
# Initialize base class attributes and additional attribute
super().__init__(name, price)
self.size = size

def __str__(self):
# Override string representation to include size
return f"{self.name} (${self.price}) - {self.size}ml"

# Derived class Dessert from MenuItem


class Dessert(MenuItem):
def __init__(self, name, price, is_vegan):
# Initialize base class attributes and additional attribute
super().__init__(name, price)
self.is_vegan = is_vegan

def __str__(self):
# Override string representation to include vegan info
vegan_str = "Vegan" if self.is_vegan else "Non-Vegan"
return f"{self.name} (${self.price}) - {vegan_str}"

# Example usage
restaurant = Restaurant()

# Creating menu items


dish1 = Dish("Pasta", 12.99, 600)
drink1 = Drink("Orange Juice", 3.99, 250)
dessert1 = Dessert("Chocolate Cake", 5.49, False)

# Adding items to the menu


restaurant.add_to_menu(dish1)
restaurant.add_to_menu(drink1)
restaurant.add_to_menu(dessert1)

# Displaying the menu


restaurant.display_menu()

# Removing an item from the menu


restaurant.remove_from_menu("Orange Juice")

# Displaying the updated menu


restaurant.display_menu()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy