Repository navigation
Expand file tree
/
Copy pathPlaylist.java
More file actions
150 lines (126 loc) · 4.85 KB
/
Copy pathPlaylist.java
File metadata and controls
150 lines (126 loc) · 4.85 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Playlist: doubly linked list data structure that stores Song nodes.
// Supports adding, removing, and navigating through songs.
public class Playlist {
private Song head = null;
private Song tail = null;
private int size = 0;
private Song playing = null; // keeps track of the song that is playing at the time.
public Playlist(){ // Constructor
addSong("20 Cigarettes", "Morgan Wallen", "/audio/20Cigarettes--MorganWallen.mp3");
addSong("All the Love", "Ye", "/audio/AlltheLove--Ye.mp3");
addSong("American Girls", "Harry Styles", "/audio/AmericanGirls--HarryStyles.mp3");
addSong("E85", "Don Toliver", "/audio/E85--DonToliver.mp3");
addSong("Hooligan", "BTS", "/audio/Hooligan--BTS.mp3");
addSong("Risk it All", "Bruno Mars", "/audio/RiskitAll--BrunoMars.mp3");
addSong("Seeing Someone", "Luke Combs", "/audio/SeeingSomeone--LukeCombs.mp3");
addSong("So Easy", "Olivia Dean", "/audio/SoEasy--OliviaDean.mp3");
addSong("Turista", "Bad Bunny", "/audio/Turista--BadBunny.mp3");
addSong("Saint James Infirmary", "Louis Armstrong", "/audio/song1.mp3");
}
// PLAYLIST METHODS
public void addSong(String title, String artist, String filepath){
Song track = new Song(title, artist, filepath); // track as in the current song
if (size == 0){
head = track;
tail = track;
playing = track; // also sets the song that is clicked to the head
} else {
track.setPrev(tail);
tail.setNext(track);
tail = track;
}
size++;
}
public void removeSong(String title){
Song songName = head;
if (size == 0){ // If playlist is EMPTY
System.out.println("Playlist is EMPTY");
} else if (size == 1){// If there is only 1 song in the playlist
if (head.getTitle().equals(title)){
if (head == playing) {
playing = null; // Moves playing pointer also
}
head = null;
tail = null;
size--;
}
} else {
while (songName != null){
if(songName.getTitle().equals(title)){
if (songName == playing){
if (songName.getNext() != null){
playing = songName.getNext();
} else {
playing = songName.getPrev();
}
}
if (songName == head) { // If the song to delete is the first one(head)
head = songName.getNext();
if (head != null){ // So code doesn't break (null exception)
head.setPrev(null);
}
size--;
break;
}
else if (songName == tail){ // if the song to delete is the last one(tail)
tail = songName.getPrev();
if (tail != null){
tail.setNext(null);
}
size--;
break;
} else { // Deletes song by name and if it doesn't satisfy the other cases.
songName.getPrev().setNext(songName.getNext());
songName.getNext().setPrev(songName.getPrev());
size--;
break;
}
}
songName = songName.getNext();
}
}
}
public Song nextSong(){
if (playing == null){
return null;
}
else if (playing.getNext() != null){
playing = playing.getNext();
} else {
// loop it back to head
playing = head;
}
return playing;
}
public Song prevSong(){
if (playing == null){
return null;
}
else if (playing.getPrev() != null){
playing = playing.getPrev();
} else {
// loop it back to tail if you're playing the first song.
playing = tail;
}
return playing;
}
public void printPlaylist(){
int counter = 0; // song counter
if (size == 0){
System.out.println("PLAYLIST IS EMPTY");
} else {
Song songName = head;
while (songName != null){
counter = counter + 1;
System.out.println(counter + ". " + songName.getTitle() + " - " + songName.getArtist());
songName = songName.getNext();
}
}
}
public int getSize(){ //
return size;
}
public Song getPlaying(){ // gets current song thet is playing
return playing;
}
}