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
Lines changed: 31 additions & 0 deletions
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+
Lines changed: 11 additions & 0 deletions
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+
Lines changed: 21 additions & 0 deletions
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+

0 commit comments

Comments
 (0)