Day 62 - OOP Playlist App - Collection Objects - Part 2

In this project, we'll be creating what's called a collection object - In other words, an object that can hold and manage other objects. First, we created the Song object. In this part, we will create the collection object, which is called clPlaylist

Starter Code

class clSong:
    def __init__(self, strTitle, strRating, strArtist, intYear):
        self.strTitle = strTitle
        self.strRating = strRating
        self.strArtist = strArtist
        self.intYear = intYear

    def __str__(self):
        return f"{self.strTitle}".ljust(25)+f"{self.strRating}".ljust(7)+f"{self.strArtist}".ljust(15)+f"{self.intYear}".ljust(7)

    def getTitle(self):
        return self.strTitle

    def getRating(self):
        return self.strRating

    def getArtist(self):
        return self.strArtist

    def getYear(self):
        return self.intYear

    def setRating(self):
        print(f"\nThe current rating is: {self.strRating}")
        strNew = input("What should the new rating be? (* - *****)")
        boolValid = True
        for strChar in strNew:
            if strChar != "*":
                boolValid = False
                break
        if len(strNew) < 1 or len(strNew) > 5:
            boolValid = False

        if boolValid == True:
            self.strRating = strNew
            print("Your rating has been updated.")
        else:
            print("Your input was not valid.")

mySong = clSong("Thriller", "**", "Jackson", 1994)

print(mySong)

mySong.setRating()

print(mySong)

Finished Code

class clSong:
    def __init__(self, strTitle, strRating, strArtist, intYear):
        self.strTitle = strTitle
        self.strRating = strRating
        self.strArtist = strArtist
        self.intYear = intYear

    def __str__(self):
        return f"{self.strTitle}".ljust(25)+f"{self.strRating}".ljust(7)+f"{self.strArtist}".ljust(15)+f"{self.intYear}".ljust(7)

    def getTitle(self):
        return self.strTitle

    def getRating(self):
        return self.strRating

    def getArtist(self):
        return self.strArtist

    def getYear(self):
        return self.intYear

    def setRating(self):
        print(f"\nThe current rating is: {self.strRating}")
        strNew = input("What should the new rating be? (* - *****)")
        boolValid = True
        for strChar in strNew:
            if strChar != "*":
                boolValid = False
                break
        if len(strNew) < 1 or len(strNew) > 5:
            boolValid = False

        if boolValid == True:
            self.strRating = strNew
            print("Your rating has been updated.")
        else:
            print("Your input was not valid.")


class clPlaylist:
    def __init__(self):
        self.liPlaylist = []

    def addSong(self):
        strT = input("What is the title? ")
        while True:
            strR = input("What is the rating? (* - *****): ")
            boolValid = True
            for strChar in strR:
                if strChar != "*":
                    boolValid = False
                    break
            if len(strR) < 1 or len(strR) > 5:
                boolValid = False

            if boolValid == True:
                break
            else:
                print("Your input was not valid.")
                print("Rating should be * to *****\n")

        strA = input("Who is the artist? ")
        while True:
            intY = input("What is the year?")
            if len(intY) == 4 and intY.isnumeric():
                intY = int(intY)
                break
            else:
                print("\nYears must be four digits.")
        self.liPlaylist.append(clSong(strT, strR, strA, intY))

    def listSongs(self):
        for song in self.liPlaylist:
            print(song)


myPL = clPlaylist()

myPL.addSong()
myPL.addSong()

myPL.listSongs()