删除播放列表中的歌曲
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 | package comm.imooc.player; public class Song { /** * 属性:歌曲id,歌曲名,演唱者 */ private String id; private String name; private String singer; /** * 无参构造方法 */ public Song() { } /** * 多参构造方法给属性赋值 * @param id * @param name * @param singer */ public Song(String id, String name, String singer) { this .id = id; this .name = name; this .singer = singer; } /** * geter和setter方法 * @return */ 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; } /** * hashCode()和equals()方法 */ @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 ; if (obj == null ) return false ; if (getClass() != obj.getClass()) return false ; Song other = (Song) obj; if (id == null ) { if (other.id != null ) return false ; } else if (!id.equals(other.id)) return false ; if (name == null ) { if (other.name != null ) return false ; } else if (!name.equals(other.name)) return false ; if (singer == null ) { if (other.singer != null ) return false ; } else if (!singer.equals(other.singer)) return false ; return true ; } /** * 重写toString方法 */ @Override public String toString() { return "歌曲信息:歌曲id:" + id + ",歌曲名:" + name + ",演唱者:" + singer; } } |
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 | package comm.imooc.player; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PlayList{ Scanner sc= new Scanner(System.in); /** * 属性:播放列表名称,播放列表中的歌曲集合 */ private String playListName; List<Song> musicList= new ArrayList<Song>(); /** * 无参构造方法 */ public PlayList() { } /** * 多参构造方法 * @param playListName * @param musicList */ public PlayList(String playListName, List<Song> musicList) { super (); this .playListName = playListName; this .musicList = musicList; } /** * getter和setter方法 * @return */ 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; } /** * 方法:将歌曲添加到播放列表 * @param song */ public void addToPlayList(Song song) { if (!(musicList.contains(song))) { musicList.add(song); } else { System.out.println( "该播放列表中已有该歌曲!" ); } } /** * 方法13:通过id查询歌曲 * @param id * @return */ public Song searchSongById(String id) { Song songg= new Song(); for (Song song:musicList) { if (song.getId().equals(id)) { songg=song; } } return songg; } /** * 方法14:通过名称查询歌曲 * @param name * @return */ public void searchSongByName(String name) { for (Song song:musicList) { if (song.getName().equals(name)) { System.out.println(song); } } } /** * 方法15:修改歌曲 * @param song */ public void updateSong(Song song) { System.out.println( "请输入修改后的歌曲名称:" ); String name=sc.next(); song.setName(name); System.out.println( "请输入修改后的演唱者:" ); String singer=sc.next(); song.setSinger(singer); System.out.println( "修改成功!" ); } /** * 方法16:从播放列表删除歌曲 * @param id */ public void deleteSong(Song song) { musicList.remove(song); System.out.println( "删除成功!" ); } /** * 方法17:显示播放列表中所有歌曲 */ public void disaplayAllSong() { System.out.println( "播放列表中的所有歌曲为:" ); for ( int i= 0 ;i<musicList.size();i++) { System.out.println(musicList.get(i)); //!!! } } /** * 方法18:导出歌单 */ public void exportPlayList() { System.out.println( "歌单导出成功!" ); } /** * 重写toString方法 */ @Override public String toString() { return playListName; } } |
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 | package comm.imooc.player; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; public class PlayListCollection { Scanner sc= new Scanner(System.in); /** * 定义存放播放列表的集合(playListMap) */ Map<String,PlayList> playListMap; /** * 无参构造方法 */ public PlayListCollection() { } /** * 多参构造方法 * @param playListMap */ public PlayListCollection(Map<String, PlayList> playListMap) { super (); this .playListMap = playListMap; } /** * getter和setter方法 * @return */ public Map<String, PlayList> getPlayListMap() { return playListMap; } public void setPlayListMap(Map<String, PlayList> playListMap) { this .playListMap = playListMap; } /** * 方法21:添加播放列表 * @param playlist */ public void addPlayList(PlayList playlist) { System.out.println( "向播放器添加播放列表" ); System.out.println( "输入要添加的播放列表名称:" ); String playlistname=sc.next(); playListMap.put(playlistname, new PlayList(playlistname, new ArrayList<Song>())); } /** * 方法22:删除播放列表 * @param playList */ public void deletePlayList(PlayList playList) { playListMap.remove(playList.getPlayListName()); System.out.println( "删除成功!" ); } /** * 方法23:通过名字查询 * @param playListName * @return */ public PlayList searchListByName(String playListName) { if (playListMap.containsKey(playListName)) { System.out.println( "该播放列表存在!" ); System.out.println( "该播放列表的名称为:" +playListName); } return null ; } /** * 方法24:显示所有播放列表名称 */ public void disaplayPlayListName() { Iterator<PlayList> it=playListMap.values().iterator(); while (it.hasNext()) { System.out.println(it.next()); } } } |
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | package comm.imooc.player; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Scanner; public class TestDemo { /** * 方法:主菜单 */ public void mainMenu() { System.out.println( "******************************" ); System.out.println( " **主菜单**" ); System.out.println( " 1--播放列表管理" ); System.out.println( " 2--播放器管理" ); System.out.println( " 0--退出" ); System.out.println( "******************************" ); System.out.println( "请输入对应数字进行操作:" ); } /** * 方法:播放列表管理菜单 */ public void playListMenu() { 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( "****************************************" ); System.out.println( "请输入对应数字对播放列表进行管理:" ); } /** * 方法:播放器菜单 */ public void playerMenu() { 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( "****************************************" ); System.out.println( "请输入对应数字对播放器进行管理:" ); } public static void main(String[] args) { Scanner sc= new Scanner(System.in); Song song= new Song(); //歌曲类对象 PlayList pl= new PlayList(); //播放列表类对象 Map<String,PlayList> playListMap= new HashMap<String,PlayList>(); //播放器类对象参数(播放器集合) PlayListCollection plc= new PlayListCollection(playListMap); //播放器类对象 TestDemo test= new TestDemo(); //测试类对象 PlayList playlist1= new PlayList( "主播放列表" , new ArrayList<Song>()); //定义主播放列表实例对象 plc.playListMap.put( "主播放列表" , playlist1); //添加主播放列表实例对象 boolean b= true ; while (b) { test.mainMenu(); int n=sc.nextInt(); Song song1; switch (n) { case 1 :test.playListMenu(); while (b) { int m=sc.nextInt(); switch (m) { case 1 : System.out.println( "将歌曲添加到主播放列表" ); System.out.println( "请输入要添加的歌曲数量:" ); int k=sc.nextInt(); for ( int i= 0 ;i<k;i++) { System.out.println( "请输入第" +(i+ 1 )+ "首歌曲:" ); System.out.println( "请输入歌曲的id:" ); String id=sc.next(); System.out.println( "请输入歌曲的名称:" ); String name=sc.next(); System.out.println( "请输入歌曲的演唱者:" ); String singer=sc.next(); song1= new Song(id,name,singer); playlist1.addToPlayList(song1); } break ; case 2 : System.out.println( "将歌曲添加到普通播放列表" ); System.out.println( "请输入要添加的播放列表名称:" ); String playlistname=sc.next(); if (!(plc.getPlayListMap().containsKey(playlistname))) { System.out.println( "该播放列表不存在,请先将播放列表添加到播放器中" ); } else if (playlistname.equals(plc.playListMap.get(playlistname).getPlayListName())){ System.out.println( "请输入要添加的歌曲数量:" ); int j=sc.nextInt(); for ( int i= 0 ;i<j;i++) { System.out.println( "请输入第" +(i+ 1 )+ "首歌曲:" ); System.out.println( "请输入歌曲的id:" ); String id=sc.next(); boolean bl= true ; if ( true ) { for ( int a= 0 ;a<playlist1.musicList.size();a++) { if (id.equals(playlist1.musicList.get(a).getId())) { plc.playListMap.get(playlistname).addToPlayList(playlist1.musicList.get(a)); bl= false ; break ; } } } if (bl== false ) { bl= true ; continue ; } else { bl= true ; } System.out.println( "该歌曲在主播放列表不存在,继续输入歌曲其他信息!" ); System.out.println( "请输入歌曲的名称:" ); String name=sc.next(); System.out.println( "请输入歌曲的演唱者:" ); String singer=sc.next(); Song song2= new Song(id,name,singer); plc.playListMap.get(playlistname).addToPlayList(song2); playlist1.addToPlayList(song2); } System.out.println( "主播放列表:" ); System.out.println( "播放列表中的所有歌曲为:" ); for ( int i= 0 ;i<playlist1.musicList.size();i++) { System.out.println(playlist1.musicList.get(i)); } System.out.println( "普通播放列表:" ); System.out.println( "播放列表中的所有歌曲为:" ); for ( int i= 0 ;i<plc.playListMap.get(playlistname).musicList.size();i++) { System.out.println(plc.playListMap.get(playlistname).musicList.get(i)); } } break ; case 3 : System.out.println( "通过歌曲id查询播放列表中的歌曲" ); System.out.println( "请输入要查询的播放列表名称:" ); String playlistname2=sc.next(); if (plc.playListMap.containsKey(playlistname2)) { if (playlistname2.equals(plc.playListMap.get(playlistname2).getPlayListName())) { System.out.println( "请输入要查询歌曲id:" ); String id=sc.next(); boolean bl= false ; for ( int a= 0 ;a<plc.playListMap.get(playlistname2).musicList.size();a++) { String idd=plc.playListMap.get(playlistname2).musicList.get(a).getId(); if (id.equals(idd)) { System.out.println(plc.playListMap.get(playlistname2).searchSongById(idd)); bl= true ; break ; } } if (!bl) { System.out.println( "该歌曲在播放列表" +plc.playListMap.get(playlistname2).getPlayListName()+ "中不存在!" ); } } } else { System.out.println( "对不起,您要查询的播放列表不存在!" ); } break ; case 4 : System.out.println( "通过歌曲名称查询播放列表中的歌曲" ); System.out.println( "请输入要查询的播放列表名称:" ); String playlistname3=sc.next(); if (plc.playListMap.containsKey(playlistname3)) { if (playlistname3.equals(plc.playListMap.get(playlistname3).getPlayListName())) { System.out.println( "请输入要查询歌曲名称:" ); String songname=sc.next(); boolean bl= false ; for ( int a= 0 ;a<plc.playListMap.get(playlistname3).musicList.size();a++) { String songName=plc.playListMap.get(playlistname3).musicList.get(a).getName(); if (songname.equals(songName)) { plc.playListMap.get(playlistname3).searchSongByName(songName); bl= true ; break ; } } if (!bl) { System.out.println( "该歌曲在播放列表" +plc.playListMap.get(playlistname3).getPlayListName()+ "中不存在!" ); } } } else { System.out.println( "对不起,您要查询的播放列表不存在!" ); } break ; case 5 : System.out.println( "修改播放列表中的歌曲" ); System.out.println( "请输入要修改的歌曲id:" ); String id=sc.next(); boolean bl= false ; for ( int i= 0 ;i<playlist1.musicList.size();i++) { if (id.equals(playlist1.musicList.get(i).getId())) { playlist1.updateSong(playlist1.searchSongById(id)); bl= true ; break ; } } if (!bl) { System.out.println( "对不起,您要查询的歌曲id在播放列表中不存在!" ); } break ; case 6 : System.out.println( "删除播放列表中的歌曲" ); System.out.println( "请输入要删除的歌曲id" ); String id1=sc.next(); boolean bll= false ; for ( int i= 0 ;i<playlist1.musicList.size();i++) { if (id1.equals(playlist1.musicList.get(i).getId())) { playlist1.deleteSong(playlist1.searchSongById(id1)); bll= true ; break ; } } if (!bll) { System.out.println( "对不起,您要查询的歌曲id在播放列表中不存在!" ); } break ; case 7 : System.out.println( "显示所有播放列表中所有歌曲" ); System.out.println( "请输入要显示的播放列表名称:" ); String plname=sc.next(); if (!(plc.getPlayListMap().containsKey(plname))) { System.out.println( "该播放列表不存在!" ); } else if (plname.equals(plc.playListMap.get(plname).getPlayListName())) { plc.playListMap.get(plname).disaplayAllSong(); } break ; case 8 : System.out.println( "导出歌单" ); System.out.println( "请输入要导出歌单的播放列表名称:" ); String plname1=sc.next(); pl.exportPlayList(); break ; } if (m== 9 ) { break ; } test.playListMenu(); } break ; case 2 :test.playerMenu(); while (b) { int m=sc.nextInt(); switch (m) { case 1 :plc.addPlayList(pl); break ; case 2 : System.out.println( "请输入要删除的播放列表名称:" ); String playlistname=sc.next(); if (playListMap.containsKey(playlistname)) { plc.deletePlayList(plc.playListMap.get(playlistname)); } else { System.out.println( "播放器中没有该播放列表!" ); } break ; case 3 : System.out.println( "请输入要查询的播放列表名称:" ); String playlistname0=sc.next(); plc.searchListByName(playlistname0); plc.playListMap.get(playlistname0).disaplayAllSong(); break ; case 4 :plc.disaplayPlayListName(); break ; } if (m== 9 ) { break ; } test.playerMenu(); } break ; case 0 : System.out.println( "退出!" ); b= false ; break ; } } } } |
在主方法中,修改播放列表中的歌曲模块和删除播放列表中的歌曲模块的格式都是一样的
问题一:为什么修改了主播放列表中的歌曲而普通播放列表中的同一首歌也会随之改变。
问题二:为什么删除了主播放列表中的歌曲而普通播放列表中的同一首歌依然在
37
收起
正在回答 回答被采纳积分+1
1回答
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧