package com.dodoke.player;
import java.util.Objects;
public class Song implements Comparable<Song>{
private String id;
private String name;
private String singer;
public Song() {
super();
}
public Song(String id, String name, String singer) {
super();
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 + '\'' +
", 歌曲名'" + name + '\'' +
", 演唱者'" + singer + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if(o == null){
return false;
}
if(this == o){
return true;
}
if(o.getClass() == Song.class) {
Song song = (Song)o;
return (song.getId().equals(this.id)) && (song.getName().equals(this.name)) && (song.getSinger().equals(this.singer));
}
return false;
}
@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 int compareTo(Song o) {
String name1 =o.getName();
int n =this.name.compareTo(name1);
return n;
}
}
package com.dodoke.player;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PlayList {
private String playListName;
private List<Song> musicList = new ArrayList<Song>();
public PlayList(String playListName) {
super();
this.playListName = playListName;
}
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){
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 displayAllSong(){
System.out.println("播放列表中所有歌曲為");
for(Song song:musicList){
System.out.println(song);
}
}
public Song searchSongById(String id){
Song song = null;
for(Song song1:musicList){
String id1 =song1.getId();
if (id1.equals(id)){
song =song1;
break;
}
}
return song;
}
public Song searchSongByName(String name){
Song song = null;
for(Song song1:musicList){
String name1 =song1.getName();
if (name1.equals(name)){
song =song1;
break;
}
}
return song;
}
public void upadteSong(String id,Song song){
Song song1 = searchSongById(id);
if(song1 == null){
System.out.println("没有找到歌曲编号为:"+id+"的歌曲信息!");
}else{
musicList.remove(song1);
musicList.add(song);
System.out.println("歌曲修改成功");
}
}
public void deleteSong(String id){
Song song =searchSongById(id);
if (song == null){
System.out.println("没有找到歌曲编号为"+id+"的歌曲信息");
}else{
musicList.remove(song);
System.out.println("删除成功");
}
}
public void sortBySongName(){
Collections.sort(musicList);
}
}
package com.dodoke.player;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Player {
public static final String PLAYERNAME = "java-125专用播放器";
Map<String,PlayList> map = new HashMap<String,PlayList>();
private static Player player= new Player();
public static Player getInstance() {
return player;
}
private Player(){
}
public Map<String,PlayList> getMap(){
return map;
}
public void setMap(Map<String,PlayList>map){
this.map = map;
}
public void addPlayList(PlayList pl){
map.put(pl.getPlayListName(),pl);
}
public void deletePlayList(PlayList pl){
map.remove(pl.getPlayListName());
System.out.println("删除成功");
}
public PlayList searchPlayListByName(String playListName){
PlayList pl = null;
Set<String> plKeySet = map.keySet();
for(String s: plKeySet){
if(s.equals(playListName)){
pl = map.get(s);
break;
}
}
return pl;
}
public void displayListName() {
Set<String> plKeySet = map.keySet();
System.out.println("播放列表名称为:");
for(String s : plKeySet) {
System.out.println(s);
}
}
}
package com.dodoke.player;
public class PlayListCollection{
public static void main(String[] args) {
PlayerSetvice play = new PlayerSetvice();
play.player();
}
}
package com.dodoke.player;
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.testSong();
}
public void testSong(){
Song song1 = new Song("s001","两只老虎1","儿童合唱团1");
Song song2 = new Song("s002","两只老虎2","儿童合唱团2");
Song song3 = new Song("s003","两只老虎3","儿童合唱团3");
Song song4 = new Song("s001","两只老虎1","儿童合唱团1");
System.out.println(song2.toString());
System.out.println(song1.equals(song3));
System.out.println(song1.equals(song4));
}
public void testPlayList(){
Song song1 = new Song("s001","两只老虎1","儿童合唱团1");
Song song2 = new Song("s002","两只老虎2","儿童合唱团2");
Song song3 = new Song("s003","两只老虎3","儿童合唱团3");
Song song4 = new Song("s001","两只老虎1","儿童合唱团1");
PlayList pl = new PlayList("主播放列表");
pl.addToPlayList(song1);
pl.addToPlayList(song2);
pl.addToPlayList(song3);
pl.addToPlayList(song4);
pl.displayAllSong();
System.out.println("s003");
System.out.println("突然的自我");
}
public void testPlay(){
Player p1 = Player.getInstance();
Player p2 = Player.getInstance();
System.out.println(p1 == p2);
Song song1 = new Song("s001","两只老虎1","儿童合唱团1");
Song song2 = new Song("s002","两只老虎2","儿童合唱团2");
Song song3 = new Song("s003","两只老虎3","儿童合唱团3");
PlayList pl = new PlayList("主播放列表");
PlayList pl2 = new PlayList("流行音乐");
p1.addPlayList(pl);
p1.addPlayList(pl2);
pl2.addToPlayList(song1);
pl.displayAllSong();
}
}
package com.dodoke.player;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class PlayerSetvice {
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 player() {
PlayerSetvice plService = new PlayerSetvice();
Scanner sc = new Scanner(System.in);
int input = 0, input1 = 0, input2 = 0;
Player player = Player.getInstance();
PlayList mPlayList = new PlayList("主播放列表");
player.addPlayList(mPlayList);
PlayList pList = null;
while(true) {
plService.mainMenu();
System.out.println("请输入对应的数字进行操作:");
input = sc.nextInt();
if(input == 0) {
System.out.println("退出程序!");
break;
}
switch(input) {
case 1: {
while(true) {
plService.playListMenu();
System.out.println("请输入对应的数字进行播放列表管理操作:");
input1 = sc.nextInt();
if(input1 == 9) {
break;
}
switch(input1) {
case 1: {
System.out.println("将歌曲添加到主播放列表!");
System.out.println("请输入要添加的歌曲数量:");
int count = sc.nextInt();
for(int i = 1; i <= count; i++) {
System.out.println("请输入第" + i + "首歌曲");
System.out.println("请输入歌曲id");
String id = sc.next();
System.out.println("请输入歌曲名称");
String name = sc.next();
System.out.println("请输入歌曲演唱者");
String singer = sc.next();
Song song = new Song(id, name, singer);
mPlayList.addToPlayList(song);
}
System.out.println("歌曲添加完毕!");
break;
}
case 2: {
System.out.println("将歌曲添加到普通播放列表!");
System.out.println("请输入要添加的播放列表名称:");
String playListName = sc.next();
pList = player.searchPlayListByName(playListName);
if(pList == null) {
System.out.println("该播放列表不存在,请先将播放列表添加到播放器中!");
} else {
System.out.println("请输入要添加的歌曲数量:");
int count = sc.nextInt();
for(int i = 1; i <= count; i++) {
System.out.println("请输入第" + i + "首歌曲");
System.out.println("请输入歌曲id");
String id = sc.next();
Song songs = mPlayList.searchSongById(id);
if(songs == null) {
System.out.println("该歌曲在主播放列表不存在,继续输入歌曲的其他信息!");
System.out.println("请输入歌曲名称");
String name = sc.next();
System.out.println("请输入歌曲演唱者");
String singer = sc.next();
Song song = new Song(id, name, singer);
pList.addToPlayList(song);
mPlayList.addToPlayList(song);
} else {
pList.addToPlayList(songs);
System.out.println("主播放列表中已有该歌曲,已将其添加到名为:" + playListName + "的歌单中!");
}
}
System.out.println("歌曲添加完毕!");
}
break;
}
case 3: {
System.out.println("通过歌曲id查询到播放列表中的歌曲");
System.out.println("请输入要查询的播放列表名称:");
String playListName = sc.next();
pList = player.searchPlayListByName(playListName);
if(pList == null) {
System.out.println("该播放列表不存在,请先将播放列表添加到播放器中!");
} else {
System.out.println("请输入要查询的歌曲id:");
String id = sc.next();
Song song = pList.searchSongById(id);
if(song == null) {
System.out.println("该歌曲在播放列表" + playListName + "中不存在!");
} else {
System.out.println("歌曲信息为:");
System.out.println(song);
}
}
break;
}
case 4: {
System.out.println("通过歌曲名称查询播放列表中的歌曲");
System.out.println("请输入要查询的播放列表名称:");
String playListName = sc.next();
pList = player.searchPlayListByName(playListName);
if(pList==null) {
System.out.println("该播放列表不存在");
break;
}else {
System.out.println("请输入要歌曲的名称:");
String name=sc.next();
Song song=pList.searchSongByName(name);
if(song==null) {
System.out.println("该歌曲在播放列表: "+playListName+"中不存在");
break;
}else {
System.out.println("该歌曲在播放列表: "+playListName+"存在");
System.out.println("歌曲的信息为:");
System.out.println(song);
}
}
break;
}
case 5: {
System.out.println("修改播放列表中的歌曲");
System.out.println("请输入要修改的歌曲");
String name = sc.next();
Song song=pList.searchSongByName(name);
if(song==null) {
System.out.println("该歌曲不存在");
break;
}else {
System.out.println("请输入要修改的歌曲id");
String id = sc.next();
Song song1=pList.searchSongById(id);
if(id==null) {
System.out.println("要修改的歌曲id不存在:");
break;
}else {
System.out.println("请输入修改之后的歌曲名称:");
String singer = sc.next();
System.out.println("请输入修改之后的歌曲演奏者:");
Song song2=new Song(id,singer,name);
pList.updateSong(id, song2);
System.out.println("修改之后的歌曲信息为:");
System.out.println(song2);
}
}
break;
}
case 6: {
System.out.println("删除播放列表中的歌曲");
System.out.println("请输入要删除的歌曲");
String name = sc.next();
Song song=pList.searchSongByName(name);
if(song==null) {
System.out.println("该歌曲不存在");
}else {
System.out.println("该歌曲存在");
System.out.println("请输入要删除的歌曲ID");
String id=sc.next();
Song song1=pList.searchSongById(id);
if(song1==null) {
System.out.println("要删除的歌曲不存在");
break;
}else {
pList.deleSong(id);
System.out.println("该歌曲已经删除");
}
}
String playListName = sc.next();
PlayList plst = player.searchPlayListByName(playListName);
if(playListName.equals("主播放列表")) {
System.out.println("抱歉!主播放列表不能删除!");
break;
}
PlayList pList1 = player.searchPlayListByName(playListName);
if(pList1 == null) {
System.out.println("该播放列表不存在!");
} else {
player.deletePlayList(pList1);
}
break;
}
case 7: {
System.out.println("显示播放列表的所有歌曲");
System.out.println("请输入要查询的播放列表");
String playListName = sc.next();
PlayList plst = player.searchPlayListByName(playListName);
if(plst == null) {
System.out.println("抱歉,您输入的列表名称没有匹配!");
} else {
System.out.println("该播放列表:"+plst.getPlayListName()+"的全部歌曲为");
plst.displayAllSong();
}
break;
}
default: {
System.out.println("该数字没有对应的操作");
break;
}
}
}
break;
}
case 2: {
while(true) {
plService.playerMenu();
System.out.println("请输入对应的数字进行播放器管理操作:");
input2 = sc.nextInt();
if(input2 == 9) {
break;
}
switch(input2) {
case 1: {
System.out.println("向播放器添加播放列表!");
System.out.println("请输入要添加的播放列表名称");
String playName = sc.next();
if(playName.equals("主播放列表")) {
System.out.println("主播放列表无需创建!");
} else {
pList = new PlayList(playName);
player.addPlayList(pList);
System.out.println("播放列表创建完毕!");
}
break;
}
case 2: {
System.out.println("从播放器删除播放列表!");
System.out.println("请输入要删除的播放列表名称:");
String playListName = sc.next();
if(playListName.equals("主播放列表")) {
System.out.println("抱歉!主播放列表不能删除!");
break;
}
PlayList pList1 = player.searchPlayListByName(playListName);
if(pList1 == null) {
System.out.println("该播放列表不存在!");
} else {
player.deletePlayList(pList1);
}
break;
}
case 3: {
System.out.println("通过名字查询播放列表信息!");
System.out.println("请输入要查询的播放列表名称:");
String playListName = sc.next();
PlayList pList2 = player.searchPlayListByName(playListName);
if(pList2 == null) {
System.out.println("该播放列表不存在!");
} else {
System.out.println("已查询到该播放列表");
System.out.println("该播放列表名称为:" + playListName);
pList2.displayAllSong();
}
break;
}
case 4: {
System.out.println("显示所有列表名称!");
System.out.println("所有播放列表的名称为:");
player.displayListName();
break;
}
default: {
System.out.println("该数字没有对应的操作");
break;
}
}
}
break;
}
default: {
System.out.println("该数字没有对应的操作");
break;
}
}
}
}
}