添加在主播放列表的歌曲也会同时添加到普通播放列表?
package com.demo19; //歌曲类 public class Song { private String id;//歌曲ID private String name;//歌曲名称 private String singer;//演唱者 //无参构造方法 public Song() { } //有参构造方法 public Song(String id, String name, String singer) { this.setId(id); this.setName(name); this.setSinger(singer); } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((singer == null) ? 0 : singer.hashCode()); return result; } @Override public boolean equals(Object obj) { if(this==obj) { return true; } //判断obj是否是Song类的对象 if(obj.getClass()==Song.class) { Song song = (Song)obj; return song.getId().equals(id)&&song.getName()==name&&song.getSinger()==singer; } return false; } @Override public String toString() { return "Song [id=" + id + ", name=" + name + ", singer=" + singer + "]"; } }
package com.demo19; //播放器类 import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PlayList { private String playListName; private List<Song> musicList; public PlayList() { musicList = new ArrayList(); } public PlayList(String playListName, List<Song> musicList) { this.playListName = playListName; this.musicList = musicList; } public String getPlayListName() { return playListName; } public void setPlayListName(String playListName) { this.playListName = playListName; } public List<Song> getMusicList() { return musicList; } public void setMusicList(List<Song> musicList) { this.musicList = musicList; } //将歌曲添加到播放列表中 public void addToPlayList(Song song) { getMusicList().add(song); } //显示播放列表中所有歌曲 public void displayAllSong() { for(int i=0;i<getMusicList().size();i++) { System.out.println(((Song) getMusicList().get(i)).getId()+" "+ ((Song)getMusicList().get(i)).getName()+" "+((Song)getMusicList().get(i)).getSinger()); } } //通过ID查询歌曲的方法 public Song searchSongById(String id) { Song song = null; for(Song temp:musicList) { if(temp.getId().equals(id)) { song = temp; break; } } return song; } //通过歌曲名称去查找歌曲 public Song searchSongByName(String name) { Song song = null; for(Song temp:musicList) { if(temp.getName().equals(name)) { song = temp; break; } } return song; } //修改歌曲 public void updateSong(Song song) { } //从播放列表删除歌曲 public void deleteSong(String id) { Song song; for(Song temp:musicList) { if(temp.getId().equals(id)) { song = temp; musicList.remove(song); System.out.println("删除成功"); break; } } } //导出歌单 public void exportPlayList() { } }
package com.demo19; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; //播放器类 import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class PlayListCollection { Map<String,PlayList> playListMap; public PlayListCollection() { playListMap = new HashMap<String,PlayList>(); } public PlayListCollection(Map<String, PlayList> playListMap) { this.setPlayListMap(playListMap); } public Map<String, PlayList> getPlayListMap() { return playListMap; } public void setPlayListMap(Map<String, PlayList> playListMap) { this.playListMap = playListMap; } //添加播放列表 public void addPlayList(PlayList playlist) { if (!playListMap.containsKey(playlist.getPlayListName())) { playListMap.put(playlist.getPlayListName(), playlist); System.out.println("该播放列表存在!"); System.out.println("该播放列表的名称为:"+playlist.getPlayListName()); } else { System.out.println("列表名称不能重复,请重新输入"); } } //删除播放列表 public void deletePlayList(String name) { if(getPlayListMap().containsKey(name)) { playListMap.remove(name); System.out.println("删除成功"); } else { System.out.println("您输入的列表名有误"); } } //通过名字查询 public PlayList searchPlayListByName(String playListName) { if(getPlayListMap().containsKey(playListName)) { System.out.println("该播放列表存在,播放列表名称为"+playListName); } else { System.out.println("该列表不存在"); } return null; } //显示所有播放列表的名称 public void displayPlayListName() { Set<String> playListNameSet=playListMap.keySet(); System.out.println("播放列表中的歌曲信息为:"); for(String name:playListNameSet){ System.out.println(name); } } }
43
收起
正在回答 回答被采纳积分+1
3回答
好帮手慕珊
2017-04-27 09:38:04
代码中的问题:
1、下面这个方法的输出语句,可以简写一下,Song类有toString方法,直接得到Song类的对象输出就可以了。
//显示播放列表中所有歌曲 public void displayAllSong() { for(int i=0;i<getMusicList().size();i++) { System.out.println(((Song) getMusicList().get(i)).getId()+" "+ ((Song)getMusicList().get(i)).getName()+" "+((Song)getMusicList().get(i)).getSinger()); } }
2、这个方法,该播放列表存在那个输出语句去掉,感觉好像以前添加过似的
//添加播放列表
public void addPlayList(PlayList playlist) { if (!playListMap.containsKey(playlist.getPlayListName())) { playListMap.put(playlist.getPlayListName(), playlist); System.out.println("该播放列表存在!"); System.out.println("该播放列表的名称为:"+playlist.getPlayListName()); } else { System.out.println("列表名称不能重复,请重新输入"); } }
3、TestDemo类中添加播放列表的代码,看代码注释
case 1: System.out.println("请输入列表的名称"); String name = sc.next(); //pl.setPlayListName(name);//要创建一个新的PlayList对象 PlayList pl1=new PlayList(name);//你用的是同一个对象,只是每次改名字了 pc.addPlayList(pl1);//主播放列表单独用一个PlayList对象
qq_星空独奏_0
2017-04-26 20:59:31
package com.demo19; import java.util.Scanner; //测试类 public class TestDemo { PlayListCollection pc = new PlayListCollection(); PlayList pl = new PlayList(); // 测试Song类 public void testSong() { } // 测试播放列表类 public void testPlayList() { } // 测试播放器类 public void testPlayListCollection() { } // 主菜单 public void mainMenu() { while (true) { Scanner sc = new Scanner(System.in); System.out.println("*****************************"); System.out.println("**主菜单**"); System.out.println("1--播放器列表管理"); System.out.println("2--播放器管理"); System.out.println("0--退出"); System.out.println("*****************************"); int key; switch (key = sc.nextInt()) { case 1: this.playListMenu(); break; case 2: this.palyerMenu(); break; default: break; } } } // 播放列表管理菜单 public void playListMenu() { PlayList playlist = new PlayList(); while (true) { Song song = new Song(); Scanner sc = new Scanner(System.in); System.out.println("*****************************"); System.out.println("**播放列表管理**"); System.out.println("1--将歌曲添加到主播放列表"); System.out.println("2--将歌曲添加到普通播放列表"); System.out.println("3--通过歌曲ID查询播放列表中的歌曲"); System.out.println("4--通过歌曲名称查询播放列表中的歌曲"); System.out.println("5--修改播放列表中的歌曲"); System.out.println("6--删除播放列表中的歌曲"); System.out.println("7--显示播放列表中的所有歌曲"); System.out.println("8--导出歌单"); System.out.println("9--返回上一级菜单"); System.out.println("*****************************"); int key; switch (key = sc.nextInt()) { case 1: // 输入ID System.out.println("请输入歌曲ID"); String id = sc.next(); song.setId(id); // 输入歌曲名 System.out.println("请输入歌曲名"); String name = sc.next(); song.setName(name); // 输入歌手名 System.out.println("请输入歌手"); String singer = sc.next(); song.setSinger(singer); pl.addToPlayList(song); pc.getPlayListMap().put("主播放列表", pl); break; case 2: // 输入播放列表名称 System.out.println("输入要插入播放列表名称"); String playlistname = sc.next(); if ((pc.getPlayListMap()).containsKey(playlistname)) { System.out.println("请输入歌曲ID"); String id2 = sc.next(); System.out.println("该歌曲不存在,继续输入其他信息"); song.setId(id2); // 输入歌曲名 System.out.println("请输入歌曲名"); String name2 = sc.next(); song.setName(name2); // 输入歌手名 System.out.println("请输入歌手"); String singer2 = sc.next(); song.setSinger(singer2); pc.getPlayListMap().put(playlistname, playlist); break; } else { System.out.println("该播放列表不存在,请先将播放列表添加到播放器中!"); break; } // 输入ID case 3: System.out.println("请输入要查询的播放列表名称"); String playlistname2 = sc.next(); if (pc.getPlayListMap().containsKey(playlistname2)) { System.out.println("请输入查找歌曲的ID"); String sid = sc.next(); song = pc.playListMap.get(playlistname2).searchSongById(sid); if (song != null) { System.out.println("该ID存在"); System.out.println("ID为" + song.getId() + "的信息如下"); System.out.println(song); break; } else { System.out.println("该ID的歌曲不存在"); break; } } else { System.out.println("该播放列表不存在,请先将播放列表添加到播放器中!"); break; } case 4: System.out.println("请输入查找歌曲的名称"); String sname = sc.next(); song = pl.searchSongByName(sname); if (song != null) { System.out.println("该歌曲存在"); System.out.println("歌曲名为" + song.getName() + "的信息如下"); System.out.println(song); } else { System.out.println("该歌曲不存在"); } break; case 5: break; case 6: break; case 7: // 输入播放列表名称 System.out.println("输入播放列表名称"); String playlistname3 = sc.next(); if ((pc.getPlayListMap()).containsKey(playlistname3)) { pc.getPlayListMap().get(playlistname3).displayAllSong(); } else { System.out.println("该播放列表不存在,请先将播放列表添加到播放器中!"); break; } break; case 8: break; case 9: this.mainMenu(); break; } } } // 播放器菜单 public void palyerMenu() { pc.getPlayListMap().put("主播放列表", pl); while (true) { System.out.println("*****************************"); System.out.println("**播放器管理**"); System.out.println("1--向播放器添加播放列表"); System.out.println("2--从播放器删除播放列表"); System.out.println("3--通过名字查询播放列表信息"); System.out.println("4--显示所有播放列表"); System.out.println("9--返回上一级菜单"); System.out.println("*****************************"); Scanner sc = new Scanner(System.in); int key; switch (key = sc.nextInt()) { case 1: System.out.println("请输入列表的名称"); String name = sc.next(); pl.setPlayListName(name); pc.addPlayList(pl); break; case 2: System.out.println("请输入要删除列表的名称"); String name1 = sc.next(); pc.deletePlayList(name1); break; case 3: System.out.println("请输入列表的名称"); String name2 = sc.next(); pc.searchPlayListByName(name2); break; case 4: pc.displayPlayListName(); break; case 9: this.mainMenu(); break; } } } // 主流程实现 public void test() { pl.setPlayListName("主播放列表"); pc.getPlayListMap().put(pl.getPlayListName(), pl); } public static void main(String[] args) { TestDemo t = new TestDemo(); t.test(); t.mainMenu(); } }
能不能详细讲一下addToPlaylist方法中判断插入的歌曲在List中是否存在是怎么实现的
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星