forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscJockey.java
More file actions
124 lines (78 loc) · 2.98 KB
/
DiscJockey.java
File metadata and controls
124 lines (78 loc) · 2.98 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
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
public class DiscJockey {
SongsOfThe70s songs70s;
SongsOfThe80s songs80s;
SongsOfThe90s songs90s;
// NEW Passing in song iterators
SongIterator iter70sSongs;
SongIterator iter80sSongs;
SongIterator iter90sSongs;
/* OLD WAY
public DiscJockey(SongsOfThe70s newSongs70s, SongsOfThe80s newSongs80s, SongsOfThe90s newSongs90s) {
songs70s = newSongs70s;
songs80s = newSongs80s;
songs90s = newSongs90s;
}
*/
// NEW WAY Initialize the iterators
public DiscJockey(SongIterator newSongs70s, SongIterator newSongs80s, SongIterator newSongs90s) {
iter70sSongs = newSongs70s;
iter80sSongs = newSongs80s;
iter90sSongs = newSongs90s;
}
public void showTheSongs(){
// Because the SongInfo Objects are stored in different
// collections everything must be handled on an individual
// basis. This is BAD!
ArrayList aL70sSongs = songs70s.getBestSongs();
System.out.println("Songs of the 70s\n");
for(int i=0; i < aL70sSongs.size(); i++){
SongInfo bestSongs = (SongInfo) aL70sSongs.get(i);
System.out.println(bestSongs.getSongName());
System.out.println(bestSongs.getBandName());
System.out.println(bestSongs.getYearReleased() + "\n");
}
SongInfo[] array80sSongs = songs80s.getBestSongs();
System.out.println("Songs of the 80s\n");
for(int j=0; j < array80sSongs.length; j++){
SongInfo bestSongs = array80sSongs[j];
System.out.println(bestSongs.getSongName());
System.out.println(bestSongs.getBandName());
System.out.println(bestSongs.getYearReleased() + "\n");
}
Hashtable<Integer, SongInfo> ht90sSongs = songs90s.getBestSongs();
System.out.println("Songs of the 90s\n");
for (Enumeration<Integer> e = ht90sSongs.keys(); e.hasMoreElements();)
{
SongInfo bestSongs = ht90sSongs.get(e.nextElement());
System.out.println(bestSongs.getSongName());
System.out.println(bestSongs.getBandName());
System.out.println(bestSongs.getYearReleased() + "\n");
}
}
// Now that I can treat everything as an Iterator it cleans up
// the code while allowing me to treat all collections as 1
public void showTheSongs2(){
System.out.println("NEW WAY WITH ITERATOR\n");
Iterator Songs70s = iter70sSongs.createIterator();
Iterator Songs80s = iter80sSongs.createIterator();
Iterator Songs90s = iter90sSongs.createIterator();
System.out.println("Songs of the 70s\n");
printTheSongs(Songs70s);
System.out.println("Songs of the 80s\n");
printTheSongs(Songs80s);
System.out.println("Songs of the 90s\n");
printTheSongs(Songs90s);
}
public void printTheSongs(Iterator iterator){
while(iterator.hasNext()){
SongInfo songInfo = (SongInfo) iterator.next();
System.out.println(songInfo.getSongName());
System.out.println(songInfo.getBandName());
System.out.println(songInfo.getYearReleased() + "\n");
}
}
}