Song.java
package com.player;
import java.util.Objects;
public class Song {
private String id;
private String name;
private String singer;
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;
}
@Override
public String toString() {
return "【歌曲信息】:" + "id为:" + id + ", 名称为:" + name + ", 演唱者为:" + singer;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Song)) return false;
Song song = (Song) o;
return getId().equals(song.getId()) &&
getName().equals(song.getName()) &&
getSinger().equals(song.getSinger());
}
@Override
public int hashCode() {
return Objects.hash(getId(), getName(), getSinger());
}
}
PlayList.java
package com.player;
import java.util.ArrayList;
import java.util.List;
public class PlayList {
private String playListName;
private List<Song> musicList;
public PlayList(String playListName) {
this.playListName = playListName;
musicList = new ArrayList<Song>();
}
// 将歌曲添加都播放列表
public void addToPlayList(Song song) {
boolean isExist = false;
for (Song dbSong : musicList) {
if (dbSong.equals(song)) {
isExist = true;
break;
}
}
if (!isExist) {
musicList.add(song);
} else {
System.out.println(song.getName() + "已经存在于播放列表中,添加失败!");
}
}
// 通过歌曲id查询
public Song searchSongById(String id) {
Song song = null;
for (Song dbSong : musicList) {
if (dbSong.getId().equals(id)) {
song = dbSong;
break;
}
}
return song;
}
// 通过歌曲名称查询
public Song searchSongByName(String name) {
Song song = null;
for (Song dbSong : musicList) {
if (dbSong.getName().equals(name)) {
song = dbSong;
break;
}
}
return song;
}
// 修改播放列表中的歌曲信息
public void updateSong(String id, Song song) {
Song dbSong = searchSongById(id);
if (dbSong == null) {
System.out.println("没有找到id为" + id + "对应的歌曲信息");
} else {
musicList.remove(dbSong);
musicList.add(song);
System.out.println("修改成功!");
}
}
// 删除播放列表中的歌曲信息
public void deleteSong(String id) {
Song dbSong = searchSongById(id);
if (dbSong == null) {
System.out.println("没有找到id为" + id + "对应的歌曲信息");
} else {
musicList.remove(dbSong);
System.out.println("删除成功!");
}
}
// 显示播放列表中的所有数据
public void displayAllSong() {
System.out.println("播放列表中的所有歌曲为:");
for (Song song : musicList) {
System.out.println(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;
}
}
PlayListCollection.java
package com.player;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class PlayListCollection {
// 为什么这里用Map,是因为可以比较好的管理播放器
// 试想一下,如果也像PlayList一样定义仅仅定义name和list属性,那么要在哪里管理这些name和list呢
Map<String, PlayList> playListMap; // 存放播放列表的集合
public PlayListCollection() {
playListMap = new HashMap<String, PlayList>();
}
// 添加播放列表
public void addPlayList(PlayList playList) {
playListMap.put(playList.getPlayListName(), playList);
}
// 删除播放列表
public void deletePlayList(PlayList playList) {
playListMap.remove(playList.getPlayListName());
System.out.println("删除成功!");
}
// 查询播放列表
public PlayList searchPlayListByName(String playListName) {
PlayList playList = null;
Set<String> playListSet = playListMap.keySet();
for (String key : playListSet) {
if (key.equals(playListName)) {
playList = playListMap.get(key);
break;
}
}
return playList;
}
// 显示所有播放列表的名称
public void displayListName() {
Set<String> playListSet = playListMap.keySet();
System.out.println("播放列表名称为:");
for (String key : playListSet) {
System.out.println(key);
}
}
public Map<String, PlayList> getPlayListMap() {
return playListMap;
}
public void setPlayListMap(Map<String, PlayList> playListMap) {
this.playListMap = playListMap;
}
}
TestDemo.java
package com.player;
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("=======================================");
}
// 播放列表管理菜单
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(" 9 -- 返回上一级菜单 ");
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("=======================================");
}
// 主流程
public void test() {
TestDemo testDemo = new TestDemo();
Scanner scanner = new Scanner(System.in);
int input = 0;
// 创建一个播放列表容器
PlayListCollection playListCollection = new PlayListCollection();
// 创建主播放列表
PlayList mainPlayList = new PlayList("主播放列表");
// 将主播放列表添加到播放器
playListCollection.addPlayList(mainPlayList);
while (true) {
testDemo.mainMenu();
System.out.println("请输入对应数字进行操作:");
try {
input = scanner.nextInt();
} catch (Exception e) {
System.out.println("请输入数字!");
scanner.next();
continue;
}
// 退出主菜单
if (input == 0) {
break;
}
switch (input) {
case 1: // 播放列表管理
testDemo.playlistManage(playListCollection);
break;
case 2: // 播放器管理
testDemo.playerManage(playListCollection);
break;
default:
System.out.println("该数字没有对应的操作");
break;
}
}
}
// 播放列表管理
public void playlistManage(PlayListCollection playListCollection) {
TestDemo testDemo = new TestDemo();
PlayList mainPlayList = playListCollection.searchPlayListByName("主播放列表");
Scanner scanner = new Scanner(System.in);
int input = 0;
while (true) {
testDemo.playListMenu();
System.out.println("请输入对应的数字对播放列表进行管理:");
try {
input = scanner.nextInt();
} catch (Exception e) {
System.out.println("请输入数字!");
scanner.next();
continue;
}
// 退出列表管理
if (input == 9) {
break;
}
switch (input) {
case 1: {
System.out.println("将歌曲添加到主播放列表");
System.out.println("请输入要添加的歌曲的数量:");
int count = scanner.nextInt();
for (int i = 1; i <= count; i++) {
System.out.println("请输入第" + i + "首歌曲");
System.out.println("请输入歌曲的id:");
String strId = scanner.next();
System.out.println("请输入歌曲的名称:");
String strName = scanner.next();
System.out.println("请输入演唱者:");
String strSinger = scanner.next();
// 创建歌曲类的对象
Song song = new Song(strId, strName, strSinger);
mainPlayList.addToPlayList(song);
}
break;
}
case 2: {
System.out.println("将歌曲添加到普通播放列表");
System.out.println("请输入要添加的播放列表名称:");
String sName = scanner.next();
// 根据名称判断列表是否在播放器中存在
PlayList commonPlayList = playListCollection.searchPlayListByName(sName);
if (commonPlayList == null) {
System.out.println("该播放列表不存在,请先将播放列表添加到播放器中");
} else {
System.out.println("请输入要添加的歌曲的数量");
int count = scanner.nextInt();
for (int i = 0; i < count; i++) {
System.out.println("请输入第" + i + "首歌曲:");
System.out.println("请输入歌曲id:");
String strId = scanner.next();
// 首先判断该id的歌曲是否在主播放列表存在
Song song = mainPlayList.searchSongById(strId);
if (song == null) {
System.out.println("请输入歌曲名称:");
String strName = scanner.next();
System.out.println("请输入演唱者:");
String strSinger = scanner.next();
// 创建一个Song类的对象
song = new Song(strId, strName, strSinger);
// 分别将歌曲添加到普通播放列表和主播放列表
mainPlayList.addToPlayList(song);
commonPlayList.addToPlayList(song);
} else {
commonPlayList.addToPlayList(song);
}
}
System.out.println("主播放列表");
mainPlayList.displayAllSong();
System.out.println("普通播放列表");
commonPlayList.displayAllSong();
}
break;
}
case 3: {
System.out.println("通过歌曲id查询播放列表中的歌曲");
System.out.println("请输入要查询的播放列表名称");
String sName = scanner.next();
// 查询播放列表是否存在
PlayList playList = playListCollection.searchPlayListByName(sName);
if (playList == null) {
System.out.println("该播放列表不存在!");
} else {
System.out.println("请输入要查询的歌曲id:");
String strId = scanner.next();
Song song = playList.searchSongById(strId);
if (song == null) {
System.out.println("该歌曲在播放列表" + sName + "中不存在!");
} else {
System.out.println(song);
}
}
break;
}
case 4: {
System.out.println("通过歌曲名称查询播放列表中的歌曲");
System.out.println("请输入要查询的播放列表名称");
String sName = scanner.next();
// 查询播放列表是否存在
PlayList playList = playListCollection.searchPlayListByName(sName);
if (playList == null) {
System.out.println("该播放列表不存在!");
} else {
System.out.println("请输入要查询的歌曲名称:");
String strName = scanner.next();
Song song = playList.searchSongByName(strName);
if (song == null) {
System.out.println("该歌曲在播放列表" + sName + "中不存在!");
} else {
System.out.println(song);
}
}
break;
}
case 5: {
System.out.println("修改播放列表中的歌曲");
break;
}
case 6:
System.out.println("删除播放列表中的歌曲");
break;
case 7: {
System.out.println("显示播放列表中的所有歌曲");
System.out.println("请输入要显示的播放列表名称");
String sName = scanner.next();
// 查询播放列表是否存在
PlayList playList = playListCollection.searchPlayListByName(sName);
if (playList == null) {
System.out.println("该播放列表不存在!");
} else {
playList.displayAllSong();
}
break;
}
default:
System.out.println("该数字没有对应的操作");
break;
}
}
}
// 播放器管理
public void playerManage(PlayListCollection playListCollection) {
TestDemo testDemo = new TestDemo();
Scanner scanner = new Scanner(System.in);
int input = 0;
while (true) {
testDemo.playerMenu();
System.out.println("请输入对应的数字对播放器进行管理:");
try {
input = scanner.nextInt();
} catch (Exception e) {
System.out.println("请输入数字!");
scanner.next();
continue;
}
// 退出列表管理
if (input == 9) {
break;
}
switch (input) {
case 1: {
System.out.println("向播放器添加播放列表");
System.out.println("输入要添加的播放列表名称:");
String playerName = scanner.next();
// 创建一个新的播放列表对象
PlayList commonPlayList = new PlayList(playerName);
// 将播放列表添加到播放器Map
playListCollection.addPlayList(commonPlayList);
break;
}
case 2: {
System.out.println("从播放器删除播放列表");
System.out.println("请输入要删除的播放列表名称:");
String playListName = scanner.next();
if (playListName.equals("主播放列表")) {
System.out.println("主播放列表不能删除");
break;
}
// 查询播放列表是否存在
PlayList playList = playListCollection.searchPlayListByName(playListName);
if (playList == null) {
System.out.println("该播放列表不存在!");
} else {
playListCollection.deletePlayList(playList);
}
break;
}
case 3: {
System.out.println("通过名字查询播放列表信息");
System.out.println("请输入要查询的播放列表名称:");
String playListName = scanner.next();
PlayList playList = playListCollection.searchPlayListByName(playListName);
if (playList == null) {
System.out.println("该播放列表不存在");
} else {
// 显示该播放盒列表名称及其所有的歌曲
playList.displayAllSong();
}
break;
}
case 4: {
System.out.println("显示所有播放列表名称");
playListCollection.displayListName();
break;
}
default:
System.out.println("该数字没有对应的操作");
break;
}
}
}
public static void main(String[] args) {
TestDemo td = new TestDemo();
// td.testSong();
// td.testPlayList();
// td.testPlayListCollection();
td.test();
}
}