Class JukeBox


public class JukeBox extends Object
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

    Fields
    Modifier and Type
    Field
    Description
    private 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
    Constructor
    Description
    JukeBox(int capacity)
    Constructs a new JukeBox with a specified capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    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
    Returns the capacity of the queue.
    boolean
    Checks if the queue is empty.
    boolean
    Checks if the queue is full.
    Song
    Plays and removes the song at the front of the queue.
    void
    Shuffles the songs present in the queue.
    int
    Returns the current number of songs in the queue.
    Provides a string representation of the jukebox queue for debugging and display.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • songQueue

      private LinkedQueue<Song> songQueue
      Queue to store songs in the jukebox in FIFO order.
    • capacity

      private int capacity
      The 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 capacity
      IllegalArgumentException - if the song already exists in the queue
    • addAlbumToQueue

      public void addAlbumToQueue(Album album)
      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:
      1. Create an ArrayList representation of all of the elements of this queue, in order
      2. Use Collections.shuffle() to create a new random ordering of the contents
      3. 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

      public String 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
      Overrides:
      toString in class Object
      Returns:
      a string representing the queue, including song details in order