Project 4: Music Store
Implement a program that manages the data of a music store. The store manages the list of CDs that can be bought. For each CD, the store has the following data: album title, singer/band name, recording house, published year, music genre, and a list of song titles.
The data are stored into a json file. The application should manage the following characteristics:
Add a new CD
Remove existing CD
Show all CDs of a given singer/band . Show all CDs containing a specific song (identified by song title)
Changes need to be stored back in the file. The above functionalities are implemented through methods of a class named TravelAgency. The application interacts with the user through a menu where the user is asked for the next action to undertake (e.g., 1-Add new CD, 2-Remove existing CD, etc.) Put comments in your code.
import pathlib
import json
class CD:
#Constructor
def __init__(self):
self.albumTitle =""
self.singerBandName =""
self.recordingHouse =""
self.publishedYear =0
self.musicGenre =""
self.listSongTitles = []
print("")
#This function allows to add a new CD
def addData(self):
self.albumTitle =input("Enter the album title of CD: ")
self.singerBandName =input("Enter the singer/band name of CD: ")
self.recordingHouse =input("Enter the recording house of CD: ")
self.publishedYear =input("Enter the published year of CD: ")
self.musicGenre =input("Enter the music genre of CD: ")
self.listSongTitles = []
numberSongTitles =int(input("Enter the number song titles: "))
for i in range(0,numberSongTitles):
self.listSongTitles.append(input("Enter song title: "))
print("")
#This function allows to set data
def setData(self,albumTitle,singerBandName,recordingHouse,publishedYear,musicGenre,listSongTitles):
self.albumTitle=albumTitle
self.singerBandName=singerBandName
self.recordingHouse=recordingHouse
self.publishedYear=publishedYear
self.musicGenre=musicGenre
self.listSongTitles=listSongTitles
#This function allows to display CD Information
def displayCDInformation(self):
print(f"The album title of CD: {self.albumTitle}")
print(f"The singer/band name of CD: {self.singerBandName}")
print(f"The recording house of CD: {self.recordingHouse}")
print(f"The published year of CD: {self.publishedYear}")
print(f"The music genre of CD: {self.musicGenre}")
print(f"Song Titles: ")
for st in self.listSongTitles:
print(st)
#This function allows to get album title
def getAlbumTitle(self):
return self.albumTitle
#This function allows to get singer/band name
def getSingerBandName(self):
return self.singerBandName
#This function allows to check if the song exist
def songExist(self,song):
for st in self.listSongTitles:
if st==song:
return True
return False
def main():
choice=""
#list of CDs
musicStore=[]
readMusicStore(musicStore)
while choice!="5":
print("1 - Add a new CD")
print("2 - Remove Existing CD")
print("3 - Show all CDs of a given singer/band")
print("4 - Show all CDs containing a specific song")
print("5 - Exit Music Store")
choice=input("Your choice: ")
if(choice=="1"):#Add a new CD
newCD=CD()
newCD.addData()
musicStore.append(newCD)
saveMusicStore(musicStore)
print("A new CD has been added.")
elif(choice=="2"):#Remove Existing CD
albumTitleRemove =input("Enter the album title of CD to remove: ")
isDeleted=False
for cd in musicStore:
if cd.getAlbumTitle()==albumTitleRemove:
musicStore.remove(cd)
print("\nThe CD has been deleted.\n")
saveMusicStore(musicStore)
isDeleted=True
if isDeleted==False:
print("\nThe CD does not exist.\n")
elif(choice=="3"):#Show all CDs of a given singer/band
singerBandName =input("Enter the singer/band of CD to show: ")
exist=False
for cd in musicStore:
if cd.getSingerBandName()==singerBandName:
cd.displayCDInformation()
print("")
exist=True
if exist==False:
print("\nThe CD does not exist.\n")
elif(choice=="4"): #Show all CDs containing a specific song
song =input("Enter the song of CD to show: ")
exist=False
for cd in musicStore:
if cd.songExist(song):
cd.displayCDInformation()
print("")
exist=True
if exist==False:
print("\nThe CD does not exist.\n")
elif(choice=="5"):# exit
pass
else:
print("\nSelect a correct menu item.\n")
#This function allows to save all CDs to the json file
def saveMusicStore(musicStore):
#File name
FILE_NAME='Music Store.json'
#convert the object to a dictionary
results = [CD.__dict__ for CD in musicStore]
with open(FILE_NAME, "w") as outfile:
json.dump(results, outfile)
#This function allows to read all CDs from the json file
def readMusicStore(musicStore):
FILE_NAME='Music Store.json'
file = pathlib.Path(FILE_NAME)
if file.exists():
with open(FILE_NAME,'r+') as file:
file_data = json.load(file)
for item in file_data:
newCD=CD()
newCD.setData(item['albumTitle'],item['singerBandName'],item['recordingHouse'],item['publishedYear'],item['musicGenre'],item['listSongTitles'])
musicStore.append(newCD)
main()
Comments
Leave a comment