-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayList.java
executable file
·171 lines (138 loc) · 4.26 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
161
162
163
164
165
166
167
168
169
170
171
/* PlayList Extensions:
> We kept track of artists
> We made a favoriteArtist method which finds the artist(s) which appear the most on your playlist
*/
package apcs.playList2;
import java.util.ArrayList;
import java.util.Collections;
public class PlayList {
private final ArrayList<Song> songList;
PlayList() {
songList = new ArrayList<>();
}
public void add(String newSong) {
songList.add(new Song(newSong));
}
public void add(int index, String newSong) {
if (index > songList.size() || index < 0)
throw new RuntimeException("The requested index is out of bounds");
else
songList.add(new Song(newSong));
}
public void add(String newSong, String artist) {
songList.add(new Song(newSong, artist));
}
public void add(int index, String newSong, String artist) {
if (index > songList.size() || index < 0)
throw new RuntimeException("The requested index is out of bounds");
else
songList.add(new Song(newSong, artist));
}
public int size() {
return songList.size();
}
String getSong(int index) {
checkOverflow(index);
return songList.get(index).getSongName();
}
int getPlays(int index) {
checkOverflow(index);
return songList.get(index).getPlays();
}
String play(int index) {
checkOverflow(index);
songList.get(index).incrementPlays();
return songList.get(index).getSongName();
}
void remove(int index) {
checkOverflow(index);
songList.remove(index);
}
public void set(int index, String songName) {
checkOverflow(index);
songList.set(index, new Song(songName));
}
int indexOf(String songName) {
for (int i = 0; i < songList.size(); i++)
if (songName.equals(songList.get(i).getSongName())) {
return i;
}
return -1;
}
ArrayList<Song> getMostPlayed() {
if (songList.isEmpty())
throw new RuntimeException("The playlist is empty.");
int maxPlays = 0;
for (Song val : songList) {
if (val.getPlays() > maxPlays)
maxPlays = val.getPlays();
ArrayList<Song> mostPlayed = new ArrayList<>();
for (Song song : songList)
if (song.getPlays() == maxPlays) {
mostPlayed.add(song);
}
return mostPlayed;
}
return null;
}
ArrayList<String> favoriteArtists() {
if (songList.isEmpty())
throw new RuntimeException("The playlist is empty.");
ArrayList<String> artists = new ArrayList<>();
for (Song val : songList)
if (val.getArtist() != null) {
artists.add(val.getArtist());
}
int highestFrequency = 0;
ArrayList<String> favoriteArtists = new ArrayList<>();
for (String val : artists)
if (Collections.frequency(artists, val) >= highestFrequency) {
highestFrequency = Collections.frequency(artists, val);
}
for (String val : artists)
if (Collections.frequency(artists, val) == highestFrequency
&& Collections.frequency(favoriteArtists, val) < 1) {
favoriteArtists.add(val);
}
return favoriteArtists;
}
void rate(int index, int rating) {
checkOverflow(index);
if (rating < 1 || rating > 5)
throw new RuntimeException("Please rate between 1-5.");
songList.get(index).setRating(rating);
}
ArrayList<Song> getFavorite() {
if (songList.isEmpty())
throw new RuntimeException("The playlist is empty.");
int maxRating = 0;
for (Song val : songList) {
if (val.getRating() > maxRating)
maxRating = val.getRating();
ArrayList<Song> favorites = new ArrayList<>();
for (Song song : songList)
if (song.getRating() == maxRating) {
favorites.add(song);
}
return favorites;
}
return null;
}
private void checkOverflow(int index) {
if (index > songList.size() - 1 || index < 0)
throw new RuntimeException("The requested index is out of bounds");
}
@Override
public String toString() {
System.out.println(songList);
if (songList.isEmpty())
return "The playlist is empty";
else {
StringBuilder playListString = new StringBuilder();
for (int i = 0; i < songList.size(); i++)
playListString.append(i).append(": ").append(songList.get(i))
.append("; \n");
return playListString.toString();
}
}
}