-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlaylist.cpp
243 lines (207 loc) · 4.78 KB
/
Playlist.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* Group 2
* Last Updated: 25 Apr 20
* COP 3330
* Group Members:
* Amani Muller
* Eric Lampley
* Zahir Cooper
* Claressa Wilson
* */
#include <string>
#include "Song.h"
#include <iomanip>
#include <iostream>
#include <vector>
#include <fstream>
#include "Playlist.h"
#include "StringHelper.h"
#include <stdlib.h>
using namespace std;
Playlist::Playlist()
{
songnumber = 0;
}
Playlist::Playlist(string title)
{
songnumber = 0;
this->title = title;
string song, artist,album, line;
int year, song_length;
ifstream file;
file.open((StringHelper::stou(title) + ".playlist").c_str());
while(getline(file,line))
{
Song s;
song = StringHelper::parse(line,',')[0];
artist = StringHelper::parse(line,',')[1];
album = StringHelper::parse(line,',')[2];
year = atoi(StringHelper::parse(line,',')[3].c_str());
song_length = atoi(StringHelper::parse(line,',')[4].c_str());
s.set(song,artist,album,song_length,year);
addSong(s);
writeToFile();
}
}
void Playlist::setTitle(string title)
{
this->title = title;
}
string Playlist::getTitle() const
{
return title;
}
//will take a Song object as a parameter and insert it into the play list.
void Playlist::addSong(Song song)
{
playlist.push_back(song);
}
//will take a Song object as a parameter and delete it from play list and return true/false.
bool Playlist:: deleteSong(Song & song)
{
for(int i = 0; i < playlist.size(); i++)
{
if(song == playlist[i])
{
playlist.erase(playlist.begin()+i);
return true;
}
}
return false;
}
Playlist:: Playlist(const Playlist & p2)
{
playlist = p2.playlist;
}
// Adds song to playlist current playlist
Playlist operator+(Playlist & p ,Song & s)
{
p.addSong(s);
return p;
}
// concatenation of the two playlist objects
Playlist operator+(Playlist & playlist, Playlist & p2)
{
vector<Song> s = p2.getPlaylist();
Playlist p3 = playlist;
for (int i = 0; i < s.size(); i++)
{
p3.addSong(s[i]);
}
return p3;
}
//removing a song(s) from a playlist
Playlist operator-(Playlist & p , Song & song)
{
p.deleteSong(song);
return p;
}
vector<Song> Playlist:: getPlaylist()
{
return playlist;
}
/*return a new playlist that is the intersection of the songs in the playlist argument and
the songs contained within the playlist object. This will contain no duplicates*/
Playlist Playlist::intersect(Playlist & p)
{
vector<Song> s = this ->playlist;
vector <Song> s1 = p.getPlaylist();
Playlist p1 = this ->title;
for (int i = 0; i < s.size(); i++)
{
if(s[i] == s1[i])
{
p1.appendToFile(s1[i]);
}
}
return p1;
}
/* return a new playlist that merges the songs in the playlist argument
and the songs contained within the playlist object which match been called.
This playlist will contain all songs, including duplicates.*/
Playlist Playlist::merge(Playlist & p)
{
Playlist p3 = this ->title;
vector<Song> v = p.getPlaylist();
for (int i = 0; i < v.size() ; i++)
{
p3.addSong(v[i]);
p3.appendToFile(v[i]);
}
return p3;
}
int Playlist::getPlayingMode()
{
int count = 0;
if(mode == 0)
{
return count;
count++;
}
if(mode == 1)
{
return count;
}
if(mode == 2)
{
return count;
}
return count;
}
// play's one song from the play list starting at first index
void Playlist::play()
{
if(mode == 0 || mode == 2)
{
cout << playlist[songnumber] << endl;
songnumber++;
}
if(mode == 1)
cout << playlist[songnumber] << endl;
if (mode == 2 && songnumber == playlist.size())
{
songnumber = 0;
}
if (mode == 0 && songnumber == playlist.size())
{
cout << " END OF PLAYLIST" << endl;
return;
}
}
// Keeps tract of the playing mode
void Playlist::setMode(char playingMode)
{
if(playingMode == 'N')
mode = 0;
if(playingMode == 'R')
mode = 1;
if(playingMode == 'L')
mode = 2;
}
void Playlist::appendToFile(Song song)
{
ofstream file;
file.open((StringHelper::stou(title) + ".playlist").c_str(), ios::app);
file << song;
}
void Playlist::writeToFile()
{
ofstream file;
file.open((StringHelper::stou(title) + ".playlist").c_str());
for (int i = 0; i < playlist.size(); i++)
{
file << playlist[i];
}
}
ostream& operator<<(ostream& os, const Playlist& playlist)
{
for(int i = 0; i < playlist.playlist.size(); i++)
{
os << playlist.playlist[i] << endl;
return os;
}
}
void Playlist::printPlaylist()
{
for(int i = 0; i < playlist.size(); i++)
cout << playlist[i] << endl;
}