-
Notifications
You must be signed in to change notification settings - Fork 0
/
JukeBox.java
54 lines (48 loc) · 1.37 KB
/
JukeBox.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.Queue;
import java.util.LinkedList;
/**
* JukeBox class which demonstrates the Singleton Design Pattern
* @author Nick Bautista
*
*/
public class JukeBox {
private Queue<String> songQueue = new LinkedList();//Has to be initialized here because constructor is empty.
private static JukeBox jukeBox;
private JukeBox() {}
/**
* Calls constructor only when no other instance exists which limits the number of instances to a maximum of 1
*/
public static JukeBox getInstance() {
if(jukeBox == null) {
System.out.println("Initializing the JukeBox!!! Let's get Dancing");
jukeBox = new JukeBox();
}
return jukeBox;
}
/**
* Removes element at the front of the queue then returns it.
*/
public void playNextSong() {
if(songQueue.isEmpty()) {
System.out.println("You need to add songs to the list.");
return;
}
System.out.println("Let's jam to " + songQueue.remove());
}
/**
* Adds parameter to the queue then displays how far it is in the queue.
*/
public void requestSong(String songName) {
songQueue.add(songName);
System.out.println(songName + " is now number " + songQueue.size() + " on the list.");
}
/**
* Checks whether queue is empty or not.
* @return
*/
public boolean hasMoreSongs() {
if(songQueue.isEmpty())
return false;
return true;
}
}