Question
In Java, You will be implementing the Singleton Design Pattern to create a Jukebox. JukeBox Class: This is your singleton, you are only able to
In Java, You will be implementing the Singleton Design Pattern to create a Jukebox.
JukeBox Class:
This is your singleton, you are only able to make one jukebox.
You will create an ArrayList of all the songs in the jukebox
You will create a Queue to hold the songs requested.
In the JukeBox's private constructor:
Read in all of the songs from the DataLoader to create the ArrayList of songs.
when you call playNextSong(), return:
Let's jam to
You need to add songs to the list
When you call requestSong:
if
Otherwise return Sorry we do not have the song
When you call hasMoreSongs:
returns true or false accordingly
MusicDriver:
The driver will create a JukeBox
Request songs
play next song
DataLoader.Java:
package singleton;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Used to read data from text files
*/
public class DataLoader {
// relative path to the file, based on the directory you currently have open
private static final String FILE_NAME = "singleton/songs.txt";
/**
* Returns a list of all songs from the text file
*
* @return An ArrayList of Songs
*/
public static ArrayList
ArrayList
try {
File file = new File(FILE_NAME);
Scanner scanner = new Scanner(file);
// loop through to get each question
while (scanner.hasNextLine()) {
String[] data = scanner.nextLine().split("-");
String title = data[0].trim();
String artist = data[1].trim();
Song song = new Song(title, artist);
songs.add(song);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("Sorry, we could not properly read the songs file");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return songs;
}
}
MuiscDriver.java
package singleton;
public class MusicDriver {
public static void main(String[] args) {
JukeBox jukeBox = JukeBox.getInstance();
System.out.println();
// Holds the list of all the songs we want to add to the jukebox
String[] myFavSongs = { "Hey Jude", "Baby Shark", "Dancing Queen", "Stand By Me", "No Woman No Cry", "YMCA",
"My Generation" };
// loops through and attempst to add songs to the jukebox
System.out.println("Entering our songs");
for (String song : myFavSongs) {
System.out.println(jukeBox.requestSong(song));
}
// prints the songs as they are played
System.out.println(" Playing our songs");
while (jukeBox.hasMoreSongs()) {
System.out.println(jukeBox.playNextSong());
}
}
}
output.txt:
Entering our songs Hey Jude is now number 1 on the list Sorry, we do not have the song Baby Shark Dancing Queen is now number 2 on the list Stand By Me is now number 3 on the list No Woman No Cry is now number 4 on the list Sorry, we do not have the song YMCA My Generation is now number 5 on the list
Playing our songs Let's jam to Hey Jude by The Beatles Let's jam to Dancing Queen by ABBA Let's jam to Stand By Me by Ben E King Let's jam to No Woman No Cry by Bob Marley Let's jam to My Generation by The Who
songs.txt:
Smells Like Teen Spirit - Nirvana Imagine - John Lennon One - U2 Billie Jean - Michael Jackson Bohemian Rhapsody - Queen Hey Jude - The Beatles Like A Rolling Stone - Bob Dylan I Can't Get No Satisfaction - Rolling Stones God Save The Queen - Sex Pistols Sweet Child O'Mine - Guns N' Roses London Calling - The Clash Waterloo Sunset - The Kinks Hotel California - The Eagles Your Song - Elton John Stairway To Heaven - Led Zeppelin The Twist - Chubby Checker Live Forever - Oasis I Will Always Love You - Whitney Houston Life On Mars? - David Bowie Heartbreak Hotel - Elvis Presley Over The Rainbow - Judy Garland What's Goin' On - Marvin Gaye Born To Run - Bruce Springsteen Be My Baby - The Ronettes Creep - Radiohead Bridge Over Troubled Water - Simon & Garfunkel Respect - Aretha Franklin Family Affair - Sly And The Family Stone Dancing Queen - ABBA Good Vibrations - The Beach Boys Purple Haze - Jimi Hendrix Yesterday - The Beatles Jonny B Good - Chuck Berry No Woman No Cry - Bob Marley Hallelujah - Jeff Buckley Every Breath You Take - The Police A Day In The Life - The Beatles Stand By Me - Ben E King Papa's Got A Brand New Bag - James Brown Gimme Shelter - The Rolling Stones What'd I Say - Ray Charles Sultans Of Swing - Dire Straits God Only Knows - The Beach Boys You've Lost That Lovin' Feeling - The Righteous Brothers My Generation - The Who Dancing In The Street - Martha Reeves and the Vandellas When Doves Cry - Prince A Change Is Gonna Come - Sam Cooke River Deep Mountain High - Ike and Tina Turner Best Of My Love - The Emotions
UML:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started