-
Notifications
You must be signed in to change notification settings - Fork 0
/
Album.java
60 lines (53 loc) · 2.24 KB
/
Album.java
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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Album {
private String name;
private String artist;
private ArrayList<Song> songs;
public Album(String name, String artist) {
this.name = name;
this.artist = artist;
this.songs = new ArrayList<Song>();
}
public boolean addSong(String title, double duration) {
if (findSong(title) == null) {
this.songs.add(new Song(title, duration));
return true;
}
return false;
}
private Song findSong(String title) {
for (Song checkedSong : this.songs) { // This is 'for each' command in Java. This is an alternative way to go
// through list of entries. What it does is instead creating varianle i
// and giving condition to it and i++, it actually goes through and
// creates a variable called checkedSong for every entry that's actually
// in our array list. It is equivalent to normal iterations of i nad do
// .get(i) as we do always.
// So, for every entry in 'this.songs' list, Java will create variable
// checkSong of type Song.
if (checkedSong.getTitle().equals(title)) {
return checkedSong;
}
}
return null;
}
public boolean addToPlaylist(int trackNumber, List<Song> playList) {
int index = trackNumber - 1;
if ((index >= 0) && (index < this.songs.size())) {
playList.add(this.songs.get(index));
return true;
}
System.out.println("This album does not have a track " + trackNumber);
return false;
}
public boolean addToPlaylist(String title, List<Song> playList) {
Song checkedSong = findSong(title);
if (checkedSong != null) {
playList.add(checkedSong);
return true;
}
System.out.println("The song " + title + " is not in this album");
return false;
}
}