我似乎在理解如何创建一个包含歌曲类型列表的地图时遇到了一些困难。我现在基本上只是对歌曲和流派进行硬编码,因为这将成为后来允许用户请求流派并显示歌曲的节目。
到目前为止我的代码:
public class Song {
private String artist;
private String title;
private String fileExtension;
private String fileName;
private int duration; //defaults to seconds.
public String getArtist(){
return artist;
}
public String getTitle(){
return title;
}
public String getFileExtension(){
return fileExtension;
}
public String getFileName(){
return fileName;
}
public int getDuration(){
return duration;
}
}
///////////////////////这是我需要帮助搞清楚如何放入包含歌曲的流派的地方。
public class Genre {
//create a selection of songs organized by a genre by using a map.
Map<String,List<Song>> genres = new HashMap();
}
非常感谢!
答案 0 :(得分:0)
这是一个可能的类结构。如果您没有每首歌曲的唯一标识符,则歌曲地图可能只是一首歌曲列表。
public class Song {
// what you have here is fine, but what about some sort of id?
// assuming the song title is the unique id for now
}
public class Genre {
private String name;
private Map<String, Song> songs; // where string is song title
public Genre(String n, Map<String, Song> s) {
name = n;
songs = s;
}
}
public class GenreBank {
private Map<String, Genre> genres; // where string is genre name
public GenreBank() {
// If you did not understand how to physically build the maps, see here
Map<String, Song> songList1 = new HashMap<String, Song>();
songList1.put("title1", new Song(...));
songList1.put("title2", new Song(...));
Map<String, Song> songList2 = new HashMap<String, Song>();
songList2.put("title3", new Song(...));
songList2.put("title4", new Song(...));
genres = new HashMap<String, Genre>();
genres.put("genre1", new Genre("genre1", songList1));
genres.put("genre2", new Genre("genre2", songList2));
}
public Song getGenre(String genre) {
return genres.get(genre);
}
public Song getSong(String title) {
for (Genre g : genres.values()) {
Song s = g.getSongs().get(title);
if (s != null) { return s; }
}
return null;
}
public Song getSong(String genre, String title) {
Genre g = genres.get(genre);
if (g != null) {
return g.getSongs().get(title);
}
return null;
}
}
答案 1 :(得分:0)
这就是我组织它的方式。使用适当的名称字段和Genre
List
创建一个Songs
类。
然后添加静态修饰符以获取所有当前List
Genres
public class Genre {
private String name;
private List<Song> songs;
public Genre(String name) {
this.name = name;
songs = new ArrayList<>();
}
public String getName() {
return name;
}
public List<Song> getSongs() {
return songs;
}
private static List<Genre> genres = new ArrayList<>();
public static List<Song> getSongs(String genreName) {
for (Genre genre : genres) {
if (genre.getName().equals(genreName)) {
return genre.getSongs();
}
}
return new ArrayList(); //Return empty list if no songs found for a given name
}
}
从这里,您可以创建其他静态方法,将歌曲添加到Genre
,创建并注册新的Genre
等等。
对于grins,这是一个按名称将Song
添加到Genre
的实现,如果不存在,则懒惰地创建Genre
:
public static void addSong(Song song, String genreName) {
for (Genre genre : genres) {
if (genre.getName().equals(genreName)) {
genre.getSongs().add(song);
return;
}
}
//If no genre was found (return would have been called), create a new one
Genre genre = new Genre(genreName);
genre.getSongs().add(song);
genres.add(genre);
}