git'n a system-desgin-primer
parent
3df19b2e4f
commit
d0a17857cb
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
1. User Class:
|
||||||
|
|
||||||
|
a. Represents a user of the streaming service.
|
||||||
|
b. Can create playlists and receive content recommendations based on their playlists.
|
||||||
|
|
||||||
|
2. Subscription Class:
|
||||||
|
|
||||||
|
a. Represents a user's subscription type and monthly fee.
|
||||||
|
|
||||||
|
3. Media Class:
|
||||||
|
|
||||||
|
a. Represents a media item (e.g., a movie or song) with attributes like title, genre, and play count.
|
||||||
|
b. Includes a method to play the media, which increments the play count.
|
||||||
|
|
||||||
|
4. Playlist Class:
|
||||||
|
|
||||||
|
a. Represents a user-created playlist that can hold multiple media items.
|
||||||
|
b. Includes methods to add media to the playlist and display the playlist.
|
||||||
|
|
||||||
|
5. MediaLibrary Class:
|
||||||
|
|
||||||
|
a. Manages the collection of media items available for streaming.
|
||||||
|
b. Includes methods to add media to the library and provide recommendations based on user playlists.
|
|
@ -0,0 +1,92 @@
|
||||||
|
class User:
|
||||||
|
def __init__(self, user_id, name, subscription):
|
||||||
|
self.user_id = user_id
|
||||||
|
self.name = name
|
||||||
|
self.subscription = subscription
|
||||||
|
self.playlists = []
|
||||||
|
|
||||||
|
def create_playlist(self, playlist_name):
|
||||||
|
playlist = Playlist(playlist_name)
|
||||||
|
self.playlists.append(playlist)
|
||||||
|
print(f"Playlist '{playlist_name}' created for {self.name}.")
|
||||||
|
|
||||||
|
def recommend_content(self, media_library):
|
||||||
|
recommendations = media_library.get_recommendations(self)
|
||||||
|
print(f"Recommendations for {self.name}: {', '.join(recommendations)}")
|
||||||
|
|
||||||
|
class Subscription:
|
||||||
|
def __init__(self, subscription_type, monthly_fee):
|
||||||
|
self.subscription_type = subscription_type
|
||||||
|
self.monthly_fee = monthly_fee
|
||||||
|
|
||||||
|
class Media:
|
||||||
|
def __init__(self, media_id, title, genre):
|
||||||
|
self.media_id = media_id
|
||||||
|
self.title = title
|
||||||
|
self.genre = genre
|
||||||
|
self.play_count = 0
|
||||||
|
|
||||||
|
def play(self):
|
||||||
|
self.play_count += 1
|
||||||
|
print(f"Playing '{self.title}'.")
|
||||||
|
|
||||||
|
class Playlist:
|
||||||
|
def __init__(self, name):
|
||||||
|
self.name = name
|
||||||
|
self.media_items = []
|
||||||
|
|
||||||
|
def add_media(self, media):
|
||||||
|
self.media_items.append(media)
|
||||||
|
print(f"Added '{media.title}' to playlist '{self.name}'.")
|
||||||
|
|
||||||
|
def show_playlist(self):
|
||||||
|
print(f"Playlist '{self.name}': {[media.title for media in self.media_items]}")
|
||||||
|
|
||||||
|
class MediaLibrary:
|
||||||
|
def __init__(self):
|
||||||
|
self.media_collection = []
|
||||||
|
|
||||||
|
def add_media(self, media):
|
||||||
|
self.media_collection.append(media)
|
||||||
|
print(f"Media '{media.title}' added to the library.")
|
||||||
|
|
||||||
|
def get_recommendations(self, user):
|
||||||
|
# Simple recommendation based on genres of user's playlists
|
||||||
|
recommended = []
|
||||||
|
for playlist in user.playlists:
|
||||||
|
for media in playlist.media_items:
|
||||||
|
if media.genre not in recommended:
|
||||||
|
recommended.append(media.genre)
|
||||||
|
|
||||||
|
# Return a list of media titles from the library that match recommended genres
|
||||||
|
recommendations = [media.title for media in self.media_collection if media.genre in recommended]
|
||||||
|
return recommendations[:5] # Limit to 5 recommendations
|
||||||
|
|
||||||
|
# Example Usage
|
||||||
|
# Create media library and add media
|
||||||
|
media_library = MediaLibrary()
|
||||||
|
media_library.add_media(Media(1, "Inception", "Sci-Fi"))
|
||||||
|
media_library.add_media(Media(2, "Titanic", "Romance"))
|
||||||
|
media_library.add_media(Media(3, "The Matrix", "Sci-Fi"))
|
||||||
|
media_library.add_media(Media(4, "The Godfather", "Crime"))
|
||||||
|
media_library.add_media(Media(5, "Interstellar", "Sci-Fi"))
|
||||||
|
|
||||||
|
# Create a subscription
|
||||||
|
basic_subscription = Subscription("Basic", 9.99)
|
||||||
|
|
||||||
|
# Create a user
|
||||||
|
user1 = User(1, "Alice", basic_subscription)
|
||||||
|
|
||||||
|
# User creates a playlist and adds media
|
||||||
|
user1.create_playlist("Favorites")
|
||||||
|
user1.playlists[0].add_media(media_library.media_collection[0]) # Inception
|
||||||
|
user1.playlists[0].add_media(media_library.media_collection[2]) # The Matrix
|
||||||
|
|
||||||
|
# Show the user's playlist
|
||||||
|
user1.playlists[0].show_playlist()
|
||||||
|
|
||||||
|
# Get recommendations for the user
|
||||||
|
user1.recommend_content(media_library)
|
||||||
|
|
||||||
|
# Play a media item
|
||||||
|
media_library.media_collection[0].play() # Play Inception
|
|
@ -0,0 +1,62 @@
|
||||||
|
class Account:
|
||||||
|
def __init__(self, account_number, account_holder, balance=0):
|
||||||
|
self.account_number = account_number
|
||||||
|
self.account_holder = account_holder
|
||||||
|
self.balance = balance
|
||||||
|
|
||||||
|
def deposit(self, amount):
|
||||||
|
self.balance += amount
|
||||||
|
print(f"Deposited {amount}. New balance: {self.balance}")
|
||||||
|
|
||||||
|
def withdraw(self, amount):
|
||||||
|
if amount > self.balance:
|
||||||
|
print("Insufficient funds.")
|
||||||
|
else:
|
||||||
|
self.balance -= amount
|
||||||
|
print(f"Withdrawn {amount}. New balance: {self.balance}")
|
||||||
|
|
||||||
|
def get_balance(self):
|
||||||
|
return self.balance
|
||||||
|
|
||||||
|
class Transaction:
|
||||||
|
def __init__(self, transaction_id, account, amount, transaction_type):
|
||||||
|
self.transaction_id = transaction_id
|
||||||
|
self.account = account
|
||||||
|
self.amount = amount
|
||||||
|
self.transaction_type = transaction_type
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
if self.transaction_type == "deposit":
|
||||||
|
self.account.deposit(self.amount)
|
||||||
|
elif self.transaction_type == "withdraw":
|
||||||
|
self.account.withdraw(self.amount)
|
||||||
|
else:
|
||||||
|
print("Invalid transaction type.")
|
||||||
|
|
||||||
|
class Bank:
|
||||||
|
def __init__(self):
|
||||||
|
self.accounts = {}
|
||||||
|
|
||||||
|
def create_account(self, account_number, account_holder):
|
||||||
|
if account_number not in self.accounts:
|
||||||
|
self.accounts[account_number] = Account(account_number, account_holder)
|
||||||
|
print(f"Account created for {account_holder}.")
|
||||||
|
else:
|
||||||
|
print("Account already exists.")
|
||||||
|
|
||||||
|
def get_account(self, account_number):
|
||||||
|
return self.accounts.get(account_number, None)
|
||||||
|
|
||||||
|
# Example Usage
|
||||||
|
bank = Bank()
|
||||||
|
bank.create_account("123456", "Alice")
|
||||||
|
bank.create_account("789012", "Bob")
|
||||||
|
|
||||||
|
alice_account = bank.get_account("123456")
|
||||||
|
transaction1 = Transaction(1, alice_account, 1000, "deposit")
|
||||||
|
transaction1.execute()
|
||||||
|
|
||||||
|
transaction2 = Transaction(2, alice_account, 500, "withdraw")
|
||||||
|
transaction2.execute()
|
||||||
|
|
||||||
|
print(f"Alice's balance: {alice_account.get_balance()}")
|
|
@ -0,0 +1,9 @@
|
||||||
|
Banking System:
|
||||||
|
|
||||||
|
1. Account: Represents a bank account, with methods to deposit,
|
||||||
|
withdraw, and check the balance.
|
||||||
|
|
||||||
|
2. Transaction: Represents a transaction, which can either be a
|
||||||
|
deposit or withdrawal.
|
||||||
|
|
||||||
|
3. Bank: Manages accounts and allows for account creation.
|
|
@ -0,0 +1,14 @@
|
||||||
|
Features of this Food Delivery System:
|
||||||
|
|
||||||
|
1. Customer Class: Represents a customer with attributes like name, address, and phone number.
|
||||||
|
|
||||||
|
2. Restaurant Class: Represents a restaurant with a menu. It allows the system to show available
|
||||||
|
items and get the price of food.
|
||||||
|
|
||||||
|
3. DeliveryPerson Class: Represents a delivery person who is assigned to deliver an order.
|
||||||
|
|
||||||
|
4. Order Class: Represents an order with details like the customer, restaurant, and the food
|
||||||
|
item ordered. It also includes the price of the order.
|
||||||
|
|
||||||
|
5. FoodDeliverySystem Class: This is the core of the system. It manages customers, restaurants,
|
||||||
|
delivery personnel, and the order placement process. It assigns a delivery person to each order.
|
|
@ -0,0 +1,134 @@
|
||||||
|
class Customer:
|
||||||
|
def __init__(self, name, address, phone):
|
||||||
|
self.name = name
|
||||||
|
self.address = address
|
||||||
|
self.phone = phone
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Customer: {self.name}, Address: {self.address}, Phone: {self.phone}"
|
||||||
|
|
||||||
|
|
||||||
|
class Restaurant:
|
||||||
|
def __init__(self, name, menu):
|
||||||
|
self.name = name
|
||||||
|
self.menu = menu # menu is a dictionary with item names as keys and prices as values
|
||||||
|
|
||||||
|
def show_menu(self):
|
||||||
|
print(f"Menu for {self.name}:")
|
||||||
|
for item, price in self.menu.items():
|
||||||
|
print(f"{item}: ${price:.2f}")
|
||||||
|
|
||||||
|
def get_price(self, item):
|
||||||
|
return self.menu.get(item, None)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Restaurant: {self.name}"
|
||||||
|
|
||||||
|
|
||||||
|
class DeliveryPerson:
|
||||||
|
def __init__(self, name):
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
def deliver(self, customer, order):
|
||||||
|
print(f"{self.name} is delivering {order.food_item} to {customer.name} at {customer.address}")
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Delivery Person: {self.name}"
|
||||||
|
|
||||||
|
|
||||||
|
class Order:
|
||||||
|
def __init__(self, customer, restaurant, food_item):
|
||||||
|
self.customer = customer
|
||||||
|
self.restaurant = restaurant
|
||||||
|
self.food_item = food_item
|
||||||
|
self.price = self.restaurant.get_price(food_item)
|
||||||
|
|
||||||
|
def display_order_details(self):
|
||||||
|
print(f"Order Details: \nCustomer: {self.customer.name}\nFood Item: {self.food_item}\n"
|
||||||
|
f"Price: ${self.price:.2f}\nRestaurant: {self.restaurant.name}")
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"Order: {self.food_item} from {self.restaurant.name} for {self.customer.name}"
|
||||||
|
|
||||||
|
|
||||||
|
class FoodDeliverySystem:
|
||||||
|
def __init__(self):
|
||||||
|
self.customers = []
|
||||||
|
self.restaurants = []
|
||||||
|
self.delivery_people = []
|
||||||
|
|
||||||
|
def add_customer(self, customer):
|
||||||
|
self.customers.append(customer)
|
||||||
|
|
||||||
|
def add_restaurant(self, restaurant):
|
||||||
|
self.restaurants.append(restaurant)
|
||||||
|
|
||||||
|
def add_delivery_person(self, delivery_person):
|
||||||
|
self.delivery_people.append(delivery_person)
|
||||||
|
|
||||||
|
def place_order(self, customer, restaurant, food_item):
|
||||||
|
if food_item not in restaurant.menu:
|
||||||
|
print(f"Sorry, {food_item} is not available at {restaurant.name}.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
order = Order(customer, restaurant, food_item)
|
||||||
|
order.display_order_details()
|
||||||
|
delivery_person = self.assign_delivery_person()
|
||||||
|
if delivery_person:
|
||||||
|
delivery_person.deliver(customer, order)
|
||||||
|
return order
|
||||||
|
|
||||||
|
def assign_delivery_person(self):
|
||||||
|
if not self.delivery_people:
|
||||||
|
print("No delivery person available at the moment.")
|
||||||
|
return None
|
||||||
|
# Assign the first available delivery person
|
||||||
|
return self.delivery_people[0]
|
||||||
|
|
||||||
|
def show_restaurants(self):
|
||||||
|
for restaurant in self.restaurants:
|
||||||
|
print(restaurant)
|
||||||
|
|
||||||
|
def show_customers(self):
|
||||||
|
for customer in self.customers:
|
||||||
|
print(customer)
|
||||||
|
|
||||||
|
|
||||||
|
# Example usage
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Initialize the system
|
||||||
|
delivery_system = FoodDeliverySystem()
|
||||||
|
|
||||||
|
# Create some customers
|
||||||
|
customer1 = Customer("Alice", "123 Main St", "555-1234")
|
||||||
|
customer2 = Customer("Bob", "456 Oak St", "555-5678")
|
||||||
|
|
||||||
|
# Add customers to the system
|
||||||
|
delivery_system.add_customer(customer1)
|
||||||
|
delivery_system.add_customer(customer2)
|
||||||
|
|
||||||
|
# Create some restaurants with menus
|
||||||
|
pizza_place = Restaurant("Pizza Place", {"Pizza": 10.99, "Burger": 7.99, "Pasta": 8.99})
|
||||||
|
sushi_spot = Restaurant("Sushi Spot", {"Sushi Roll": 12.99, "Tempura": 9.99, "Miso Soup": 3.99})
|
||||||
|
|
||||||
|
# Add restaurants to the system
|
||||||
|
delivery_system.add_restaurant(pizza_place)
|
||||||
|
delivery_system.add_restaurant(sushi_spot)
|
||||||
|
|
||||||
|
# Create a delivery person
|
||||||
|
delivery_person1 = DeliveryPerson("John")
|
||||||
|
|
||||||
|
# Add delivery person to the system
|
||||||
|
delivery_system.add_delivery_person(delivery_person1)
|
||||||
|
|
||||||
|
# Display available restaurants
|
||||||
|
print("Available Restaurants:")
|
||||||
|
delivery_system.show_restaurants()
|
||||||
|
|
||||||
|
# Customer places an order
|
||||||
|
print("\nCustomer 1 places an order:")
|
||||||
|
delivery_system.place_order(customer1, pizza_place, "Pizza")
|
||||||
|
|
||||||
|
# Another customer places an order
|
||||||
|
print("\nCustomer 2 places an order:")
|
||||||
|
delivery_system.place_order(customer2, sushi_spot, "Sushi Roll")
|
|
@ -0,0 +1,22 @@
|
||||||
|
Explanation
|
||||||
|
|
||||||
|
1. Room Class:
|
||||||
|
|
||||||
|
a. Represents a hotel room with attributes for room number, type, price, and availability.
|
||||||
|
b. Methods include book_room (to book the room) and release_room (to make the room available again).
|
||||||
|
|
||||||
|
2. Customer Class:
|
||||||
|
|
||||||
|
a. Represents a customer who can book rooms.
|
||||||
|
b. Includes a method make_booking that attempts to book a room and create a Booking object if successful.
|
||||||
|
|
||||||
|
3. Booking Class:
|
||||||
|
|
||||||
|
a. Represents a booking made by a customer.
|
||||||
|
b. Calculates total price based on the duration of the stay and the room price.
|
||||||
|
c. Includes a method cancel_booking to release the room and remove the booking from the customer’s list.
|
||||||
|
|
||||||
|
4. Hotel Class:
|
||||||
|
|
||||||
|
a. Manages the collection of rooms.
|
||||||
|
b. Provides methods to add rooms, search for available rooms, and display all rooms.
|
|
@ -0,0 +1,96 @@
|
||||||
|
class Room:
|
||||||
|
def __init__(self, room_number, room_type, price):
|
||||||
|
self.room_number = room_number
|
||||||
|
self.room_type = room_type
|
||||||
|
self.price = price
|
||||||
|
self.is_available = True
|
||||||
|
|
||||||
|
def book_room(self):
|
||||||
|
if self.is_available:
|
||||||
|
self.is_available = False
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def release_room(self):
|
||||||
|
self.is_available = True
|
||||||
|
|
||||||
|
class Customer:
|
||||||
|
def __init__(self, customer_id, name):
|
||||||
|
self.customer_id = customer_id
|
||||||
|
self.name = name
|
||||||
|
self.bookings = []
|
||||||
|
|
||||||
|
def make_booking(self, room, check_in, check_out):
|
||||||
|
if room.book_room():
|
||||||
|
booking = Booking(self, room, check_in, check_out)
|
||||||
|
self.bookings.append(booking)
|
||||||
|
print(f"Booking successful for {self.name} in room {room.room_number}.")
|
||||||
|
return booking
|
||||||
|
print(f"Room {room.room_number} is not available.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
class Booking:
|
||||||
|
def __init__(self, customer, room, check_in, check_out):
|
||||||
|
self.customer = customer
|
||||||
|
self.room = room
|
||||||
|
self.check_in = check_in
|
||||||
|
self.check_out = check_out
|
||||||
|
self.total_price = self.calculate_price()
|
||||||
|
|
||||||
|
def calculate_price(self):
|
||||||
|
# Assuming check_in and check_out are datetime.date objects
|
||||||
|
stay_duration = (self.check_out - self.check_in).days
|
||||||
|
return stay_duration * self.room.price
|
||||||
|
|
||||||
|
def cancel_booking(self):
|
||||||
|
self.room.release_room()
|
||||||
|
self.customer.bookings.remove(self)
|
||||||
|
print(f"Booking for room {self.room.room_number} has been canceled.")
|
||||||
|
|
||||||
|
class Hotel:
|
||||||
|
def __init__(self):
|
||||||
|
self.rooms = []
|
||||||
|
|
||||||
|
def add_room(self, room_number, room_type, price):
|
||||||
|
room = Room(room_number, room_type, price)
|
||||||
|
self.rooms.append(room)
|
||||||
|
print(f"Room {room_number} added.")
|
||||||
|
|
||||||
|
def search_rooms(self):
|
||||||
|
available_rooms = [room for room in self.rooms if room.is_available]
|
||||||
|
return available_rooms
|
||||||
|
|
||||||
|
def display_rooms(self):
|
||||||
|
for room in self.rooms:
|
||||||
|
status = "Available" if room.is_available else "Booked"
|
||||||
|
print(f"Room {room.room_number}: {room.room_type}, Price: ${room.price}, Status: {status}")
|
||||||
|
|
||||||
|
# Example Usage
|
||||||
|
from datetime import date
|
||||||
|
|
||||||
|
hotel = Hotel()
|
||||||
|
hotel.add_room(101, "Single", 100)
|
||||||
|
hotel.add_room(102, "Double", 150)
|
||||||
|
hotel.add_room(103, "Suite", 250)
|
||||||
|
|
||||||
|
# Display all rooms
|
||||||
|
hotel.display_rooms()
|
||||||
|
|
||||||
|
# Create a customer
|
||||||
|
customer1 = Customer(1, "Alice")
|
||||||
|
|
||||||
|
# Search for available rooms
|
||||||
|
available_rooms = hotel.search_rooms()
|
||||||
|
if available_rooms:
|
||||||
|
# Customer books the first available room
|
||||||
|
booking1 = customer1.make_booking(available_rooms[0], date(2024, 10, 25), date(2024, 10, 28))
|
||||||
|
|
||||||
|
# Display bookings for the customer
|
||||||
|
print(f"Bookings for {customer1.name}: {[booking.room.room_number for booking in customer1.bookings]}")
|
||||||
|
|
||||||
|
# Cancel booking
|
||||||
|
if booking1:
|
||||||
|
booking1.cancel_booking()
|
||||||
|
|
||||||
|
# Display rooms after cancellation
|
||||||
|
hotel.display_rooms()
|
|
@ -0,0 +1,10 @@
|
||||||
|
Ride-Sharing Service:
|
||||||
|
|
||||||
|
1. User: Represents a user who can request rides.
|
||||||
|
|
||||||
|
2. Driver: Represents a driver who can accept rides.
|
||||||
|
|
||||||
|
3. Ride: Represents a ride request, including the user and locations.
|
||||||
|
|
||||||
|
4. RideSharingService: Manages users and drivers and includes logic
|
||||||
|
to match rides.
|
|
@ -0,0 +1,64 @@
|
||||||
|
import random
|
||||||
|
|
||||||
|
class User:
|
||||||
|
def __init__(self, user_id, name):
|
||||||
|
self.user_id = user_id
|
||||||
|
self.name = name
|
||||||
|
self.rides = []
|
||||||
|
|
||||||
|
def request_ride(self, pickup_location, dropoff_location):
|
||||||
|
ride = Ride(self, pickup_location, dropoff_location)
|
||||||
|
return ride
|
||||||
|
|
||||||
|
class Driver:
|
||||||
|
def __init__(self, driver_id, name):
|
||||||
|
self.driver_id = driver_id
|
||||||
|
self.name = name
|
||||||
|
self.rides = []
|
||||||
|
|
||||||
|
def accept_ride(self, ride):
|
||||||
|
self.rides.append(ride)
|
||||||
|
ride.driver = self
|
||||||
|
print(f"Driver {self.name} accepted ride from {ride.user.name}.")
|
||||||
|
|
||||||
|
class Ride:
|
||||||
|
def __init__(self, user, pickup_location, dropoff_location):
|
||||||
|
self.user = user
|
||||||
|
self.pickup_location = pickup_location
|
||||||
|
self.dropoff_location = dropoff_location
|
||||||
|
self.driver = None
|
||||||
|
self.ride_id = random.randint(1000, 9999) # Random ID for ride
|
||||||
|
|
||||||
|
class RideSharingService:
|
||||||
|
def __init__(self):
|
||||||
|
self.drivers = []
|
||||||
|
self.users = []
|
||||||
|
|
||||||
|
def register_driver(self, driver_id, name):
|
||||||
|
driver = Driver(driver_id, name)
|
||||||
|
self.drivers.append(driver)
|
||||||
|
print(f"Driver {name} registered.")
|
||||||
|
|
||||||
|
def register_user(self, user_id, name):
|
||||||
|
user = User(user_id, name)
|
||||||
|
self.users.append(user)
|
||||||
|
print(f"User {name} registered.")
|
||||||
|
|
||||||
|
def match_ride(self, ride):
|
||||||
|
if self.drivers:
|
||||||
|
selected_driver = random.choice(self.drivers) # Simple random matching
|
||||||
|
selected_driver.accept_ride(ride)
|
||||||
|
else:
|
||||||
|
print("No drivers available.")
|
||||||
|
|
||||||
|
# Example Usage
|
||||||
|
ride_sharing_service = RideSharingService()
|
||||||
|
ride_sharing_service.register_driver(1, "John")
|
||||||
|
ride_sharing_service.register_driver(2, "Jane")
|
||||||
|
|
||||||
|
ride_sharing_service.register_user(101, "Alice")
|
||||||
|
ride_sharing_service.register_user(102, "Bob")
|
||||||
|
|
||||||
|
alice = ride_sharing_service.users[0]
|
||||||
|
ride_request = alice.request_ride("Location A", "Location B")
|
||||||
|
ride_sharing_service.match_ride(ride_request)
|
|
@ -0,0 +1,5 @@
|
||||||
|
Video Conferencing System:
|
||||||
|
|
||||||
|
1. User: Represents a user in the system.
|
||||||
|
|
||||||
|
2. Meeting: Handles participant management and audio/video streaming.
|
|
@ -0,0 +1,60 @@
|
||||||
|
import random
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
|
class User:
|
||||||
|
def __init__(self, user_id, name):
|
||||||
|
self.user_id = user_id
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
class Meeting:
|
||||||
|
def __init__(self, meeting_id):
|
||||||
|
self.meeting_id = meeting_id
|
||||||
|
self.participants = []
|
||||||
|
self.is_active = True
|
||||||
|
|
||||||
|
def add_participant(self, user):
|
||||||
|
self.participants.append(user)
|
||||||
|
print(f"{user.name} joined the meeting {self.meeting_id}.")
|
||||||
|
|
||||||
|
def remove_participant(self, user):
|
||||||
|
self.participants.remove(user)
|
||||||
|
print(f"{user.name} left the meeting {self.meeting_id}.")
|
||||||
|
|
||||||
|
def stream_audio_video(self):
|
||||||
|
# Simulated audio/video streaming
|
||||||
|
while self.is_active:
|
||||||
|
print(f"Streaming audio/video in meeting {self.meeting_id}...")
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
def end_meeting(self):
|
||||||
|
self.is_active = False
|
||||||
|
print(f"Meeting {self.meeting_id} has ended.")
|
||||||
|
|
||||||
|
class VideoConferencingSystem:
|
||||||
|
def __init__(self):
|
||||||
|
self.meetings = {}
|
||||||
|
|
||||||
|
def create_meeting(self):
|
||||||
|
meeting_id = random.randint(1000, 9999)
|
||||||
|
meeting = Meeting(meeting_id)
|
||||||
|
self.meetings[meeting_id] = meeting
|
||||||
|
print(f"Meeting {meeting_id} created.")
|
||||||
|
return meeting
|
||||||
|
|
||||||
|
# Example Usage
|
||||||
|
vcs = VideoConferencingSystem()
|
||||||
|
meeting = vcs.create_meeting()
|
||||||
|
|
||||||
|
user1 = User(1, "Alice")
|
||||||
|
user2 = User(2, "Bob")
|
||||||
|
|
||||||
|
meeting.add_participant(user1)
|
||||||
|
meeting.add_participant(user2)
|
||||||
|
|
||||||
|
stream_thread = threading.Thread(target=meeting.stream_audio_video)
|
||||||
|
stream_thread.start()
|
||||||
|
|
||||||
|
time.sleep(5)
|
||||||
|
meeting.end_meeting()
|
||||||
|
stream_thread.join()
|
|
@ -0,0 +1,7 @@
|
||||||
|
Payment Processing System:
|
||||||
|
|
||||||
|
1. User: Represents a user with a balance.
|
||||||
|
|
||||||
|
2. Transaction: Represents a payment transaction between users.
|
||||||
|
|
||||||
|
3. PaymentProcessingSystem: Processes transactions and handles fund transfers.
|
|
@ -0,0 +1,34 @@
|
||||||
|
import random
|
||||||
|
|
||||||
|
class User:
|
||||||
|
def __init__(self, user_id, name, balance):
|
||||||
|
self.user_id = user_id
|
||||||
|
self.name = name
|
||||||
|
self.balance = balance
|
||||||
|
|
||||||
|
class Transaction:
|
||||||
|
def __init__(self, sender, receiver, amount):
|
||||||
|
self.transaction_id = random.randint(1000, 9999)
|
||||||
|
self.sender = sender
|
||||||
|
self.receiver = receiver
|
||||||
|
self.amount = amount
|
||||||
|
|
||||||
|
class PaymentProcessingSystem:
|
||||||
|
def process_payment(self, transaction):
|
||||||
|
if transaction.sender.balance >= transaction.amount:
|
||||||
|
transaction.sender.balance -= transaction.amount
|
||||||
|
transaction.receiver.balance += transaction.amount
|
||||||
|
print(f"Transaction {transaction.transaction_id} successful: {transaction.sender.name} paid {transaction.receiver.name} ${transaction.amount}.")
|
||||||
|
else:
|
||||||
|
print(f"Transaction {transaction.transaction_id} failed: Insufficient funds.")
|
||||||
|
|
||||||
|
# Example Usage
|
||||||
|
user1 = User(1, "Alice", 100)
|
||||||
|
user2 = User(2, "Bob", 50)
|
||||||
|
|
||||||
|
payment_system = PaymentProcessingSystem()
|
||||||
|
transaction = Transaction(user1, user2, 30)
|
||||||
|
payment_system.process_payment(transaction)
|
||||||
|
|
||||||
|
print(f"{user1.name}'s balance: ${user1.balance}")
|
||||||
|
print(f"{user2.name}'s balance: ${user2.balance}")
|
|
@ -0,0 +1,36 @@
|
||||||
|
import threading
|
||||||
|
|
||||||
|
class Document:
|
||||||
|
def __init__(self, title):
|
||||||
|
self.title = title
|
||||||
|
self.content = ""
|
||||||
|
self.lock = threading.Lock()
|
||||||
|
|
||||||
|
def edit(self, new_content):
|
||||||
|
with self.lock:
|
||||||
|
self.content = new_content
|
||||||
|
print(f"Document '{self.title}' updated: {self.content}")
|
||||||
|
|
||||||
|
class User:
|
||||||
|
def __init__(self, user_id, name):
|
||||||
|
self.user_id = user_id
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
def edit_document(self, document, new_content):
|
||||||
|
print(f"{self.name} is editing the document '{document.title}'...")
|
||||||
|
document.edit(new_content)
|
||||||
|
|
||||||
|
# Example Usage
|
||||||
|
document = Document("Project Plan")
|
||||||
|
user1 = User(1, "Alice")
|
||||||
|
user2 = User(2, "Bob")
|
||||||
|
|
||||||
|
# Simulating concurrent edits
|
||||||
|
thread1 = threading.Thread(target=user1.edit_document, args=(document, "Draft by Alice."))
|
||||||
|
thread2 = threading.Thread(target=user2.edit_document, args=(document, "Draft by Bob."))
|
||||||
|
|
||||||
|
thread1.start()
|
||||||
|
thread2.start()
|
||||||
|
|
||||||
|
thread1.join()
|
||||||
|
thread2.join()
|
|
@ -0,0 +1,7 @@
|
||||||
|
1. Document: Represents a document being edited, with content locked for
|
||||||
|
safe concurrent editing.
|
||||||
|
|
||||||
|
2. User: Represents a user editing the document.
|
||||||
|
|
||||||
|
3. Each user can edit the document concurrently, with proper locking to prevent
|
||||||
|
data corruption.
|
|
@ -0,0 +1,5 @@
|
||||||
|
Smart Home System:
|
||||||
|
|
||||||
|
1. SmartDevice: Represents a smart device with on/off control.
|
||||||
|
|
||||||
|
2. SmartHomeSystem: Manages devices and controls their states.
|
|
@ -0,0 +1,44 @@
|
||||||
|
class SmartDevice:
|
||||||
|
def __init__(self, device_id, device_type):
|
||||||
|
self.device_id = device_id
|
||||||
|
self.device_type = device_type
|
||||||
|
self.is_on = False
|
||||||
|
|
||||||
|
def turn_on(self):
|
||||||
|
self.is_on = True
|
||||||
|
print(f"{self.device_type} {self.device_id} turned on.")
|
||||||
|
|
||||||
|
def turn_off(self):
|
||||||
|
self.is_on = False
|
||||||
|
print(f"{self.device_type} {self.device_id} turned off.")
|
||||||
|
|
||||||
|
class SmartHomeSystem:
|
||||||
|
def __init__(self):
|
||||||
|
self.devices = {}
|
||||||
|
|
||||||
|
def add_device(self, device):
|
||||||
|
self.devices[device.device_id] = device
|
||||||
|
print(f"Added {device.device_type} {device.device_id}.")
|
||||||
|
|
||||||
|
def control_device(self, device_id, action):
|
||||||
|
device = self.devices.get(device_id)
|
||||||
|
if device:
|
||||||
|
if action == "on":
|
||||||
|
device.turn_on()
|
||||||
|
elif action == "off":
|
||||||
|
device.turn_off()
|
||||||
|
else:
|
||||||
|
print("Invalid action.")
|
||||||
|
else:
|
||||||
|
print("Device not found.")
|
||||||
|
|
||||||
|
# Example Usage
|
||||||
|
smart_home = SmartHomeSystem()
|
||||||
|
light = SmartDevice(1, "Light")
|
||||||
|
thermostat = SmartDevice(2, "Thermostat")
|
||||||
|
|
||||||
|
smart_home.add_device(light)
|
||||||
|
smart_home.add_device(thermostat)
|
||||||
|
|
||||||
|
smart_home.control_device(1, "on") # Turn on the light
|
||||||
|
smart_home.control_device(2, "off") # Turn off the thermostat
|
Binary file not shown.
After Width: | Height: | Size: 120 KiB |
Binary file not shown.
After Width: | Height: | Size: 94 KiB |
Binary file not shown.
After Width: | Height: | Size: 112 KiB |
Binary file not shown.
After Width: | Height: | Size: 114 KiB |
Loading…
Reference in New Issue