-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaylist.java
More file actions
181 lines (159 loc) · 5.89 KB
/
Playlist.java
File metadata and controls
181 lines (159 loc) · 5.89 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.util.Collections;
import java.util.LinkedList;
/**
* Author Jonathan Falvey
* ID 19239718
*/
public class Playlist {
private String name; //name of the playlist
private LinkedList<Track> tracks; // list of tracks contained in the playlist
/**
* constructor for new playlists. initialises the tracks list and sets the name
* of the playlist to "Playlist" by default
*/
public Playlist() {
this.tracks = new LinkedList<>();
this.name = "Playlist";
}
/**
* This method returns a string containing all the tracks in the playlist in the order they were added
*
* @return String containing the details of every track in the playlist
*/
public String toString() {
String temp = "";
for(Track track : tracks) {
temp = temp + track.toString() + System.lineSeparator();
}
return temp;
}
/**
* simple getter method that returns the name of the playlist
*
* @return String with name of the playlist
*/
public String getName() { return name; }
/**
* simple setter method that changes the name of the playlist
*
* @param name new name of the playlist
*/
public void setName(String name) {
this.name = name;
}
/**
* first add method. adds a new track to the playlist with
* the title and artist specified by the user
*
* @param title title of the track
* @param artist name of the artist of the track
*/
public void add(String title, String artist) {
tracks.addLast(new Track(title, artist));
}
/**
* second add method. adds a new track to the playlist with the title,
* artist, year and duration in seconds specified by the user
*
* @param title name of the track
* @param artist name of the artist of the track
* @param year year the track was released
* @param duration duration of the track in seconds
*/
public void add(String title, String artist, int year, int duration) {
tracks.addLast(new Track(title, artist, year, duration));
}
/**
* third add method. adds an already existing track to the playlist
* @param track Track already created elsewhere
*/
public void add(Track track) {
tracks.addLast(track);
}
/**
* this method will remove the first instance of a track with the title specified by the user.
* it first ensures the playlist is not empty before continuing. it then searches through the list
* for a track with the desired title, and removes it if found. the search is case insensitive
*
* @param title the title of the specific track to be removed
* @return boolean that indicates if the remove was successful. tue if yes, false if no
*/
public boolean remove(String title) {
if(tracks.size() > 0) { // check if the playlist is not empty
for(Track track : tracks) {
if(title.equalsIgnoreCase(track.getTitle())) { //case insensitive comparison of the titles of tracks
tracks.remove(track);
return true; //return true if successful
}
}
}
return false; //return false if playlist is empty or no track was removed
}
/**
* This method will print the names and artists of each track in the playlist, but only if the playlist is not empty
*/
public void showList() {
if (tracks.size() > 0) {
for(Track track : tracks) {
System.out.println(track.getTitle() + " - " + track.getArtist());
}
} else {
System.out.println("The list is empty."); //print this if the playlist is empty
}
}
/**
* This method will "play" all of the tracks in the playlist.
* (It wil display the details of every track)
*
* @param random if true, it will shuffle the playlist and then play all the tracks
* if false, it will play all the tracks in their current order
*/
public void playAll(boolean random) {
if(random) { // create a copy of the list of tracks and shuffle it. then play all the tracks
LinkedList<Track> shuffleTracks = (LinkedList) tracks.clone(); //our copy to be shuffled
Collections.shuffle(shuffleTracks);
for(Track track : shuffleTracks) { //play all the now shuffled tracks
System.out.println(track.toString());
}
} else { // play all tracks in the list in the order they were added
for(Track track : tracks) {
System.out.println(track.toString());
}
}
}
/**
* This method will play all tracks in the playlist by a specific artist
* the name entered can be case insensitive
*
* @param artist name of the artist
*/
public void playOnly(String artist) {
for(Track track : tracks) {
if(artist.equalsIgnoreCase(track.getArtist())) { //case insensitive comparison
System.out.println(track.toString());
}
}
}
/**
* this method will play all the tracks in the playlist that were released
* in a certain year
*
* @param year the year the tracks were released
*/
public void playOnly(int year) {
for (Track track : tracks) {
if (year == track.getYear()) {
System.out.println(track.toString());
}
}
}
/**
* Alternate constructor that allows the user to immediately specify the name of the playlist
*
* @param name
*/
public Playlist(String name) {
this.tracks = new LinkedList<>();
this.name = name;
}
}