Guest User

Untitled

a guest
Aug 13th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. class MovieWorld:
  2. def __init__(self, name: str):
  3. self.name = name
  4. self.customers = []
  5. self.dvds = []
  6.  
  7. @staticmethod
  8. def dvd_capacity():
  9. return 15
  10.  
  11. @staticmethod
  12. def customer_capacity():
  13. return 10
  14.  
  15. def add_customer(self, customer):
  16. if len(self.customers) < self.customer_capacity():
  17. self.customers.append(customer)
  18.  
  19. def add_dvd(self, dvd):
  20. if len(self.dvds) < self.dvd_capacity():
  21. self.dvds.append(dvd)
  22.  
  23. def rent_dvd(self, customer_id: int, dvd_id: int):
  24. customer_idx = self.customer_id(customer_id)
  25. dvd_idx = self.dvd_id(dvd_id)
  26. customer = self.customers[customer_idx]
  27. dvd = self.dvds[dvd_idx]
  28. if dvd in customer.rented_dvds:
  29. return f"{customer.name} has already rented {dvd.name}"
  30. if dvd.is_rented:
  31. return 'DVD is already rented'
  32. if customer.age < dvd.age_restriction:
  33. return f"{customer.name} should be at least {dvd.age_restriction} to rent this movie"
  34. self.dvds[dvd_idx].is_rented = True
  35. self.customers[customer_idx].rented_dvds.append(dvd)
  36. return f"{customer.name} has successfully rented {dvd.name}"
  37.  
  38. def return_dvd(self, customer_id: int, dvd_id: int):
  39. customer_idx = self.customer_id(customer_id)
  40. dvd_idx = self.dvd_id(dvd_id)
  41. customer = self.customers[customer_idx]
  42. dvd = self.dvds[dvd_idx]
  43. if dvd in customer.rented_dvds:
  44. self.customers[customer_idx].rented_dvds.pop(self.customers[customer_idx].rented_dvds.index(dvd))
  45. self.dvds[dvd_idx].is_rented = False
  46. return f"{customer.name} has successfully returned {dvd.name}"
  47. else:
  48. return f"{customer.name} does not have that DVD"
  49.  
  50. def customer_id(self, searched):
  51. for i in range(len(self.customers)):
  52. if self.customers[i].id == searched:
  53. return i
  54.  
  55. def dvd_id(self, searched):
  56. for i in range(len(self.dvds)):
  57. if self.dvds[i].id == searched:
  58. return i
  59.  
  60. def __repr__(self):
  61. string = []
  62. for i in self.customers:
  63. string.append(i)
  64. for i in self.dvds:
  65. string.append(i)
  66. return '\n'.join([str(i) for i in string])
  67.  
Add Comment
Please, Sign In to add comment