Advertisement
GeorgiLukanov87

Python OOP Retake Exam - 18 April 2022 - Movie App

Nov 12th, 2022 (edited)
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.11 KB | None | 0 0
  1. # Python OOP Retake Exam - 18 April 2022 - Movie App
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/3431#1
  4. =============================================================================================
  5. # file name: movie.py
  6.  
  7. from abc import ABC, abstractmethod
  8.  
  9.  
  10. class Movie(ABC):
  11.     def __init__(self, title: str, year: int, owner: object, age_restriction: int):
  12.         self.title = title
  13.         self.year = year
  14.         self.owner = owner
  15.         self.age_restriction = age_restriction
  16.         self.likes = 0
  17.  
  18.     @property
  19.     def title(self):
  20.         return self.__title
  21.  
  22.     @title.setter
  23.     def title(self, value):
  24.         if value == '':
  25.             raise ValueError('The title cannot be empty string!')
  26.         self.__title = value
  27.  
  28.     @property
  29.     def year(self):
  30.         return self.__year
  31.  
  32.     @year.setter
  33.     def year(self, value):
  34.         if value < 1888:
  35.             raise ValueError("Movies weren't made before 1888!")
  36.         self.__year = value
  37.  
  38.     @property
  39.     def owner(self):
  40.         return self.__owner
  41.  
  42.     @owner.setter
  43.     def owner(self, value):
  44.         if not type(value).__name__ == 'User':
  45.             raise ValueError('The owner must be an object of type User!')
  46.         self.__owner = value
  47.  
  48.     @abstractmethod
  49.     def details(self):
  50.         ...
  51.  
  52.  
  53. =============================================================================================
  54. # file name: action.py
  55.  
  56. from project.movie_specification.movie import Movie
  57.  
  58.  
  59. class Action(Movie):
  60.     def __init__(self, title: str, year: int, owner: object, age_restriction=12):
  61.         super().__init__(title, year, owner, age_restriction)
  62.  
  63.     @property
  64.     def age_restriction(self):
  65.         return self.__age_restriction
  66.  
  67.     @age_restriction.setter
  68.     def age_restriction(self, value):
  69.         if value < 12:
  70.             raise ValueError("Action movies must be restricted for audience under 12 years!")
  71.         self.__age_restriction = value
  72.  
  73.     def details(self):
  74.         return f"Action - Title:{self.title}, Year:{self.year}, " \
  75.                f"Age restriction:{self.age_restriction}, Likes:{self.likes}, Owned by:{self.owner.username}"
  76.  
  77.  
  78. =============================================================================================
  79. # file name: fantasy.py
  80.  
  81. from project.movie_specification.movie import Movie
  82.  
  83.  
  84. class Fantasy(Movie):
  85.     def __init__(self, title, year, owner, age_restriction=6):
  86.         super().__init__(title, year, owner, age_restriction)
  87.  
  88.     @property
  89.     def age_restriction(self):
  90.         return self.__age_restriction
  91.  
  92.     @age_restriction.setter
  93.     def age_restriction(self, value):
  94.         if value < 6:
  95.             raise ValueError("Fantasy movies must be restricted for audience under 6 years!")
  96.         self.__age_restriction = value
  97.  
  98.     def details(self):
  99.         return f"Fantasy - Title:{self.title}, Year:{self.year}, " \
  100.                f"Age restriction:{self.age_restriction}, Likes:{self.likes}, Owned by:{self.owner.username}"
  101.  
  102. =============================================================================================
  103. # file name: thriller.py
  104.  
  105. from project.movie_specification.movie import Movie
  106.  
  107.  
  108. class Thriller(Movie):
  109.     def __init__(self, title, year, owner, age_restriction=16):
  110.         super().__init__(title, year, owner, age_restriction)
  111.  
  112.     @property
  113.     def age_restriction(self):
  114.         return self.__age_restriction
  115.  
  116.     @age_restriction.setter
  117.     def age_restriction(self, value):
  118.         if value < 16:
  119.             raise ValueError("Thriller movies must be restricted for audience under 16 years!")
  120.         self.__age_restriction = value
  121.  
  122.     def details(self):
  123.         return f"Fantasy - Title:{self.title}, Year:{self.year}, " \
  124.                f"Age restriction:{self.age_restriction}, Likes:{self.likes}, Owned by:{self.owner.username}"
  125.  
  126. =============================================================================================
  127. # file name: user.py
  128.  
  129. class User:
  130.     def __init__(self, username: str, age: int):
  131.         self.username = username
  132.         self.age = age
  133.         self.movies_liked = []  # Objs
  134.         self.movies_owned = []  # Objs
  135.  
  136.     @property
  137.     def username(self):
  138.         return self.__username
  139.  
  140.     @username.setter
  141.     def username(self, value):
  142.         if len(value) == 0:
  143.             raise ValueError('Invalid username!')
  144.         self.__username = value
  145.  
  146.     @property
  147.     def age(self):
  148.         return self.__age
  149.  
  150.     @age.setter
  151.     def age(self, value):
  152.         if value < 6:
  153.             raise ValueError('Users under the age of 6 are not allowed!')
  154.         self.__age = value
  155.  
  156.     def __str__(self):
  157.         result_str = [f'Username: {self.username}, Age: {self.age}', 'Liked movies:']
  158.         if len(self.movies_liked) > 0:
  159.             for liked in self.movies_liked:
  160.                 result_str.append(liked.details())
  161.         else:
  162.             result_str.append('No movies liked.')
  163.         result_str.append('Owned movies:')
  164.         if len(self.movies_owned) > 0:
  165.             for owned in self.movies_owned:
  166.                 result_str.append(owned.details())
  167.         else:
  168.             result_str.append('No movies owned.')
  169.         return '\n'.join(result_str)
  170.  
  171. =============================================================================================
  172. # file name: movie_app.py
  173.  
  174. from project.user import User
  175.  
  176.  
  177. class MovieApp:
  178.     def __init__(self):
  179.         self.movies_collection = []
  180.         self.users_collection = []
  181.  
  182.     def check_if_user_exists(self, username):
  183.         for user in self.users_collection:
  184.             if user.username == username:
  185.                 return True
  186.         return False
  187.  
  188.     def check_if_movie_exists(self, title):
  189.         for movie in self.movies_collection:
  190.             if movie.title == title:
  191.                 return True
  192.         return False
  193.  
  194.     def check_if_user_liked_movie(self, username, movie_title):
  195.         for user in self.users_collection:
  196.             if user.username == username:
  197.                 for movie in user.movies_liked:
  198.                     if movie.title == movie_title:
  199.                         return True
  200.                 return False
  201.  
  202.     def register_user(self, username, age):
  203.         if self.check_if_user_exists(username):
  204.             raise Exception('User already exists!')
  205.         new_user = User(username, age)
  206.         self.users_collection.append(new_user)
  207.         return f'{username} registered successfully.'
  208.  
  209.     def upload_movie(self, username, movie):
  210.         if not self.check_if_user_exists(username):
  211.             raise Exception('This user does not exist!')
  212.         elif self.check_if_movie_exists(movie.title):
  213.             raise Exception('Movie already added to the collection!')
  214.         elif not username == movie.owner.username:
  215.             raise Exception(f'{username} is not the owner of the movie {movie.title}!')
  216.         else:
  217.             self.movies_collection.append(movie)
  218.             for user in self.users_collection:
  219.                 if user.username == username:
  220.                     user.movies_owned.append(movie)
  221.                     return f'{username} successfully added {movie.title} movie.'
  222.  
  223.     def edit_movie(self, username, movie, **kwargs):
  224.         if not self.check_if_movie_exists(movie.title):
  225.             raise Exception(f'The movie {movie.title} is not uploaded!')
  226.         elif not username == movie.owner.username:
  227.             raise Exception(f'{username} is not the owner of the movie {movie.title}!')
  228.         else:
  229.             for attr, new_value in kwargs.items():
  230.                 setattr(movie, attr, new_value)
  231.             return f'{username} successfully edited {movie.title} movie.'
  232.  
  233.     def delete_movie(self, username, movie):
  234.         if not self.check_if_movie_exists(movie.title):
  235.             raise Exception(f'The movie {movie.title} is not uploaded!')
  236.         elif not username == movie.owner.username:
  237.             raise Exception(f'{username} is not the owner of the movie {movie.title}!')
  238.         else:
  239.             self.movies_collection.pop(self.movies_collection.index(movie))
  240.             for user in self.users_collection:
  241.                 if user.username == username:
  242.                     user.movies_owned.pop(user.movies_owned.index(movie))
  243.                     return f'{username} successfully deleted {movie.title} movie.'
  244.  
  245.     def like_movie(self, username, movie):
  246.         if username == movie.owner.username:
  247.             raise Exception(f'{username} is the owner of the movie {movie.title}!')
  248.         elif self.check_if_user_liked_movie(username, movie.title):
  249.             raise Exception(f'{username} already liked the movie {movie.title}!')
  250.         else:
  251.             movie.likes += 1
  252.             for user in self.users_collection:
  253.                 if user.username == username:
  254.                     user.movies_liked.append(movie)
  255.             return f'{username} liked {movie.title} movie.'
  256.  
  257.     def dislike_movie(self, username, movie):
  258.         if not self.check_if_user_liked_movie(username, movie.title):
  259.             raise Exception(f'{username} has not liked the movie {movie.title}!')
  260.         else:
  261.             movie.likes -= 1
  262.             for user in self.users_collection:
  263.                 if user.username == username:
  264.                     user.movies_liked.pop(user.movies_liked.index(movie))
  265.             return f'{username} disliked {movie.title} movie.'
  266.  
  267.     def display_movies(self):
  268.         if len(self.movies_collection) == 0:
  269.             return 'No movies found.'
  270.         else:
  271.             result_str = []
  272.             for movie in sorted(self.movies_collection, key=lambda x: (-x.year, x.title)):
  273.                 result_str.append(movie.details())
  274.             return '\n'.join(result_str)
  275.  
  276.     def __str__(self):
  277.         if len(self.users_collection) == 0:
  278.             users = 'No users.'
  279.         else:
  280.             users = ', '.join([user.username for user in self.users_collection])
  281.         if len(self.movies_collection) == 0:
  282.             movies = 'No movies.'
  283.         else:
  284.             movies = ', '.join([movie.title for movie in self.movies_collection])
  285.  
  286.         return f'All users: {users}\nAll movies: {movies}'
  287.  
  288.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement