有部分功能没有完成,望老师指导
public class Song { private String id;// 歌曲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; } // getter和setter方法 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; if (obj.getClass() == Song.class) { Song song = (Song) obj; return song.getId().equals(id) && song.getName().equals(name); } return false; } public String toString() { return "歌曲信息:id:" + this.getId() + ",歌曲名称:" + this.getName() + ",演唱者:" + this.getSinger(); } } import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class PlayList { private String playListName; private List<Song> musicList = new ArrayList<>(); // 构造方法 public PlayList() { } public PlayList(String playListName, List<Song> musicList) { this.setMusicList(musicList); this.setPlayListName(playListName); musicList = new ArrayList<>(); } // getter和setter方法 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 = null; Scanner sc = new Scanner(System.in); System.out.println("请输入要添加歌曲的个数:"); int i = sc.nextInt(); for (int n = 1; n <= i; n++) { boolean flag = true; song = new Song(); while (flag) { System.out.println("请输入第" + n + "首歌曲的编号:"); String s = sc.next(); if (musicList.size() == 0) { flag = false; song.setId(s); } else { for (int j = 0; j < musicList.size(); j++) { flag = true; if (s.equals(musicList.get(j).getId())) { System.out.println("该编号已存在,请重新输入!"); break; } else { song.setId(s); flag = false; } } } } while (!flag) { System.out.println("请输入第" + n + "首歌曲的名称:"); String s1 = sc.next(); if (musicList.size() == 0) { song.setName(s1); flag = true; } else { for (int j = 0; j < musicList.size(); j++) { flag = false; if (s1.equals(musicList.get(j).getName())) { System.out.println("该名称已存在,请重新输入!"); break; } else { song.setName(s1); flag = true; } } } } System.out.println("请输入第" + n + "首歌曲的演唱者:"); String s2 = sc.next(); song.setSinger(s2); musicList.add(song); } } /** * 显示播放列表中所有歌曲 */ public void displayAllSong() { if (musicList.size() == 0) { System.out.println("播放列表为空,请按提示添加!"); } else { for (Song s : musicList) { System.out.println(s); } } } /** * 通过ID查询歌曲 * * @param id * 歌曲id */ public Song searchSongById(String id) { Scanner sc = new Scanner(System.in); boolean flag = false; System.out.println("请输入要查找的歌曲id:"); id = sc.next(); for (Song s : musicList) { if (s.getId().equals(id)) { System.out.println(s); flag = true; break; } } if (!flag) { System.out.println("未找到该歌曲"); } return null; } /** * 通过名称查找歌曲 * * @param n * 歌曲名称 */ public Song searchSongByName(String n) { Scanner sc = new Scanner(System.in); boolean flag = false; System.out.println("请输入查找的歌曲名称:"); n = sc.next(); for (Song s : musicList) { if (s.getName().equals(n)) { System.out.println(s); flag = true; } } if (!flag) { System.out.println("未找到该歌曲"); } return null; } /** * 修改歌曲 * * @param song */ public void updateSong() { Scanner sc = new Scanner(System.in); boolean flag = true; System.out.println("请输入修改的歌曲id:"); String n = sc.next(); for (Song s : musicList) { if (s.getId().equals(n)) { System.out.println("请输入修改后的歌曲名:"); String n1 = sc.next(); s.setName(n1); System.out.println("请输入修改后的演唱者:"); String n2 = sc.next(); s.setSinger(n2); flag = false; for (Song i : musicList) { System.out.println(i); } break; } } if (flag) { System.out.println("该歌曲不存在!"); } } /** * 删除歌曲 * * @param id * 歌曲id */ public void deleteSong(String id) { boolean flag = true; for (Song s : musicList) { if (s.getId().equals(id)) { musicList.remove(s); flag = false; break; } } if (flag) { System.out.println("未找到该歌曲"); } else { System.out.println("删除成功!"); } } } import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Scanner; public class PlayListCollection { private Map<String, PlayList> playListMap; // 构造方法 public PlayListCollection() { } public PlayListCollection(Map<String, PlayList> playListMap) { playListMap = new HashMap<>(); } // getter和setter方法 public Map<String, PlayList> getPlayListMap() { return playListMap; } public void setPlayListMap(Map<String, PlayList> playListMap) { this.playListMap = playListMap; } Scanner sc = new Scanner(System.in); /** * 添加播放列表 * * @param playList */ public void addPlayList(PlayList playList) { System.out.println("请输入要添加的列表名称:"); String n = sc.next(); playList.setPlayListName(n); playListMap.put(playList.getPlayListName(), playList); } /** * 删除播放列表 * * @param playList */ public void deletePlayList(PlayList playList) { System.out.println("请输入要删除的列表名称:"); String n = sc.next(); if (playListMap.containsKey(n)) { playListMap.remove(n); } } /** * 通过名字查询 * * @param playListName * @return */ public PlayList searchPlayListByName(String playListName) { System.out.println("请输入要查找的名称:"); playListName = sc.next(); if (playListMap.containsKey(playListName)) { System.out.println("该列表已找到"); } else { System.out.println("该列表不存在"); } return null; } /** * 显示所有播放列表名称 */ public void displayPlayListName() { System.out.println("所有播放列表名称为:"); Iterator<String> it = playListMap.keySet().iterator(); while (it.hasNext()) { System.out.println(it.next()); } } } import java.util.InputMismatchException; import java.util.Scanner; public class TestDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Song song=new Song(); PlayList pl = new PlayList(); PlayListCollection plc = new PlayListCollection(); boolean flag = false; int n = 0; try { while (!flag) { System.out.println("主菜单"); System.out.println("1、播放列表管理"); System.out.println("2、播放器管理"); System.out.println("0、退出"); System.out.println("请输入对应数字进行操作:"); n = sc.nextInt(); if (n >= 0 || n <= 2) { switch (n) { case 1: kk: while (true) { 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("请输入对应数字进行操作:"); n = sc.nextInt(); if (n >= 1 && n <= 9) { switch (n) { case 1: pl.addToPlayList(); case 2: case 3: System.out.println("请输入查询的歌曲ID:"); String id = sc.next(); pl.searchSongById(id); case 4: System.out.println("请输入查询的歌曲名称:"); String k = sc.next(); pl.searchSongByName(k); case 5: pl.updateSong(); case 6: System.out.println("请输入要删除的歌曲id:"); id = sc.next(); pl.deleteSong(id); case 7: pl.displayAllSong(); case 8: case 9: break kk; } } else { System.out.println("您的输入有误,请重新输入"); } } case 2: jj: while (true) { 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("请输入对应数字进行操作:"); n = sc.nextInt(); if (n >= 1 && n <= 4 || n == 9) { switch (n) { case 1: plc.addPlayList(null); case 2: plc.deletePlayList(null); case 3: plc.searchPlayListByName(null); case 4: plc.displayPlayListName(); case 9: break jj; } } else { System.out.println("您的输入有误,请重新输入!"); } } case 0: System.out.println("谢谢使用,欢迎下次光临"); flag = true; break; } } else { System.out.println("您的输入有误,请重新输入!"); continue; } } } catch (InputMismatchException e) { System.out.println("您的输入有误,欢迎下次使用"); } catch (Exception e) { System.out.println("您的输入有误,欢迎下次使用"); } } }
63
收起
正在回答
1回答
导出歌单功能思路如下,可以参考:提示用户选择要导出的播放列表名称,查询此列表是否存在,如果存在则将此列表中的歌曲依次写出,建议将导出的歌单命名为当前播放列表的名称~如果还有其他疑问可以继续提问,祝学习愉快~
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星