使用LinkedList集合模拟点歌系统

package com.czie.iot1913.lps;

import java.util.LinkedList;
import java.util.Scanner;

/**
* @author 1944900433@qq.com
* @date 2022-03-17 21:16
*/
public class KTVByArrayList {
public static void main(String[] args) {
System.out.println("------------欢迎来到品水KTV点歌系统------------");
System.out.println("0-添加歌曲到列表哈");
System.out.println("1-将此歌曲置顶");
System.out.println("2-将歌曲提前一位");
System.out.println("3-退出");
LinkedList<Object> lineUplist = new LinkedList<>();
addMusicList(lineUplist);//显示默认歌单
while (true){
System.out.println("请输入您的指令序号!!!");
Scanner sc = new Scanner(System.in);
int command=sc.nextInt();
switch (command){
case 0:
addMusic(lineUplist);
break;
case 1:
setTop(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 addMusicList(LinkedList lineUpList){
lineUpList.add("稻香");
lineUpList.add("追梦赤子心");
lineUpList.add("See you again");
lineUpList.add("白羊");
lineUpList.add("c u again");
System.out.println("初始歌曲列表为"+lineUpList);
}
//添加歌曲
private static void addMusic(LinkedList lineUpList){
System.out.println("请输入要添加的歌名:");
String musicName = new Scanner(System.in).nextLine();
lineUpList.add(musicName);//添加歌曲
System.out.println("已添加歌曲"+musicName);
}
//将此歌曲置顶
public static void setTop(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 setBefore(LinkedList lineUpList){
System.out.println("请输入你需要前移的歌曲名称:");
String musicName = new Scanner(System.in).nextLine();
int position=lineUpList.indexOf(musicName);
if (position<0){
System.out.println("当前列表没有这首歌");
}else if(position==0){
System.out.println("这首歌已经置顶");
}else {
lineUpList.remove(musicName);
lineUpList.add(position-1,musicName);
}
System.out.println("前移的歌曲"+musicName+"成功");
}
private static void exit(){
System.out.println("------------退出------------");
System.out.println("您已经退出系统");
System.exit(0);
}
}
posted @ 2022-03-17 21:53  刘品水  阅读(80)  评论(0编辑  收藏  举报