forked from asafflesch/CsHomework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKiPod.java
More file actions
155 lines (136 loc) · 3.19 KB
/
Copy pathKiPod.java
File metadata and controls
155 lines (136 loc) · 3.19 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
import java.util.Iterator;
/**
* The Class representing the KiPod media player.
*/
public class KiPod{
private AVLTree playTree;
private AVLTree songTree;
/**
* Instantiates a new kiPod.
*/
public KiPod(){
}
/**
* Adds the song to the player.
*
* @param song the song
*/
public void addSong(Song song){
songTree.add(song.getTitle(), song);
}
/**
* Adds the playlist to the player.
*
* @param playlist the playlist
*/
public void addPlaylist(Playlist playlist){
playTree.add(playlist.getName(), playlist);
}
/**
* Adds the song to playlist. assumes that {@link #addSong(Song)} was called for the song.
*
* @param song the song
* @param playlist the playlist
*/
public void addSongToPlaylist(Song song, Playlist playlist){
song.add(playlist);
playlist.add(song);
}
/**
* Removes the playlist.
*
* @param playlist the playlist
*/
public void removePlaylist(Playlist playlist){
/* Find all the songs it features in,
remove from them*/
LinkedList songlist = playlist.getKthtillHthSongs
(1, playlist.size());
Iterator it = songlist.iterator();
while (it.hasNext()){
removeSongFromPlaylist((Song)(it.next()), playlist);
}
/* Remove from data structure*/
playTree.remove(playlist.getName());
}
/**
* Removes the song from playlist.
*
* @param song the song
* @param playlist the playlist
*/
public void removeSongFromPlaylist(Song song, Playlist playlist){
song.remove(playlist);
playlist.remove(song);
}
/**
* Removes the song.
*
* @param song the song
*/
public void removeSong(Song song){
/* Find all the playlists it features in,
remove from them*/
Iterator it = song.getPlaylists().iterator();
while (it.hasNext()){
removeSongFromPlaylist(song, (Playlist)(it.next()));
}
/* Remove from data structure*/
songTree.remove(song.getTitle());
}
/**
* Find kth song in playlist.
*
* @param playlist the playlist
* @param k the k
* @return the song
*/
public Song findKthSongInPlaylist(Playlist playlist,int k){
return playlist.findKthSong(k);
}
/**
* Gets the kth till hth song in playlist.
*
* @param playlist the playlist
* @param k the k
* @param h the h
* @return the kth till hth song in playlist
*/
public LinkedList getKthTillHthSongInPlaylist(Playlist playlist,int k,int h){
return playlist.getKthtillHthSongs(k, h);
}
/**
* Find song.
*
* @param name the name
* @return the song
*/
public Song findSong(String name){
return (Song)(songTree.find(name));
}
/**
* Find a playlist by a name.
*
* @param name the name
* @return the playlist
*/
public Playlist findPlaylist(String name){
return (Playlist)(playTree.find(name));
}
/**
* Num of songs.
*
* @return the int
*/
public int numOfSongs(){
return songTree.size();
}
/**
* Num of playlists.
*
* @return the int
*/
public int numOfPlaylists(){
return playTree.size();
}
}