-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJukebox.java
252 lines (205 loc) · 5.42 KB
/
Jukebox.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package jukebox;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.LinkedList;
import java.util.TreeSet;
public class Jukebox {
private LinkedList <Song> playList = new LinkedList <Song> ();
private final String FILE_NAME = "playList.ser";
File myFile = new File("MyFile.txt");
FileInputStream fis;
FileOutputStream fos;
PrintStream ps;
BufferedInputStream bis;
BufferedReader br;
public Jukebox(){}
private void serialize() {
try{
//declare output streams
FileOutputStream out = new FileOutputStream(this.FILE_NAME);
ObjectOutputStream objOut = new ObjectOutputStream(out);
//save data
objOut.writeObject(this.playList);
objOut.close();
} catch(IOException e){
e.printStackTrace();
}
}
public void writeOutPlaylist(){
try{
fos = new FileOutputStream(this.myFile);
ps = new PrintStream(fos);
for(Song tempSong : this.playList){
ps.println(tempSong.getTitle());
}
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
public void readInPlaylist(){
try{
fis = new FileInputStream(this.myFile);
bis = new BufferedInputStream(fis);
br = new BufferedReader(new InputStreamReader(bis));
while(br.ready()) {
System.out.println(br.readLine());
}
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
private void deserialize() {
try{
//declare input streams
FileInputStream in = new FileInputStream(this.FILE_NAME);
ObjectInputStream objIn = new ObjectInputStream(in);
//assign playlist to loaded playlist
this.playList = (LinkedList) objIn.readObject();
} catch(ClassNotFoundException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
public void savePlaylist(){
serialize();
}
public void loadPlaylist(){
deserialize();
}
public Jukebox(String triggerSample){
Song s1 = new Song("Hello");
Song s2 = new Song("Hungry Eyes");
Song s3 = new Song("Take On Me");
Song s4 = new Song("I Feel like a Woman");
Song s5 = new Song("California Love");
Song s6 = new Song("Big Papa");
Song s7 = new Song("A Change is Gonna Come");
LinkedList<Song> songs = new LinkedList<Song>();
songs.add(s1);
songs.add(s2);
songs.add(s3);
songs.add(s4);
songs.add(s5);
songs.add(s6);
songs.add(s7);
this.playList = songs;
}
public Jukebox(LinkedList<Song> input){
this.playList = input;
}
public void addSong(Song s){
this.playList.add(s);
}
public void play(){
this.playList.removeFirst();
}
public String printPlayList(){
String output = "";
for ( Song tempSong : playList ){
output += tempSong.getTitle();
output += "\n";
}
return output;
}
//more complex methods
public void sort(){
TreeSet<Song> ordered = new TreeSet<Song>();
for (Song tempSong : this.playList){
ordered.add(tempSong);
}
this.playList.clear();
for (Song tempSong : ordered){
this.playList.add(tempSong);
}
}
public void reverseSort(){
TreeSet<Song> ordered = new TreeSet<Song>();
for (Song tempSong : this.playList){
ordered.add(tempSong);
}
this.playList.clear();
for (Song tempSong : ordered){
this.playList.addFirst(tempSong);;
}
}
public int numberPopular(){
int total = 0;
for (Song tempSong : this.playList){
if(tempSong.checkIfContains()){
total++;
}
}
return total;
}
public int numberUnusual(){
int total = 0;
for (Song tempSong : this.playList){
if(tempSong.checkIfContains()){
//do nothing
} else {
total++;
}
}
return total;
}
public void removeSoppySongs(){
LinkedList<Song> tempList = new LinkedList<Song>();
String triggerWord = "love";
for(Song tempSong : this.playList){
try{
if(tempSong.getTitle().toLowerCase().contains(triggerWord)){
throw new noSoppyLoveSong();
}
}catch(noSoppyLoveSong e){
tempList.add(tempSong);
}
}
this.playList.removeAll(tempList);
}
public LinkedList<Song> getPlaylist(){
return this.playList;
}
public boolean playlistContains(Song input){
boolean contains = false;
for(Song tempsong : this.playList){
if(input.getTitle().matches(tempsong.getTitle())){
contains = true;
}
}
return contains;
}
public boolean playlistContains(String input){
boolean contains = false;
Song inputSong = new Song(input);
for(Song tempsong : this.playList){
if(inputSong.getTitle().toLowerCase().matches(tempsong.getTitle().toLowerCase())){
contains = true;
}
}
return contains;
}
public void removeSong(String input){
LinkedList<Song> tempList = new LinkedList<Song>();
for(Song tempSong : this.playList){
if(tempSong.getTitle().toLowerCase().contentEquals(input.toLowerCase())){
tempList.add(tempSong);
}
}
this.playList.removeAll(tempList);
}
}