上一阶段课程,播放器作业,怎样实现普通列表和主播放列表的歌曲分别存放在不同的列表?
怎样实现普通列表和主播放列表的歌曲分别存放在不同的列表?
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | package com.player.model; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; //播放列表类 public class PlayList { private String playListName; private List<Song> musicList= new ArrayList<Song>(); public PlayList() {} 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) { musicList.add(song); } // -显示播放列表中所有歌曲: public void displayAllSong() { for ( int i= 0 ;i<musicList.size();i++) { System.out.println(musicList.get(i)); } } // -通过id查询歌曲: public Song searchSongById(String id) { for ( int i= 0 ;i<musicList.size();i++) { if (id.equals(musicList.get(i).getId())) { return musicList.get(i); //因为有序,所以可以通过遍历,list.get(i)返回list对象 } } return null ; } // -通过名称查询歌曲: public Song searchSongByName(String n) { for ( int i= 0 ;i<musicList.size();i++) { if (n.equals(musicList.get(i).getName())) { return musicList.get(i); } } return null ; } // -修改歌曲: public void updateSong(String id,Song song) { musicList.remove(searchSongById(id)); //删除 musicList.add(song); //添加 } // -从播放列表删除歌曲: public void deleteSong(String id) { musicList.remove(searchSongById(id)); } // - 导出歌单 : public void exportPlayList() { try { FileOutputStream fos = new FileOutputStream( this .getPlayListName()+ "的歌单.txt" ); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(musicList); oos.flush(); oos.close(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public String toString() { return "播放列表名称:" +getPlayListName(); } } package com.player.model; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; //播放器类 public class PlayListCollection{ private Map<String,PlayList> playListMap= new HashMap<String,PlayList>(); public PlayListCollection() {} public PlayListCollection(Map<String,PlayList> playListMap) { this .playListMap = playListMap; } public Map<String, PlayList> getPlayListMap() { return playListMap; } public void setPlayListMap(Map<String, PlayList> playListMap) { this .playListMap = playListMap; } // 添加播放列表: public void addPlayList(PlayList playList) { String key=playList.getPlayListName(); PlayList value=playList; playListMap.put(key, value); } // -删除播放列表: public void deletePlayList(PlayList playList) { playListMap.remove(playList.getPlayListName()); //Map.remove(key); } // -通过名字查询: public PlayList searchPlayListByName(String playListName) { Set<String> keySet=playListMap.keySet(); //Map的查询方法,放入Set中,用keySet()方法得到key值 for (String key:keySet) { if (playListName.equals(key)) { return (playListMap.get(key)); //通过Map.get(key)找到value值 } } return null ; } // -显示所有播放列表名称: public void displayPlayListName() { Iterator<PlayList> it=playListMap.values().iterator(); while (it.hasNext()) { System.out.print(it.next().getPlayListName()+ " " ); //System.out.print(it.next()+" ");查看代码发现,it.next()是输出播放列表的对象,应该改为it.next().getPlayListName() } } } package com.player.model; //歌曲类 public class Song implements java.io.Serializable{ // 导出歌单报错,Song类需要实现序列化接口 private String id; private String name; private String singer; public Song() {} public Song(String id,String name,String singer){ this .id = id; this .name = name; this .singer = 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; } public String toString() { return "歌曲信息为:歌曲id:" +getId()+ "歌曲名:" +getName()+ "演唱者:" +getSinger(); } @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 ; } } package com.player.test; import java.util.ArrayList; import java.util.HashMap; import java.util.InputMismatchException; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; import com.player.model.PlayList; import com.player.model.PlayListCollection; import com.player.model.Song; public class TestDemo { boolean flag= false ; boolean flag1= false ; List<Song> musicList= new ArrayList<Song>(); PlayList p1 = new PlayList( "主播放列表" ,musicList); Map<String,PlayList> playListMap= new HashMap<String,PlayList>(); Set<Entry<String,PlayList>> entrySet=playListMap.entrySet(); //entrySet方法取出所有key和value值 PlayListCollection plc= new PlayListCollection(playListMap); Scanner console = new Scanner(System.in); int i = 0 ; public void menu() { plc.addPlayList(p1); //因为HashMap不会加入重复的 System.out.println( "***********************************" ); System.out.println( "**主菜单**" ); System.out.println( "1--播放列表管理" ); System.out.println( "2--播放器管理" ); System.out.println( "0--退出" ); System.out.println( "***********************************" ); System.out.println( "请输入对应数字进行操作:" ); do { flag= false ; try { i = console.nextInt(); } catch (InputMismatchException e) { System.out.println( "请输入数字" ); console.next(); // 抛出异常,重新接收输入 } if (i == 0 || i == 1 || i == 2 ) { switch (i) { case 1 : flag= false ; playList(); break ; case 2 : flag= false ; playListCollection(); break ; case 0 : flag= false ; System.out.println( "谢谢光临" ); break ; } } else { System.out.println( "请输入0~2:" ); flag= true ; } } while (flag== true ); } public void playList() { while (!flag1) { 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( "请输入对应的数字对播放列表进行管理:" ); do { flag= false ; try { i = console.nextInt(); } catch (InputMismatchException e) { System.out.println( "请输入数字" ); console.next(); // 抛出异常,重新接收输入 continue ; } if (i < 10 ) { switch (i) { case 1 : System.out.println( "将歌曲添加到主播放列表" ); System.out.println( "请输入要添加的歌曲的数量:" ); int j = 0 ; // 用于循环次数而已 try { j = console.nextInt(); } catch (InputMismatchException e) { System.out.println( "请输入数字" ); console.next(); // 抛出异常,重新接收输入 } for ( int k = 1 ; k <= j; k++) { System.out.println( "请输入第" + k + "首歌曲" ); System.out.println( "请输入歌曲的id:" ); String sid = console.next(); System.out.println( "请输入歌曲的名称:" ); String sname = console.next(); System.out.println( "请输入歌曲的演唱者:" ); String ssinger = console.next(); p1.addToPlayList( new Song(sid, sname, ssinger)); // 参数直接new Song("","","")同时传参 } break ; case 2 : System.out.println( "将歌曲添加到普通放列表" ); System.out.println( "请输入要添加的播放列表名称:" ); String lname=console.next(); if (plc.searchPlayListByName(lname)== null ) { //悖论 // if(p1==null) { System.out.println( "该播放列表不存在,请先将播放列表添加到播放器中!" ); } else { System.out.println( "请输要添加的歌曲数量:" ); j = 0 ; // 用于循环次数而已 do { flag= false ; try { j = console.nextInt(); } catch (InputMismatchException e) { System.out.println( "请输入数字" ); flag= true ; console.next(); // 抛出异常,重新接收输入 } } while (flag== true ); for ( int k= 1 ;k<=j;k++) { System.out.println( "请输入第" +k+ "首歌曲" ); System.out.println( "请输入歌曲的id:" ); String sid=console.next(); flag= false ; for ( int q= 0 ;q<p1.getMusicList().size();q++) { if (sid.equals(p1.getMusicList().get(q).getId())) { // playListMap.get(lname).addToPlayList(playListMap.get(lname).searchSongById(sid));//playListMap.get(lname)返回PlayList对象 // 另外,向普通播放列表添加数据,也不应该是从普通播放列表查找数据添加进去,应该是从主播放列表查找数据进行添加。 playListMap.get(lname).addToPlayList(p1.searchSongById(sid)); flag= true ; break ; } } if (!flag) { System.out.println( "请输入歌曲的名称:" ); String sname=console.next(); System.out.println( "请输入歌曲的演唱者:" ); String ssinger=console.next(); playListMap.get(lname).addToPlayList( new Song(sid,sname,ssinger)); p1.addToPlayList( new Song(sid,sname,ssinger)); //主播放列表也添加该歌曲 } } } break ; case 3 : System.out.println( "通过歌曲id查询播放列表中的歌曲" ); System.out.println( "请输入要查询的播放列表名称:" ); lname=console.next(); if (plc.searchPlayListByName(lname)== null ) { System.out.println( "无此播放列表" ); } else { System.out.println( "请输入要查询的歌曲id:" ); String sid=console.next(); flag= false ; for ( int q= 0 ;q<p1.getMusicList().size();q++) { if (sid.equals(plc.searchPlayListByName(lname).getMusicList().get(q).getId())) { System.out.println(plc.searchPlayListByName(lname).searchSongById(sid)); flag= true ; break ; } } if (!flag) { System.out.println( "该歌曲不存在!" ); flag= true ; } //else {System.out.println("该歌曲在播放列表"+plc.searchPlayListByName(lname)+"中不存在!");} //切记不要在循环中写else,因为如果循环执行多次,肯定有歌曲是不符合要求的,即使歌曲存在,也会将不存在的信息多次输出。 } break ; case 4 : System.out.println( "通过歌曲名称查询播放列表中的歌曲" ); System.out.println( "请输入要查询的播放列表名称:" ); lname=console.next(); if (playListMap== null ){ System.out.println( "无此播放列表" ); } else if (plc.searchPlayListByName(lname)== null ) { System.out.println( "无此播放列表" ); } else { System.out.println( "请输入要查询的歌曲名称:" ); String sname=console.next(); flag= false ; for ( int q= 0 ;q<p1.getMusicList().size();q++) { if (sname.equals(plc.searchPlayListByName(lname).getMusicList().get(q).getName())) { System.out.println(plc.searchPlayListByName(lname).searchSongByName(sname)); flag= true ; break ; } } if (!flag) { System.out.println( "抱歉,没有该歌曲哟" ); } } break ; case 5 : System.out.println( "修改播放列表中的歌曲" ); System.out.println( "请输入要修改歌曲的id:" ); String sid=console.next(); System.out.println( "请输入要修改歌曲的名称:" ); String sname=console.next(); System.out.println( "请输入要修改歌曲的演唱者:" ); String ssinger=console.next(); for (Entry<String,PlayList> entry:entrySet) { (entry.getValue()).updateSong(sid, new Song(sid,sname,ssinger)); } break ; case 6 : System.out.println( "删除播放列表中的歌曲" ); System.out.println( "请输入要的操作的播放列表" ); lname=console.next(); System.out.println( "请输入要删除歌曲的id:" ); sid=console.next(); if (lname.equals( "主播放列表" )) { for (Entry<String,PlayList> entry:entrySet) { (entry.getValue()).deleteSong(sid); } } else { plc.searchPlayListByName(lname).deleteSong(sid); } break ; case 7 : System.out.println( "显示播放列表中的所有歌曲" ); System.out.println( "请输入要显示的播放列表的名称:" ); lname=console.next(); if (playListMap== null ){ System.out.println( "无此播放列表" ); } else if (plc.searchPlayListByName(lname)== null ) { System.out.println( "无此播放列表" ); } else { plc.searchPlayListByName(lname).displayAllSong(); //此处遍历的是所有播放列表存放的歌单,并且list方式会重复 } break ; case 8 : System.out.println( "导出歌单" ); System.out.println( "请输入要导出的播放列表的名称:" ); lname=console.next(); plc.searchPlayListByName(lname).exportPlayList(); break ; case 9 : menu(); flag1= true ; break ; } } else { System.out.println( "error" ); flag= true ; } } while (flag1== true ); } } public void playListCollection() { while (!flag1) { 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( "请输入对应的数字对播放器进行管理:" ); try { i=console.nextInt(); } catch (InputMismatchException e) { System.out.println( "请输入数字" ); console.next(); //抛出异常,重新接收输入 continue ; //前面用flag,do,while也可以。但是continue更简捷,但是非指定数字未处理,此处省略,处理见上。 } if (i== 1 ||i== 2 ||i== 3 ||i== 4 ||i== 9 ) { switch (i) { case 1 : System.out.println( "向播放器添加播放列表" ); System.out.println( "请输入要添加的播放列表的名称:" ); String lname=console.next(); // List<Song> p=new ArrayList<Song>(); plc.addPlayList( new PlayList(lname,musicList)); break ; case 2 : System.out.println( "向播放器删除播放列表" ); lname=console.next(); if (playListMap== null ){ System.out.println( "无此播放列表" ); } else if (plc.searchPlayListByName(lname)== null ) { System.out.println( "无此播放列表" ); } // 删除播放列表时,先判断查询的播放列表是否为null,如果为null要给出错误提示,如果不为null再删除。 // 之所以会报错,是因为如果playList为null,在调用getPlayListName()方法时就会报错。 else { plc.deletePlayList(plc.searchPlayListByName(lname)); System.out.println( "删除成功!" ); } break ; case 3 : System.out.println( "通过名字查询播放列表信息" ); lname=console.next(); if (playListMap== null ){ System.out.println( "无此播放列表" ); } else if (plc.searchPlayListByName(lname)== null ) { System.out.println( "无此播放列表" ); } else { System.out.println( "该列表找到了!" ); } break ; case 4 : System.out.println( "显示所有播放列表名称" ); plc.displayPlayListName(); p1.toString(); break ; case 9 : System.out.println( "返回上一级菜单" ); menu(); flag1= true ; break ; } } } } public static void main(String[] args) { TestDemo testDemo= new TestDemo(); testDemo.menu(); } } |
0
收起
正在回答 回答被采纳积分+1
3回答
相似问题
登录后可查看更多问答,登录/注册
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧