import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Scanner;
public class Songs {
public static void main(String[] args) {
LinkedList<String> songs = new LinkedList<>();
songs.add("Song 1");
songs.add("Song 2");
songs.add("Song 3");
songs.add("Song 4");
songs.add("Song 5");
songs.add("Song 6");
Scanner scanner = new Scanner(System.in);
boolean quit=false;
boolean forward=true;
ListIterator currentSong = songs.listIterator();
printMenu();
while(quit==false){
switch(scanner.nextInt()){
case 1:
printMenu();
break;
case 2:
if(!forward){
if(currentSong.hasNext()){
currentSong.next();
}
forward=true;
}
if(currentSong.hasNext()){
System.out.println("Now playing " + currentSong.next());
}
else{
System.out.println("Reached the end of the playlist");
}
break;
case 3:
if(forward){
if(currentSong.hasPrevious()){
currentSong.previous();
}
forward=false;
}
if(currentSong.hasPrevious()){
System.out.println("Now playing " + currentSong.previous());
}
else{
System.out.println("We are at the start of the playlist");
}
break;
case 4:
if(currentSong.hasPrevious()){
currentSong.previous();
System.out.println("Replaying " + currentSong.next());
}
else if(currentSong.hasNext()){
currentSong.next();
System.out.println("Replaying " + currentSong.previous());
}
break;
case 5:
if(songs.size()>0){
currentSong.remove();
if(currentSong.hasNext()){
currentSong.next();
}
else if(currentSong.hasPrevious()){
currentSong.previous();
}
}
break;
case 6:
for(String song: songs){
System.out.println(song);
}
break;
case 7:
quit=true;
break;
default:
printMenu();
break;
}
}
}
private static void printMenu(){
System.out.println("1 - Print Menu\n" +
"2 - Go to the next song\n" +
"3 - Go to previous song\n" +
"4 - Replay current song\n" +
"5 - Remove current song\n" +
"6 - Print all songs\n" +
"7 - Quit\n" +
"============================");
}
}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE