forked from erasmuss22/Music-Player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
202 lines (188 loc) · 4.8 KB
/
Library.java
File metadata and controls
202 lines (188 loc) · 4.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
///////////////////////////////////////////////////////////////////////////////
// ALL STUDENTS COMPLETE THESE SECTIONS
// Main Class File: Browser.java
// File: Library.java
// Semester: Spring 2011
//
// Author: Erin Rasmussen ejrasmussen2@wisc.edu
// CS Login: rasmusse
// Lecturer's Name: Beck Hasti
// Lab Section: Lecture 2
//
//
//////////////////////////// 80 columns wide //////////////////////////////////
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.io.IOException;
/**
* Concrete implementation of a music library, consisting of a set of songs.
* <p>
* <strong>Modify this class to implement the required {@link LibraryInterface}
* methods along with any private fields or methods you feel are
* necessary.</strong>
*/
public class Library implements LibraryInterface {
private LinkedSet<Song> songList = new LinkedSet<Song>();
private LinkedSet<String> allAlbums = new LinkedSet<String>();
private LinkedSet<String> allArtists = new LinkedSet<String>();
private LinkedSet<String> allGenres = new LinkedSet<String>();
private Iterator<Song> iter;
/**
* Create a new music library, reading song metadata from the given
* filename.
*
* @param filename
* name of the file containing song metadata
* @throws IOException
* if the named file cannot be found or read
*/
public Library(final File filename) throws IOException {
try {
Scanner in = new Scanner(filename);
String songTitle;
String albumTitle;
String artist;
String genre;
int position;
while (in.hasNextLine()){
position = 0;
String a = in.nextLine();
while(a.charAt(position) != ';'){
position++;
}
songTitle = a.substring(0,position);
position++;
int j = position;
while(a.charAt(position) != ';'){
position++;
}
albumTitle = a.substring(j, position);
position++;
j = position;
while(a.charAt(position) != ';'){
position++;
}
artist = a.substring(j, position);
position++;
genre = a.substring(position);
songList.add(new Song(songTitle, albumTitle, artist, genre));
}
} catch(FileNotFoundException e){
System.out.println("Problem with file: <" + filename + ">");
System.exit(0);
}
allArtists = (LinkedSet<String>) allArtists();
allAlbums = (LinkedSet<String>) allAlbums();
allGenres = (LinkedSet<String>) allGenres();
}
@Override
public SimpleSet<Song> songsHavingGenre(String genre) {
LinkedSet<Song> sameGenre = new LinkedSet<Song>();
iter = songList.iterator();
String g;
Song s;
while (iter.hasNext()){
s = iter.next();
g = s.getGenre();
if (g.equals(genre)){
sameGenre.add(s);
}
}
return sameGenre;
}
@Override
public SimpleSet<Song> songsHavingArtist(String artist) {
LinkedSet<Song> sameArtist = new LinkedSet<Song>();
iter = songList.iterator();
String g;
Song s;
while (iter.hasNext()){
s = iter.next();
g = s.getArtist();
if (g.equals(artist)){
sameArtist.add(s);
}
}
return sameArtist;
}
@Override
public SimpleSet<Song> songsHavingAlbum(String album) {
LinkedSet<Song> sameAlbum = new LinkedSet<Song>();
iter = songList.iterator();
String g;
Song s;
while (iter.hasNext()){
s = iter.next();
g = s.getAlbum();
if (g.equals(album)){
sameAlbum.add(s);
}
}
return sameAlbum;
}
@Override
public SimpleSet<Song> allSongs() {
return songList;
}
@Override
public SimpleSet<String> allGenres() {
LinkedSet<String> allGenres = new LinkedSet<String>();
iter = songList.iterator();
String g;
String t;
while (iter.hasNext()){
g = iter.next().getGenre();
Iterator<String> iter2 = allGenres.iterator();
boolean found = false;
while(iter2.hasNext() && !found){
t = iter2.next();
found = g.equals(t);
}
if(!found){
allGenres.add(g);
}
}
return allGenres;
}
@Override
public SimpleSet<String> allArtists() {
LinkedSet<String> allArtists = new LinkedSet<String>();
iter = songList.iterator();
String g;
String t;
while (iter.hasNext()){
g = iter.next().getArtist();
Iterator<String> iter2 = allArtists.iterator();
boolean found = false;
while(iter2.hasNext() && !found){
t = iter2.next();
found = g.equals(t);
}
if(!found){
allArtists.add(g);
}
}
return allArtists;
}
@Override
public SimpleSet<String> allAlbums() {
LinkedSet<String> allAlbums = new LinkedSet<String>();
iter = songList.iterator();
String g;
String t;
while (iter.hasNext()){
g = iter.next().getAlbum();
Iterator<String> iter2 = allAlbums.iterator();
boolean found = false;
while(iter2.hasNext() && !found){
t = iter2.next();
found = g.equals(t);
}
if(!found){
allAlbums.add(g);
}
}
return allAlbums;
}
}