forked from erasmuss22/Music-Player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrowser.java
More file actions
261 lines (241 loc) · 6.86 KB
/
Browser.java
File metadata and controls
261 lines (241 loc) · 6.86 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
///////////////////////////////////////////////////////////////////////////////
// ALL STUDENTS COMPLETE THESE SECTIONS
// Title: Music Library
// Files: Browser.java, Library.java, LibrayInterface.java,
// SimpleSet.java, DoubleListnode.java, LinkedSet.java,
// LinkedSetIterator.java, Song.java
// Semester: Spring 2011
//
// Author: Erin Rasmussen ejrasmussen2@wisc.edu
// CS Login: rasmusse
// Lecturer's Name: Beck Hasti
// Lab Section: Lecture 2
//
//
// STUDENTS WHO GET HELP FROM ANYONE OTHER THAN THEIR PARTNER
// Credits: TA Jason Power
//////////////////////////// 80 columns wide //////////////////////////////////
import java.util.*;
import java.io.File;
import java.io.IOException;
/**
* Main song browser application.
*/
public class Browser {
/**
* Main song browser application. Build a library of available songs, then
* read a series of song browser selections and show the matching songs for
* each.
*
* @param args
* command-line arguments
* @throws IOException
*/
public static void main(final String[] args) throws IOException {
if (args.length != 2){
System.out.println("Usage: java Browser <MusicLibraryFileName>" +
" <SelectionsFileName>");
System.exit(0);
}
//The following are for SimpleSets of genre, album, and artist names
SimpleSet<String> ge;
SimpleSet<String> alb;
SimpleSet<String> art;
//The following are for SimpleSets of genre, album, and artist songs
SimpleSet<Song> genre = new LinkedSet<Song>();
SimpleSet<Song> album= new LinkedSet<Song>();
SimpleSet<Song> artist = new LinkedSet<Song>();
//The following are for computing unions and intersections although the
//names are intended for intersection, they are reused for unions
SimpleSet<Song> intermediate;
SimpleSet<Song> intermediateIntersection;
SimpleSet<Song> intersect;
Iterator<String> iterString;
File filename = new File(args[0]);
Library library = new Library(filename);
File file2 = new File(args[1]);
Scanner in = new Scanner(file2);
String g;
int count = 1;;
int position;
while (in.hasNextLine()){
if (count == 1){
ge = new LinkedSet<String>();
genre = new LinkedSet<Song>();
intersect = new LinkedSet<Song>();
intermediate = new LinkedSet<Song>();
intermediateIntersection = new LinkedSet<Song>();
g = in.nextLine();
position = 0;
boolean found = false;
if (g.length() != 0){
for (int i = 0; i < g.length(); i++){
if (g.charAt(i) == ';'){
found = true;
}
}
if (found){
while(g.charAt(position) != ';'){
position++;
}
ge.add((g.substring(0,position)));
int j = position + 1;
for (int i = position + 1; i < g.length(); i++){
if (g.charAt(i) == ';'){
ge.add(g.substring(j, i));
j = i + 1;
}
if (i == g.length()-1){
ge.add(g.substring(j));
}
}
}
else {
ge.add(g);
}
}
else{
genre = library.allSongs();
}
count = 2;
iterString = ge.iterator();
if (iterString.hasNext()){
intersect = library.songsHavingGenre(iterString.next());
genre = intersect;
iterString = ge.iterator();
while (iterString.hasNext()){
g = iterString.next();
intermediate = genre;
genre = library.songsHavingGenre(g);
intermediateIntersection = genre.union(intermediate);
intersect = intersect.union(intermediateIntersection);
}
genre = intersect;
}
}
else if(count == 2){
artist = new LinkedSet<Song>();
intersect = new LinkedSet<Song>();
intermediate = new LinkedSet<Song>();
intermediateIntersection = new LinkedSet<Song>();
art = new LinkedSet<String>();
g = in.nextLine();
position = 0;
boolean done = false;
if (g.length() != 0){
for (int i = 0; i < g.length(); i++){
if (g.charAt(i) == ';'){
done = true;
}
}
if (done){
while(g.charAt(position) != ';'){
position++;
}
art.add((g.substring(0,position)));
int j = position + 1;
for (int i = position + 1; i < g.length(); i++){
if (g.charAt(i) == ';'){
art.add(g.substring(j, i));
j = i + 1;
}
if (i == g.length()-1){
art.add(g.substring(j));
}
}
}
else {
art.add(g);
}
}
else{
artist = library.allSongs();
}
count = 3;
iterString = art.iterator();
if (iterString.hasNext()){
intersect = library.songsHavingArtist(iterString.next());
iterString = art.iterator();
artist = intersect;
while (iterString.hasNext()){
g = iterString.next();
intermediate = artist;
artist = library.songsHavingArtist(g);
intermediateIntersection = artist.union(intermediate);
intersect = intersect.union(intermediateIntersection);
}
artist = intersect;
}
}
else if(count == 3){
album = new LinkedSet<Song>();
intersect = new LinkedSet<Song>();
intermediate = new LinkedSet<Song>();
intermediateIntersection = new LinkedSet<Song>();
alb = new LinkedSet<String>();
g = in.nextLine();
position = 0;
count = 4;
boolean finished = false;
if (g.length() != 0){
for (int i = 0; i < g.length(); i++){
if (g.charAt(i) == ';'){
finished = true;
}
}
if (finished){
while(g.charAt(position) != ';'){
position++;
}
alb.add((g.substring(0,position)));
int j = position + 1;
for (int i = position + 1; i < g.length(); i++){
if (g.charAt(i) == ';'){
alb.add(g.substring(j, i));
j = i + 1;
}
if (i == g.length()-1){
alb.add(g.substring(j));
}
}
}
}
else{
album = library.allSongs();
}
iterString = alb.iterator();
if (iterString.hasNext()){
intersect = library.songsHavingAlbum(iterString.next());
iterString = alb.iterator();
album = intersect;
while (iterString.hasNext()){
g = iterString.next();
intermediate = album;
album = library.songsHavingAlbum(g);
intermediateIntersection = album.union(intermediate);
intersect = intersect.union(intermediateIntersection);
}
album = intersect;
}
}
else {
intersect = artist.intersection(album.intersection(genre));
printInOrder(intersect);
System.out.println();
count = 1;
}
}
intersect = artist.intersection(album.intersection(genre));
printInOrder(intersect);
System.out.println();
}
private static void printInOrder(SimpleSet<Song> songs) {
// Do not remove or modify this method!
ArrayList<String> list = new ArrayList<String>();
for (Song song : songs)
list.add(song.toString());
Collections.sort(list);
for (String item : list)
System.out.println(item);
}
}