summaryrefslogtreecommitdiff
path: root/MusicPlaylist.java
diff options
context:
space:
mode:
Diffstat (limited to 'MusicPlaylist.java')
-rw-r--r--MusicPlaylist.java95
1 files changed, 95 insertions, 0 deletions
diff --git a/MusicPlaylist.java b/MusicPlaylist.java
new file mode 100644
index 0000000..c25e4eb
--- /dev/null
+++ b/MusicPlaylist.java
@@ -0,0 +1,95 @@
+import java.util.*;
+
+public class MusicPlaylist {
+
+ private Queue<String> playlist;
+ private Stack<String> history;
+
+ public MusicPlaylist() {
+ playlist = new LinkedList<>();
+ history = new Stack<>();
+ }
+
+ // add song to playlist according to user input
+ // no return
+ public void addSong(String songToAdd) {
+ System.out.print("Enter song name: ");
+ 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 void playSong() {
+ // 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 void printHistory() {
+ 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();
+ }
+
+ public void clearHistory() {
+ playlist.clear();
+ }
+
+ // delete from history according to user input, starting either from most recent or oldest history
+ // return modified history
+ public void deleteFromHistory(int numToDelete) {
+ 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 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));
+ }
+ }
+ }