1.歌曲类(Song)
2.播放列表类(PlayList)
3、播放器类(PlayListCollection)
4.测试类(TestDemo)
song.java
/*
* 歌曲类
*/
public class Song{
private String id;//歌曲id
private String name;//歌曲名
private String singer;//演唱者
public Song(){
}
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 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 == null)
return false;
if (getClass() != obj.getClass())
return false;
Song other = (Song) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (singer == null) {
if (other.singer != null)
return false;
} else if (!singer.equals(other.singer))
return false;
return true;
}
//toString方法
@Override
public String toString() {
return "歌曲信息 [歌曲id:" + id + ", 歌曲名称:" + name + ", 演唱者:" + singer + "]";
}
}
Playlist.java
import com.sun.corba.se.spi.ior.ObjectKey;
import javax.swing.text.html.parser.Entity;
import java.io.File;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
/*
* 播放列表类
*/
public class PlayList{
private String playListname;//播放列表名称
public List musicList = new ArrayList();//播放列表中的歌曲合集
public String mainName;//主播放列表名称
public List mainList = new ArrayList();//主播放列表
public PlayListCollection p = new PlayListCollection();
//构造方法
public PlayList(){
}
public PlayList(String playListname,List musicList){
super();
this.playListname = playListname;
this.musicList = musicList;
}
//getter & setter
//playlist
public String getPlayListname() {
return playListname;
}
public String setPlayListname(String playListname) {
return this.playListname = playListname;
}
public List getMusicList() {
return musicList;
}
public void setMusicList(List musicList) {
this.musicList = musicList;
}
// mainList
public String getMainName() {
return mainName;
}
public String setMainName(String mainName) {
return this.mainName = mainName;
}
public List getMainList() {
return mainList;
}
public void setMainList(List mainList) {
this.mainList = mainList;
}
public PlayListCollection getP(){
return p;
}
public void setP(PlayListCollection p){
this.p = p;
}
/*
将歌曲添加到播放列表:public void addToPlayList(Song song)
显示播放列表中所有歌曲:public displayAllSong()
通过id查询歌曲:public Song searchSongById(String id)
通名称查询歌曲: public Song searchSongByName(String name)
修改歌曲:public void updateSong(String id,Song song)
从播放列表(通过id)删除歌曲:public void deleteSong(String id)
导出所有歌曲
*/
//将歌曲添加到播放列表:public void addToPlayList(Song song)
public void addToPlayList(Song song,boolean state) {
Scanner sc = new Scanner(System.in);
if (state) {
//如果歌曲是添加到 主列表
String key = setMainName("main");
System.out.println("请输入要添加的歌曲的数量:");
int n = sc.nextInt();
int i = 0;
while (i < n) {
//每创建一个对象,让Song的对象产生不同的id
Song song1 = new Song();
System.out.println("请输入第" + (i + 1) + "首歌曲的id:");
song1.setId(sc.next());
System.out.println("请输入第" + (i + 1) + "首歌曲的名称:");
song1.setName(sc.next());
System.out.println("请输入第" + (i + 1) + "首歌曲的演唱者:");
song1.setSinger(sc.next());
//将歌曲添加到播放列表
mainList.add(song1);
//使用ArrayList实现一个key对应一个ArrayList
((List) p.playListMap.get(key)).add(song1);
i++;
}
System.out.println("主播放列表的歌曲为:");
Iterator it = p.playListMap.values().iterator();
while (it.hasNext()) {
Iterator it2 = ((List) it.next()).iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
}
}
Set<Map.Entry<String, Object>> entrySet = p.playListMap.entrySet();
for (Map.Entry<String, Object> entry : entrySet) {
System.out.println(entry.getValue() + "-" + entry.getKey());
}
new TestDemo().playListMenu();
} else {
//如果添加到的是普通列表
System.out.println("请输入你要添加的列表名称:");
String s = sc.next();
if (new PlayListCollection().playListMap.isEmpty())
System.out.println("还没有创建列表");
//判断是否存在此列表
if (!new PlayListCollection().playListMap.containsKey(s)) {
System.out.println("列表不存在");
new TestDemo().playMenu();
} else {
System.out.println("请输入要添加歌曲的数量:");
int n = sc.nextInt();// 获取歌曲的数量,从而做几次循环
int i = 0;
while (i < n) {
// 每次创建一个对象,让Song的对象产生不同的id
Song song1 = new Song();
System.out.println("请输入第" + (i + 1) + "首歌曲的id");
song1.setId(sc.next());
System.out.println("请输入第" + (i + 1) + "首歌曲的名称");
song1.setName(sc.next());
System.out.println("请输入第" + (i + 1) + "首歌曲的演唱者");
song1.setSinger(sc.next());
// 将歌曲添加到播放列表
musicList.add(song1);
// 使用ArrayList实现一个Key对应一个ArrayList,实现一对多
((List) p.playListMap.get(s)).add(song1);
i++;
}
System.out.println("播放列表[" + s + "]的歌曲列表为:");
Iterator it = p.playListMap.values().iterator();
while (it.hasNext()) {
Iterator it2 = ((List) it.next()).iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
}
}
Set<Map.Entry<String, Object>> entrySet = p.playListMap.entrySet();
//打印输出key-value的值,通过entryset方法得到key-value
for (Map.Entry<String, Object> entry : entrySet) {
System.out.println(entry.getKey() + "-" + entry.getValue());
}
new TestDemo().playListMenu();
}
}
}
// 通过id查询歌曲
public Song searchSongById(String id) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入要搜索的歌曲id:");
String searchId=sc.next();
Song song=new Song();
boolean flag=false;
//通过entrySet方法得到key-value
Set<Map.Entry<String,Object>>entrySet=p.playListMap.entrySet();
//打印输出key-value的值 通过entrySet方法得到key-value
for(Map.Entry<String,Object>entry:entrySet) {
Set<String> keySet=p.playListMap.keySet();
//List的遍历,通过get(key)得到value因为value是List所以强转换成List类型。再用List遍历
Iterator it2=((List)(p.playListMap.get(entry.getKey()))).iterator();
while(it2.hasNext()) {
song=(Song)it2.next();
//将找到的数据转换成song,如果满足与输入结果相同,则返回这个结果
if(song.getId().equals(searchId)) {
flag=true;
System.out.println(song);
break;
}
}
}
if(flag)
System.out.println("搜索成功");
else {
System.out.println("无此歌曲id");
}
return song;
}
// 通过名称查询歌曲
public Song searchSongByName(String name) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入要搜索的歌曲名称:");
String searchName=sc.next();
Song song=new Song();
boolean flag=false;
//通过entrySet方法得到key-value
Set<Map.Entry<String,Object>>entrySet=p.playListMap.entrySet();
//打印输出key-value的值 通过entrySet方法得到key-value
for(Map.Entry<String,Object>entry:entrySet) {
Set<String>keySet=p.playListMap.keySet();
//List的遍历,通过get(key)得到value因为value是List所以强转换成List类型。再用List遍历
Iterator it2=((List)(p.playListMap.get(entry.getKey()))).iterator();
while(it2.hasNext()) {
song=(Song)it2.next();
//将找到的数据转换成song,如果满足与输入结果相同,则返回这个结果
if(song.getName().equals(searchName)){
flag=true;
System.out.println(song);
break;
}
}
}
if(flag)
System.out.println("搜索成功");
else {
System.out.println("无此歌曲名称");
}
return song;
}
// 修改歌曲
public void updateSong(String id, Song song) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入要修改的歌曲:");
String searchId=sc.next();
song=new Song();
boolean flag=false;
Set<Map.Entry<String,Object>>entrySet=p.playListMap.entrySet();
for(Map.Entry<String,Object>entry:entrySet) {
Set<String>keySet=p.playListMap.keySet();
Iterator it2=((List)(p.playListMap.get((entry).getKey()))).iterator();
while(it2.hasNext()) {
song=(Song)it2.next();
if(song.getId().equals(searchId)) {
flag=true;
((List)(p.playListMap.get(entry.getKey()))).remove(song);
System.out.println("请输入修改后的歌曲名称:");
song.setName(sc.next());
System.out.println("请输入修改后的歌曲演唱者:");
song.setSinger(sc.next());
System.out.println(song);
break;
}
}
}
if(flag)
System.out.println("修改成功");
else {
System.out.println("无此歌曲id");
}
}
//显示播放列表中所有歌曲:public displayAllSong()
public void displayAllSong(){
boolean flag = false;
//使用KeySet
Set<String> keySet = p.playListMap.keySet();
for(String key:keySet){
//如果是主播放列表
if ("main".equals(key)){
flag = true;
System.out.println("主播放列表歌曲为:");
Iterator it2 = ((List)p.playListMap.get(key)).iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
break;
}
Set<Map.Entry<String,Object>> entrySet = p.playListMap.entrySet();
for(Map.Entry<String,Object>entry:entrySet) {
// entry.getValue();
// entry.getKey();
// System.out.println(entry.getKey()+"-"+entry.getValue());
// System.out.println(entry.getKey());//获取key的值
//3遍历keySet
for(String key2:keySet) {
if(entry.getKey().equals(key)) {
flag=true;
System.out.println("主播放播放列表歌曲为:");
Iterator it3=((List)(p.playListMap.get(entry.getKey()))).iterator();
while(it3.hasNext()) {
System.out.println((Song)(it3.next()));
// System.out.println(it3.next());
}
break;
}
}
}
}
}
}
// 从播放列表删除歌曲
public void deleteSong(String id) {
Scanner sc=new Scanner(System.in);
String searchId=sc.next();
Song song=new Song();
boolean flag=false;
Set<Map.Entry<String,Object>>entrySet=p.playListMap.entrySet();
for(Map.Entry<String,Object>entry:entrySet) {
Set<String>keySet=p.playListMap.keySet();
Iterator it2=((List)(p.playListMap.get(entry.getKey()))).iterator();
while(it2.hasNext()) {
song=(Song)it2.next();
if(song.getId().equals(searchId)) {
flag=true;
((List)(p.playListMap.get(entry.getKey()))).remove(song);
break;
}
}
}
if(flag)
System.out.println("删除歌曲成功");
else {
System.out.println("无此歌曲");
}
}
public void outPut() {
System.out.println("为输入导出的歌单命名:");
Scanner sc=new Scanner(System.in);
//创建File文件song.txt
File file=new File(sc.next()+".txt");
if(!file.exists()) {
//判断文件是否存在
System.out.println("创建成功");
try {
file.createNewFile();
PrintStream ps=System.out;//临时存一下
System.setOut(new PrintStream(file));//将system.out的内容打印到.txt
this.displayAllSong();
System.setOut(ps);//设置回标准输入流
new TestDemo().playListMenu();
}catch(FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else {
System.out.println("文件名已存在");
outPut();
}
}
}
PlayListCollection
import java.util.*;
import java.util.Map.Entry;
//播放器类
public class PlayListCollection {
//存放播放列表的集合
static Map<String,Object> playListMap = new HashMap<String,Object>();
//static List list = new ArrayList<>();
//构造方法
public PlayListCollection(){
}
/*
添加播放列表:public void addPlayList(PlayList playList);
删除播放列表:public void deletePlayList(PlayList playList);
通过名字查询:public PlayList searchPlayListByName(String playListName);
显示所有播放列表的名称:public void displayPlayListName()
*/
//添加播放列表
public void addPlayList(PlayList playList){
Scanner sc = new Scanner(System.in);
String key = playList.setPlayListname(sc.next());
List value = playList.getMainList();
playListMap.put(key,value);
//sc.close();
Set<Entry<String,Object>> entrySet = playListMap.entrySet();
//打印输出key-balue的值,通过entrySet方法得到key-value
for (Entry<String,Object>entry : entrySet){
System.out.println(entry.getKey()+"-"+entry.getValue());
}
}
//删除播放列表
public void deletePlayList(PlayList playList){
Scanner sc = new Scanner(System.in);
//通过输入的列表名称删除播放列表
System.out.println("请输入要删除的播放名称:");
if (playListMap.containsKey(sc.next())){
playListMap.remove(sc.next());
System.out.println("删除该列表成功");
}else {
System.out.println("无此播放列表");
}
}
//通过名字查询:public PlayList searchPlayListByName(String playListName);
public PlayList searchPlayListByName(String playListName){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查询的播放列表名称:");
String key = sc.next();
if (playListMap.containsKey(key)){
if (((List)(playListMap.get(key))).size()==0){
System.out.println("该列表为空");
}else {
System.out.println("查询列表成功");
System.out.println(playListMap.get(key));
}
}else{
System.out.println("无此播放列表");
}
return null;
}
//显示所有播放列表的名称:public void displayPlayListName()
public void displayPlayListName(){
Set<Entry<String,Object>> entrySet = playListMap.entrySet();
System.out.println("找到以下列表");
for (Entry<String,Object>entry : entrySet){
System.out.println(entry.getKey());
}
}
}
TestDEMO
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
//测试类
public class TestDemo {
public static PlayListCollection p = new PlayListCollection();
public TestDemo(){
}
//测试歌曲类
public void testSong(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要添加歌曲的数量:");
int j = sc.nextInt();
Scanner console = new Scanner(System.in);
Map<String,Song> songMap = new HashMap<String, Song>();
int i = 0;
while(i<j){
System.out.println("请输入第"+(i+1)+"首歌曲:");
System.out.println("请输入歌曲编号:");
String id = console.next();
//判断歌曲id是否存在
if (songMap.containsKey(id)){
System.out.println("该歌曲编号已经存在!请重新输入!");
continue;
}
System.out.println("请输入歌曲的名称:");
String name = console.next();
System.out.println("请输入此歌曲的演唱者:");
String singer = console.next();
Song song = new Song(id,name,singer);
//讲歌曲信息添加到HashMap中
songMap.put(id,song);
i++;
}
//遍历Map.输出歌曲信息
System.out.println("歌曲的全部信息:");
Iterator<Song> itSong = songMap.values().iterator();
while (itSong.hasNext()){
System.out.println(itSong.next());
}
}
//测试播放列表类
public void testPlayList(){
}
//测试播放器类
public void testPlayListCollection(){
}
//主菜单
public void mainMenu(){
System.out.println("***************************************");
System.out.println(" " + "****主菜单*****" + " ");
System.out.println(" " + "1--播放列表管理" + " ");
System.out.println(" " + "2--播放器管理 " + " ");
System.out.println(" " + "0--退出 " + " ");
System.out.println("***************************************");
System.out.println("请输入对应数字进行操作:");
Scanner sc = new Scanner(System.in);
while(!sc.hasNext("0")){
//扫描到0就退出循坏
int n = sc.nextInt();
switch (n){
case 1:
//显示播放器列表管理
new TestDemo().playListMenu();
break;
case 2:
//播放器管理
new TestDemo().playMenu();
break;
case 0:
//退出
break;
default:
//判断是否输入选项中的数字
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(" " + "8--导出菜单");
System.out.println(" " + "9--返回上一级菜单");
System.out.println("******************************************************");
System.out.println("请输入对应数字进行操作:");
// 从键盘中接收数据
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
PlayList playList = new PlayList();
Song song = new Song();
switch (n) {
// a无任何意义
case 1:
// 将歌曲添加到主播放列表
playList.addToPlayList(song, true);
break;
case 2:
// 将歌曲添加到普通放列表
playList.addToPlayList(song, false);
break;
case 3:
// 通过歌曲id查询播放列表中的歌曲
playList.searchSongById("a");
break;
case 4:
// 将歌曲名称查询播放列表中的歌曲
playList.searchSongByName("a");
break;
case 5:
// 修改播放列表中的歌曲
playList.updateSong("a", song);
break;
case 6:
// 删除播放列表中的歌曲
playList.deleteSong("a");
break;
case 7:
// 显示播放列表中的所有歌曲
playList.displayAllSong();
break;
case 8:
// 导出菜单
playList.outPut();
break;
case 9:
// 返回上一级菜单
mainMenu();
break;
case 0:
break;
default: //判断是否输入选项中的数字
System.out.println("请输入选项中的数字:");
}
}
}
// 播放器菜单
public void playMenu() {
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("******************************************************");
System.out.println("请输入对应数字进行操作:");
// 从键盘中接收数据
PlayList playList = new PlayList();
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
switch (n) {
case 1:
// 向播放器添加播放列表
new PlayListCollection().addPlayList(new PlayList());
break;
case 2:
// 从播放器删除播放列表
new PlayListCollection().deletePlayList(playList);
break;
case 3:
// 通过名字查询播放列表信息
new PlayListCollection().searchPlayListByName("a");
break;
case 4:
// 显示所有播放列表名称
new PlayListCollection().displayPlayListName();
break;
case 9:
//返回上一级菜单
new TestDemo().playListMenu();
break;
case 0:
break;
default://判断是否输入选项中的数字
System.out.println("请输入选项中的数字:");
}
}
}
// 主流程实现
public void test() {
}
// 主方法
public static void main(String[] args) {
String key = "main";
List value = new PlayList().getMainList();
p.playListMap.put(key,value);
new TestDemo().mainMenu();
}
}