-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaylist.cpp
More file actions
274 lines (232 loc) · 7.07 KB
/
Copy pathPlaylist.cpp
File metadata and controls
274 lines (232 loc) · 7.07 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include "Playlist.h"
#include "Song.h"
#include <fstream>
#include <sstream>
Playlist::Playlist()
{
}
Playlist::Playlist(std::vector<Song> &p) : playlist(p)
{
}
Playlist::~Playlist()
{
}
void Playlist::get_playlist_creator()
{
std::cout << "Playlist made by: Mat & ";
}
bool Playlist::check_song(const Song &song)
{
for (int i = 0; i < playlist.size(); i++)
if (playlist[i] == song)
return 0;
return 1;
}
void Playlist::add_song(const Song &song)
{
if (check_song(song) == 0)
{
throw my_exception("Song is already added to the playlist");
}
playlist.push_back(song);
}
const Song &Playlist::operator[](int i) const
{
if (i < 0 || i >= playlist.size())
{
throw std::out_of_range("Index out of bounds");
}
return playlist[i];
}
Song &Playlist::operator[](int i)
{
if (i < 0 || i >= playlist.size())
{
throw std::out_of_range("Index out of bounds");
}
return playlist[i];
}
void Playlist::skip_song(int i) const
{
if (i + 1 < 0 || i + 1 >= playlist.size())
{
throw std::out_of_range("No more songs to skip to");
}
const Song &next = playlist[i + 1];
std::cout << "Now Playing: " << next << '\n';
std::cout << "By: ";
next.get_artist().print();
std::cout << '\n';
std::cout << "From album: " << next.get_album() << '\n';
int t = next.get_time();
std::cout << "Duration: " << t / 60 << ":" << (t % 60 < 10 ? "0" : "") << t % 60 << " minutes" << '\n';
}
int Playlist::size() const
{
return playlist.size();
}
int Playlist::total_duration(const Playlist &p)
{
int total = 0;
for (const auto &song : p.playlist)
{
total += song.get_time();
}
return total;
}
void Playlist::sort_by_title()
{
std::sort(playlist.begin(), playlist.end(), [](const Song &a, const Song &b) {
return a.get_name() < b.get_name();
});
}
void Playlist::sort_by_duration()
{
std::sort(playlist.begin(), playlist.end(), [](const Song &a, const Song &b) {
return a.get_time() < b.get_time();
});
}
std::vector<int> Playlist::search_song(const std::string &query) const
{
std::vector<int> results;
std::string q = query;
std::transform(q.begin(), q.end(), q.begin(), ::tolower);
for (int i = 0; i < playlist.size(); i++)
{
std::string name = playlist[i].get_name();
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
if (name.find(q) != std::string::npos)
{
results.push_back(i);
}
}
return results;
}
void Playlist::save_to_file(const std::string &filename) const
{
std::ofstream out(filename);
if (!out) return;
for (const auto &s : playlist)
{
std::string name = s.get_name();
std::replace(name.begin(), name.end(), '|', ' '); // Sanitize pipes
out << name << "|";
const Artist &a = s.get_artist();
std::string artist_name = a.get_name();
std::replace(artist_name.begin(), artist_name.end(), '|', ' ');
out << artist_name << "|";
const FeaturedArtist *fa = dynamic_cast<const FeaturedArtist*>(&a);
if (fa) {
std::string feat = fa->get_featured_name();
std::replace(feat.begin(), feat.end(), '|', ' ');
out << feat;
}
std::string album = s.get_album().get_name();
std::replace(album.begin(), album.end(), '|', ' ');
out << "|" << album << "|" << s.get_time() << "|" << s.get_play_count() << "\n";
}
}
void Playlist::save_song_to_file(const std::string &filename, const Song &s) const
{
std::ofstream out(filename, std::ios::app);
if (!out) return;
out << s.get_name() << "|";
const Artist &a = s.get_artist();
out << a.get_name() << "|";
const FeaturedArtist *fa = dynamic_cast<const FeaturedArtist*>(&a);
if (fa) {
out << fa->get_featured_name();
}
out << "|" << s.get_album().get_name() << "|" << s.get_time() << "|" << s.get_play_count() << "\n";
}
void Playlist::load_from_file(const std::string &filename)
{
std::ifstream in(filename);
if (!in) return;
playlist.clear();
std::string line;
while (std::getline(in, line))
{
if (line.empty()) continue;
try {
std::stringstream ss(line);
std::string name, artist, feat, album, duration, playcount;
std::getline(ss, name, '|');
std::getline(ss, artist, '|');
std::getline(ss, feat, '|');
std::getline(ss, album, '|');
std::getline(ss, duration, '|');
std::getline(ss, playcount, '|');
if (name.empty() || artist.empty() || album.empty() || duration.empty()) continue;
Artist artist_obj(artist);
Song s;
if (!feat.empty()) {
FeaturedArtist fa(artist_obj, feat);
s = Song(name, fa, Album(album));
} else {
s = Song(name, artist_obj, Album(album));
}
s.set_time(std::stoi(duration));
if (!playcount.empty()) {
s.set_play_count(std::stoi(playcount));
}
playlist.push_back(s);
} catch (...) {
continue; // Skip malformed lines
}
}
}
void Playlist::show_top_songs(int n) const
{
std::vector<Song> sorted = playlist;
std::sort(sorted.begin(), sorted.end(), [](const Song &a, const Song &b) {
return a.get_play_count() > b.get_play_count();
});
std::cout << "\n--- Top " << n << " Songs ---" << std::endl;
for (int i = 0; i < n && i < sorted.size(); i++) {
std::cout << i + 1 << ". " << sorted[i].get_name() << " - Played " << sorted[i].get_play_count() << " times" << std::endl;
}
}
#include <map>
void Playlist::show_top_artists(int n) const
{
std::map<std::string, int> artist_counts;
for (const auto &s : playlist) {
artist_counts[s.get_artist().get_name()] += s.get_play_count();
}
std::vector<std::pair<std::string, int>> sorted_artists(artist_counts.begin(), artist_counts.end());
std::sort(sorted_artists.begin(), sorted_artists.end(), [](const std::pair<std::string, int> &a, const std::pair<std::string, int> &b) {
return a.second > b.second;
});
std::cout << "\n--- Top " << n << " Artists ---" << std::endl;
for (int i = 0; i < n && i < sorted_artists.size(); i++) {
std::cout << i + 1 << ". " << sorted_artists[i].first << " - Total " << sorted_artists[i].second << " plays" << std::endl;
}
}
CollaborativePlaylist::CollaborativePlaylist()
{
}
CollaborativePlaylist::~CollaborativePlaylist()
{
}
void CollaborativePlaylist::share_song(const std::string &user)
{
get_playlist_creator();
if (user == "")
{
throw std::runtime_error("User name cannot be empty");
}
std::cout << user << std::endl;
}
my_exception::my_exception(const std::string &msg) : message(msg)
{
}
const char *my_exception::what() const noexcept
{
return message.c_str();
}