首页 > 学院 > 开发设计 > 正文

播放器管理系统

2019-11-11 03:17:38
字体:
来源:转载
供稿:网友

详细设计 一、类 1、歌曲类(Song) 属性:歌曲ID,歌曲名称,演唱者 方法:构造方法、getter/setter方法、hashCode()和equals()重写方法、toString重写方法 2、播放列表类(PlayList) 属性:播放列表名称、播放列表中的歌曲集合(List类型) 方法:构造方法、getter/setter方法、public void addToPlayList(Song song)将歌曲添加到播放列表的方法、public void displayAllSong()显示播放列表中所有歌曲的方法、public Song searchSongById(String id)通过id查询歌曲的方法、public Song searchSongByName(String Name)通过名称查询歌曲的方法、public void updateSong(Song song)修改歌曲的方法、public void deleteSong(String id)从播放列表删除歌曲的方法、public void exportPlayList()导出歌单的方法 3、播放器类(PlayListCollection) 属性:存放播放列表的集合(Map类型) 方法:构造方法、getter/setter方法、public void addPlayList(PlayList playlist)添加播放列表到播放器的方法、public void deletePlayList(PlayList playlist)删除播放列表的方法、public PlayList searchPlayListByName(String Name)通过名字查询播放列表的方法、public void displayPlayListName()显示所有播放列表名称的方法 4、测试类(Test)

歌曲类package com.imooc.model;import java.io.Serializable;/*由于之后要利用对象序列化输出歌单,因此需要实现序列化接口*/public class Song implements Serializable{ PRivate String songId; private String songName; private String songSinger; public Song() { } public Song(String songId,String songName,String songSinger) { setSongId(songId); setSongName(songName); setSongSinger(songSinger); } public String getSongId() { return songId; } public void setSongId(String songId) { this.songId = songId; } public String getSongName() { return songName; } public void setSongName(String songName) { this.songName = songName; } public String getSongSinger() { return songSinger; } public void setSongSinger(String songSinger) { this.songSinger = songSinger; } @Override public String toString() { return "[歌曲编号:"+songId+" 歌曲名:"+songName+" 演唱者:"+songSinger+"]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((songId == null) ? 0 : songId.hashCode()); result = prime * result + ((songName == null) ? 0 : songName.hashCode()); result = prime * result + ((songSinger == null) ? 0 : songSinger.hashCode()); return result; } @Override public boolean equals(Object obj) { if(this==obj) return true; if(obj instanceof Song) { return this.songId.equals(((Song) obj).songId); } else return false; }}播放列表类package com.imooc.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;//播放列表中的歌曲集合,且只允许存放Song类型的对象 public PlayList() { } public PlayList(String playListName) { setPlayListName(playListName); setMusicList(new ArrayList<Song>()); } 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) { int i; if(musicList.size()==0)//若列表中不含任何歌曲则直接添加 { musicList.add(song); System.out.println("添加成功!"); } else { for(i=0;i<musicList.size();i++) { //判断添加的歌曲是否重复,若无重复再进行添加 if((song.getSongSinger()).equals((musicList.get(i)).getSongSinger())&(song.getSongName()).equals((musicList.get(i)).getSongName())) { System.out.println("此歌曲已存在!添加失败!"); break; } if(i==(musicList.size()-1)) { musicList.add(song); System.out.println("添加成功!"); } } } } //显示播放列表中的所有歌曲 public void displayAllSong() { for(int i=0;i<musicList.size();i++) { System.out.println(musicList.get(i)); } System.out.println("歌曲显示完毕!"); } //通过歌曲ID查找歌曲 public Song searchSongById(String songId) { int i; boolean flag=false; for(i=0;i<musicList.size();i++)//遍历List { if(songId.equals(musicList.get(i).getSongId())) { System.out.println("找到了!歌曲信息为:"); System.out.println(musicList.get(i)); flag=true; break; } if(i==(musicList.size()-1)) { System.out.println("不存在此歌曲!"); } } if(flag) return musicList.get(i); else return null; } //通过歌曲名查找歌曲 public Song searchSongByName(String songName) { int i; boolean flag=false; for(i=0;i<musicList.size();i++)//遍历List { if(songName.equals(musicList.get(i).getSongName())) { System.out.println("找到了!歌曲信息为:"); System.out.println(musicList.get(i)); flag=true; break;//找到了就跳出循环 } if(i==(musicList.size()-1)) { System.out.println("不存在此歌曲!"); } } if(flag) return musicList.get(i); else return null; } //修改歌曲 public void updateSong(Song song) { for(int i=0;i<musicList.size();i++) { if((song.getSongId()).equals((musicList.get(i)).getSongId())) { musicList.set(i,song); System.out.println("修改成功!"); break; } } } //根据歌曲ID删除歌曲 public void deleteSong(String songId) { for(int i=0;i<musicList.size();i++) { if(songId.equals((musicList.get(i)).getSongId())) { musicList.remove(i); System.out.println("删除成功!"); break; } if(i==(musicList.size()-1)) { System.out.println("不存在此歌曲!"); } } } //导出歌单 public void exportPlayList() { try { FileOutputStream fos = new FileOutputStream(playListName+"歌单"); ObjectOutputStream oos=new ObjectOutputStream(fos); for(int i=0;i<musicList.size();i++) { oos.writeObject(musicList.get(i)); oos.flush(); } System.out.println("导出歌单/""+playListName+"/"成功!"); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}package com.imooc.model;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class PlayListCollection { private Map<String,PlayList> playListMap;//存放播放列表的集合 public PlayListCollection() { //在无参构造中为成员属性赋值 this.playListMap=new HashMap<String,PlayList>(); } 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())) { System.out.println("此播放列表已存在!"); } else { playListMap.put(playlist.getPlayListName(),playlist); System.out.println("成功将播放列表添加到播放器!"); } } //删除播放列表 public void deletePlayList(String playListName) { if(playListMap.containsKey(playListName)) { playListMap.remove(playListName); System.out.println("删除播放列表成功!"); } else { System.out.println("不存在此播放列表!删除失败!"); } } public void searchPlayListByName(String playListName) { if(playListMap.containsKey(playListName)) { System.out.println("存在此播放列表!"); } else { System.out.println("不存在此播放列表!"); } } //显示所有播放列表名称 public void displayPlayListName() { Iterator<PlayList> it=playListMap.values().iterator(); while(it.hasNext()) { System.out.println(it.next().getPlayListName()); } System.out.println("已显示全部播放列表名称!"); }}测试类package com.imooc.test;import java.util.Iterator;import java.util.Scanner;import com.imooc.model.PlayList;import com.imooc.model.PlayListCollection;import com.imooc.model.Song;public class Test { public static void printStar() { System.out.println("*********************************"); } /*主菜单*/ public static void mainMenu() { printStar(); System.out.println("/t**主菜单**"); System.out.println("/t1--播放列表管理"); System.out.println("/t2--播放器管理"); System.out.println("/t0--退出"); printStar(); System.out.println("请输入对应的数字进行操作:"); } /*播放列表管理菜单*/ public static void playListMenu() { printStar(); System.out.println("/t**播放列表管理**"); 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--返回上一级菜单"); printStar(); System.out.println("请输入对应的数字对播放列表进行管理:"); } /*播放器菜单*/ public static void playerMenu() { printStar(); System.out.println("/t**播放器管理**"); System.out.println("/t1--向播放器添加播放列表"); System.out.println("/t2--从播放器删除播放列表"); System.out.println("/t3--通过名字查询播放列表信息"); System.out.println("/t4--显示所有播放列表名称"); System.out.println("/t9--返回上一级菜单"); printStar(); } /*主流程实现*/ public static void test() { } public static void main(String[] args) { PlayList mainPlayList=new PlayList("主播放列表");//创建主播放列表 PlayListCollection playlistcollection=new PlayListCollection();//创建播放器对象 playlistcollection.addPlayList(mainPlayList);//将主播放列表加入播放器 Scanner sc=new Scanner(System.in); int mainMenuInput=3; while(mainMenuInput!=0) { mainMenu(); mainMenuInput=sc.nextInt(); switch(mainMenuInput) { case 1: { int playListMenuInput=0; while(true) { if(playListMenuInput==9)/*利用break跳出while循环实现回到上一级菜单的功能,(前提是有循环while嵌套才能回到mainMenu()这个语句)*/ { break; } playListMenu(); playListMenuInput=sc.nextInt(); switch(playListMenuInput) { case 1: { int songNumber=0; System.out.println("请输入要添加的歌曲的数量:"); songNumber=sc.nextInt(); for(int i=1;i<=songNumber;i++) { System.out.println("请输入第"+i+"首歌曲的id:"); String songId=sc.next(); System.out.println("请输入第"+i+"首歌曲的名字:"); String songName=sc.next(); System.out.println("请输入第"+i+"首歌曲的演唱者:"); String songSinger=sc.next(); Song song=new Song(songId,songName,songSinger); mainPlayList.addToPlayList(song); } break; } case 2: { int songNumber=0; String playListName; System.out.println("请输入播放列表名称:"); playListName=sc.next(); if(playlistcollection.getPlayListMap().containsKey(playListName)) { System.out.println("请输入要添加歌曲的数量:"); songNumber=sc.nextInt(); for(int i=1;i<=songNumber;i++) { boolean flag=true;//用来判断输入的歌曲ID是否存在 System.out.println("请输入第"+i+"首歌曲的id:"); String songId=sc.next(); /*如果查找到了输入的歌曲ID已存在,则直接根据ID添加歌曲至播放列表,并且不再输入名字和演唱者*/ for(int j=0;j<mainPlayList.getMusicList().size();j++) { if(songId.equals(((Song)(mainPlayList.getMusicList().get(j))).getSongId())) { playlistcollection.getPlayListMap().get(playListName).addToPlayList((Song)playlistcollection.getPlayListMap().get(playListName).getMusicList().get(j)); flag=false; break; } } if(flag) { System.out.println("该歌曲在主播放列表不存在!请继续输入歌曲其他信息以完成新歌曲的创建!"); System.out.println("请输入第"+i+"首歌曲的名字:"); String songName=sc.next(); System.out.println("请输入第"+i+"首歌曲的演唱者:"); String songSinger=sc.next(); Song song=new Song(songId,songName,songSinger); playlistcollection.getPlayListMap().get(playListName).addToPlayList(song); mainPlayList.addToPlayList(song);//向普通列表添加歌曲时同时也添加到主播放列表 } System.out.println("主播放列表中的歌曲:"); mainPlayList.displayAllSong(); System.out.println("普通播放列表中的歌曲:"); playlistcollection.getPlayListMap().get(playListName).displayAllSong(); } } else { System.out.println("不存在该播放列表!请先将播放列表添加至播放器!"); } break; } case 3: { System.out.println("请输入要查询的播放列表名称:"); String playListName=sc.next(); if(playlistcollection.getPlayListMap().containsKey(playListName)) { System.out.println("请输入要查询的歌曲id:"); String songId=sc.next(); playlistcollection.getPlayListMap().get(playListName).searchSongById(songId); } else { System.out.println("不存在该播放列表!请先将播放列表添加至播放器!"); } break; } case 4: { System.out.println("请输入要查询的播放列表名称:"); String playListName=sc.next(); if(playlistcollection.getPlayListMap().containsKey(playListName)) { System.out.println("请输入要查询的歌曲名称:"); String songName=sc.next(); playlistcollection.getPlayListMap().get(playListName).searchSongByName(songName); } else { System.out.println("不存在该播放列表!请先将播放列表添加至播放器!"); } break; } case 5: { System.out.println("请输入要修改的歌曲id:"); String songId=sc.next(); Song song=mainPlayList.searchSongById(songId); System.out.println("请输入修改后的歌曲名称:"); String songName=sc.next(); System.out.println("请输入修改后的歌曲演唱者:"); String songSinger=sc.next(); song.setSongName(songName); song.setSongSinger(songSinger); mainPlayList.updateSong(song); break; } case 6: { /*利用迭代器遍历删除每一个播放列表里id为输入id的歌曲*/ System.out.println("请输入要删除的歌曲id:"); String songId=sc.next(); Iterator<PlayList> it=playlistcollection.getPlayListMap().values().iterator(); while(it.hasNext()) { PlayList array=(PlayList)it.next(); for(int i=0;i<(array).getMusicList().size();i++) { if(songId.equals(((Song)(((array).getMusicList()).get(i))).getSongId())) { array.deleteSong(songId); break; } } } break; } case 7: { System.out.println("请输入要显示的播放列表名称:"); String playListName=sc.next(); if(playlistcollection.getPlayListMap().containsKey(playListName)) { playlistcollection.getPlayListMap().get(playListName).displayAllSong(); } else { System.out.println("不存在该播放列表!请先将播放列表添加至播放器!"); } break; } case 8: { System.out.println("请输入要导出歌单的播放列表名称:"); String playListName=sc.next(); if(playlistcollection.getPlayListMap().containsKey(playListName)) { playlistcollection.getPlayListMap().get(playListName).exportPlayList(); } else { System.out.println("不存在该播放列表!请先将播放列表添加至播放器!"); } break; } case 9: { break; } default :; } } break; } case 2: { int playerMenuInput=0; while(true) { if(playerMenuInput==9)/*break跳出while循环回到上级菜单*/ break; playerMenu(); playerMenuInput=sc.nextInt(); switch(playerMenuInput) { case 1: { System.out.println("输入要添加的播放列表名称:"); String playListName=sc.next(); playlistcollection.addPlayList(new PlayList(playListName)); break; } case 2: { System.out.println("输入要删除的播放列表名称:"); String playListName=sc.next(); playlistcollection.deletePlayList(playListName); break; } case 3: { System.out.println("请输入要查询的播放列表名称:"); String playListName=sc.next(); playlistcollection.searchPlayListByName(playListName); break; } case 4: { playlistcollection.displayPlayListName(); break; } case 9: { break; } } } break; } case 0:System.out.println("成功退出播放器!");System.exit(-1);break; default :System.out.println("数据输入错误!"); } } sc.close(); }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表