From 3813621611945a0be23384c0c4d395b4c4a614ad Mon Sep 17 00:00:00 2001 From: pants Date: Mon, 21 Sep 2026 14:17:15 -0700 Subject: init --- java/MusicPlaylist.java | 135 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 java/MusicPlaylist.java (limited to 'java/MusicPlaylist.java') diff --git a/java/MusicPlaylist.java b/java/MusicPlaylist.java new file mode 100644 index 0000000..b127dd7 --- /dev/null +++ b/java/MusicPlaylist.java @@ -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 playlist = new LinkedList<>(); + Stack 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 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 playlist, Stack 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 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 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 history) { + int size = history.size(); + List 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)); + } + } +} -- cgit v1.2.3