Class Album
java.lang.Object
Album
Represents an album consisting of a stack of songs. The Album class allows adding and removing
songs in LIFO order.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
The name of the album.private int
The number of songs currently in the album.private LinkedStack<Song>
Stack to store songs in the album. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addSong
(Song s) Adds a song to the top of the album's track list and adds the Album reference to the song.Song
Retrieves the song that is currently at the top of the album's track list, without removing it from the stack.Retrieves the name of the album.Song
Removes the most recently added song from the album.int
size()
Returns the number of songs currently in the album.toString()
Returns a string representation of the album, with the name of the album as the first line and listing all songs from the top of the stack to the bottom.
-
Field Details
-
trackList
Stack to store songs in the album. -
albumName
The name of the album. -
size
private int sizeThe number of songs currently in the album.
-
-
Constructor Details
-
Album
Constructs an empty Album with a new LinkedStack to store song and size as zero.- Parameters:
albumName
- the name of the album- Throws:
IllegalArgumentException
- if the name is null or empty
-
-
Method Details
-
addSong
public void addSong(Song s) Adds a song to the top of the album's track list and adds the Album reference to the song.- Parameters:
s
- the Song object to be added to the album- Throws:
IllegalArgumentException
- if the song already exists in the album.
-
removeSong
public Song removeSong()Removes the most recently added song from the album.- Returns:
- the Song object removed from the top of the album
- Throws:
NoSuchElementException
- if the album is empty
-
firstSong
public Song firstSong()Retrieves the song that is currently at the top of the album's track list, without removing it from the stack.- Returns:
- the Song object at the top of the album, or null if the album is empty
-
getAlbumName
Retrieves the name of the album.- Returns:
- the the name of the album.
-
size
public int size()Returns the number of songs currently in the album.- Returns:
- the number of songs in the album
-
toString
Returns a string representation of the album, with the name of the album as the first line and listing all songs from the top of the stack to the bottom. The output string should separate Songs using a new line (\n). Top of the stack should be the first line of the string.
-