-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSongLibrary.java
More file actions
88 lines (76 loc) · 2.71 KB
/
SongLibrary.java
File metadata and controls
88 lines (76 loc) · 2.71 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
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class SongLibrary {
/** contains the songs inside the playlist */
private List<Song> myLibrary;
/** "head" of SongLibrary playlist**/
private int whereInList;
public SongLibrary() {
myLibrary = new ArrayList<>();
whereInList = 0;
}
/** checks and then if it is possible to get a next, it returns it (or the absence of it) through an Optional<Song> object through which an object Song can be accessed through the `.get()` method implemented in the Optional library
*/
public Optional<Song> next(){
if(!(myLibrary.isEmpty())&& whereInList < myLibrary.size()-1){
whereInList++;
return Optional.of(myLibrary.get(whereInList));
}
else if (myLibrary.isEmpty()){
System.out.println("Can't show next. No songs in playlist");
return Optional.empty();
}
else{
System.out.println("Last song in list. Returning to first");
whereInList = 0;
return Optional.of(myLibrary.get(whereInList));
}
}
public Optional<Song> previous(){
if(!(myLibrary.isEmpty()) && whereInList>0){
whereInList--;
return Optional.of(myLibrary.get(whereInList));
}else if(myLibrary.isEmpty()){
System.out.println("Can't show previous. No songs in list.");
return Optional.empty();
}else{
whereInList = myLibrary.size()-1;
System.out.println("First song in list. Returning to last song.");
return Optional.of(myLibrary.get(whereInList));
}
}
public void addSong(Song s){
myLibrary.add(s);
System.out.println("Song "+ s.getTitle() + " by " + s.getAuthor() + " added to library.");
}
public void removeCurrentSong(){
if(!(myLibrary.isEmpty())){
myLibrary.remove(whereInList);
}else
System.out.println("No songs to remove.");
}
public Optional<Song> getCurrentSong(){
if(!(myLibrary.isEmpty())){
return Optional.of(myLibrary.get(whereInList));
} else {
System.out.println("Can't get current song. No songs in list");
return Optional.empty();
}
}
public void modifyPlaylist(List<Song> modifiedList){
this.myLibrary = modifiedList;
}
public void modifyWhereInList(int modifiedHead){
this.whereInList = modifiedHead;
}
public boolean isEmpty(){
return myLibrary.isEmpty();
}
public List<Song> getMyLibrary() {
return myLibrary;
}
public int getWhereInList() {
return whereInList;
}
}