In: Computer Science
Objectives:
The class files are here: https://drive.google.com/file/d/1yCCQeZCS-uLoL_CK0Et9dX-KCaokXQxR/view?usp=sharing
class MyTunes
Creates an object of type MyTunes class that partially simulate the digital jukebox TouchTunes, using a queue which holds playlist. Tests the implementation of your Queue and Jukebox classes.
From main your program:
class Jukebox
Define the class Jukebox to manage three objects of type Queue<SongEntry>. The class Jukebox manages three objects of type Queue. An instance of the class may read a file which includes the user's requests for a the name of a song to be added to a specific playlist. It will then add songs to the three playlists "favorites", "lounge", and "road trip" accordingly.
Attributes:
Methods:
A method called fillPlaylists() reads the test file and then adds songs to one of the three queues. For example, if the file contains line:
favorites,title
Then the first song found that equals the title will be placed in the favorites playlist.
class Queue<Type>
Define the parameterized class Queue which implements Iterable. Objects of type Queue manage items in a singly linked list where we can enqueue() items to the end and dequeue() items from the front of the queue.
Attributes:
Methods:
Input File Format:
The format of the "tunes.txt" input file is:
favorites,Shadows - Original road trip,Tom's Diner favorites,Take Me Away road trip,Here With Me lounge,Nuvole Bianche lounge,Luka favorites,Stoned road trip,Get Happy favorites,We Belong road trip,Let's Dance road trip,Oh What a Feeling favorites,Stairway To The Stars road trip,Separate Ways (Worlds Apart) road trip,Road Home lounge,Traffic
Output of example 1 showing non-empty queues:
Welcome! We have over 59600 in FoothillTunes store! Total number of songs in playlists: 16 Songs in each playlist: favorites: [Shadows - Original, 0:0:25, Blue Oyster Cult, classic pop and rock; Take Me Away, 0:4:32, Blue Oyster Cult, classic pop and rock; Stoned, 0:11:47, Dido, classic pop and rock; We Belong, 0:3:43, Pat Benatar, classic pop and rock; Stairway To The Stars, 0:3:46, Blue Oyster Cult, classic pop and rock] lounge: [Solo, 0:4:41, Working Week, classic pop and rock; Nuvole Bianche, 0:5:58, Ludovico Einaudi, classical; Luka, 0:3:52, Suzanne Vega, classic pop and rock; Traffic, 0:4:5, Dawn Landes, classic pop and rock] road trip: [Tom's Diner, 0:2:40, Suzanne Vega, classic pop and rock; Here With Me, 0:4:41, Dido, classic pop and rock; Get Happy, 0:6:36, Jean Knight, classic pop and rock; Let's Dance, 0:2:45, Jake Shimabukuro, folk; Oh What a Feeling, 0:3:42, Gregory Isaac, classic pop and rock; Separate Ways (Worlds Apart), 0:5:25, Journey, classic pop and rock; Road Home, 0:5:8, The String Cheese Incident, folk] Enter your the number of songs you would like to play: 7 Playing 7 number of songs... Playing song title "Shadows - Original" Playing song title "Solo" Playing song title "Tom's Diner" Playing song title "Take Me Away" Playing song title "Nuvole Bianche" Playing song title "Here With Me" Playing song title "Stoned" Checking the size of each playlist: Playlist "favorites" is 2 song(s) remaining. Playlist "lounge" is 2 song(s) remaining. Playlist "road trip" is 5 song(s) remaining. Songs in each list: favorites: [We Belong, 0:3:43, Pat Benatar, classic pop and rock; Stairway To The Stars, 0:3:46, Blue Oyster Cult, classic pop and rock] lounge: [Luka, 0:3:52, Suzanne Vega, classic pop and rock; Traffic, 0:4:5, Dawn Landes, classic pop and rock] road trip: [Get Happy, 0:6:36, Jean Knight, classic pop and rock; Let's Dance, 0:2:45, Jake Shimabukuro, folk; Oh What a Feeling, 0:3:42, Gregory Isaac, classic pop and rock; Separate Ways (Worlds Apart), 0:5:25, Journey, classic pop and rock; Road Home, 0:5:8, The String Cheese Incident, folk] Done with MyTunes.
Output of example 2 with input test file "tunes_truncated.txt" where some queues are empty when calling dequeue() method of class Queue:
Welcome! We have over 59600 in FoothillTunes store! Total number of songs in playlists: 3 Songs in each playlist: favorites: [Shadows - Original, 0:0:25, Blue Oyster Cult, classic pop and rock] lounge: [Solo, 0:4:41, Working Week, classic pop and rock] road trip: [Tom's Diner, 0:2:40, Suzanne Vega, classic pop and rock] Enter your the number of songs you would like to play: 4 Playing 4 number of songs... Playing song title "Shadows - Original" Playing song title "Solo" Playing song title "Tom's Diner" Checking the size of each playlist: Playlist "favorites" has *no* songs remaining. Playlist "lounge" has *no* songs remaining. Playlist "road trip" has *no* songs remaining. Songs in each list: favorites: [ ] lounge: [ ] road trip: [ ] Done with MyTunes.
Answer: See the code below:
1. Queue class: Queue.java
------------------------------------------
package mytunesdemo;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Queue class
* @param <T>
*
*/
public class Queue<T> implements Iterable<T>{
//node in the queue
class Node
{
T item;
Node next;
}
private String name; //name of instance
private Node head; //front of the queue
private Node tail; //end of the queue
private int numItems; //number of items in
queue
/**
* @param name
*/
public Queue(String name) {
this.name = name;
this.head = null;
this.tail = null;
this.numItems = 0;
}
/**
*
* @return true if queue is empty.
*/
public boolean isEmpty()
{
return (size() == 0);
}
/**
* enqueues an item to the end of the queue
* @param item
*/
public void enqueue(T item)
{
Node n = new Node();
n.item = item;
n.next = null;
if(isEmpty())
{
head = n;
tail = n;
}
else
{
tail.next =
n;
tail = n;
}
numItems++;
}
/**
* dequeues an item from the front of list
* @return
*/
public Node dequeue()
{
Node n = null;
if (isEmpty())
throw new
NoSuchElementException();
else
{
n = head;
head =
head.next;
}
numItems--;
return n;
}
/**
*
* @return most recently added element to the
queue.
*/
public Node peek()
{
if(isEmpty())
return
null;
else
return
head;
}
/**
*
* @return size of the queue.
*/
public int size()
{
return numItems;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Queue [" + (name != null ?
"name=" + name + ", " : "") + (head != null ? "head=" + head + ", "
: "")
+ (tail != null ? "tail=" + tail + ", " : "") +
"numItems=" + numItems + "]";
}
@Override
public Iterator<T> iterator() {
// TODO Auto-generated method
stub
return null;
}
}
--------------------------------------
2. JukeBox class: Jukebox.java
------------------------------------------------
package mytunesdemo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* JukeBox class
*
*/
public class JukeBox {
private Queue<SongEntry> fovoritePL; //fovorite
playlist
private Queue<SongEntry> roadTripPL; //road trip
playlist
private Queue<SongEntry> loungePL; //lounge
playlist
/**
* Default constructor
*/
public JukeBox() {
fovoritePL = new
Queue<SongEntry>("favorites");
roadTripPL = new
Queue<SongEntry>("road trip");
loungePL = new
Queue<SongEntry>("lounge");
}
/**
* fills play lists from the file specified
*/
public void fillPlayLists(String filename)
{
try {
File inputFile =
new File(filename);
BufferedReader
reader = new BufferedReader(new FileReader(inputFile));
String
line;
while((line=reader.readLine())!=null)
{
String[] tokens = line.split(",");
String listType = tokens[0];
String songTitle = tokens[1];
SongEntry songEntry = new
SongEntry(songTitle);
if(listType.equals("fovorites"))
{
fovoritePL.enqueue(songEntry);
}
else if(listType.equals("road trip"))
{
roadTripPL.enqueue(songEntry);
}
else if(listType.equals("lounge"))
{
loungePL.enqueue(songEntry);
}
}
reader.close();
} catch (FileNotFoundException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
/**
* @return the fovoritePL
*/
public Queue<SongEntry> getFovoritePL() {
return fovoritePL;
}
/**
* @return the roadTripPL
*/
public Queue<SongEntry> getRoadTripPL() {
return roadTripPL;
}
/**
* @return the loungePL
*/
public Queue<SongEntry> getLoungePL() {
return loungePL;
}
}
--------------------------------------------