summaryrefslogtreecommitdiff
path: root/MusicPlaylist.java
blob: c25e4eb1bdb0e7430ea04196aac5d5e00282f4b2 (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
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));
		}
	}
	}