Untitled
unknown
plain_text
a year ago
3.6 kB
5
Indexable
class SongRecord:
def __init__(self, title, artist, minutes, seconds):
self.title = title
self.artist = artist
self.minutes = minutes
if 0 <= seconds <= 59:
self.seconds = seconds
else:
raise ValueError("Seconds must be between 0 and 59")
def get_title(self):
return self.title
def set_title(self, title):
self.title = title
def get_artist(self):
return self.artist
def set_artist(self, artist):
self.artist = artist
def get_minutes(self):
return self.minutes
def set_minutes(self, minutes):
if minutes >= 0:
self.minutes = minutes
else:
raise ValueError("Minutes must be non-negative")
def get_seconds(self):
return self.seconds
def set_seconds(self, seconds):
if 0 <= seconds <= 59:
self.seconds = seconds
else:
raise ValueError("Seconds must be between 0 and 59")
def __str__(self):
return f"Title: {self.title}, Artist: {self.artist}, Duration: {self.minutes}:{self.seconds:02}"
class Playlist:
MAX_SONGS = 50
def __init__(self):
self.songs = []
self.size = 0
def add_song(self, song, position):
if 1 <= position <= self.size + 1 and self.size < self.MAX_SONGS:
self.songs.insert(position - 1, song)
self.size += 1
print(f"Song Added: {song.get_title()} By {song.get_artist()}")
else:
if position < 1 or position > self.size + 1:
print("Invalid position. Position must be between 1 and", self.size + 1)
else:
print("Playlist is full. Cannot add more songs.")
def remove_song(self, position):
if 1 <= position <= self.size:
removed_song = self.songs.pop(position - 1)
self.size -= 1
print(f"Removed: {removed_song.get_title()} By {removed_song.get_artist()}")
else:
print("Invalid position. Position must be between 1 and", self.size)
def get_song(self, position):
if 1 <= position <= self.size:
return self.songs[position - 1]
else:
print("Invalid position. Position must be between 1 and", self.size)
return None
def print_all_songs(self):
if self.size == 0:
print("Playlist is empty.")
else:
print("Song Title Artist Length")
for i, song in enumerate(self.songs, start=1):
print(f"{i}. {song}")
# Sample usage of the SongRecord and Playlist classes
if __name__ == "__main__":
playlist = Playlist()
while True:
print("\nA) Add Song\nR) Remove Song\nP) Print All Songs\nQ) Quit")
choice = input("Select a menu option: ").strip().lower()
if choice == 'a':
title = input("Enter the song title: ")
artist = input("Enter the song artist: ")
minutes = int(input("Enter the song length (minutes): "))
seconds = int(input("Enter the song length (seconds): "))
position = int(input("Enter the position: "))
song = SongRecord(title, artist, minutes, seconds)
playlist.add_song(song, position)
elif choice == 'r':
position = int(input("Enter the position to remove: "))
playlist.remove_song(position)
elif choice == 'p':
playlist.print_all_songs()
elif choice == 'q':
break
else:
print("Invalid choice. Please select a valid menu option.")Editor is loading...
Leave a Comment