Class JukeBox
java.lang.Object
JukeBox
Represents a jukebox that holds a queue of songs for playback. The jukebox has a maximum capacity
to limit the number of songs that can be queued at once.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
The maximum number of songs the jukebox can hold.private LinkedQueue<Song>
Queue to store songs in the jukebox in FIFO order. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAlbumToQueue
(Album album) Adds an entire album to the queue if space allows.void
addSongToQueue
(Song song) Adds a single song to the end of the queue if space allows.int
capacity()
Returns the capacity of the queue.boolean
isEmpty()
Checks if the queue is empty.boolean
isFull()
Checks if the queue is full.Song
playSong()
Plays and removes the song at the front of the queue.void
Shuffles the songs present in the queue.int
size()
Returns the current number of songs in the queue.toString()
Provides a string representation of the jukebox queue for debugging and display.
-
Field Details
-
songQueue
Queue to store songs in the jukebox in FIFO order. -
capacity
private int capacityThe maximum number of songs the jukebox can hold.
-
-
Constructor Details
-
JukeBox
public JukeBox(int capacity) Constructs a new JukeBox with a specified capacity.- Parameters:
capacity
- the maximum number of songs that can be held in the jukebox queue- Throws:
IllegalArgumentException
- if the provided capacity is negative
-
-
Method Details
-
addSongToQueue
public void addSongToQueue(Song song) Adds a single song to the end of the queue if space allows.- Parameters:
song
- the Song object to be added to the queue- Throws:
IllegalStateException
- if the queue is at maximum capacityIllegalArgumentException
- if the song already exists in the queue
-
addAlbumToQueue
Adds an entire album to the queue if space allows. Each song in the album is added individually in order. Add as many songs as possible if album has size greater than space, skip songs already added to queue.- Parameters:
album
- a list of Song objects representing an album
-
playSong
public Song playSong()Plays and removes the song at the front of the queue.- Returns:
- the Song object that was played from the front.
- Throws:
NoSuchElementException
- if the queue is empty
-
shuffleSongQueue
public void shuffleSongQueue()Shuffles the songs present in the queue. Size and capacity after this operation should remain the same. Randomly reorder the contents of the song queue:- Create an ArrayList representation of all of the elements of this queue, in order
- Use
Collections.shuffle()
to create a new random ordering of the contents - REPLACE the current contents of the queue with the contents in their new order from the ArrayList
- See Also:
-
size
public int size()Returns the current number of songs in the queue.- Returns:
- the number of songs in the queue
-
capacity
public int capacity()Returns the capacity of the queue.- Returns:
- the maximum number of songs that can be in the queue
-
isFull
public boolean isFull()Checks if the queue is full.- Returns:
- true if the queue has reached its capacity, false otherwise
-
isEmpty
public boolean isEmpty()Checks if the queue is empty.- Returns:
- true if the queue has no songs, false otherwise
-
toString
Provides a string representation of the jukebox queue for debugging and display. Song representation followed by -> and finishes with "END". Song1: Artist1 (Album1) -> Song2: Artist2 (Album2) -> END
-