-
Notifications
You must be signed in to change notification settings - Fork 0
/
Playlist.java
160 lines (148 loc) · 5.01 KB
/
Playlist.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
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
/**
* PlayList class stores the list of tracks to be played
*
* @author
* Dawid Kocik 19233116
* Tommy Crowley 19263546
* Ronan McMorrow 19235208
* Oscar Bogenberger 19275153
*
* @version 16/04/2020
*/
import java.util.*;
public class PlayList {
public ArrayList<Track> playList;
private String playListName;
// we have to make instance variable for the Collection of Tracks
// 2 constructors
public PlayList() {
this.playList = new ArrayList<Track>();
setName("NewPlaylist");
// initialises empty playlist called My playlist#1 (or next, consecutive number)
}
public PlayList(String playListName) {
this.playList = new ArrayList<Track>();
this.playListName = playListName;
// initialises empty playlist from name given
}
public void setName(String name) {
this.playListName = name;
// allows us to change playlist name
// add a number if to the name if it repeats
}
public String getName() {
// returns name of this playlist
return playListName;
}
public String toString() {
String playListContents="Playlist Name: "+playListName+"\n"+"Tracks:\t";
for (int i=0; i<playList.size(); i++) {
playListContents = playListContents+playList.get(i).toString()+"\n \t";
}
// return instance values in readable format
return playListContents;
}
public void add(String title, String artist) {
Track t = new Track(title, artist);
add(t);
// adds track to the PlayList
}
public void add(String title, String artist, int year, int duration) {
Track t = new Track(title, artist, year, duration);
add(t);
// adds track to the PlayList
}
public void add(Track t) {
this.playList.add(t);
sort();
// adds track to the PlayList
}
public boolean remove(String title) {
int sizeBefore = playList.size();
for (int i=0; i<playList.size(); i++) {
if (playList.get(i).getTitle().equalsIgnoreCase(title)) {
playList.remove(i);
return true;
}
}
//improved fast removal from java 8
// return true if removal successful, else false
// case insensitive
// If the PlayList contains two or more tracks with the same track title
// then the remove operation should only remove the FIRST one located in the PlayList.
return false;
}
public void showList() {
if (playList.size() == 0) {
System.out.println(toString());
System.out.println("The list is empty ");
} else {
System.out.println(toString());
}
// that displays the PlayList on the screen in sequential order
// (i.e. in the order that the tracks appear in the list
// if no tracks display "list is empty"
}
void sort() {
Collections.sort(playList);
}
public void playAll(boolean random) {
if (random) {
int pos;
int length = playList.size();
ArrayList<Integer> ranPos = new ArrayList<Integer>(length);
while (ranPos.size() < length ) {
pos=(int)(Math.random()*playList.size());
if(!ranPos.contains(pos)) {
ranPos.add(pos);
}// only added if it is a unique value
}
for (int i=0; i< ranPos.size(); i++) {
System.out.println(playList.get(ranPos.get(i)).toString());
}
} else {
for (int i=0; i<playList.size(); i++) {
System.out.println(playList.get(i).toString());
}
}
}
/*public String getTitle(int position){
return playList.get(position).getTitle();
}
public String getArtist(int position){
return playList.get(position).getArtist();
}
public int getDuration(int position){
return playList.get(position).getDuration();
}
public int getYear(int position){
return playList.get(position).getYear();
}
public void setTitle(int position, String newTitle){
(playList.get(position)).setTitle(newTitle);
}
public void setTitle(int position, String newTitle){
(playList.get(position)).setTitle(newTitle);
}
public void setTitle(int position, String newTitle){
(playList.get(position)).setTitle(newTitle);
}
public void setTitle(int position, String newTitle){
(playList.get(position)).setTitle(newTitle);
}*/
public void playOnly(String artist){
artist = artist.toUpperCase();
for(int i=0; i<playList.size(); i++){
if(playList.get(i).getArtist().toUpperCase().contains(artist)){
System.out.println(playList.get(i).toString());
}
}
}
public void playOnly(int year){
for(int i=0; i<playList.size(); i++){
if(playList.get(i).getYear() == year){
System.out.println(playList.get(i).toString());
}
}
}
}