diff --git a/lib/__pycache__/song.cpython-313.pyc b/lib/__pycache__/song.cpython-313.pyc new file mode 100644 index 000000000..9528d021d Binary files /dev/null and b/lib/__pycache__/song.cpython-313.pyc differ diff --git a/lib/song.py b/lib/song.py index b19f240bc..55a5af040 100644 --- a/lib/song.py +++ b/lib/song.py @@ -1,2 +1,53 @@ class Song: - pass + # Class attributes + count = 0 + genres = [] + artists = [] + genre_count = {} + artist_count = {} + + def __init__(self, name, artist, genre): + """Initialize a new Song instance with name, artist, and genre.""" + self.name = name + self.artist = artist + self.genre = genre + + # Call class methods to update class attributes + self.add_song_to_count() + self.add_to_genres(genre) + self.add_to_artists(artist) + self.add_to_genre_count(genre) + self.add_to_artist_count(artist) + + @classmethod + def add_song_to_count(cls): + """Increment the total count of songs.""" + cls.count += 1 + + @classmethod + def add_to_genres(cls, genre): + """Add genre to genres list if it's not already present.""" + if genre not in cls.genres: + cls.genres.append(genre) + + @classmethod + def add_to_artists(cls, artist): + """Add artist to artists list if it's not already present.""" + if artist not in cls.artists: + cls.artists.append(artist) + + @classmethod + def add_to_genre_count(cls, genre): + """Update the count of songs for each genre.""" + if genre in cls.genre_count: + cls.genre_count[genre] += 1 + else: + cls.genre_count[genre] = 1 + + @classmethod + def add_to_artist_count(cls, artist): + """Update the count of songs for each artist.""" + if artist in cls.artist_count: + cls.artist_count[artist] += 1 + else: + cls.artist_count[artist] = 1 \ No newline at end of file diff --git a/test_song.py b/test_song.py new file mode 100644 index 000000000..9d7569e7f --- /dev/null +++ b/test_song.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +import sys +import os +sys.path.append(os.path.join(os.path.dirname(__file__), 'lib')) + +from song import Song + +# Reset class attributes for testing +Song.count = 0 +Song.genres = [] +Song.artists = [] +Song.genre_count = {} +Song.artist_count = {} + +# Test basic functionality +print("Testing Song class implementation...") + +# Create some songs +song1 = Song("99 Problems", "Jay Z", "Rap") +song2 = Song("Halo", "Beyonce", "Pop") +song3 = Song("Smells Like Teen Spirit", "Nirvana", "Rock") +song4 = Song("Out of Touch", "Hall and Oates", "Pop") + +# Test instance attributes +print(f"Song1 name: {song1.name}") +print(f"Song1 artist: {song1.artist}") +print(f"Song1 genre: {song1.genre}") + +# Test class attributes +print(f"Total song count: {Song.count}") +print(f"Genres: {Song.genres}") +print(f"Artists: {Song.artists}") +print(f"Genre count: {Song.genre_count}") +print(f"Artist count: {Song.artist_count}") + +# Create another song to test counting +song5 = Song("Sara Smile", "Hall and Oates", "Pop") +print(f"After adding another song:") +print(f"Total song count: {Song.count}") +print(f"Genre count: {Song.genre_count}") +print(f"Artist count: {Song.artist_count}") + +print("All tests completed successfully!") \ No newline at end of file