Day 63 - OOP Playlist App - Saving Data Within Objects - Part 3

Today, we will build out the menu structure of the app, and add the ability to save and load data within the playlist object from a file using pickle. 

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.")


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

Finished Code

import os, pickle

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(20)+f"{self.strRating}".ljust(12)+f"{self.strArtist}".ljust(20)+f"{self.intYear}".rjust(8)

    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):
        if os.path.exists("playlist_data.txt"):
            file = open("playlist_data.txt", "rb")
            self.liPlaylist = pickle.load(file)
            file.close()
        else:
            self.liPlaylist = []

        self.saveData()

    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))
        self.saveData()

    def listSongs(self):
        print(f"Title".ljust(20)+f"Rating".ljust(12)+f"Artist".ljust(20)+f"Year".ljust(8))
        print("-"*60)
        for song in self.liPlaylist:
            print(song)

    def saveData(self):
        file = open("playlist_data.txt", "wb")
        pickle.dump(self.liPlaylist, file)
        file.close()

print("*"*60)
print("Welcome to my Playlist".center(60))
print("*"*60+"\n")

myPL = clPlaylist()


while True:
    print("""
    -----------------
    Main Menu
    -----------------
    1. List Songs
    2. Add a Song
    3. Remove a Song
    4. Change a Rating
    0. Exit the Program
    """)
    strMenu = input("What would you like to do? ")
    if strMenu == "1":
        myPL.listSongs()
        print("\n")

    elif strMenu == "2":
        myPL.addSong()
        print("\n")

    elif strMenu == "3":
        pass

    elif strMenu == "4":
        pass

    elif strMenu == "0":
        print("Thanks for using my Playlist App!")
        break
    else:
        print("That's not an option.")









#
# myPL.addSong()
# myPL.addSong()
#
# myPL.listSongs()