Skip to content

Commit a1cbefe

Browse files
committed
Added Static and Class Methods - Exercise and Polymorphism and Abstraction - Lab
1 parent 3b51233 commit a1cbefe

File tree

25 files changed

+481
-0
lines changed

25 files changed

+481
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from math import ceil
2+
3+
4+
class PhotoAlbum:
5+
6+
def __init__(self, pages):
7+
self.pages = pages
8+
self.photos = [[] for _ in range(pages)]
9+
10+
@classmethod
11+
def from_photos_count(cls, photos_count):
12+
return cls(ceil(photos_count / 4))
13+
14+
def add_photo(self, label):
15+
for row in range(len(self.photos)):
16+
if len(self.photos[row]) < 4:
17+
self.photos[row].append(label)
18+
print(self.photos)
19+
return f"{label} photo added successfully on page {row + 1} slot {len(self.photos[row])}"
20+
21+
return "No more free slots"
22+
23+
def display(self):
24+
photos = ["-" * 11]
25+
26+
for row in self.photos:
27+
photos.append(("[] " * len(row)).rstrip())
28+
photos.append("-" * 11)
29+
30+
return '\n'.join(photos)
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Customer:
2+
3+
def __init__(self, name, age, id):
4+
self.name = name
5+
self.age = age
6+
self.id = id
7+
self.rented_dvds = []
8+
9+
def __repr__(self):
10+
return f"{self.id}: {self.name} of age {self.age} has {len(self.rented_dvds)} rented DVD's ({', '.join([dvd.name for dvd in self.rented_dvds])})"
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from calendar import month_name
2+
3+
4+
class DVD:
5+
6+
def __init__(self, name, id, creation_year, creation_month, age_restriction):
7+
self.name = name
8+
self.id = id
9+
self. creation_year = creation_year
10+
self.creation_month = creation_month
11+
self.age_restriction = age_restriction
12+
self.is_rented = False
13+
14+
@classmethod
15+
def from_date(cls, id, name, date, age_restriction):
16+
day, month, year = [int(x) for x in date.split(".")]
17+
return cls(name, id, year, month_name[month], age_restriction)
18+
19+
def __repr__(self):
20+
return f"{self.id}: {self.name} ({self.creation_month} {self.creation_year}) has age restriction {self.age_restriction}. Status: {'rented' if self.is_rented else 'not rented'}"
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from project.customer import Customer
2+
from project.dvd import DVD
3+
4+
5+
class MovieWorld:
6+
7+
def __init__(self, name):
8+
self.name = name
9+
self.customers = []
10+
self.dvds = []
11+
12+
@staticmethod
13+
def dvd_capacity():
14+
return 15
15+
16+
@staticmethod
17+
def customer_capacity():
18+
return 10
19+
20+
def add_customer(self, customer: Customer):
21+
if len(self.customers) < self.customer_capacity():
22+
self.customers.append(customer)
23+
24+
def add_dvd(self, dvd: DVD):
25+
if len(self.dvds) < self.dvd_capacity():
26+
self.dvds.append(dvd)
27+
28+
def rent_dvd(self, customer_id, dvd_id):
29+
customer = [x for x in self.customers if x.id == customer_id][0]
30+
dvd = [x for x in self.dvds if x.id == dvd_id][0]
31+
32+
if dvd in customer.rented_dvds:
33+
return f"{customer.name} has already rented {dvd.name}"
34+
35+
if dvd.is_rented:
36+
return "DVD is already rented"
37+
38+
if customer.age < dvd.age_restriction:
39+
return f"{customer.name} should be at least {dvd.age_restriction} to rent this movie"
40+
41+
customer.rented_dvds.append(dvd)
42+
dvd.is_rented = True
43+
44+
return f"{customer.name} has successfully rented {dvd.name}"
45+
46+
def return_dvd(self, customer_id, dvd_id):
47+
customer = [x for x in self.customers if x.id == customer_id][0]
48+
dvd = [x for x in self.dvds if x.id == dvd_id][0]
49+
50+
if dvd not in customer.rented_dvds:
51+
return f"{customer.name} does not have that DVD"
52+
53+
customer.rented_dvds.remove(dvd)
54+
dvd.is_rented = False
55+
56+
return f"{customer.name} has successfully returned {dvd.name}"
57+
58+
def __repr__(self):
59+
return "\n".join([
60+
*[str(c) for c in self.customers],
61+
*[str(d) for d in self.dvds]
62+
])
63+

OOP/5.Static and Class Methods/Static and Class Methods - Exercise/03. Document Management/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Category:
2+
3+
def __init__(self, id, name):
4+
self.id = id
5+
self.name = name
6+
7+
def edit(self, new_name):
8+
self.name = new_name
9+
10+
def __repr__(self):
11+
return f"Category {self.id}: {self.name}"
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Document:
2+
3+
def __init__(self, id, category_id, topic_id, file_name):
4+
self.id = id
5+
self.category_id = category_id
6+
self.topic_id = topic_id
7+
self.file_name = file_name
8+
self.tags = []
9+
10+
@classmethod
11+
def from_instances(cls, id, category, topic, file_name):
12+
return cls(id, category.id, topic.id, file_name)
13+
14+
def add_tag(self, tag_content):
15+
if tag_content not in self.tags:
16+
self.tags.append(tag_content)
17+
18+
def remove_tag(self, tag_content):
19+
if tag_content in self.tags:
20+
self.tags.remove(tag_content)
21+
22+
def edit(self, filename):
23+
self.file_name = filename
24+
25+
def __repr__(self):
26+
return f"Document {self.id}: {self.file_name}; category {self.category_id}, topic {self.topic_id}, tags: {', '.join(self.tags)}"
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from project.topic import Topic
2+
from project.category import Category
3+
from project.document import Document
4+
5+
6+
class Storage:
7+
8+
def __init__(self):
9+
self.categories = []
10+
self.topics = []
11+
self.documents = []
12+
13+
def add_category(self, category: Category):
14+
if category not in self.categories:
15+
self.categories.append(category)
16+
17+
def add_topic(self, topic: Topic):
18+
if topic not in self.topics:
19+
self.topics.append(topic)
20+
21+
def add_document(self, document: Document):
22+
if document not in self.documents:
23+
self.documents.append(document)
24+
25+
def edit_category(self, category_id, new_name):
26+
category = [x for x in self.categories if x.id == category_id][0]
27+
category.edit(new_name)
28+
29+
def edit_topic(self, topic_id, new_topic, new_storage_folder):
30+
topic = [x for x in self.topics if x.id == topic_id][0]
31+
topic.edit(new_topic, new_storage_folder)
32+
33+
def edit_document(self, document_id, new_file_name):
34+
document = [x for x in self.documents if x.id == document_id][0]
35+
document.edit(new_file_name)
36+
37+
def delete_category(self, category_id):
38+
self.categories = [x for x in self.categories if x.id != category_id]
39+
40+
def delete_topic(self, topic_id):
41+
self.topics = [x for x in self.topics if x.id != topic_id]
42+
43+
def delete_document(self, document_id):
44+
self.documents.remove(self.get_document(document_id))
45+
46+
def get_document(self, document_id):
47+
return [x for x in self.documents if x.id == document_id][0]
48+
49+
def __repr__(self):
50+
return '\n'.join([str(doc) for doc in self.documents])
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Topic:
2+
3+
def __init__(self, id, topic, storage_folder):
4+
self.id = id
5+
self.topic = topic
6+
self.storage_folder = storage_folder
7+
8+
def edit(self, new_topic, new_storage_folder):
9+
self.topic, self.storage_folder = new_topic, new_storage_folder
10+
11+
def __repr__(self):
12+
return f"Topic {self.id}: {self.topic} in {self.storage_folder}"
13+

OOP/5.Static and Class Methods/Static and Class Methods - Exercise/04. Gym/project/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class ClassIdMixin:
2+
3+
@classmethod
4+
def get_next_id(cls):
5+
cls.id += 1
6+
7+
return cls.id
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from project.class_id_mixin import ClassIdMixin
2+
3+
4+
class Customer(ClassIdMixin):
5+
id = 0
6+
7+
def __init__(self, name, address, email):
8+
self.name = name
9+
self.address = address
10+
self.email = email
11+
self.id = self.get_next_id()
12+
13+
def __repr__(self):
14+
return f"Customer <{self.id}> {self.name}; Address: {self.address}; Email: {self.email}"
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from project.class_id_mixin import ClassIdMixin
2+
3+
4+
class Equipment(ClassIdMixin):
5+
id = 0
6+
7+
def __init__(self, name):
8+
self.name = name
9+
self.id = self.get_next_id()
10+
11+
def __repr__(self):
12+
return f"Equipment <{self.id}> {self.name}"
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from project.class_id_mixin import ClassIdMixin
2+
3+
4+
class ExercisePlan(ClassIdMixin):
5+
id = 0
6+
7+
def __init__(self, trainer_id, equipment_id, duration):
8+
self.trainer_id = trainer_id
9+
self.equipment_id = equipment_id
10+
self.duration = duration
11+
self.id = self.get_next_id()
12+
13+
@classmethod
14+
def from_hours(cls, trainer_id, equipment_id, hours):
15+
return cls(trainer_id, equipment_id, hours * 60)
16+
17+
def __repr__(self):
18+
return f"Plan <{self.id}> with duration {self.duration} minutes"
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from project.customer import Customer
2+
from project.equipment import Equipment
3+
from project.exercise_plan import ExercisePlan
4+
from project.subscription import Subscription
5+
from project.trainer import Trainer
6+
7+
8+
class Gym:
9+
10+
def __init__(self):
11+
self.customers = []
12+
self.trainers = []
13+
self.equipment = []
14+
self.plans = []
15+
self.subscriptions = []
16+
17+
def add_customer(self, customer: Customer):
18+
if customer not in self.customers:
19+
self.customers.append(customer)
20+
21+
def add_trainer(self, trainer: Trainer):
22+
if trainer not in self.trainers:
23+
self.trainers.append(trainer)
24+
25+
def add_equipment(self, equipment: Equipment):
26+
if equipment not in self.equipment:
27+
self.equipment.append(equipment)
28+
29+
def add_plan(self, plan: ExercisePlan):
30+
if plan not in self.plans:
31+
self.plans.append(plan)
32+
33+
def add_subscription(self, subscription: Subscription):
34+
if subscription not in self.subscriptions:
35+
self.subscriptions.append(subscription)
36+
37+
def subscription_info(self, subscription_id):
38+
subscription = [s for s in self.subscriptions if s.id == subscription_id][0]
39+
customer = [c for c in self.customers if c.id == subscription.customer_id][0]
40+
trainer = [t for t in self.trainers if t.id == subscription.trainer_id][0]
41+
plan = [p for p in self.plans if p.id == subscription.exercise_id][0]
42+
equipment = [e for e in self.equipment if e.id == plan.equipment_id][0]
43+
44+
return f"{subscription}\n{customer}\n{trainer}\n{equipment}\n{plan}"
45+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from project.class_id_mixin import ClassIdMixin
2+
3+
4+
class Subscription(ClassIdMixin):
5+
id = 0
6+
7+
def __init__(self, date, customer_id, trainer_id, exercise_id):
8+
self.date = date
9+
self.customer_id = customer_id
10+
self.trainer_id = trainer_id
11+
self.exercise_id = exercise_id
12+
self.id = self.get_next_id()
13+
14+
def __repr__(self):
15+
return f"Subscription <{self.id}> on {self.date}"
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from project.class_id_mixin import ClassIdMixin
2+
3+
4+
class Trainer(ClassIdMixin):
5+
id = 0
6+
7+
def __init__(self, name):
8+
self.name = name
9+
self.id = self.get_next_id()
10+
11+
def __repr__(self):
12+
return f"Trainer <{self.id}> {self.name}"
13+

0 commit comments

Comments
 (0)