# Static and Class Methods - Exercise
# https://judge.softuni.org/Contests/Compete/Index/2431#3
============================================================================
# 01. Photo Album
# 02. Movie World
# 03. Document Management
# 04. Gym
============================================================================
________________________
| |
| 01. Photo Album |
|________________________|
from math import ceil
class PhotoAlbum:
PHOTO_COUNT = 4
DELIMITER = 11 * '-'
def __init__(self, pages: int):
self.pages = pages
self.photos = self.build_photos()
self.index_pages = 0
self.index_slots = 0
def build_photos(self):
result = []
for _ in range(self.pages):
result.append([] * self.PHOTO_COUNT)
return result
@classmethod
def from_photos_count(cls, photos_count: int):
return cls(ceil(photos_count / PhotoAlbum.PHOTO_COUNT))
def add_photo(self, label: str):
over = False
for el in self.photos:
over = False
if len(el) == 4:
over = True
if over:
return 'No more free slots'
self.index_slots += 1
if self.index_slots % 5 == 0:
self.index_slots = 1
self.index_pages += 1
self.photos[self.index_pages].append(label)
return f'{label} photo added successfully on page {self.index_pages + 1} slot {self.index_slots}'
def display(self):
result = ''
for el in self.photos:
result += PhotoAlbum.DELIMITER + '\n'
counter = 0
for _ in el:
counter += 1
result += '[]' + ' '
if counter == len(el) - 1:
result += '[]'
break
result += '\n'
result += PhotoAlbum.DELIMITER + '\n'
return result
============================================================================
________________________
| |
| 02. Movie World |
|________________________|
# file name: customer.py
class Customer:
def __init__(self, name: str, age: int, id: int):
self.name = name
self.age = age
self.id = id
self.rented_dvds = []
def __repr__(self):
return f"{self.id}: {self.name} of age {self.age} has {len(self.rented_dvds)} " \
f"rented DVD's ({', '.join([n.name for n in self.rented_dvds])})"
----------------------------------------------------------------------------
# file name: dvd
class DVD:
def __init__(self, name, id, creation_year, creation_month, age_restriction):
self.name = name
self.id = id
self.creation_year = creation_year
self.creation_month = creation_month
self.age_restriction = age_restriction
self.is_rented = False
@classmethod
def from_date(cls, id, name, date, age_restriction):
global month
month_dict = {
'01': 'January',
'02': 'February',
'03': 'March',
'04': 'April',
'05': 'May',
'06': 'June',
'07': 'July',
'08': 'August',
'09': 'September',
'10': 'October',
'11': 'November',
'12': 'December'
}
details = [int(x) for x in date.split('.')]
year = details[2]
if str(details[1]) in month_dict:
month = month_dict[str(details[1])]
return cls(name, id, year, month, age_restriction)
def __repr__(self):
status = "rented" if self.is_rented else 'not rented'
return f'{self.id}: {self.name} ({self.creation_month} {self.creation_year})' \
f' has age restriction {self.age_restriction}. Status: {status}'
----------------------------------------------------------------------------
# file name: movie_world.py
from project.customer import Customer
from project.dvd import DVD
class MovieWorld:
DVD_CAPACITY = 15
CUSTOMER_CAPACITY = 10
def __init__(self, name: str):
self.name = name
self.customers = [] # list of Customers OBJECTS!
self.dvds = [] # list of DVD OBJECTS!
@staticmethod
def dvd_capacity():
return MovieWorld.DVD_CAPACITY
@staticmethod
def customer_capacity():
return MovieWorld.CUSTOMER_CAPACITY
def add_customer(self, customer: Customer):
if len(self.customers) == MovieWorld.customer_capacity():
return
self.customers.append(customer)
def add_dvd(self, dvd: DVD):
if len(self.dvds) == MovieWorld.dvd_capacity():
return
self.dvds.append(dvd)
def rent_dvd(self, customer_id: int, dvd_id: int):
dvd = [d for d in self.dvds if d.id == dvd_id][0]
customer = [c for c in self.customers if c.id == customer_id][0]
if dvd in customer.rented_dvds:
return f"{customer.name} has already rented {dvd.name}"
if dvd.is_rented:
return "DVD is already rented"
if dvd.age_restriction >= customer.age:
return f"{customer.name} should be at least {dvd.age_restriction} to rent this movie"
dvd.is_rented = True
customer.rented_dvds.append(dvd)
return f"{customer.name} has successfully rented {dvd.name}"
def return_dvd(self, customer_id, dvd_id):
dvd = [d for d in self.dvds if d.id == dvd_id][0]
customer = [c for c in self.customers if c.id == customer_id][0]
if dvd in customer.rented_dvds:
customer.rented_dvds.remove(dvd)
dvd.is_rented = False
return f"{customer.name} has successfully returned {dvd.name}"
return f"{customer.name} does not have that DVD"
def __repr__(self):
result = ''
for customer in self.customers:
result += '\n' + customer.__repr__()
for dvd in self.dvds:
result += '\n' + dvd.__repr__()
return result.strip()
============================================================================
________________________________
| |
| 03. Document Management |
|________________________________|
# file name: category.py
class Category:
def __init__(self, id, name):
self.id = id
self.name = name
def edit(self, new_name):
self.name = new_name
def __repr__(self):
return f"Category {self.id}: {self.name}"
----------------------------------------------------------------------------
# file name: document.py
from project.category import Category
from project.topic import Topic
class Document:
def __init__(self, id, category_id, topic_id, file_name):
self.id = id
self.category_id = category_id
self.topic_id = topic_id
self.file_name = file_name
self.tags = []
@classmethod
def from_instances(cls, id, category: Category, topic: Topic, file_name):
return cls(id, category.id, topic.id, file_name)
def add_tag(self, tag_content):
if tag_content not in self.tags:
self.tags.append(tag_content)
def remove_tag(self, tag_content):
if tag_content in self.tags:
self.tags.remove(tag_content)
def edit(self, file_name):
self.file_name = file_name
def __repr__(self):
tags_ = ', '.join(t for t in self.tags)
return f"Document {self.id}: {self.file_name}; category " \
f"{self.category_id}, topic {self.topic_id}, tags: {tags_}"
----------------------------------------------------------------------------
# file name: storage.py
from project.category import Category
from project.document import Document
from project.topic import Topic
class Storage:
def __init__(self):
self.categories = []
self.topics = []
self.documents = []
def __find_by_id(self, storage_type, current_id): # types: categories , topics or documents
for obj in storage_type:
if obj.id == current_id:
return obj
def add_category(self, category: Category):
if category not in self.categories:
self.categories.append(category)
def add_topic(self, topic: Topic):
if topic not in self.topics:
self.topics.append(topic)
def add_document(self, document: Document):
if document not in self.documents:
self.documents.append(document)
def edit_category(self, category_id, new_name):
category = self.__find_by_id(self.categories, category_id)
category.name = new_name
def edit_topic(self, topic_id, new_topic, new_storage_folder):
topic = self.__find_by_id(self.topics, topic_id)
topic.edit(new_topic, new_storage_folder)
def edit_document(self, document_id, new_file_name):
document = self.__find_by_id(self.documents, document_id)
document.edit(new_file_name)
def delete_category(self, category_id):
category = self.__find_by_id(self.categories, category_id)
self.categories.remove(category)
def delete_topic(self, topic_id):
topic = self.__find_by_id(self.topics, topic_id)
self.topics.remove(topic)
def delete_document(self, document_id):
document = self.__find_by_id(self.documents, document_id)
if document:
self.documents.remove(document)
def get_document(self, document_id):
document = self.__find_by_id(self.documents, document_id)
return document
def __repr__(self):
return "\n".join([repr(doc) for doc in self.documents])
----------------------------------------------------------------------------
# file name: topic.py
class Topic:
def __init__(self, id, topic, storage_folder):
self.id = id
self.topic = topic
self.storage_folder = storage_folder
def edit(self, new_topic, new_storage_folder):
self.topic = new_topic
self.storage_folder = new_storage_folder
def __repr__(self):
return f"Topic {self.id}: {self.topic} in {self.storage_folder}"
============================================================================
_______________
| |
| 04. Gym |
|_______________|
# file name: gym.py
from project.customer import Customer
from project.equipment import Equipment
from project.exercise_plan import ExercisePlan
from project.subscription import Subscription
from project.trainer import Trainer
class Gym:
def __init__(self):
self.customers = [] # OBJECTS!
self.trainers = [] # OBJECTS!
self.equipment = [] # OBJECTS!
self.plans = [] # OBJECTS!
self.subscriptions = [] # OBJECTS!
def add_customer(self, customer: Customer):
if customer not in self.customers:
self.customers.append(customer)
def add_trainer(self, trainer: Trainer):
if trainer not in self.trainers:
self.trainers.append(trainer)
def add_equipment(self, equipment: Equipment):
if equipment not in self.equipment:
self.equipment.append(equipment)
def add_plan(self, plan: ExercisePlan):
if plan not in self.plans:
self.plans.append(plan)
def add_subscription(self, subscription: Subscription):
if subscription not in self.subscriptions:
self.subscriptions.append(subscription)
def subscription_info(self, subscription_id):
result = ''
subscription = [s for s in self.subscriptions if s.id == subscription_id][0]
customer = [c for c in self.customers if c.id == subscription.customer_id][0]
trainer = [t for t in self.trainers if t.id == subscription.trainer_id][0]
exercise = [e for e in self.plans if e.id == subscription.exercise_id][0]
equipment = [e for e in self.equipment if e.id == exercise.equipment_id][0]
result += f"{subscription}\n"
result += f"{customer}\n"
result += f"{trainer}\n"
result += f"{equipment}\n"
result += f"{exercise}\n"
return result
----------------------------------------------------------------------------
# file name: customer.py
class Customer:
id = 1
def __init__(self, name, address, email):
self.id = self.get_next_id()
self.name = name
self.address = address
self.email = email
@staticmethod
def get_next_id():
result = Customer.id
Customer.id += 1
return result
def __repr__(self):
return f"Customer <{self.id}> {self.name}; " \
f"Address: {self.address}; Email: {self.email}"
----------------------------------------------------------------------------
# file name: exercise_plan.py
class ExercisePlan:
id = 1
def __init__(self, trainer_id, equipment_id, duration): # Duration in Minutes!
self.id = self.get_next_id()
self.trainer_id = trainer_id
self.equipment_id = equipment_id
self.duration = duration
@classmethod
def from_hours(cls, trainer_id, equipment_id, hours):
return cls(trainer_id, equipment_id, hours * 60)
@staticmethod
def get_next_id():
result = ExercisePlan.id
ExercisePlan.id += 1
return result
def __repr__(self):
return f"Plan <{self.id}> with duration {self.duration} minutes"
----------------------------------------------------------------------------
# file name: subscription.py
class Subscription:
id = 1
def __init__(self, date, customer_id, trainer_id, exercise_id):
self.id = self.get_next_id()
self.date = date
self.customer_id = customer_id
self.trainer_id = trainer_id
self.exercise_id = exercise_id
@staticmethod
def get_next_id():
result = Subscription.id
Subscription.id += 1
return result
def __repr__(self):
return f"Subscription <{self.id}> on {self.date}"
----------------------------------------------------------------------------
# file name: trainer.py
class Trainer:
id = 1
def __init__(self, name):
self.id = self.get_next_id()
self.name = name
@staticmethod
def get_next_id():
result = Trainer.id
Trainer.id += 1
return result
def __repr__(self):
return f"Trainer <{self.id}> {self.name}"
----------------------------------------------------------------------------
# file name: equipment.py
class Equipment:
id = 1
def __init__(self, name):
self.id = self.get_next_id()
self.name = name
@staticmethod
def get_next_id():
result = Equipment.id
Equipment.id += 1
return result
def __repr__(self):
return f"Equipment <{self.id}> {self.name}"