summaryrefslogtreecommitdiff
path: root/java/MusicPlaylist.java
blob: b127dd7a0bb9718d32fa0f552944a76603c2bbcc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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));
        }
    }
}