Skip to content

Commit 7cf2d80

Browse files
Aditiido777
Aditi
authored andcommitted
git'n a system-desgin-primer
1 parent 9f28e4e commit 7cf2d80

File tree

22 files changed

+725
-0
lines changed

22 files changed

+725
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
1. User Class:
3+
4+
a. Represents a user of the streaming service.
5+
b. Can create playlists and receive content recommendations based on their playlists.
6+
7+
2. Subscription Class:
8+
9+
a. Represents a user's subscription type and monthly fee.
10+
11+
3. Media Class:
12+
13+
a. Represents a media item (e.g., a movie or song) with attributes like title, genre, and play count.
14+
b. Includes a method to play the media, which increments the play count.
15+
16+
4. Playlist Class:
17+
18+
a. Represents a user-created playlist that can hold multiple media items.
19+
b. Includes methods to add media to the playlist and display the playlist.
20+
21+
5. MediaLibrary Class:
22+
23+
a. Manages the collection of media items available for streaming.
24+
b. Includes methods to add media to the library and provide recommendations based on user playlists.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
class User:
2+
def __init__(self, user_id, name, subscription):
3+
self.user_id = user_id
4+
self.name = name
5+
self.subscription = subscription
6+
self.playlists = []
7+
8+
def create_playlist(self, playlist_name):
9+
playlist = Playlist(playlist_name)
10+
self.playlists.append(playlist)
11+
print(f"Playlist '{playlist_name}' created for {self.name}.")
12+
13+
def recommend_content(self, media_library):
14+
recommendations = media_library.get_recommendations(self)
15+
print(f"Recommendations for {self.name}: {', '.join(recommendations)}")
16+
17+
class Subscription:
18+
def __init__(self, subscription_type, monthly_fee):
19+
self.subscription_type = subscription_type
20+
self.monthly_fee = monthly_fee
21+
22+
class Media:
23+
def __init__(self, media_id, title, genre):
24+
self.media_id = media_id
25+
self.title = title
26+
self.genre = genre
27+
self.play_count = 0
28+
29+
def play(self):
30+
self.play_count += 1
31+
print(f"Playing '{self.title}'.")
32+
33+
class Playlist:
34+
def __init__(self, name):
35+
self.name = name
36+
self.media_items = []
37+
38+
def add_media(self, media):
39+
self.media_items.append(media)
40+
print(f"Added '{media.title}' to playlist '{self.name}'.")
41+
42+
def show_playlist(self):
43+
print(f"Playlist '{self.name}': {[media.title for media in self.media_items]}")
44+
45+
class MediaLibrary:
46+
def __init__(self):
47+
self.media_collection = []
48+
49+
def add_media(self, media):
50+
self.media_collection.append(media)
51+
print(f"Media '{media.title}' added to the library.")
52+
53+
def get_recommendations(self, user):
54+
# Simple recommendation based on genres of user's playlists
55+
recommended = []
56+
for playlist in user.playlists:
57+
for media in playlist.media_items:
58+
if media.genre not in recommended:
59+
recommended.append(media.genre)
60+
61+
# Return a list of media titles from the library that match recommended genres
62+
recommendations = [media.title for media in self.media_collection if media.genre in recommended]
63+
return recommendations[:5] # Limit to 5 recommendations
64+
65+
# Example Usage
66+
# Create media library and add media
67+
media_library = MediaLibrary()
68+
media_library.add_media(Media(1, "Inception", "Sci-Fi"))
69+
media_library.add_media(Media(2, "Titanic", "Romance"))
70+
media_library.add_media(Media(3, "The Matrix", "Sci-Fi"))
71+
media_library.add_media(Media(4, "The Godfather", "Crime"))
72+
media_library.add_media(Media(5, "Interstellar", "Sci-Fi"))
73+
74+
# Create a subscription
75+
basic_subscription = Subscription("Basic", 9.99)
76+
77+
# Create a user
78+
user1 = User(1, "Alice", basic_subscription)
79+
80+
# User creates a playlist and adds media
81+
user1.create_playlist("Favorites")
82+
user1.playlists[0].add_media(media_library.media_collection[0]) # Inception
83+
user1.playlists[0].add_media(media_library.media_collection[2]) # The Matrix
84+
85+
# Show the user's playlist
86+
user1.playlists[0].show_playlist()
87+
88+
# Get recommendations for the user
89+
user1.recommend_content(media_library)
90+
91+
# Play a media item
92+
media_library.media_collection[0].play() # Play Inception
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class Account:
2+
def __init__(self, account_number, account_holder, balance=0):
3+
self.account_number = account_number
4+
self.account_holder = account_holder
5+
self.balance = balance
6+
7+
def deposit(self, amount):
8+
self.balance += amount
9+
print(f"Deposited {amount}. New balance: {self.balance}")
10+
11+
def withdraw(self, amount):
12+
if amount > self.balance:
13+
print("Insufficient funds.")
14+
else:
15+
self.balance -= amount
16+
print(f"Withdrawn {amount}. New balance: {self.balance}")
17+
18+
def get_balance(self):
19+
return self.balance
20+
21+
class Transaction:
22+
def __init__(self, transaction_id, account, amount, transaction_type):
23+
self.transaction_id = transaction_id
24+
self.account = account
25+
self.amount = amount
26+
self.transaction_type = transaction_type
27+
28+
def execute(self):
29+
if self.transaction_type == "deposit":
30+
self.account.deposit(self.amount)
31+
elif self.transaction_type == "withdraw":
32+
self.account.withdraw(self.amount)
33+
else:
34+
print("Invalid transaction type.")
35+
36+
class Bank:
37+
def __init__(self):
38+
self.accounts = {}
39+
40+
def create_account(self, account_number, account_holder):
41+
if account_number not in self.accounts:
42+
self.accounts[account_number] = Account(account_number, account_holder)
43+
print(f"Account created for {account_holder}.")
44+
else:
45+
print("Account already exists.")
46+
47+
def get_account(self, account_number):
48+
return self.accounts.get(account_number, None)
49+
50+
# Example Usage
51+
bank = Bank()
52+
bank.create_account("123456", "Alice")
53+
bank.create_account("789012", "Bob")
54+
55+
alice_account = bank.get_account("123456")
56+
transaction1 = Transaction(1, alice_account, 1000, "deposit")
57+
transaction1.execute()
58+
59+
transaction2 = Transaction(2, alice_account, 500, "withdraw")
60+
transaction2.execute()
61+
62+
print(f"Alice's balance: {alice_account.get_balance()}")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Banking System:
2+
3+
1. Account: Represents a bank account, with methods to deposit,
4+
withdraw, and check the balance.
5+
6+
2. Transaction: Represents a transaction, which can either be a
7+
deposit or withdrawal.
8+
9+
3. Bank: Manages accounts and allows for account creation.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Features of this Food Delivery System:
2+
3+
1. Customer Class: Represents a customer with attributes like name, address, and phone number.
4+
5+
2. Restaurant Class: Represents a restaurant with a menu. It allows the system to show available
6+
items and get the price of food.
7+
8+
3. DeliveryPerson Class: Represents a delivery person who is assigned to deliver an order.
9+
10+
4. Order Class: Represents an order with details like the customer, restaurant, and the food
11+
item ordered. It also includes the price of the order.
12+
13+
5. FoodDeliverySystem Class: This is the core of the system. It manages customers, restaurants,
14+
delivery personnel, and the order placement process. It assigns a delivery person to each order.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
class Customer:
2+
def __init__(self, name, address, phone):
3+
self.name = name
4+
self.address = address
5+
self.phone = phone
6+
7+
def __str__(self):
8+
return f"Customer: {self.name}, Address: {self.address}, Phone: {self.phone}"
9+
10+
11+
class Restaurant:
12+
def __init__(self, name, menu):
13+
self.name = name
14+
self.menu = menu # menu is a dictionary with item names as keys and prices as values
15+
16+
def show_menu(self):
17+
print(f"Menu for {self.name}:")
18+
for item, price in self.menu.items():
19+
print(f"{item}: ${price:.2f}")
20+
21+
def get_price(self, item):
22+
return self.menu.get(item, None)
23+
24+
def __str__(self):
25+
return f"Restaurant: {self.name}"
26+
27+
28+
class DeliveryPerson:
29+
def __init__(self, name):
30+
self.name = name
31+
32+
def deliver(self, customer, order):
33+
print(f"{self.name} is delivering {order.food_item} to {customer.name} at {customer.address}")
34+
35+
def __str__(self):
36+
return f"Delivery Person: {self.name}"
37+
38+
39+
class Order:
40+
def __init__(self, customer, restaurant, food_item):
41+
self.customer = customer
42+
self.restaurant = restaurant
43+
self.food_item = food_item
44+
self.price = self.restaurant.get_price(food_item)
45+
46+
def display_order_details(self):
47+
print(f"Order Details: \nCustomer: {self.customer.name}\nFood Item: {self.food_item}\n"
48+
f"Price: ${self.price:.2f}\nRestaurant: {self.restaurant.name}")
49+
50+
def __str__(self):
51+
return f"Order: {self.food_item} from {self.restaurant.name} for {self.customer.name}"
52+
53+
54+
class FoodDeliverySystem:
55+
def __init__(self):
56+
self.customers = []
57+
self.restaurants = []
58+
self.delivery_people = []
59+
60+
def add_customer(self, customer):
61+
self.customers.append(customer)
62+
63+
def add_restaurant(self, restaurant):
64+
self.restaurants.append(restaurant)
65+
66+
def add_delivery_person(self, delivery_person):
67+
self.delivery_people.append(delivery_person)
68+
69+
def place_order(self, customer, restaurant, food_item):
70+
if food_item not in restaurant.menu:
71+
print(f"Sorry, {food_item} is not available at {restaurant.name}.")
72+
return None
73+
74+
order = Order(customer, restaurant, food_item)
75+
order.display_order_details()
76+
delivery_person = self.assign_delivery_person()
77+
if delivery_person:
78+
delivery_person.deliver(customer, order)
79+
return order
80+
81+
def assign_delivery_person(self):
82+
if not self.delivery_people:
83+
print("No delivery person available at the moment.")
84+
return None
85+
# Assign the first available delivery person
86+
return self.delivery_people[0]
87+
88+
def show_restaurants(self):
89+
for restaurant in self.restaurants:
90+
print(restaurant)
91+
92+
def show_customers(self):
93+
for customer in self.customers:
94+
print(customer)
95+
96+
97+
# Example usage
98+
if __name__ == "__main__":
99+
# Initialize the system
100+
delivery_system = FoodDeliverySystem()
101+
102+
# Create some customers
103+
customer1 = Customer("Alice", "123 Main St", "555-1234")
104+
customer2 = Customer("Bob", "456 Oak St", "555-5678")
105+
106+
# Add customers to the system
107+
delivery_system.add_customer(customer1)
108+
delivery_system.add_customer(customer2)
109+
110+
# Create some restaurants with menus
111+
pizza_place = Restaurant("Pizza Place", {"Pizza": 10.99, "Burger": 7.99, "Pasta": 8.99})
112+
sushi_spot = Restaurant("Sushi Spot", {"Sushi Roll": 12.99, "Tempura": 9.99, "Miso Soup": 3.99})
113+
114+
# Add restaurants to the system
115+
delivery_system.add_restaurant(pizza_place)
116+
delivery_system.add_restaurant(sushi_spot)
117+
118+
# Create a delivery person
119+
delivery_person1 = DeliveryPerson("John")
120+
121+
# Add delivery person to the system
122+
delivery_system.add_delivery_person(delivery_person1)
123+
124+
# Display available restaurants
125+
print("Available Restaurants:")
126+
delivery_system.show_restaurants()
127+
128+
# Customer places an order
129+
print("\nCustomer 1 places an order:")
130+
delivery_system.place_order(customer1, pizza_place, "Pizza")
131+
132+
# Another customer places an order
133+
print("\nCustomer 2 places an order:")
134+
delivery_system.place_order(customer2, sushi_spot, "Sushi Roll")
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Explanation
2+
3+
1. Room Class:
4+
5+
a. Represents a hotel room with attributes for room number, type, price, and availability.
6+
b. Methods include book_room (to book the room) and release_room (to make the room available again).
7+
8+
2. Customer Class:
9+
10+
a. Represents a customer who can book rooms.
11+
b. Includes a method make_booking that attempts to book a room and create a Booking object if successful.
12+
13+
3. Booking Class:
14+
15+
a. Represents a booking made by a customer.
16+
b. Calculates total price based on the duration of the stay and the room price.
17+
c. Includes a method cancel_booking to release the room and remove the booking from the customer’s list.
18+
19+
4. Hotel Class:
20+
21+
a. Manages the collection of rooms.
22+
b. Provides methods to add rooms, search for available rooms, and display all rooms.

0 commit comments

Comments
 (0)