-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalProject.java
More file actions
425 lines (413 loc) · 19.8 KB
/
FinalProject.java
File metadata and controls
425 lines (413 loc) · 19.8 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import java.io.*;
import java.util.*;
public class FinalProject
{
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException
{
System.out.println("Welcome to the playlist program. Please wait while the playlist is generated.");
SortedABList<Song> songList = new SortedABList<Song>(); //main list will hold all songs
SortedABList<Song> filteredList = new SortedABList<Song>(); //filtered list hold subset of songs as specified
//read file
final String fileName = "songlist.dat";
FileInputStream inStream = new FileInputStream(fileName);
ObjectInputStream objectInputFile = new ObjectInputStream(inStream);
ArrayList<Song> readArrayList = (ArrayList<Song>) objectInputFile.readObject();
//reassign to sortedABList
while (readArrayList.isEmpty() == false)
{
Song addSong = readArrayList.remove(0);
songList.add(addSong);
}
//while loop for user selection
boolean done = false;
Scanner keyboard = new Scanner(System.in);
int orderChoice = 1; //playlist order set to "song title-ascending" by default
boolean filterExists = false;
while (done == false)
{
//get user input
int userChoice = 0;
System.out.println("What would you like to do?\nEnter 1 to create and edit a filtered playlist."
+ "\nEnter 2 to edit songs in the list.\nEnter 3 to delete songs from the list.\nEnter 4 to view or reorder the full list"
+ "\nEnter 5 exit.");
while (!keyboard.hasNextInt()) //validate input
{
System.out.println("Please enter 1 to view or edit a filtered playlist, 2 to edit a song, or 3 to delete a song.");
}
userChoice = keyboard.nextInt();
keyboard.nextLine();
if (userChoice < 2 && userChoice > 5)
{
userChoice = 1;
}
if (userChoice == 1) //view or edit filtered playlist
{
int viewChoice = 0;
while (viewChoice < 4)
{
System.out.println("What would you like to do?\nEnter 1 to view filtered playlist."
+ "\nEnter 2 to filter playlist."
+ "\nEnter 3 to clear all filters\nEnter 4 to return to main menu");
while (!keyboard.hasNextInt()) //check for integer
{
System.out.println("What would you like to do?\nEnter 1 to view filtered playlist."
+ "\nEnter 2 to filter playlist."
+ "\nEnter 3 to clear all filters\nEnter 4 to return to main menu");
}
viewChoice = keyboard.nextInt();
while (viewChoice < 1 && viewChoice > 4)
{
System.out.println("What would you like to do?\nEnter 1 to view filtered playlist."
+ "\nEnter 2 to create a filtered playlist."
+ "\nEnter 3 to clear all filters\nEnter 4 to return to main menu");
viewChoice = keyboard.nextInt();
}
keyboard.nextLine();
if (viewChoice == 1)//view playlist
{
if (filteredList == null)
{
System.out.println("Filtered list has not been created.");
}
else if (filteredList.isEmpty())
{
System.out.println("Filtered list has not been created.");
} else
{
filteredList.printString();
System.out.println("");
}
} else if (viewChoice == 2) //filter playlist
{
int filterChoice = 0;
System.out.println("Enter 1 to filter by artist, or 2 to filter by album.");
while (!keyboard.hasNextInt())
{
System.out.println("Enter 1 to filter by artist, 2 to filter by album, or 3 to remove all filters.");
}
filterChoice = keyboard.nextInt();
keyboard.nextLine();
if (filterChoice == 1) //filter by artist
{
String artistChoice = "";
System.out.println("Enter name of artist.");
artistChoice = keyboard.nextLine();
if (filterExists == false)
//create a new list using correct comparator, then filter
{
int numberSongs = songList.size() - 1;
SortedABList<Song> tempList = new SortedABList<Song>(Song.artistComparator());
while (numberSongs >= 0)
{
Song tempSong = songList.get(numberSongs);
tempList.add(tempSong);
numberSongs--;
}
boolean allRetrieved = false;
while (allRetrieved != true)
{
Song tempSong = new Song("", artistChoice, "", 0);
int songIndex = tempList.indexOf(tempSong);
if (songIndex != -1)
{
Song tempSong2 = tempList.remove(songIndex);
filteredList.add(tempSong2);
} else
{
allRetrieved = true;
}
}
if (filteredList.isEmpty() == false)
{
filterExists = true;
} else
{
System.out.println("Artist not found.");
}
} else if (filterExists == true)
//create a new list using correct comparator, then swap to that new list, then filter
{
SortedABList<Song> tempList1 = new SortedABList<Song>(Song.artistComparator());
int numberSongs = filteredList.size() - 1;
while (numberSongs >= 0)
{
Song tempSong = filteredList.remove(numberSongs);
tempList1.add(tempSong);
numberSongs--;
}
boolean allRetrieved = false;
SortedABList<Song> tempList2 = new SortedABList<Song>(Song.artistComparator());
while (allRetrieved != true)
{
Song tempSong = new Song("", artistChoice, "", 0);
int songIndex = tempList1.indexOf(tempSong);
if (songIndex != -1)
{
Song tempSong2 = tempList1.remove(songIndex);
tempList2.add(tempSong2);
} else
{
allRetrieved = true;
}
filteredList = tempList2;
}
}
} else if (filterChoice == 2) //filter by album
{
String albumChoice = "";
System.out.println("Enter name of album.");
albumChoice = keyboard.nextLine();
if (filterExists == false)
{
int numberSongs = songList.size() - 1;
SortedABList<Song> tempList = new SortedABList<Song>(Song.albumComparator());
while (numberSongs >= 0)
{
Song tempSong = songList.get(numberSongs);
tempList.add(tempSong);
numberSongs--;
}
boolean allRetrieved = false;
while (allRetrieved != true)
{
Song tempSong = new Song("", "", albumChoice, 0);
int songIndex = tempList.indexOf(tempSong);
if (songIndex != -1)
{
Song tempSong2 = tempList.remove(songIndex);
filteredList.add(tempSong2);
} else
{
allRetrieved = true;
}
}
if (filteredList.isEmpty() != true)
{
filterExists = true;
} else
{
System.out.println("Album not found.");
}
} else if (filterExists == true)
{
SortedABList<Song> tempList1 = new SortedABList<Song>(Song.albumComparator());
int numberSongs = filteredList.size() - 1;
while (numberSongs >= 0)
{
Song tempSong = filteredList.remove(numberSongs);
tempList1.add(tempSong);
numberSongs--;
}
boolean allRetrieved = false;
SortedABList<Song> tempList2 = new SortedABList<Song>(Song.albumComparator());
while (allRetrieved != true)
{
Song tempSong = new Song("", "", albumChoice, 0);
int songIndex = tempList1.indexOf(tempSong);
if (songIndex != -1)
{
Song tempSong2 = tempList1.remove(songIndex);
tempList2.add(tempSong2);
} else
{
allRetrieved = true;
}
filteredList = tempList2;
}
}
}
} else if (viewChoice == 3)
{
filteredList = new SortedABList<Song>();
filterExists = false;
System.out.println("Filter removed.");
}
}
} else if (userChoice == 2) //edit song
{
System.out.println("Enter 1 to edit title,\nenter 2 to edit artist,\nenter 3 to edit album,"
+ "\nenter 4 to return to main menu.");
int editChoice = 0;
while (!keyboard.hasNextInt())
{
System.out.println("Enter 1 to edit title,\nenter 2 to edit artist,\nenter 3 to edit album,"
+ "\nenter 4 to return to main menu.");
}
editChoice = keyboard.nextInt();
keyboard.nextLine();
if (editChoice != 2 && editChoice != 3 && editChoice != 1)
{
editChoice = 4;
}
if (editChoice >= 1 && editChoice <= 3)
//create new song based on user specifications
{
System.out.println("Please enter information about the song to be edited.");
System.out.println("Enter the song title.");
String songChoice = keyboard.nextLine();
System.out.println("Enter the artist.");
String artistChoice = keyboard.nextLine();
System.out.println("Enter the album.");
String albumChoice = keyboard.nextLine();
Song addSong = new Song(songChoice, artistChoice, albumChoice, 0);
//find the song
int songNumber = songList.indexOf(addSong);
if (songNumber == -1)
{
System.out.println("Song not found. Returning to main menu.");
editChoice = 4;
} else
{
int songLength = songList.get(songNumber).getLength();
addSong.setLength(songLength);
}
if (editChoice == 1)
{
System.out.println("Please enter the new title.");
String editTitle = keyboard.nextLine();
songList.remove(songNumber);
addSong.setTitle(editTitle);
songList.add(addSong);
System.out.println("Song edited. Returning to main menu.");
} else if (editChoice == 2)
{
System.out.println("Please enter the new artist.");
String editArtist = keyboard.nextLine();
songList.remove(addSong);
addSong.setTitle(editArtist);
songList.add(addSong);
System.out.println("Song edited. Returning to main menu.");
} else if (editChoice == 3)
{
System.out.println("Please enter the new album.");
String editAlbum = keyboard.nextLine();
songList.remove(addSong);
addSong.setTitle(editAlbum);
songList.add(addSong);
System.out.println("Song edited. Returning to main menu.");
}
}
} else if (userChoice == 3) //delete song
{
System.out.println("Please enter information about the song to be deleted.");
System.out.println("Enter the song title.");
String songChoice = keyboard.nextLine();
System.out.println("Enter the artist.");
String artistChoice = keyboard.nextLine();
System.out.println("Enter the album.");
String albumChoice = keyboard.nextLine();
Song removeSong = new Song(songChoice, artistChoice, albumChoice, 0);
int songNumber = songList.indexOf(removeSong);
if (songNumber == -1)
{
System.out.println("Song not found. Returning to main menu.");
} else
{
System.out.println("Song deleted. Returning to main menu.");
songList.remove(songNumber);
}
} else if (userChoice == 4) //view or reorder playlist
{
int listView = 0;
while (listView < 3)
{
System.out.println("Enter 1 to print out the entire playlist,\nEnter 2 to reorder playlist, or"
+ "\nEnter 3 to return to main menu");
while (!keyboard.hasNextInt())
{
System.out.println("Enter 1 to print out the entire playlist,\nEnter 2 to reorder playlist, or"
+ "\nEnter 3 to return to main menu");
}
listView = keyboard.nextInt();
if (listView < 1 && listView > 2)
{
listView = 3;
}
if (listView == 1)
{
songList.printString();
System.out.println("");
} else if (listView == 2)
{
int reorderChoice = 0;
System.out.println("How would you like to order the playlist?\nEnter 1 to order by song title descending, \n2 to "
+ "order by song title ascending,\n3 to order by song length descending,\nor 4 to order by song length ascending");
while (!keyboard.hasNextInt())
{
System.out.println("Enter 1 to order by song title descending,\n2 to "
+ "order by song title ascending,\n3 to order by song length descending,\n or 4 to order by song length ascending");
}
reorderChoice = keyboard.nextInt();
keyboard.nextLine();
if (reorderChoice != 2 && reorderChoice != 3 && reorderChoice != 4)
{
reorderChoice = 1;
}
int numberSongs = songList.size() - 1;
if (reorderChoice == 1)
{
System.out.println("Reordering playlist by song title descending.");
orderChoice = 2;
SortedABList<Song> tempList = new SortedABList<Song>(Song.titleDescComparator());
while (numberSongs >= 0)
{
Song tempSong = songList.remove(numberSongs);
tempList.add(tempSong);
numberSongs--;
}
songList = tempList;
} else if (reorderChoice == 2)
{
System.out.println("Reordering playlist by song title ascending.");
orderChoice = 1;
SortedABList<Song> tempList = new SortedABList<Song>();
while (numberSongs >= 0)
{
Song tempSong = songList.remove(numberSongs);
tempList.add(tempSong);
numberSongs--;
}
songList = tempList;
} else if (reorderChoice == 3)
{
System.out.println("Reordering playlist by song length descending.");
orderChoice = 3;
SortedABList<Song> tempList = new SortedABList<Song>(Song.lengthDescComparator());
while (numberSongs >= 0)
{
Song tempSong = songList.remove(numberSongs);
tempList.add(tempSong);
numberSongs--;
}
songList = tempList;
} else //reorderChoice4
{
System.out.println("Reordering playlist by song length ascending.");
orderChoice = 4;
SortedABList<Song> tempList = new SortedABList<Song>(Song.lengthAscComparator());
while (numberSongs >= 0)
{
Song tempSong = songList.remove(numberSongs);
tempList.add(tempSong);
numberSongs--;
}
songList = tempList;
}
}
}
} else
{
System.out.println("Saving updated playlist.");
while (songList.isEmpty() == false)
{
Song newSong = songList.remove(0);
readArrayList.add(newSong);
}
ObjectOutputStream objectOutputFile = new ObjectOutputStream(new FileOutputStream(fileName));
objectOutputFile.writeObject(readArrayList);
System.out.println("Program exited. Happy Holidays.");
done = true;
}
}
}
}