你好,我是新来的。
我目前正在使用 Java 制作播放列表软件。我想Song
来自一个文件并将其存储在播放列表中。我应该在我的歌曲类中放一些东西来表明它应该来自一个文件吗?我缺少什么?请回答我。
public class Song {
private Float playTime;
private String songName;
private String artist;
//getters
public Float getPlayTime() { return playTime; }
public String getSongName() { return songName; }
public String getArtist() { return artist; }
}
下面是我的播放列表课程。(不是我的,我在网上某处得到的。)
public class Playlist {
//Instance variable
private List<Song> songs; //arraylist of songs
private String playlistName; //Playlist name
public Playlist(String name) {
songs = new ArrayList<Song>(); //initalize Arraylist to hold Song type
playlistName = name;
}
//Methods:
//Returns the playlist name
public String playlistName() {
return playlistName;
}
//adds Song s to Playlist
public boolean addSong(Song s) {
return songs.add(s);
}
//return list. Note return is List<Song>.
public List<Song> getList() {
System.out.print(playlistName + ":");
for (int i = 0; i < songs.size(); i++)
System.out.print(songs.get(i));
return songs;
}
public boolean removeSong(Song s) {
return songs.remove(s);
}
public double playlistTime() {
double sum = 0;
for (int j = 0; j < songs.size(); j++)
sum = sum + songs.get(j).getPlayTime();
return sum;
}
public boolean isSongInPlaylist(String name) {
boolean x = false;
for (int i = 0; i < songs.size(); i++)
if (songs.get(i).getSongName().equalsIgnoreCase(name))
x = true;
return x;
}
public void songsByArtist(String name) {
for (int i = 0; i < songs.size(); i++)
if( songs.get(i).getArtist().equals(name))
System.out.println(songs.get(i).getArtist());
}
public boolean addSongsFrom(Playlist p) { // enter code here
boolean x = true;
for (int i = 0; i < p.getList().size(); i++)
if (p.getList().get(i).getSongName().equals(songs.get(i).getSongName()))
x = x;
else
this.addSong(p.getList().get(i));
return x;
}
public int totalSongs() {
return songs.size();
}
public String toString() {
return playlistName;
}
}