init
This commit is contained in:
135
java/MusicPlaylist.java
Normal file
135
java/MusicPlaylist.java
Normal file
@@ -0,0 +1,135 @@
|
||||
// Nik Johnson
|
||||
// 1-31-2024
|
||||
// TA: Andy Ruan
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// music playlist manager with functionality to add and play songs while keeping track of play history,
|
||||
// which can be cleared, or deleted from in batches
|
||||
public class MusicPlaylist {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner console = new Scanner(System.in);
|
||||
Queue<String> playlist = new LinkedList<>();
|
||||
Stack<String> history = new Stack<>();
|
||||
System.out.println("Welcome to the CSE 122 Music Playlist!");
|
||||
|
||||
String destination = navigator(console);
|
||||
while (!destination.equalsIgnoreCase("Q")) {
|
||||
if (destination.equalsIgnoreCase("A")) {
|
||||
addSong(console, playlist);
|
||||
} else if (destination.equalsIgnoreCase("P")) {
|
||||
playSong(playlist, history);
|
||||
} else if (destination.equalsIgnoreCase("Pr")) {
|
||||
printHistory(history);
|
||||
} else if (destination.equalsIgnoreCase("C")) {
|
||||
clearHistory(history);
|
||||
} else if (destination.equalsIgnoreCase("D")) {
|
||||
deleteFromHistory(console, history);
|
||||
}
|
||||
destination = navigator(console);
|
||||
}
|
||||
}
|
||||
|
||||
// menu function, called initially and after any other functions
|
||||
// return String for use in main menu loop
|
||||
public static String navigator(Scanner console) {
|
||||
System.out.println("(A) Add song");
|
||||
System.out.println("(P) Play song");
|
||||
System.out.println("(Pr) Print history");
|
||||
System.out.println("(C) Clear history");
|
||||
System.out.println("(D) Delete from history");
|
||||
System.out.println("(Q) Quit");
|
||||
System.out.println();
|
||||
System.out.print("Enter your choice: ");
|
||||
String destination = console.nextLine();
|
||||
return destination;
|
||||
}
|
||||
|
||||
// add song to playlist according to user input
|
||||
// no return
|
||||
public static void addSong(Scanner console, Queue<String> playlist) {
|
||||
System.out.print("Enter song name: ");
|
||||
String songToAdd = console.nextLine();
|
||||
playlist.add(songToAdd);
|
||||
System.out.println("Successfully added " + songToAdd);
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// play song at the front of the playlist
|
||||
// no return
|
||||
public static void playSong(Queue<String> playlist, Stack<String> history) {
|
||||
// if user attempts to play song when playlist is empty, throw exception
|
||||
if (playlist.isEmpty()) throw new IllegalStateException();
|
||||
String songToPlay = playlist.remove();
|
||||
System.out.println("Playing song: " + songToPlay);
|
||||
history.push(songToPlay);
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// print history of played songs
|
||||
// return unmodified history after printing it
|
||||
public static void printHistory(Stack<String> history) {
|
||||
int size = history.size();
|
||||
// if there is no history and user attempts to print history, throw exception
|
||||
if (history.isEmpty()) throw new IllegalStateException();
|
||||
String[] historyArray = new String[size];
|
||||
if (size != 0) {
|
||||
for (int i = (size - 1); i >= 0; i--) {
|
||||
historyArray[i] = history.pop();
|
||||
System.out.println(" " + historyArray[i]);
|
||||
}
|
||||
for (int n = 0; n < size; n++) {
|
||||
history.push(historyArray[n]);
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// clear history when called
|
||||
// return empty history
|
||||
public static void clearHistory(Stack<String> history) {
|
||||
history.clear();
|
||||
}
|
||||
|
||||
// delete from history according to user input, starting either from most recent or oldest history
|
||||
// return modified history
|
||||
public static void deleteFromHistory(Scanner console, Stack<String> history) {
|
||||
int size = history.size();
|
||||
List<String> historyArrayList = new ArrayList<>();
|
||||
if (!history.isEmpty()) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
historyArrayList.add(history.pop());
|
||||
}
|
||||
}
|
||||
System.out.println("A positive number will delete from recent history.");
|
||||
System.out.println("A negative number will delete from the beginning of history.");
|
||||
System.out.print("Enter number of songs to delete: ");
|
||||
int numToDelete = Integer.parseInt(console.nextLine());
|
||||
int absNum = Math.abs(numToDelete);
|
||||
if (absNum > size) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
System.out.println();
|
||||
if (size != 0 && absNum != 0) {
|
||||
if (absNum == size) {
|
||||
historyArrayList.clear();
|
||||
} else if (numToDelete < 0) {
|
||||
int lastIndex = size - 1;
|
||||
for (int n = lastIndex; n >= (size - absNum); n--) {
|
||||
historyArrayList.remove(n);
|
||||
}
|
||||
} else if (numToDelete > 0) {
|
||||
for (int i = 0; i < numToDelete; i++) {
|
||||
historyArrayList.remove(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int n = (historyArrayList.size() - 1); n >= 0; n--) {
|
||||
history.push(historyArrayList.get(n));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user