Answer:
Check the explanation
Step-by-step explanation:
Below is the code in Python3 for the above question:
******************************(Class Movie)******************************
# Movie class
class Movie:
# constructor
def __init__(self,title,genre,barcode,rating=0.0):
self.title = title
self.genre = genre
self.barcode = barcode
self.rating = rating
# function __str__
def __str__(self):
return (f"#{self.barcode}: {self.title} - {self.genre} ({self.rating}%)")
# function __repr__
def __repr__(self):
return (f"Movie(\"{self.title}\",\"{self.genre}\",\"{self.barcode}\",\"{self.rating}\")")
# function __eq__
def __eq__(self,other):
if(self.title == other.title and self.genre == other.genre and self.barcode == other.barcode
and self.rating == other.rating):
return True
return False
# function to adjust
def adjust_rating(self, amount = 0):
self.rating += amount
# if rating is more than max then
# assign max value.
if(self.rating>100):
self.rating = 100
# if rating is less than min then
# assign min value.
elif(self.rating<=0):
self.rating = 0
******************************(Class MovieStore)******************************
# MovieStore class
class MovieStore:
# constructor
def __init__(self, name, inventory = None):
self.name = name
if(inventory == None):
self.inventory = []
else:
self.inventory = inventory
# function __str__
def __str__(self):
if(len(self.inventory) == 0):
return (f"Store inventory: <empty>")
l = [str(i) for i in self.inventory]
return (f"Store Inventory: {str(l)}")
# function __repr__
def __repr__(self):
return (f"MovieStore(\"Store\",{self.inventory})")
# function buy
def buy(self, movie):
# adding to the end of inventory list.
self.inventory.append(movie)
# function sell
def sell(self, movie):
self.inventory.remove(movie)
# function clear_inventory
def clear_inventory(self):
# if movie rating is less than 50
# removing it from inventory
for i in self.inventory:
if(int(i.rating)<50):
self.inventory.remove(i)
# function lookup
def lookup(self, movie):
for i in self.inventory:
if(i == movie):
return True
return False
# function recommend
def recommend(self, genre):
movieName=""
rating = 0
for i in self.inventory:
if(i.genre == genre):
if(i.rating>=rating):
movieName = i.title
rating = i.rating
return movieName
# function apparaise
def appraise(self, adjustments):
for key in adjustments:
for i in self.inventory:
if(i.barcode == key):
i.adjust_rating(adjustments[key])
# function top_results
def top_results(self, genre, percent):
newList = []
for i in self.inventory:
if(i.genre == genre and i.rating >= percent):
newList.append(i)
# sorting newList on the basis of rating of the movies
# using lamda function.
newList.sort(key=lambda movie: movie.rating)
return newList
# sample test
movie1 = Movie("Thor", "Action", "1234", 98)
movie2 = Movie("Avengers", "Action", "7895", 95)
movie3 = Movie("Forst Gump", "Drama", "4725", 99)
moviestore1 = MovieStore("MovieCollection",[movie1,movie2])
moviestore1.buy(movie3)
movie1.adjust_rating(98)
print(movie1.rating)
Refer to the screenshot images attached below to better understand the code and indentation including the output:
******************************(Class Movie)******************************