Song.java

    1. package com.player;
    2. import java.util.Objects;
    3. public class Song {
    4. private String id;
    5. private String name;
    6. private String singer;
    7. public Song(String id, String name, String singer) {
    8. this.id = id;
    9. this.name = name;
    10. this.singer = singer;
    11. }
    12. public String getId() {
    13. return id;
    14. }
    15. public void setId(String id) {
    16. this.id = id;
    17. }
    18. public String getName() {
    19. return name;
    20. }
    21. public void setName(String name) {
    22. this.name = name;
    23. }
    24. public String getSinger() {
    25. return singer;
    26. }
    27. public void setSinger(String singer) {
    28. this.singer = singer;
    29. }
    30. @Override
    31. public String toString() {
    32. return "【歌曲信息】:" + "id为:" + id + ", 名称为:" + name + ", 演唱者为:" + singer;
    33. }
    34. @Override
    35. public boolean equals(Object o) {
    36. if (this == o) return true;
    37. if (!(o instanceof Song)) return false;
    38. Song song = (Song) o;
    39. return getId().equals(song.getId()) &&
    40. getName().equals(song.getName()) &&
    41. getSinger().equals(song.getSinger());
    42. }
    43. @Override
    44. public int hashCode() {
    45. return Objects.hash(getId(), getName(), getSinger());
    46. }
    47. }

    PlayList.java

    1. package com.player;
    2. import java.util.ArrayList;
    3. import java.util.List;
    4. public class PlayList {
    5. private String playListName;
    6. private List<Song> musicList;
    7. public PlayList(String playListName) {
    8. this.playListName = playListName;
    9. musicList = new ArrayList<Song>();
    10. }
    11. // 将歌曲添加都播放列表
    12. public void addToPlayList(Song song) {
    13. boolean isExist = false;
    14. for (Song dbSong : musicList) {
    15. if (dbSong.equals(song)) {
    16. isExist = true;
    17. break;
    18. }
    19. }
    20. if (!isExist) {
    21. musicList.add(song);
    22. } else {
    23. System.out.println(song.getName() + "已经存在于播放列表中,添加失败!");
    24. }
    25. }
    26. // 通过歌曲id查询
    27. public Song searchSongById(String id) {
    28. Song song = null;
    29. for (Song dbSong : musicList) {
    30. if (dbSong.getId().equals(id)) {
    31. song = dbSong;
    32. break;
    33. }
    34. }
    35. return song;
    36. }
    37. // 通过歌曲名称查询
    38. public Song searchSongByName(String name) {
    39. Song song = null;
    40. for (Song dbSong : musicList) {
    41. if (dbSong.getName().equals(name)) {
    42. song = dbSong;
    43. break;
    44. }
    45. }
    46. return song;
    47. }
    48. // 修改播放列表中的歌曲信息
    49. public void updateSong(String id, Song song) {
    50. Song dbSong = searchSongById(id);
    51. if (dbSong == null) {
    52. System.out.println("没有找到id为" + id + "对应的歌曲信息");
    53. } else {
    54. musicList.remove(dbSong);
    55. musicList.add(song);
    56. System.out.println("修改成功!");
    57. }
    58. }
    59. // 删除播放列表中的歌曲信息
    60. public void deleteSong(String id) {
    61. Song dbSong = searchSongById(id);
    62. if (dbSong == null) {
    63. System.out.println("没有找到id为" + id + "对应的歌曲信息");
    64. } else {
    65. musicList.remove(dbSong);
    66. System.out.println("删除成功!");
    67. }
    68. }
    69. // 显示播放列表中的所有数据
    70. public void displayAllSong() {
    71. System.out.println("播放列表中的所有歌曲为:");
    72. for (Song song : musicList) {
    73. System.out.println(song);
    74. }
    75. }
    76. public String getPlayListName() {
    77. return playListName;
    78. }
    79. public void setPlayListName(String playListName) {
    80. this.playListName = playListName;
    81. }
    82. public List<Song> getMusicList() {
    83. return musicList;
    84. }
    85. public void setMusicList(List<Song> musicList) {
    86. this.musicList = musicList;
    87. }
    88. }

    PlayListCollection.java

    1. package com.player;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. import java.util.Set;
    5. public class PlayListCollection {
    6. // 为什么这里用Map,是因为可以比较好的管理播放器
    7. // 试想一下,如果也像PlayList一样定义仅仅定义name和list属性,那么要在哪里管理这些name和list呢
    8. Map<String, PlayList> playListMap; // 存放播放列表的集合
    9. public PlayListCollection() {
    10. playListMap = new HashMap<String, PlayList>();
    11. }
    12. // 添加播放列表
    13. public void addPlayList(PlayList playList) {
    14. playListMap.put(playList.getPlayListName(), playList);
    15. }
    16. // 删除播放列表
    17. public void deletePlayList(PlayList playList) {
    18. playListMap.remove(playList.getPlayListName());
    19. System.out.println("删除成功!");
    20. }
    21. // 查询播放列表
    22. public PlayList searchPlayListByName(String playListName) {
    23. PlayList playList = null;
    24. Set<String> playListSet = playListMap.keySet();
    25. for (String key : playListSet) {
    26. if (key.equals(playListName)) {
    27. playList = playListMap.get(key);
    28. break;
    29. }
    30. }
    31. return playList;
    32. }
    33. // 显示所有播放列表的名称
    34. public void displayListName() {
    35. Set<String> playListSet = playListMap.keySet();
    36. System.out.println("播放列表名称为:");
    37. for (String key : playListSet) {
    38. System.out.println(key);
    39. }
    40. }
    41. public Map<String, PlayList> getPlayListMap() {
    42. return playListMap;
    43. }
    44. public void setPlayListMap(Map<String, PlayList> playListMap) {
    45. this.playListMap = playListMap;
    46. }
    47. }

    TestDemo.java

    1. package com.player;
    2. import java.util.Scanner;
    3. public class TestDemo {
    4. // 主菜单
    5. public void mainMenu() {
    6. System.out.println("=======================================");
    7. System.out.println(" 主菜单 ");
    8. System.out.println(" 1 -- 播放列表管理 ");
    9. System.out.println(" 2 -- 播放器管理 ");
    10. System.out.println(" 0 -- 退出 ");
    11. System.out.println("=======================================");
    12. }
    13. // 播放列表管理菜单
    14. public void playListMenu() {
    15. System.out.println("=======================================");
    16. System.out.println(" 播放列表管理 ");
    17. System.out.println(" 1 -- 将歌曲添加到主播放列表 ");
    18. System.out.println(" 2 -- 将歌曲添加到普通播放列表 ");
    19. System.out.println(" 3 -- 通过歌曲id查询播放列表中的歌曲 ");
    20. System.out.println(" 4 -- 通过歌曲名称查询播放列表中的歌曲 ");
    21. System.out.println(" 5 -- 修改播放列表中的歌曲 ");
    22. System.out.println(" 6 -- 删除播放列表中的歌曲 ");
    23. System.out.println(" 7 -- 显示播放列表中的所有歌曲 ");
    24. System.out.println(" 9 -- 返回上一级菜单 ");
    25. System.out.println("=======================================");
    26. }
    27. // 播放器菜单
    28. public void playerMenu() {
    29. System.out.println("=======================================");
    30. System.out.println(" 播放器管理 ");
    31. System.out.println(" 1 -- 向播放器添加播放列表 ");
    32. System.out.println(" 2 -- 从播放器删除播放列表 ");
    33. System.out.println(" 3 -- 通过名字查询播放列表信息 ");
    34. System.out.println(" 4 -- 显示所有播放列表名称 ");
    35. System.out.println(" 9 -- 返回上一级菜单 ");
    36. System.out.println("=======================================");
    37. }
    38. // 主流程
    39. public void test() {
    40. TestDemo testDemo = new TestDemo();
    41. Scanner scanner = new Scanner(System.in);
    42. int input = 0;
    43. // 创建一个播放列表容器
    44. PlayListCollection playListCollection = new PlayListCollection();
    45. // 创建主播放列表
    46. PlayList mainPlayList = new PlayList("主播放列表");
    47. // 将主播放列表添加到播放器
    48. playListCollection.addPlayList(mainPlayList);
    49. while (true) {
    50. testDemo.mainMenu();
    51. System.out.println("请输入对应数字进行操作:");
    52. try {
    53. input = scanner.nextInt();
    54. } catch (Exception e) {
    55. System.out.println("请输入数字!");
    56. scanner.next();
    57. continue;
    58. }
    59. // 退出主菜单
    60. if (input == 0) {
    61. break;
    62. }
    63. switch (input) {
    64. case 1: // 播放列表管理
    65. testDemo.playlistManage(playListCollection);
    66. break;
    67. case 2: // 播放器管理
    68. testDemo.playerManage(playListCollection);
    69. break;
    70. default:
    71. System.out.println("该数字没有对应的操作");
    72. break;
    73. }
    74. }
    75. }
    76. // 播放列表管理
    77. public void playlistManage(PlayListCollection playListCollection) {
    78. TestDemo testDemo = new TestDemo();
    79. PlayList mainPlayList = playListCollection.searchPlayListByName("主播放列表");
    80. Scanner scanner = new Scanner(System.in);
    81. int input = 0;
    82. while (true) {
    83. testDemo.playListMenu();
    84. System.out.println("请输入对应的数字对播放列表进行管理:");
    85. try {
    86. input = scanner.nextInt();
    87. } catch (Exception e) {
    88. System.out.println("请输入数字!");
    89. scanner.next();
    90. continue;
    91. }
    92. // 退出列表管理
    93. if (input == 9) {
    94. break;
    95. }
    96. switch (input) {
    97. case 1: {
    98. System.out.println("将歌曲添加到主播放列表");
    99. System.out.println("请输入要添加的歌曲的数量:");
    100. int count = scanner.nextInt();
    101. for (int i = 1; i <= count; i++) {
    102. System.out.println("请输入第" + i + "首歌曲");
    103. System.out.println("请输入歌曲的id:");
    104. String strId = scanner.next();
    105. System.out.println("请输入歌曲的名称:");
    106. String strName = scanner.next();
    107. System.out.println("请输入演唱者:");
    108. String strSinger = scanner.next();
    109. // 创建歌曲类的对象
    110. Song song = new Song(strId, strName, strSinger);
    111. mainPlayList.addToPlayList(song);
    112. }
    113. break;
    114. }
    115. case 2: {
    116. System.out.println("将歌曲添加到普通播放列表");
    117. System.out.println("请输入要添加的播放列表名称:");
    118. String sName = scanner.next();
    119. // 根据名称判断列表是否在播放器中存在
    120. PlayList commonPlayList = playListCollection.searchPlayListByName(sName);
    121. if (commonPlayList == null) {
    122. System.out.println("该播放列表不存在,请先将播放列表添加到播放器中");
    123. } else {
    124. System.out.println("请输入要添加的歌曲的数量");
    125. int count = scanner.nextInt();
    126. for (int i = 0; i < count; i++) {
    127. System.out.println("请输入第" + i + "首歌曲:");
    128. System.out.println("请输入歌曲id:");
    129. String strId = scanner.next();
    130. // 首先判断该id的歌曲是否在主播放列表存在
    131. Song song = mainPlayList.searchSongById(strId);
    132. if (song == null) {
    133. System.out.println("请输入歌曲名称:");
    134. String strName = scanner.next();
    135. System.out.println("请输入演唱者:");
    136. String strSinger = scanner.next();
    137. // 创建一个Song类的对象
    138. song = new Song(strId, strName, strSinger);
    139. // 分别将歌曲添加到普通播放列表和主播放列表
    140. mainPlayList.addToPlayList(song);
    141. commonPlayList.addToPlayList(song);
    142. } else {
    143. commonPlayList.addToPlayList(song);
    144. }
    145. }
    146. System.out.println("主播放列表");
    147. mainPlayList.displayAllSong();
    148. System.out.println("普通播放列表");
    149. commonPlayList.displayAllSong();
    150. }
    151. break;
    152. }
    153. case 3: {
    154. System.out.println("通过歌曲id查询播放列表中的歌曲");
    155. System.out.println("请输入要查询的播放列表名称");
    156. String sName = scanner.next();
    157. // 查询播放列表是否存在
    158. PlayList playList = playListCollection.searchPlayListByName(sName);
    159. if (playList == null) {
    160. System.out.println("该播放列表不存在!");
    161. } else {
    162. System.out.println("请输入要查询的歌曲id:");
    163. String strId = scanner.next();
    164. Song song = playList.searchSongById(strId);
    165. if (song == null) {
    166. System.out.println("该歌曲在播放列表" + sName + "中不存在!");
    167. } else {
    168. System.out.println(song);
    169. }
    170. }
    171. break;
    172. }
    173. case 4: {
    174. System.out.println("通过歌曲名称查询播放列表中的歌曲");
    175. System.out.println("请输入要查询的播放列表名称");
    176. String sName = scanner.next();
    177. // 查询播放列表是否存在
    178. PlayList playList = playListCollection.searchPlayListByName(sName);
    179. if (playList == null) {
    180. System.out.println("该播放列表不存在!");
    181. } else {
    182. System.out.println("请输入要查询的歌曲名称:");
    183. String strName = scanner.next();
    184. Song song = playList.searchSongByName(strName);
    185. if (song == null) {
    186. System.out.println("该歌曲在播放列表" + sName + "中不存在!");
    187. } else {
    188. System.out.println(song);
    189. }
    190. }
    191. break;
    192. }
    193. case 5: {
    194. System.out.println("修改播放列表中的歌曲");
    195. break;
    196. }
    197. case 6:
    198. System.out.println("删除播放列表中的歌曲");
    199. break;
    200. case 7: {
    201. System.out.println("显示播放列表中的所有歌曲");
    202. System.out.println("请输入要显示的播放列表名称");
    203. String sName = scanner.next();
    204. // 查询播放列表是否存在
    205. PlayList playList = playListCollection.searchPlayListByName(sName);
    206. if (playList == null) {
    207. System.out.println("该播放列表不存在!");
    208. } else {
    209. playList.displayAllSong();
    210. }
    211. break;
    212. }
    213. default:
    214. System.out.println("该数字没有对应的操作");
    215. break;
    216. }
    217. }
    218. }
    219. // 播放器管理
    220. public void playerManage(PlayListCollection playListCollection) {
    221. TestDemo testDemo = new TestDemo();
    222. Scanner scanner = new Scanner(System.in);
    223. int input = 0;
    224. while (true) {
    225. testDemo.playerMenu();
    226. System.out.println("请输入对应的数字对播放器进行管理:");
    227. try {
    228. input = scanner.nextInt();
    229. } catch (Exception e) {
    230. System.out.println("请输入数字!");
    231. scanner.next();
    232. continue;
    233. }
    234. // 退出列表管理
    235. if (input == 9) {
    236. break;
    237. }
    238. switch (input) {
    239. case 1: {
    240. System.out.println("向播放器添加播放列表");
    241. System.out.println("输入要添加的播放列表名称:");
    242. String playerName = scanner.next();
    243. // 创建一个新的播放列表对象
    244. PlayList commonPlayList = new PlayList(playerName);
    245. // 将播放列表添加到播放器Map
    246. playListCollection.addPlayList(commonPlayList);
    247. break;
    248. }
    249. case 2: {
    250. System.out.println("从播放器删除播放列表");
    251. System.out.println("请输入要删除的播放列表名称:");
    252. String playListName = scanner.next();
    253. if (playListName.equals("主播放列表")) {
    254. System.out.println("主播放列表不能删除");
    255. break;
    256. }
    257. // 查询播放列表是否存在
    258. PlayList playList = playListCollection.searchPlayListByName(playListName);
    259. if (playList == null) {
    260. System.out.println("该播放列表不存在!");
    261. } else {
    262. playListCollection.deletePlayList(playList);
    263. }
    264. break;
    265. }
    266. case 3: {
    267. System.out.println("通过名字查询播放列表信息");
    268. System.out.println("请输入要查询的播放列表名称:");
    269. String playListName = scanner.next();
    270. PlayList playList = playListCollection.searchPlayListByName(playListName);
    271. if (playList == null) {
    272. System.out.println("该播放列表不存在");
    273. } else {
    274. // 显示该播放盒列表名称及其所有的歌曲
    275. playList.displayAllSong();
    276. }
    277. break;
    278. }
    279. case 4: {
    280. System.out.println("显示所有播放列表名称");
    281. playListCollection.displayListName();
    282. break;
    283. }
    284. default:
    285. System.out.println("该数字没有对应的操作");
    286. break;
    287. }
    288. }
    289. }
    290. public static void main(String[] args) {
    291. TestDemo td = new TestDemo();
    292. // td.testSong();
    293. // td.testPlayList();
    294. // td.testPlayListCollection();
    295. td.test();
    296. }
    297. }