Music store is an application used to asses the knowledge of OOP concepts in python. The application is a simple music store that allows users to add, list and search for music disc. The application is implemented using classes and objects in python.
The model of the application is as follows:
The application code is incomplete, the idea is to complete it taking into account the following steps.
-
Complete de class
Transactiontaking into account the following requirements:-
The class should have a constant
SELLof typeintwith value1. -
The class should have a constant
SUPPLYof typeintwith value2. -
The class should have an
__init__method that receives the following parameters:typeof typeint.copiesof typeint.
In the
__init__method the class should initialize the attributestypeandcopieswith the values received as parameters. -
The class should have an attribute
dateof typedatetimethat should be initialized with the current date and time (you can use thedatetime.now()function to get the current date an time).
-
-
Complete the
Discclass taking into account the following requirements:-
The class should have an
__init__method that receives the following parameters:sidof typestr.titleof typestr.artistof typestr.sale_priceof typefloat.purchase_priceof typefloat.quantityof typeint.
In the
__init__method the class should initialize the attributessid,title,artist,sale_price,purchase_priceandquantitywith the values received as parameters. -
The class should have the attributes
transactionsof typelist[Transaction]andsong_listof typelist[str]. Both attributes should be initialized as an empty list. -
The class should have an instance method
add_songthat receives a parametersongof typestrand adds the song to thesong_listattribute. -
The class should have an instance method
sellthat receives a parameterquantityof typeintand does the following:- If the parameter
copiesis greater than thequantityattribute of the disc, the method should returnFalse. - Otherwise, the method decreases the
quantityattribute of the disc by the value of the parametercopiesand adds a newTransactionobject to thetransactionslist with the typeTransaction.SELLand the number ofcopiessold. - The method should return
True.
- If the parameter
-
The class should have an instance method
supplythat receives a parametercopiesof typeintand does the following:- Increases the
quantityattribute of the disc by the value of the parametercopies - Adds a new
Transactionobject to thetransactionslist with the typeTransaction.SUPPLYand the number ofcopiessupplied.
- Increases the
-
The class should have an instance method
copies_soldthat returns anintwith the total number of copies sold.Hint: you could add the number of copies of each transaction of type
Transaction.SELL. -
The class should have an instance method
__str__that return astrwith the following format:SID: {sid} Title: {title} Artist: {artist} Song List: {song_list}Where
{sid},{title}and{artist}should be replaced with the values of the attributes of the disc. The{song_list}should be replaced with the list of songs of the disc separated by a comma and a space.Hint: you could use an f-string (
f"") to format the string and\nwithin the string for a new line.
-
-
Complete the
MusicStoreclass taking into account the following requirements:-
The class should have an
__init__method that initializes thediscsattribute of typedict[str, Disc]as an empty dictionary. -
The class should have an instance method
add_discthat receives the parameterssidof typestr,titleof typestr,artistof typestr,sale_priceof typefloat,purchase_priceof typefloatandquantityof typeintand does the following:- Checks if the
sidis not already in thediscsdictionary. - If the
sidis not in thediscsdictionary, the method creates a newDiscobject with the received parameters and adds it to thediscsdictionary using thesidas the key.
- Checks if the
-
The class should have an instance method
search_by_sidthat receives the parametersidof typestrand returnsDisc | None. The method should return the disc with thesidreceived as parameter orNoneif the disc is not found. -
The class should have an instance method
search_by_artistthat receives the parameterartistof typestrand returnslist[Disc]. The method should return a list with all the discs that have theartistreceived as parameter. -
The class should have the instance methods
sell_discandsupply_discthat receive the parameterssidof typestrandcopiesof typeint. Copy the following code to theMusicStoreclass to complete the methods:def sell_disc(self, sid: str, copies: int) -> bool: disc = self.search_by_sid(sid) if disc is None: return False return disc.sell(copies) def supply_disc(self, sid: str, copies: int) -> bool: disc = self.search_by_sid(sid) if disc is None: return False disc.supply(copies) return True
-
The class should have an instance method
worst_selling_discthat returnsDisc | Nonewith the disc that has sold the least number of copies orNoneif there are no discs in the store.
-
