-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaylist.cpp
More file actions
223 lines (174 loc) · 4.54 KB
/
Playlist.cpp
File metadata and controls
223 lines (174 loc) · 4.54 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**@file Playlist.cpp
* Implementation of Playlist.h to create Playlist objects
* Each Playlist is a LinkedList of Songs
* @authors Forrest Wargo, Rup Patel, Elias Platt
*/
#include "Playlist.h"
/**
* Default Constructor
*/
Playlist::Playlist() {
front = nullptr;
end = nullptr;
title = "Untitled Playlist";
numOfSongs = 0;
duration=0;
}
/**
* Constructor
*/
Playlist::Playlist(std::string titleIn) {
front = nullptr;
end = nullptr;
title = titleIn;
numOfSongs = 0;
duration=0;
}
//Destructor
Playlist::~Playlist() {
LinkedNode *temp = front;
while (front != nullptr) {
LinkedNode *temp = front;
front = front->getNext();
delete temp;
}
end = nullptr;
}
std::string Playlist::getTitle(){
return title;
}
/**
* gives a string representation of the current list
* @returns a string representing the given list in the exact format shown
*{0: here comes the sun (Artist: beatles; Length: 212 secs; Year: 1967), 1: Billie Jean (Artist: Michael Jackson; Length: 356 secs; Year: 1980)}
*/
std::string Playlist::toString() {
if (isEmpty()){
return "{}";
}
std::string result = "{duration = ";
result+= std::to_string(duration);
result+= " seconds, songs left: ";
LinkedNode* temp = front;
for(int i=0; i<numOfSongs-1; i++){
result += temp->getSong()->getTitle();
result += ", ";
temp = temp->getNext();
}
result += temp->getSong()->getTitle();
result+="}";
return result;
}
int Playlist::getDuration(){
return duration;
}
/**
* Searches a playlist based on the title of the song and artist of the song
* @return the index of the first occurrence of numToFind if it is present, otherwise returns -1
*/
int Playlist::findSong(std::string title, std::string artist) {
if (isEmpty()){
return -1;
}
int index = 0;
LinkedNode *temp;
temp = front;
while(temp != nullptr) {
if (temp->getSong()->getTitle() == title && temp->getSong()->getArtist() == artist) {
return index;
} else {
temp = temp->getNext();
index++;
}
}
return -1;
}
/**
* checks if there are any valid items in the list
* @return true if there are no valid items in the list, false otherwise
*/
bool Playlist::isEmpty() {
return numOfSongs == 0;
}
/**
* returns a count of valid items currently in the list
* @returns the number of valid items in the list
*/
int Playlist::getNumSongs(){
return numOfSongs;
}
/**
* removes the item at index from the list, and returns a copy of that item
* @param index the location from which to get the value
* @post the item at index is removed from the list, everything else is shifted down one
* @return a copy of the item at index
*/
std::string Playlist::removeSong(int index) { //returns song information
if (this->isEmpty()){
return "no songs";
}
if(index >= numOfSongs || index <0){
return "out of bounds";
}
if(index ==0){
LinkedNode* temp = front;
duration -= front->getSong()->getLength();
front = front->getNext();
delete temp;
numOfSongs--;
return "song deleted";
}
LinkedNode* temp = front;
LinkedNode* tempBefore;
for(int i=0; i<index; i++){
tempBefore = temp;
temp = temp->getNext();
}
tempBefore->setNext(temp->getNext());
duration -= temp->getSong()->getLength();
delete temp;
numOfSongs--;
return "song deleted";
}
/**
* appends the new item to the end of the playlist
* @post the playlist has an additional song in it, at the end
*/
void Playlist::insertAtEnd(Song* song) {
LinkedNode *newNode = new LinkedNode(song);
if (front == nullptr) {
front=newNode;
end=newNode;
duration+=song->getLength();
} else {
LinkedNode *temp = front;
while(temp->getNext() != nullptr){
temp=temp->getNext();
}
temp->setNext(newNode);
end=newNode;
duration+=song->getLength();
}
numOfSongs++;
}
/**
*
* @param index
* @return gets pointer to the song at the specified index
*/
Song* Playlist::getSong(int index){
if (this->isEmpty()){
throw std::out_of_range("playlist is empty");
}
if(index >= numOfSongs || index <0){
return nullptr;
}
LinkedNode* temp = front;
for(int i=0; i<index; i++){
temp = temp->getNext();
}
return temp->getSong();
}
LinkedNode* Playlist::getFront() {
return front;
}