-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
314 lines (289 loc) · 11.9 KB
/
Copy pathmain.cpp
File metadata and controls
314 lines (289 loc) · 11.9 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <iostream>
#include <cstring>
#include <vector>
#include <random>
#include <limits>
#include <cmath>
#include <algorithm>
#include <memory>
#include "Song.h"
#include "Artist.h"
#include "Album.h"
#include "Playlist.h"
#include "Audio.h"
#include "Vector.hpp"
int main()
{
std::cout << "My Playlist:" << '\n';
Playlist p;
p.load_from_file("playlist.txt");
for (int i = 0; i < p.size(); i++)
std::cout << i + 1 << ") " << p[i] << '\n';
std::cout << '\n';
std::cout << "Choose what you want to do:" << '\n';
std::cout << "Play a song (1)" << '\n';
std::cout << "Shuffle playlist (2)" << '\n';
std::cout << "Add a song (3)" << '\n';
std::cout << "Download playlist (4)" << '\n';
std::cout << "Make the playlist collaborative (5)" << '\n';
std::cout << "Listen to a podcast instead (6)" << '\n';
std::cout << "Search for a song (7)" << '\n';
std::cout << "Sort playlist (8)" << '\n';
std::cout << "Queue management (9)" << '\n';
std::cout << "Show Top Stats (10)" << '\n';
std::cout << "Exit (0)" << '\n';
int choice;
std::vector<Song> play_queue;
while (std::cout << "\nChoice: " && std::cin >> choice && choice != 0)
{
if (choice == 1)
{
std::cout << "Choose the song number (1-" << p.size() << "):" << '\n';
int index;
std::cin >> index;
index--;
if (index < 0 || index >= p.size())
{
std::cout << "Invalid index" << std::endl;
}
else
{
std::cout << "Now Playing: " << p[index] << '\n';
std::cout << "By: ";
p[index].get_artist().print();
std::cout << '\n';
std::cout << "From album: " << p[index].get_album() << '\n';
std::cout << "Duration: " << p[index].get_time() / 60 << ":" << (p[index].get_time() % 60 < 10 ? "0" : "") << p[index].get_time() % 60 << " minutes" << '\n';
p[index].increment_play_count();
std::cout << "Skip song? y/n" << '\n';
char answer;
std::cin >> answer;
while (answer == 'y')
{
try {
p.skip_song(index);
index++;
std::cout << "Skip song? y/n" << '\n';
std::cin >> answer;
} catch (const std::out_of_range& e) {
std::cout << e.what() << std::endl;
break;
}
}
}
}
else if (choice == 2)
{
if (p.size() == 0) {
std::cout << "Playlist is empty" << std::endl;
} else {
std::random_device r;
std::default_random_engine e1(r());
std::uniform_int_distribution<int> uniform_dist(0, p.size() - 1);
int random_index = uniform_dist(e1);
std::cout << "Now Playing: " << p[random_index] << '\n';
std::cout << "By: ";
p[random_index].get_artist().print();
std::cout << '\n';
std::cout << "From album: " << p[random_index].get_album() << '\n';
int t = p[random_index].get_time();
std::cout << "Duration: " << t / 60 << ":" << (t % 60 < 10 ? "0" : "") << t % 60 << " minutes" << '\n';
}
}
else if (choice == 3)
{
std::string input_song;
std::cout << "Enter song name:" << '\n';
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, input_song);
std::string artist_name;
std::cout << "Enter artist name:" << '\n';
std::getline(std::cin, artist_name);
Artist artist9(artist_name);
std::string album_name;
std::cout << "Enter album name:" << '\n';
std::getline(std::cin, album_name);
Album album9(album_name);
Song song9(input_song, artist9, album9);
float input_time;
std::cout << "Enter the song's duration (ex 4.20 for 4 min 20 sec):" << '\n';
std::cin >> input_time;
int minutes = (int)input_time;
int seconds = (int)round((input_time - minutes) * 100);
song9.set_time(minutes * 60 + seconds);
try {
p.add_song(song9);
std::cout << "Song added successfully" << std::endl;
} catch (const std::exception& e) {
std::cout << "Error: " << e.what() << std::endl;
}
}
else if (choice == 4)
{
std::cout << "--Playlist downloaded--" << '\n';
p.save_to_file("playlist.txt");
std::cout << "Changes saved to playlist.txt" << std::endl;
std::cout << "Number of songs in playlist: " << p.size() << '\n';
int total = Playlist::total_duration(p);
std::cout << "Playlist duration: " << total / 60 << " minutes and " << total % 60 << " seconds" << std::endl;
}
else if (choice == 5)
{
std::string your_name;
std::cout << "Enter your name: " << '\n';
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, your_name);
if (your_name == "")
{
std::cout << "Username cannot be empty" << std::endl;
}
else
{
CollaborativePlaylist cp;
cp.share_song(your_name);
}
}
else if (choice == 6)
{
std::string podcast_name;
std::string podcast_host;
int nr_of_eps;
std::cout << "Enter the name of the podcast: " << '\n';
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, podcast_name);
std::cout << "Enter the host of the podcast: " << '\n';
std::getline(std::cin, podcast_host);
std::cout << "Enter the number of episodes: " << '\n';
if (!(std::cin >> nr_of_eps)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input for episodes" << std::endl;
} else {
try
{
if (nr_of_eps < 1)
{
throw std::invalid_argument("Number of episodes must be >= 1");
}
Podcast pod(podcast_name, podcast_host, nr_of_eps);
std::cout << "--Now listening to: " << '\n';
pod.print_details();
std::cout << std::endl;
int pod_index = 1;
std::cout << " Episode " << pod_index << "/" << pod.get_nr_of_ep() << '\n';
std::cout << "Next episode? (y/n)" << '\n';
char pod_answer;
std::cin >> pod_answer;
while (pod_answer == 'y')
{
pod_index++;
if (pod_index > pod.get_nr_of_ep())
{
std::cout << "There are no more episodes left" << std::endl;
break;
}
else
{
std::cout << "Now playing episode " << pod_index << "/" << pod.get_nr_of_ep() << '\n';
std::cout << "Next episode? (y/n)" << '\n';
std::cin >> pod_answer;
}
}
}
catch (const std::invalid_argument &e)
{
std::cout << "Error: " << e.what() << std::endl;
}
}
}
else if (choice == 7)
{
std::string query;
std::cout << "Enter search query: ";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::getline(std::cin, query);
std::vector<int> results = p.search_song(query);
if (results.empty()) {
std::cout << "No songs found" << std::endl;
} else {
std::cout << "Found " << results.size() << " songs:" << std::endl;
for (int idx : results) {
std::cout << idx + 1 << ") " << p[idx] << " by " << p[idx].get_artist() << std::endl;
}
}
}
else if (choice == 8)
{
std::cout << "Sort by: (1) Title, (2) Duration: ";
int sort_choice;
std::cin >> sort_choice;
if (sort_choice == 1) {
p.sort_by_title();
std::cout << "Playlist sorted by title" << std::endl;
} else if (sort_choice == 2) {
p.sort_by_duration();
std::cout << "Playlist sorted by duration" << std::endl;
} else {
std::cout << "Invalid sort choice" << std::endl;
}
std::cout << "\nUpdated Playlist" << std::endl;
for (int i = 0; i < p.size(); i++)
std::cout << i + 1 << ") " << p[i] << " by " << p[i].get_artist() << '\n';
}
else if (choice == 9)
{
std::cout << "Queue Management:\n (1) Add to queue\n (2) View queue\n (3) Play next\n (4) Clear queue\n";
int q_choice;
std::cin >> q_choice;
if (q_choice == 1) {
std::cout << "Enter song number to add (1-" << p.size() << "): ";
int idx;
std::cin >> idx;
if (idx >= 1 && idx <= p.size()) {
play_queue.push_back(p[idx-1]);
std::cout << "Added to queue: " << p[idx-1] << std::endl;
} else {
std::cout << "Invalid song number" << std::endl;
}
} else if (q_choice == 2) {
if (play_queue.empty()) {
std::cout << "Queue is empty" << std::endl;
} else {
std::cout << "Current Queue:" << std::endl;
for (size_t i = 0; i < play_queue.size(); i++) {
std::cout << i + 1 << ") " << play_queue[i] << " by " << play_queue[i].get_artist() << std::endl;
}
}
} else if (q_choice == 3) {
if (play_queue.empty()) {
std::cout << "Queue is empty" << std::endl;
} else {
Song next = play_queue[0];
play_queue.erase(play_queue.begin());
std::cout << "Now Playing from Queue: " << next << std::endl;
std::cout << "By: ";
next.get_artist().print();
std::cout << "\nDuration: " << next.get_time() / 60 << ":" << (next.get_time() % 60 < 10 ? "0" : "") << next.get_time() % 60 << std::endl;
std::vector<int> results = p.search_song(next.get_name());
if (!results.empty()) {
p[results[0]].increment_play_count();
}
}
} else if (q_choice == 4) {
play_queue.clear();
std::cout << "Queue cleared" << std::endl;
} else {
std::cout << "Invalid choice" << std::endl;
}
}
else if (choice == 10)
{
p.show_top_songs(5);
p.show_top_artists(5);
}
else {
std::cout << "Invalid choice" << std::endl;
}
}
return 0;
}