模拟KTV点歌系统
package 任务1;
import java.util.LinkedList;
import java.util.Scanner;
public class KTVBYLinkedList {
public static void main(String[] args) {
System.out.println("----欢迎来到点歌系统----");
System.out.println("0.添加歌曲");
System.out.println("1.将歌曲置顶");
System.out.println("2.将歌曲前移一步");
System.out.println("3.退出");
LinkedList lineUpList=new LinkedList();
addMusicList(lineUpList);
}
private static void addMusicList(LinkedList lineUpList) {
lineUpList.add("稻香");
lineUpList.add("夜曲");
lineUpList.add("夜的第七章");
lineUpList.add("听妈妈的话");
lineUpList.add("龙卷风");
System.out.println("初始化歌曲列表:"+lineUpList);
while(true) {
System.out.println("请输入要执行的操作歌曲");
Scanner in =new Scanner(System.in);
int command=in.nextInt();
switch(command) {
case 0:
addMusic(lineUpList);
break;
case 1:
seTop(lineUpList);
break;
case 2:
setBefore(lineUpList);
break;
case 3:
exit();
break;
default:
System.out.println("--------------");
System.out.println("功能选择有误,请输入正确的功能序号");
break;
}
System.out.println("当前歌曲列表"+lineUpList);
}
}
private static void exit() {
System.out.println("退出----------");
System.out.println("您已退出");
System.exit(0);
}
private static void setBefore(LinkedList lineUpList) {
System.out.println("请输入要置前的歌曲名称");
String musicName=new Scanner(System.in).nextLine();
int postion=lineUpList.indexOf(musicName);
if(postion<0) {
System.out.println("没有该歌曲");
}else if(postion==0) {
System.out.println("歌曲已在顶位");
}else {
lineUpList.remove(musicName);
lineUpList.add(postion-1, musicName);
System.out.println("已将歌曲"+musicName+"置前一位");
}
}
private static void seTop(LinkedList lineUpList) {
System.out.println("请输入要置顶的歌曲:");
String musicName=new Scanner(System.in).nextLine();
int position=lineUpList.indexOf(musicName);
if(position<0) {
System.out.println("当前没有这歌曲");
}else {
lineUpList.remove(musicName);
lineUpList.addFirst(musicName);
System.out.println("以将"+musicName+"置顶");
}
}
private static void addMusic(LinkedList lineUpList) {
System.out.println("请输入要添加歌曲名称");
String musicName=new Scanner(System.in).nextLine();
lineUpList.addLast(musicName);
System.out.println("以添加歌曲"+lineUpList);
}
}