150k views
0 votes
Class Movie: Represents a movie with a proper title, a barcode so it can be sold, a particular genre, and a rating (out of 100) based on the quality of the film. • def __init__(self, title, genre, barcode, rating=0.0): assigns all non-self parameters to fields of the same name (this is the most common kind of python constructor). All fields are strings with the exception of rating, which is a floating point number. • def __str__(self): returns a str that follows this pattern: o "#1234: TheFilm - Genre (45.7%)" • def __repr__(self): returns a str that follow this pattern: o 'Movie("TheFilm", "Genre", "1234", 45.7)' • def __eq__(self,other): both parameters are Movies. returns True when both movies have the same four fields, returns False otherwise. • def adjust_rating(self, amount=0): change the rating of this Movie by the given amount. The maximum rating is 100, and the minimum rating is 0. If a change would go above the max or below the min, set the rating to the maximum or minimum respectively.

1 Answer

2 votes

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)******************************

Class Movie: Represents a movie with a proper title, a barcode so it can be sold, a-example-1
Class Movie: Represents a movie with a proper title, a barcode so it can be sold, a-example-2
Class Movie: Represents a movie with a proper title, a barcode so it can be sold, a-example-3
Class Movie: Represents a movie with a proper title, a barcode so it can be sold, a-example-4
Class Movie: Represents a movie with a proper title, a barcode so it can be sold, a-example-5
User Lbrendanl
by
4.0k points