详细设计
歌曲类
播放列表类
播放器类
实例代码如下
Song类
package com.entity;
import java.util.Objects;
public class Song {
private String id;
private String name;
private String singer;
@Override
public String toString() {
return "Song{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", singer='" + 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 boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Song song = (Song) o;
return Objects.equals(id, song.id) &&
Objects.equals(name, song.name) &&
Objects.equals(singer, song.singer);
}
@Override
public int hashCode() {
return Objects.hash(id, name, singer);
}
}
歌单列表类
package com.entity;
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 Song searchSongById(String id){
Song song=null;
for (Song song1 : musicList) {
if(song1.getId().equals(id)){
song = song1;break;
}
}
return song;
}
public Song searchSongByName(String name){
Song song=null;
for (Song song1 : musicList) {
if(song1.getName().equals(name)){
song = song1;break;
}
}
return song;
}
public void addToPlayList(Song song){
boolean flag=false;
for(Song song1:musicList){
if(song1.equals(song)){
flag=true;
break;
}
}
if(flag){
System.out.println("歌单中已经存在该歌曲,存储失败!");
}else{
musicList.add(song);
}
}
public void updateSong(String id,Song song){
Song song1 = searchSongById(id);
if(song1!=null){
musicList.remove(song1);
musicList.add(song);
System.out.println("更新成功!");
}else{
System.out.println("未找到该歌曲");
}
}
public void deleteSong(String id){
Song song1 = searchSongById(id);
if(song1!=null){
musicList.remove(song1);
}else{
System.out.println("列表中没有这首歌,删除失败!");
}
}
public void displayAllSong(){
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;
}
}
歌单集合类
package com.entity;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class PlayListCollection {
private Map<String,PlayList> PlayListMap;
public Map<String, PlayList> getPlayListMap() {
return PlayListMap;
}
public void setPlayListMap(Map<String, PlayList> playListMap) {
PlayListMap = playListMap;
}
public PlayListCollection(){
PlayListMap = new HashMap<String,PlayList>();
}
public void addPlayList(PlayList playList){
PlayListMap.put(playList.getPlayListName(),playList);
}
public PlayList searchPlayListByName(String playListName){
PlayList playList=null;
Set<String> set = PlayListMap.keySet();
for (String s : set) {
if(s.equals(playListName)){
playList=PlayListMap.get(s);break;
}
}
return playList;
}
public void displayListName(){
PlayList playList=null;
Set<String> strings = PlayListMap.keySet();
for (String string : strings) {
playList=PlayListMap.get(string);
System.out.println(playList.getPlayListName());
}
}
public void deletePlayList(PlayList playList){
PlayListMap.remove(playList.getPlayListName());
}
}
主函数和测试类
package com.test;
import com.entity.PlayList;
import com.entity.PlayListCollection;
import com.entity.Song;
import java.util.Scanner;
public class TestDemo {
public void SongTest(){
Song s1 = new Song("s001", "十七岁", "刘德华");
Song s2 = new Song("s002", "七里香", "周杰伦");
Song s3 = new Song("s003", "江南", "林俊杰");
Song s4 = new Song("s004", "春天里", "汪峰 ");
System.out.println(s1);
System.out.println(s1.equals(s2));
System.out.println(s3.equals(s4));
}
public void PlayListTest(){
Song s1 = new Song("s001", "十七岁", "刘德华");
Song s2 = new Song("s002", "七里香", "周杰伦");
Song s3 = new Song("s003", "江南", "林俊杰");
Song s4 = new Song("s004", "春天里", "汪峰 ");
PlayList mainPlayList = new PlayList("歌曲主菜单");
mainPlayList.addToPlayList(s1);
mainPlayList.addToPlayList(s2);
mainPlayList.addToPlayList(s3);
mainPlayList.addToPlayList(s3);
mainPlayList.displayAllSong();
Song song=null;
Song songNew = new Song("s006","怒放的生命","小汪");
mainPlayList.updateSong("s002",songNew);
mainPlayList.displayAllSong();
mainPlayList.deleteSong("s001");
System.out.println("删除后歌单中的歌曲");
mainPlayList.displayAllSong();
}
public void PlayListCollectionTest(){
Song s1 = new Song("s001", "十七岁", "刘德华");
Song s2 = new Song("s002", "七里香", "周杰伦");
Song s3 = new Song("s003", "江南", "林俊杰");
PlayList mainPlayList = new PlayList("主播放列表");
mainPlayList.addToPlayList(s1);
mainPlayList.addToPlayList(s2);
mainPlayList.addToPlayList(s3);
PlayList favoritePlayList = new PlayList("最喜爱的音乐");
favoritePlayList.addToPlayList(mainPlayList.getMusicList().get(0));
favoritePlayList.addToPlayList(mainPlayList.getMusicList().get(1));
favoritePlayList.displayAllSong();
PlayListCollection playListCollection = new PlayListCollection();
playListCollection.addPlayList(mainPlayList);
playListCollection.addPlayList(favoritePlayList);
playListCollection.displayListName();
System.out.println("-----实现通过搜索获取歌单的歌曲--------");
PlayList list1 = playListCollection.searchPlayListByName("主播放列表");
list1.displayAllSong();
System.out.println("原来有歌单为:");
playListCollection.displayListName();
System.out.println("-----实现删除歌单功能,并进行打印--------");
playListCollection.deletePlayList(mainPlayList);
playListCollection.displayListName();
}
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 td = new TestDemo();
Scanner sc = new Scanner(System.in);
int input=0,input1=0,input2=0;
PlayListCollection playListCollection = new PlayListCollection();
PlayList mainPlayList = new PlayList("主播放歌单");
playListCollection.addPlayList(mainPlayList);
PlayList favoritePlayList = null;
while(true){
td.mainMenu();
System.out.println("请输入对应数字进行操作");
input=sc.nextInt();
if(input==0){
System.out.println(" **退出系统**");
break;
}
switch (input){
case 1:
while (true){
playListMenu();
System.out.println("请输入对应数字进行操作");
input1=sc.nextInt();
if (input1==9){
break;
}
switch (input1){
case 1:
System.out.println("请输入要添加歌曲的数量");
int count=sc.nextInt();
for (int i = 0; i <count ; i++) {
System.out.println("请输入歌曲的id");
String strId = sc.next();
System.out.println("请输入歌曲的名字");
String strName=sc.next();
System.out.println("请输入作者的名字");
String strWriter = sc.next();
mainPlayList.addToPlayList(new Song(strId,strName,strWriter));
mainPlayList.displayAllSong();
}
break;
case 2:
System.out.println("请输入要添加歌曲到的播放列表名字:");
String strPlayListCollection = sc.next();
favoritePlayList = playListCollection.searchPlayListByName(strPlayListCollection);
if(favoritePlayList==null){
System.out.println("该播放列表不存在,请先将播放列表添加到播放器中");
}else{
System.out.println("请输入要添加的歌曲数量:");
int count2=sc.nextInt();
for (int i=1;i<=count2;i++){
System.out.println("请输入第"+i+"首歌");
System.out.println("请输入id");
String strId =sc.next();
Song song = mainPlayList.searchSongById(strId);
if(song==null){
System.out.println("该歌曲在主播放列表中不存在,请输入歌曲其他信息");
System.out.println("请输入歌曲名称:");
String strName=sc.next();
System.out.println("请输入歌曲作者");
String songer = sc.next();
song = new Song(strId, strName, songer);
favoritePlayList.addToPlayList(song);
mainPlayList.addToPlayList(song);
}else{
favoritePlayList.addToPlayList(song);
}
}
System.out.println("主播放列表");
mainPlayList.displayAllSong();
System.out.println("普通播放列表");
favoritePlayList.displayAllSong();
}
break;
case 3:
System.out.println("请输入要查询的播放列表名称");
String strPlayListName =sc.next();
PlayList pl = playListCollection.searchPlayListByName(strPlayListName);
if(pl==null){
System.out.println("该播放列表不存在");break;
}else{
System.out.println("请输入要查询歌曲id");
String strId =sc.next();
Song song = pl.searchSongById(strId);
System.out.println("通过id查找到歌曲为:");
System.out.println(song);
}
break;
case 4:
System.out.println("请输入要查询的播放列表名称");
String strPlayListName2 =sc.next();
PlayList pl2 = playListCollection.searchPlayListByName(strPlayListName2);
if(pl2==null){
System.out.println("该播放列表不存在");break;
}else{
System.out.println("请输入歌曲名称");
String strName =sc.next();
Song song1 = mainPlayList.searchSongByName(strName);
System.out.println("通过歌曲名字查找到歌曲为:");
System.out.println(song1);
}
break;
case 5:
System.out.println("请输入要修改的播放歌曲列表");
String strPlayListName3 =sc.next();
PlayList pl3 = playListCollection.searchPlayListByName(strPlayListName3);
if(pl3==null){
System.out.println("该播放列表不存在");
}else{
System.out.println("请输入要修改播放歌曲的id");
String strId = sc.next();
System.out.println("请输入修改后的歌曲信息");
System.out.println("请输入ID");
String strId2 =sc.next();
System.out.println("请输入歌曲名称:");
String strName=sc.next();
System.out.println("请输入歌曲作者");
String songer = sc.next();
Song song = new Song(strId2, strName, songer);
pl3.updateSong(strId,song);
}
break;
case 6:
System.out.println("请输入要删除歌曲所在的播放歌曲列表");
String strPlayListName4 =sc.next();
PlayList pl4 = playListCollection.searchPlayListByName(strPlayListName4);
if(pl4==null){
System.out.println("该播放列表不存在");
}else{
System.out.println("请输入要删除歌曲的id");
String strId =sc.next();
pl4.deleteSong(strId);
}
case 7:mainPlayList.displayAllSong();break;
}
}
break;
case 2:
while (true){
td.playerMenu();
System.out.println("请输入数字进行对应操作");
input2=sc.nextInt();
if (input2==9){
break;
}
switch (input2){
case 1:
System.out.println("请输入要添加的播放列表的名称");
String strName=sc.next();
favoritePlayList = new PlayList(strName);
playListCollection.addPlayList(favoritePlayList);
System.out.println("添加成功");
break;
case 2:
System.out.println("请输入要删除的播放列表名称");
String strName2=sc.next();
PlayList playList = playListCollection.searchPlayListByName(strName2);
if(strName2=="主播放器列表"){
System.out.println("主播放器列表不能删除,请重新选择");
}else{
if(playList==null){
System.out.println("未找到该歌曲");
}else{
playListCollection.deletePlayList(playList);
System.out.println("删除成功");
}
}
break;
case 3:
System.out.println("请输入要查询列表名称");
String strName3 =sc.next();
PlayList playList1 = playListCollection.searchPlayListByName(strName3);
if(playList1==null){
System.out.println("未找到该歌曲列表");
}else{
System.out.println("该播放列表中的歌曲有");
playList1.displayAllSong();
}
break;
case 4:playListCollection.displayListName();
}
}
break;
default:
System.out.println("该数字没有对应的操作");
break;
}
}
}
public static void main(String[] args) {
TestDemo testDemo = new TestDemo();
testDemo.test();
}
}
注意: 这里需要注意 在使用列表类构造方法时,可以直接通过构造方法实现里面集合的创建
public PlayListCollection(){
PlayListMap = new HashMap<String,PlayList>();
}
|