forked from bethrobson/Head-First-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJukebox6.java
More file actions
executable file
·98 lines (79 loc) · 2.17 KB
/
Copy pathJukebox6.java
File metadata and controls
executable file
·98 lines (79 loc) · 2.17 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
package chap16;
import java.util.*;
import java.io.*;
public class Jukebox6
{
ArrayList<SongBad> songList = new ArrayList<SongBad>();
public static void main(String[] args) {
new Jukebox6().go();
}
public void go() {
getSongs();
System.out.println(songList);
Collections.sort(songList);
System.out.println(songList);
HashSet<SongBad> songSet = new HashSet<SongBad>();
songSet.addAll(songList);
System.out.println(songSet);
}
void getSongs() {
try {
File file = new File("SongListMore.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
addSong(line);
}
} catch (Exception ex) { ex.printStackTrace(); }
}
void addSong(String lineToParse) {
String[]tokens = lineToParse.split("/");
SongBad nextSong = new SongBad(tokens[0], tokens[1], tokens[2], tokens[3]);
songList.add(nextSong);
}
}
class SongBad implements Comparable <SongBad>
{
String title;
String artist;
String rating;
String bpm;
public SongBad(String t, String a, String r, String b) {
title = t;
artist = a;
rating = r;
bpm = b;
}
public boolean equals(Object aSong) {
SongBad s = (SongBad) aSong;
return getTitle().equals(s.getTitle());
}
//leaving this out makes this a bad form of song. Uncomment this to get rid of the duplicates
/*public int hashCode() {
return title.hashCode();
}
*/
public int compareTo(SongBad s)
{
return title.compareTo(s.getTitle());
}
public String getArtist()
{
return artist;
}
public String getBpm()
{
return bpm;
}
public String getRating()
{
return rating;
}
public String getTitle()
{
return title;
}
public String toString() {
return title;
}
}