This repository has been archived by the owner on Apr 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
MusicCollection.java
178 lines (162 loc) · 4.49 KB
/
MusicCollection.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import java.util.ArrayList;
import java.util.Iterator;
/**
* A class to hold details of audio files.
*
* @author David J. Barnes and Michael K�lling
* @version 2011.07.31
*/
public class MusicCollection
{
// An ArrayList for storing the file names of music files.
private ArrayList<Music> files;
// A player for the music files.
private MusicPlayer player;
/**
* Create a MusicCollection
*/
public MusicCollection()
{
files= new ArrayList<>();
player= new MusicPlayer();
}
/**
* Add a file to the collection.
* @param filename The file to be added.
*/
public void addFile(Music musicFile)
{
files.add(musicFile);
return;
}
/**
* Return the number of files in the collection.
* @return The number of files in the collection.
*/
public int getNumberOfFiles()
{
return files.size();
}
/**
* List a file from the collection.
* @param index The index of the file to be listed.
*/
public void listFile(int index)
{
if(validIndex(index)){
System.out.printf((index+1)+") ");
files.get(index).printinfo();
}
return;
}
/**
* Show a list of all the files in the collection.
*/
public void listAllFiles()
{
System.out.println("Number of songs in this collection: "+ getNumberOfFiles());
for(int i=0; i<getNumberOfFiles(); i++){
listFile(i);
}
}
/**
* Remove a file from the collection.
* @param index The index of the file to be removed.
*/
public void removeFile(int index)
{
if(validIndex(index)){
Iterator <Music> it= files.iterator();
int i=0;
while(it.hasNext()){
it.next();
if(i==index){
it.remove();
break;
}
i++;
}
}
return;
}
/**
* Start playing a file in the collection.
* Use stopPlaying() to stop it playing.
* @param index The index of the file to be played.
*/
public void startPlaying(int index)
{
player.startPlaying(files.get(index).getFileAddress());
return;
}
/**
* Stop the player.
*/
public void stopPlaying()
{
player.stop();
return;
}
/**
* Determine whether the given index is valid for the collection.
* Print an error message if it is not.
* @param index The index to be checked.
* @return true if the index is valid, false otherwise.
*/
private boolean validIndex(int index)
{
if(index>=0 && index<getNumberOfFiles()){
return true;
}
return false;
}
/**
* method for setting a song as favorite
* @param index of the song in the files array list
*/
public void setFavorite(int index){
files.get(index).setFav(true);
return;
}
/**
* @param index of the song in the files array list
*/
public void unfav(int index){
files.get(index).setFav(false);
return;
}
/**
* print the favorite songs of this collection
*/
public void printFavorite(){
int j=1;
for(int i=0; i<getNumberOfFiles(); i++){
if(files.get(i).getIsFav()){
System.out.println(j+") "+ files.get(i).getFileAddress());
j++;
}
}
if(j==1){
System.out.println("no favorite songs in the list");
}
return;
}
/**
* this method iterates in the files array list and searchs for a Music object with filename or singername same as the searched string
* @param searched is the string searched by the user
*/
public void printResultSearch(String searched){
int j=1;
for(int i=0; i<getNumberOfFiles(); i++){
if(files.get(i).getFileAddress().equals(searched)||files.get(i).getSingerName().equals(searched)){
System.out.println(j+") ");
files.get(i).printinfo();
j++;
}
}
if(j==1){
System.out.println("didn't find anything with name of:"+ searched);
}
return;
}
}