GeorgiLukanov87

Static and Class Methods - Exercise

Oct 28th, 2022 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.06 KB | None | 0 0
  1. # Static and Class Methods - Exercise
  2.  
  3. # https://judge.softuni.org/Contests/Compete/Index/2431#3
  4. ============================================================================
  5. # 01. Photo Album
  6. # 02. Movie World
  7. # 03. Document Management
  8. # 04. Gym
  9. ============================================================================
  10.  
  11.  ________________________
  12. |                        |
  13. |    01. Photo Album     |
  14. |________________________|
  15.  
  16.  
  17. from math import ceil
  18.  
  19.  
  20. class PhotoAlbum:
  21.     PHOTO_COUNT = 4
  22.     DELIMITER = 11 * '-'
  23.  
  24.     def __init__(self, pages: int):
  25.         self.pages = pages
  26.         self.photos = self.build_photos()
  27.         self.index_pages = 0
  28.         self.index_slots = 0
  29.  
  30.     def build_photos(self):
  31.         result = []
  32.         for _ in range(self.pages):
  33.             result.append([] * self.PHOTO_COUNT)
  34.         return result
  35.  
  36.     @classmethod
  37.     def from_photos_count(cls, photos_count: int):
  38.         return cls(ceil(photos_count / PhotoAlbum.PHOTO_COUNT))
  39.  
  40.     def add_photo(self, label: str):
  41.         over = False
  42.         for el in self.photos:
  43.             over = False
  44.             if len(el) == 4:
  45.                 over = True
  46.         if over:
  47.             return 'No more free slots'
  48.  
  49.         self.index_slots += 1
  50.         if self.index_slots % 5 == 0:
  51.             self.index_slots = 1
  52.             self.index_pages += 1
  53.         self.photos[self.index_pages].append(label)
  54.         return f'{label} photo added successfully on page {self.index_pages + 1} slot {self.index_slots}'
  55.  
  56.     def display(self):
  57.         result = ''
  58.         for el in self.photos:
  59.             result += PhotoAlbum.DELIMITER + '\n'
  60.             counter = 0
  61.             for _ in el:
  62.                 counter += 1
  63.                 result += '[]' + ' '
  64.                 if counter == len(el) - 1:
  65.                     result += '[]'
  66.                     break
  67.             result += '\n'
  68.         result += PhotoAlbum.DELIMITER + '\n'
  69.         return result
  70.  
  71.  
  72. ============================================================================
  73.  
  74.  ________________________
  75. |                        |
  76. |    02. Movie World     |
  77. |________________________|
  78.  
  79.  
  80. # file name: customer.py
  81. class Customer:
  82.     def __init__(self, name: str, age: int, id: int):
  83.         self.name = name
  84.         self.age = age
  85.         self.id = id
  86.         self.rented_dvds = []
  87.  
  88.     def __repr__(self):
  89.         return f"{self.id}: {self.name} of age {self.age} has {len(self.rented_dvds)} " \
  90.                f"rented DVD's ({', '.join([n.name for n in self.rented_dvds])})"
  91.  
  92. ----------------------------------------------------------------------------
  93.  
  94. # file name: dvd
  95. class DVD:
  96.     def __init__(self, name, id, creation_year, creation_month, age_restriction):
  97.         self.name = name
  98.         self.id = id
  99.         self.creation_year = creation_year
  100.         self.creation_month = creation_month
  101.         self.age_restriction = age_restriction
  102.         self.is_rented = False
  103.  
  104.     @classmethod
  105.     def from_date(cls, id, name, date, age_restriction):
  106.         global month
  107.         month_dict = {
  108.             '01': 'January',
  109.             '02': 'February',
  110.             '03': 'March',
  111.             '04': 'April',
  112.             '05': 'May',
  113.             '06': 'June',
  114.             '07': 'July',
  115.             '08': 'August',
  116.             '09': 'September',
  117.             '10': 'October',
  118.             '11': 'November',
  119.             '12': 'December'
  120.         }
  121.         details = [int(x) for x in date.split('.')]
  122.         year = details[2]
  123.         if str(details[1]) in month_dict:
  124.             month = month_dict[str(details[1])]
  125.         return cls(name, id, year, month, age_restriction)
  126.  
  127.     def __repr__(self):
  128.         status = "rented" if self.is_rented else 'not rented'
  129.         return f'{self.id}: {self.name} ({self.creation_month} {self.creation_year})' \
  130.                f' has age restriction {self.age_restriction}. Status: {status}'
  131.  
  132. ----------------------------------------------------------------------------
  133.  
  134. # file name: movie_world.py
  135. from project.customer import Customer
  136. from project.dvd import DVD
  137.  
  138.  
  139. class MovieWorld:
  140.     DVD_CAPACITY = 15
  141.     CUSTOMER_CAPACITY = 10
  142.  
  143.     def __init__(self, name: str):
  144.         self.name = name
  145.         self.customers = []  # list of Customers OBJECTS!
  146.         self.dvds = []  # list of DVD OBJECTS!
  147.  
  148.     @staticmethod
  149.     def dvd_capacity():
  150.         return MovieWorld.DVD_CAPACITY
  151.  
  152.     @staticmethod
  153.     def customer_capacity():
  154.         return MovieWorld.CUSTOMER_CAPACITY
  155.  
  156.     def add_customer(self, customer: Customer):
  157.         if len(self.customers) == MovieWorld.customer_capacity():
  158.             return
  159.         self.customers.append(customer)
  160.  
  161.     def add_dvd(self, dvd: DVD):
  162.         if len(self.dvds) == MovieWorld.dvd_capacity():
  163.             return
  164.         self.dvds.append(dvd)
  165.  
  166.     def rent_dvd(self, customer_id: int, dvd_id: int):
  167.         dvd = [d for d in self.dvds if d.id == dvd_id][0]
  168.         customer = [c for c in self.customers if c.id == customer_id][0]
  169.  
  170.         if dvd in customer.rented_dvds:
  171.             return f"{customer.name} has already rented {dvd.name}"
  172.         if dvd.is_rented:
  173.             return "DVD is already rented"
  174.         if dvd.age_restriction >= customer.age:
  175.             return f"{customer.name} should be at least {dvd.age_restriction} to rent this movie"
  176.         dvd.is_rented = True
  177.         customer.rented_dvds.append(dvd)
  178.         return f"{customer.name} has successfully rented {dvd.name}"
  179.  
  180.     def return_dvd(self, customer_id, dvd_id):
  181.         dvd = [d for d in self.dvds if d.id == dvd_id][0]
  182.         customer = [c for c in self.customers if c.id == customer_id][0]
  183.         if dvd in customer.rented_dvds:
  184.             customer.rented_dvds.remove(dvd)
  185.             dvd.is_rented = False
  186.             return f"{customer.name} has successfully returned {dvd.name}"
  187.         return f"{customer.name} does not have that DVD"
  188.  
  189.     def __repr__(self):
  190.         result = ''
  191.         for customer in self.customers:
  192.             result += '\n' + customer.__repr__()
  193.         for dvd in self.dvds:
  194.             result += '\n' + dvd.__repr__()
  195.         return result.strip()
  196.  
  197.  
  198. ============================================================================
  199.  
  200.  ________________________________
  201. |                                |
  202. |    03. Document Management     |
  203. |________________________________|
  204.  
  205.  
  206. # file name: category.py
  207. class Category:
  208.     def __init__(self, id, name):
  209.         self.id = id
  210.         self.name = name
  211.  
  212.     def edit(self, new_name):
  213.         self.name = new_name
  214.  
  215.     def __repr__(self):
  216.         return f"Category {self.id}: {self.name}"
  217.  
  218. ----------------------------------------------------------------------------
  219.  
  220. # file name: document.py
  221. from project.category import Category
  222. from project.topic import Topic
  223.  
  224.  
  225. class Document:
  226.     def __init__(self, id, category_id, topic_id, file_name):
  227.         self.id = id
  228.         self.category_id = category_id
  229.         self.topic_id = topic_id
  230.         self.file_name = file_name
  231.         self.tags = []
  232.  
  233.     @classmethod
  234.     def from_instances(cls, id, category: Category, topic: Topic, file_name):
  235.         return cls(id, category.id, topic.id, file_name)
  236.  
  237.     def add_tag(self, tag_content):
  238.         if tag_content not in self.tags:
  239.             self.tags.append(tag_content)
  240.  
  241.     def remove_tag(self, tag_content):
  242.         if tag_content in self.tags:
  243.             self.tags.remove(tag_content)
  244.  
  245.     def edit(self, file_name):
  246.         self.file_name = file_name
  247.  
  248.     def __repr__(self):
  249.         tags_ = ', '.join(t for t in self.tags)
  250.         return f"Document {self.id}: {self.file_name}; category " \
  251.                f"{self.category_id}, topic {self.topic_id}, tags: {tags_}"
  252. ----------------------------------------------------------------------------
  253.  
  254. # file name: storage.py
  255.  
  256. from project.category import Category
  257. from project.document import Document
  258. from project.topic import Topic
  259.  
  260.  
  261. class Storage:
  262.     def __init__(self):
  263.         self.categories = []
  264.         self.topics = []
  265.         self.documents = []
  266.  
  267.     def __find_by_id(self, storage_type, current_id):  # types: categories , topics or documents
  268.         for obj in storage_type:
  269.             if obj.id == current_id:
  270.                 return obj
  271.  
  272.     def add_category(self, category: Category):
  273.         if category not in self.categories:
  274.             self.categories.append(category)
  275.  
  276.     def add_topic(self, topic: Topic):
  277.         if topic not in self.topics:
  278.             self.topics.append(topic)
  279.  
  280.     def add_document(self, document: Document):
  281.         if document not in self.documents:
  282.             self.documents.append(document)
  283.  
  284.     def edit_category(self, category_id, new_name):
  285.         category = self.__find_by_id(self.categories, category_id)
  286.         category.name = new_name
  287.  
  288.     def edit_topic(self, topic_id, new_topic, new_storage_folder):
  289.         topic = self.__find_by_id(self.topics, topic_id)
  290.         topic.edit(new_topic, new_storage_folder)
  291.  
  292.     def edit_document(self, document_id, new_file_name):
  293.         document = self.__find_by_id(self.documents, document_id)
  294.         document.edit(new_file_name)
  295.  
  296.     def delete_category(self, category_id):
  297.         category = self.__find_by_id(self.categories, category_id)
  298.         self.categories.remove(category)
  299.  
  300.     def delete_topic(self, topic_id):
  301.         topic = self.__find_by_id(self.topics, topic_id)
  302.         self.topics.remove(topic)
  303.  
  304.     def delete_document(self, document_id):
  305.         document = self.__find_by_id(self.documents, document_id)
  306.         if document:
  307.             self.documents.remove(document)
  308.  
  309.     def get_document(self, document_id):
  310.         document = self.__find_by_id(self.documents, document_id)
  311.         return document
  312.  
  313.     def __repr__(self):
  314.         return "\n".join([repr(doc) for doc in self.documents])
  315.    
  316. ----------------------------------------------------------------------------
  317.  
  318. # file name: topic.py
  319. class Topic:
  320.     def __init__(self, id, topic, storage_folder):
  321.         self.id = id
  322.         self.topic = topic
  323.         self.storage_folder = storage_folder
  324.  
  325.     def edit(self, new_topic, new_storage_folder):
  326.         self.topic = new_topic
  327.         self.storage_folder = new_storage_folder
  328.  
  329.     def __repr__(self):
  330.         return f"Topic {self.id}: {self.topic} in {self.storage_folder}"
  331.  
  332.  
  333.  
  334. ============================================================================
  335.  
  336.  _______________
  337. |               |
  338. |   04. Gym     |
  339. |_______________|
  340.  
  341.  
  342. # file name: gym.py
  343. from project.customer import Customer
  344. from project.equipment import Equipment
  345. from project.exercise_plan import ExercisePlan
  346. from project.subscription import Subscription
  347. from project.trainer import Trainer
  348.  
  349.  
  350. class Gym:
  351.     def __init__(self):
  352.         self.customers = []  # OBJECTS!
  353.         self.trainers = []  # OBJECTS!
  354.         self.equipment = []  # OBJECTS!
  355.         self.plans = []  # OBJECTS!
  356.         self.subscriptions = []  # OBJECTS!
  357.  
  358.     def add_customer(self, customer: Customer):
  359.         if customer not in self.customers:
  360.             self.customers.append(customer)
  361.  
  362.     def add_trainer(self, trainer: Trainer):
  363.         if trainer not in self.trainers:
  364.             self.trainers.append(trainer)
  365.  
  366.     def add_equipment(self, equipment: Equipment):
  367.         if equipment not in self.equipment:
  368.             self.equipment.append(equipment)
  369.  
  370.     def add_plan(self, plan: ExercisePlan):
  371.         if plan not in self.plans:
  372.             self.plans.append(plan)
  373.  
  374.     def add_subscription(self, subscription: Subscription):
  375.         if subscription not in self.subscriptions:
  376.             self.subscriptions.append(subscription)
  377.  
  378.     def subscription_info(self, subscription_id):
  379.         result = ''
  380.         subscription = [s for s in self.subscriptions if s.id == subscription_id][0]
  381.         customer = [c for c in self.customers if c.id == subscription.customer_id][0]
  382.         trainer = [t for t in self.trainers if t.id == subscription.trainer_id][0]
  383.         exercise = [e for e in self.plans if e.id == subscription.exercise_id][0]
  384.         equipment = [e for e in self.equipment if e.id == exercise.equipment_id][0]
  385.  
  386.         result += f"{subscription}\n"
  387.         result += f"{customer}\n"
  388.         result += f"{trainer}\n"
  389.         result += f"{equipment}\n"
  390.         result += f"{exercise}\n"
  391.  
  392.         return result
  393. ----------------------------------------------------------------------------
  394. # file name: customer.py
  395.  
  396. class Customer:
  397.     id = 1
  398.  
  399.     def __init__(self, name, address, email):
  400.         self.id = self.get_next_id()
  401.         self.name = name
  402.         self.address = address
  403.         self.email = email
  404.  
  405.     @staticmethod
  406.     def get_next_id():
  407.         result = Customer.id
  408.         Customer.id += 1
  409.         return result
  410.  
  411.     def __repr__(self):
  412.         return f"Customer <{self.id}> {self.name}; " \
  413.                f"Address: {self.address}; Email: {self.email}"
  414.  
  415. ----------------------------------------------------------------------------
  416. # file name: exercise_plan.py
  417.  
  418. class ExercisePlan:
  419.     id = 1
  420.  
  421.     def __init__(self, trainer_id, equipment_id, duration):  # Duration in Minutes!
  422.         self.id = self.get_next_id()
  423.         self.trainer_id = trainer_id
  424.         self.equipment_id = equipment_id
  425.         self.duration = duration
  426.  
  427.     @classmethod
  428.     def from_hours(cls, trainer_id, equipment_id, hours):
  429.         return cls(trainer_id, equipment_id, hours * 60)
  430.  
  431.     @staticmethod
  432.     def get_next_id():
  433.         result = ExercisePlan.id
  434.         ExercisePlan.id += 1
  435.         return result
  436.  
  437.     def __repr__(self):
  438.         return f"Plan <{self.id}> with duration {self.duration} minutes"
  439.  
  440. ----------------------------------------------------------------------------
  441. # file name: subscription.py
  442.  
  443. class Subscription:
  444.     id = 1
  445.  
  446.     def __init__(self, date, customer_id, trainer_id, exercise_id):
  447.         self.id = self.get_next_id()
  448.         self.date = date
  449.         self.customer_id = customer_id
  450.         self.trainer_id = trainer_id
  451.         self.exercise_id = exercise_id
  452.  
  453.     @staticmethod
  454.     def get_next_id():
  455.         result = Subscription.id
  456.         Subscription.id += 1
  457.         return result
  458.  
  459.     def __repr__(self):
  460.         return f"Subscription <{self.id}> on {self.date}"
  461.  
  462. ----------------------------------------------------------------------------
  463. # file name: trainer.py
  464. class Trainer:
  465.     id = 1
  466.  
  467.     def __init__(self, name):
  468.         self.id = self.get_next_id()
  469.         self.name = name
  470.  
  471.     @staticmethod
  472.     def get_next_id():
  473.         result = Trainer.id
  474.         Trainer.id += 1
  475.         return result
  476.  
  477.     def __repr__(self):
  478.         return f"Trainer <{self.id}> {self.name}"
  479.  
  480.  
  481. ----------------------------------------------------------------------------
  482. # file name: equipment.py
  483.  
  484. class Equipment:
  485.     id = 1
  486.  
  487.     def __init__(self, name):
  488.         self.id = self.get_next_id()
  489.         self.name = name
  490.  
  491.     @staticmethod
  492.     def get_next_id():
  493.         result = Equipment.id
  494.         Equipment.id += 1
  495.         return result
  496.  
  497.     def __repr__(self):
  498.         return f"Equipment <{self.id}> {self.name}"
  499.  
Add Comment
Please, Sign In to add comment