老师,如何才能导出歌单,程序还有哪些错误?
package com.song.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;
public class Test {
// 主菜单
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("8--导出歌单信息 ");
System.out.println("9--返回上一级菜单 ");
System.out.println("**************************************");
}
// 播放器管理
public void player() {
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() {
Test test = new Test();
Scanner sc = new Scanner(System.in);
int input = 0, input1 = 1, input2 = 2;
// 创建一个播放器
PlayListCollection plc = new PlayListCollection();
// 创建主播放列表
PlayList mainPlayList = new PlayList("创建主播放列表");
// 将主播放列表添加到播放器
plc.addPlayList(mainPlayList);
PlayList favouritePlayList = null;
while (true) {
test.mainMenu();
System.out.println("请输入相应的数字进行操作:");
input = sc.nextInt();
if (input == 0) {
break;
}
switch (input) {
case 1:
// 播放列表管理
while (true) {
test.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 strId = sc.next();
System.out.println("请输入歌曲的名称:");
String strName = sc.next();
System.out.println("请输入演唱者:");
String strSinger = sc.next();
// 创建歌曲类的对象
Song song = new Song(strId, strName, strSinger);
mainPlayList.addToPlayList(song);
// mainPlayList.displayAllSong();
}
break;
case 2:
System.out.println("将歌曲添加 到普通播放列表");
System.out.println("请输入要添加的播放列表名称:");
String sName = sc.next();
// 根据名称判断播放列表是否在播放器中存在
favouritePlayList = plc.searchPlayListByName(sName);
if (favouritePlayList == null) {
System.out.println("该播放列表不存在,请先将播放列表添加到播放器中!");
} else {
System.out.println("请输入要添加的歌曲的数量");
int count1 = sc.nextInt();
for (int i = 1; i <= count1; i++) {
System.out.println("请输入第" + i + "首歌曲:");
System.out.println("请输入歌曲id:");
String strId = sc.next();
// 首先判断id的歌曲是否在主播放列表中存在
Song song = mainPlayList.searchSongById(strId);
if (song == null) {
System.out.println("该歌曲在主播放列表中不存在,继续输入歌曲的其他信息");
System.out.println("请输入歌曲的名称:");
String strName = sc.next();
System.out.println("请输入演唱者:");
String strSinger = sc.next();
// 创建一个Song类的对象
song = new Song(strId, strName, strSinger);
// 分别将歌曲添加普通播放列表和主播放列表
favouritePlayList.addToPlayList(song);
mainPlayList.addToPlayList(song);
} else {
// 如果歌曲存在于主播放列表,则直接添加到现在的播放列表
favouritePlayList.addToPlayList(song);
}
}
// 显示播放列表中的歌曲信息
System.out.println("主播放列表");
mainPlayList.displayAllSong();
System.out.println("普通播放列表");
favouritePlayList.displayAllSong();
}
break;
case 3:
System.out.println("通过歌曲id查询播放列表中的歌曲 ");
System.out.println("请输入要查询的播放列表名称: ");
String strPlayListName1 = sc.next();
// 查询播放列表是否存在
PlayList pl = plc.searchPlayListByName(strPlayListName1);
if (pl == null) {
System.out.println("该播放列表不存在!");
break;
} else {
System.out.println("请输入要查询的歌曲id:");
String strId1 = sc.next();
Song s = pl.searchSongById(strId1);
if (s == null) {
System.out.println("该歌曲在播放列表" + strPlayListName1 + "中不存在!");
} else {
System.out.println("该歌曲的信息为:");
System.out.println(s);
}
}
break;
case 4:
System.out.println("通过歌曲名称查询播放列表中的歌曲 ");
System.out.println("请输入要查询的播放列表名称: ");
String strPlayListName2 = sc.next();
PlayList p2 = plc.searchPlayListByName(strPlayListName2);
if (p2 == null) {
System.out.println("该播放列表不存在!");
break;
} else {
System.out.println("请输入要查询的歌曲名称:");
String strName = sc.next();
Song s = p2.searchSongByName(strName);
if (s == null) {
System.out.println("该歌曲在播放列表" + strPlayListName2 + "中不存在!");
} else {
System.out.println("该歌曲的信息为:");
System.out.println(s);
}
}
break;
case 5:
System.out.println("修改播放列表中的歌曲");
System.out.println("请输入要修改的播放列表名称");
String strPlayListName3 = sc.next();
PlayList p3 = plc.searchPlayListByName(strPlayListName3);
if (p3 == null) {
System.out.println("该播放列表不存在!");
break;
} else {
System.out.println("请输入要修改的歌曲id:");
String strId3 = sc.next();
Song s = p3.searchSongById(strId3);
if (s == null) {
System.out.println("该歌曲在播放列表" + strPlayListName3 + "中不存在!");
} else {
System.out.println("修改前的歌曲的名称:" + "\n" + s.getName());
System.out.println("修改前的歌曲的演唱者:" + "\n" + s.getSinger());
System.out.println("修改后的歌曲的名称:");
String name = sc.next();
s.setName(name);
System.out.println("修改后的歌曲的演唱者:");
String singer = sc.next();
s.setSinger(singer);
}
}
break;
case 6:
System.out.println("删除播放列表中的歌曲");
System.out.println("请输入要删除的歌曲的所在播放列表:");
String strPlayListname4 = sc.next();
PlayList p4 = plc.searchPlayListByName(strPlayListname4);
if (p4 == null) {
System.out.println("该播放列表不存在!");
} else {
System.out.println("请输入要删除的歌曲id:");
String strId4 = sc.next();
Song s = p4.searchSongById(strId4);
if (s == null) {
System.out.println("该歌曲在播放列表" + strPlayListname4 + "中不存在!");
} else {
System.out.println("删除的歌曲id:");
p4.deleteSong(strId4);
System.out.println("删除成功!");
}
}
break;
case 7:
System.out.println("显示播放列表中的所有歌曲");
System.out.println("请输入要显示所有歌曲的播放列表名称:");
String strPlayListname5 = sc.next();
PlayList p5 = plc.searchPlayListByName(strPlayListname5);
if (p5 == null) {
System.out.println("该播放列表不存在!");
} else {
p5.displayAllSong();
}
break;
case 8:// 导出歌单
System.out.println("请输入要导出的播放列表名称:");
String strPlayListname6 = sc.next();
PlayList p6 = plc.searchPlayListByName(strPlayListname6);
//p6.getMusicList();
if (p6 == null) {
System.out.println("该播放列表不存在!");
} else {
try {
FileOutputStream fio = new FileOutputStream("two.txt");
ObjectOutputStream oos = new ObjectOutputStream(fio);
FileInputStream fis = new FileInputStream("two.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
oos.writeObject(p6);
ois.read();
fio.close();
oos.close();
fis.close();
ois.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
default:
System.out.println("该数字没有对应的操作!");
break;
}
}
break;
case 2:
// 播放器管理
while (true) {
test.player();
System.out.println("请输入对应的数字对播放器进行管理:");
input2 = sc.nextInt();
if (input2 == 9)
break;
switch (input2) {
case 1:
System.out.println("向播放器添加播放列表");
System.out.println("输入要添加的播放列表名称:");
String playerName = sc.next();
// 创建一个新播放列表对象
favouritePlayList = new PlayList(playerName);
// 将播放列表添加到播放器Map
plc.addPlayList(favouritePlayList);
break;
case 2:
System.out.println("从播放器删除播放列表");
System.out.println("请输入要删除的播放列表");
String strPlayListName = sc.next();
if (strPlayListName.equals("主播放列表")) {
System.out.println("主播放列表不能删除");
break;
}
// 查询播放列表是否存在
PlayList playList1 = plc.searchPlayListByName(strPlayListName);
if (playList1 == null) {
System.out.println("该播放列表不存在!");
} else {
// 存在则删除
plc.deletePlayList(playList1);
}
break;
case 3:
System.out.println("通过名字查询播放列表信息");
System.out.println("请输入要查询的播放列表名称:");
String strPlayList1 = sc.next();
PlayList playList2 = plc.searchPlayListByName(strPlayList1);
if (playList2 == null) {
System.out.println("该播放列表不存在!");
} else {
// 显示该播放列表名称及其中的所有歌曲
System.out.println("该播放列表存在!");
System.out.println("该播放列表名称为:" + strPlayList1);
playList2.displayAllSong();
}
break;
case 4:
System.out.println("显示所有播放列表名称");
plc.displayListName();
break;
default:
System.out.println("该数字没有对应的操作!");
break;
}
}
break;
default:
System.out.println("该数字没有对应的操作!");
break;
}
}
mainPlayList.displayAllSong();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test testone = new Test();
// test.testsong();
// test.testPlayList();
// test.testPlayListCollection();
testone.test();
}
}5
收起
正在回答 回答被采纳积分+1
2回答
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星