-
Notifications
You must be signed in to change notification settings - Fork 0
/
MusicPlaylist.java
115 lines (108 loc) · 3.62 KB
/
MusicPlaylist.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
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
package pr.java;
public class MusicPlaylist {
public SongNode head;
public void playPlaylist() {
if (head == null) {
System.out.println("Playlist is empty.");
return;
}
SongNode current = head;
System.out.println("Playing playlist:");
while (current != null) {
System.out.println(current.songTitle + " - " + current.artist + " (" + current.duration + " seconds)");
current = current.nextSong;
}
}
public void addSong(String title, String artist, int duration) {
SongNode newSong = new SongNode(title, artist, duration);
if (head == null) {
head = newSong;
} else {
SongNode current = head;
while (current.nextSong != null) {
current = current.nextSong;
}
current.nextSong = newSong;
}
}
public void removeSong(String title) {
if (head == null) {
System.out.println("Playlist is empty.");
return;
}
if (head.songTitle.equals(title)) {
head = head.nextSong;
} else {
SongNode current = head;
while (current.nextSong != null && !current.nextSong.songTitle.equals(title)) {
current = current.nextSong;
}
if (current.nextSong == null) {
System.out.println("Song not found.");
} else {
current.nextSong = current.nextSong.nextSong;
}
}
}
public void rearrangeSong(String title, int newPosition) {
if (head == null) {
System.out.println("Playlist is empty.");
return;
}
SongNode current = head;
SongNode prev = null;
int currentPosition = 1;
while (current != null && !current.songTitle.equals(title)) {
prev = current;
current = current.nextSong;
currentPosition++;
}
if (current == null) {
System.out.println("Song not found.");
return;
}
if (newPosition < 1 || newPosition > currentPosition) {
System.out.println("Invalid position.");
return;
}
if (currentPosition == newPosition) {
System.out.println("Song is already in that position.");
return;
}
if (newPosition == 1) {
head = current.nextSong;
current.nextSong = null;
addSongAtPosition(current, 1);
} else {
prev.nextSong = current.nextSong;
current.nextSong = null;
addSongAtPosition(current, newPosition);
}
}
private void addSongAtPosition(SongNode newSong, int position) {
if (head == null) {
head = newSong;
return;
}
if (position == 1) {
newSong.nextSong = head;
head = newSong;
return;
}
SongNode current = head;
int currentPosition = 1;
while (currentPosition < position - 1 && current.nextSong != null) {
current = current.nextSong;
currentPosition++;
}
newSong.nextSong = current.nextSong;
current.nextSong = newSong;
}
public void printPlaylist() {
SongNode current = head;
while (current != null) {
System.out.println(current.songTitle + " by " + current.artist + " (" + current.duration + "s)");
current = current.nextSong;
}
}
}