-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibrary.cpp
More file actions
399 lines (332 loc) · 12.9 KB
/
Copy pathLibrary.cpp
File metadata and controls
399 lines (332 loc) · 12.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// Created by Morgan Ziegler on 11/19/19.
//
#include "Library.h"
#include "ArrayLib.h"
#include "Playlist.h"
#include "Song.h"
#include <iostream>
#include <fstream>
#include <sstream>
Library::Library(){
songs = new ArrayList(30);
playlists = new PlaylistArrayList(15);
loadLibrary();
}
Library::~Library(){
delete songs;
delete playlists;
}
void Library::loadLibrary(){
std::string filename = "library.txt";
std::ifstream infile(filename);
if (infile){
while (infile){
std::string line;
getline(infile, line);
std::stringstream splitter (line);
std::string words;
//std::cout << line << std::endl;
getline(splitter, words, ':');
if(line != "" && words == "Title"){
while(infile) {
getline(splitter, words, '\n');
std::string name = words;
newPlaylist(name);
while (infile) {
getline(infile, line);
std::stringstream splitter(line);
getline(splitter, words, ':');
if (words == "Title") {
getline(splitter, words, '\n');
break;
}
if (line != "") {
Song temp = Song(line);
addToPlaylist(name, temp.getArtist(), temp.getTitle());
}
}
}
}
else{
if(line != ""){
addSongToLibrary(line);
}
}
}
}else {
std::cout << "save file can not be read file!" << std::endl;
}
infile.close();
}
void Library::quit(){
std::ofstream myfile;
std::cout << "saving..." << std::endl;
std::ofstream file("library.txt", std::ios::trunc);
myfile.open("library.txt");
for(int i = 0; i <songs->itemCount();i++){
myfile << songs->getValueAt(i).toStringtoFile();
myfile << "\n";
}
for(int i = 0; i<playlists->itemCount();i++){
myfile <<"Title:" + playlists->getValueAt(i).getName();
myfile<<"\n";
for(int j = 0; j < playlists->getValueAt(i).playListLength(); j++) {
myfile << playlists->getValueAt(i).getSong(j);
myfile<<"\n";
}
}
std::cout << "library has been saved" << std::endl;
myfile.close();
}
void Library::AddSongsFromFile(std::string file_name){
std::string filename = file_name;
std::ifstream infile(filename);
if (infile){
std::cout << "reading file..." << std::endl;
while (infile){
std::string line;
getline(infile, line);
//std::cout << line << std::endl;
if(line != ""){
addSongToLibrary(line);
}
}
std::cout << "read" << std::endl;
}else {
std::cout << "file can not be read file!" << std::endl;
}
infile.close();
}
void Library::displaySongs() {
std::cout << "Library: " << std::endl;
for (int i =0; i < songs->itemCount(); i++){
std::cout << "\t" + songs->getValueAt(i).toString() + "\n";
}
}
void Library::discontinue(std::string file_name) {
std::string filename = file_name;
std::ifstream infile(filename);
if (infile){
std::cout << "reading file..." << std::endl;
while (infile){
std::string line;
getline(infile, line);
if(line != "") {
Song songIn = Song(line);
removeFromPlaylists(songIn.getArtist(), songIn.getTitle());
removeSongFromLibrary(line);
}
}
std::cout << "read" << std::endl;
}else {
std::cout << "file can not be read file!" << std::endl;
}
infile.close();
}
void Library::displayPlaylists() {
std::cout << "Playlists: " << std::endl;
int count = 0;
for (int i =0; i< playlists->itemCount(); i++){
std::cout << "Name: " + playlists->getValueAt(i).getName() + " Duration: " + playlists->getValueAt(i).calcDuration() << std::endl;
count ++;
}
if (count ==0){
std::cout << "\tNo Playlists available" << std::endl;
}
}
void Library::newPlaylist(std::string nameIn) {
int index = playlists->find(nameIn);
if (index == -1){
std::cout << "Playlist was created, Name : " + nameIn << std::endl;
playlists->insertAtEnd(Playlist(nameIn));
}
else{
std::cout << "Playlist with that name already exists" << std::endl;
}
}
void Library::removeFromPlaylist(std::string nameIn, std::string artist, std::string titleIn) {
int indexOfPlaylist = playlists->find(nameIn);
if (indexOfPlaylist != -1){
Playlist temp = playlists->getValueAt(indexOfPlaylist);
temp.removeSong(artist,titleIn,1);
playlists->removeValueAt(indexOfPlaylist);
playlists->insertAt(temp,indexOfPlaylist);
std::cout << titleIn + " by " + artist + " was removed from the playlist: " + nameIn << std::endl;
}
}
void Library::removeFromPlaylists(std::string artist, std::string titleIn) {
for (int i = 0; i < playlists->itemCount(); i++){
Playlist temp = playlists->getValueAt(i);
temp.removeSong(artist,titleIn,0);
playlists->removeValueAt(i);
playlists->insertAt(temp,i);
}
}
void Library::addToPlaylist(std::string nameIn, std::string artist, std::string titleIn) {
int indexOfPlaylist = playlists->find(nameIn);
int indexOfSong = songs->findArtistandTitle(songs->itemCount(),artist,titleIn);
if (indexOfSong == -1) {
std::cout << "\tSong is not in Library" <<std::endl;
}
else if (indexOfPlaylist == -1){
std::cout << "\tPlaylist with that name does not exist" << std::endl;
}
else{
Playlist tempPlay = playlists->getValueAt(indexOfPlaylist);
Song temp = songs->getValueAt(indexOfSong);
tempPlay.addSong(temp.toStringtoFile());
playlists->removeValueAt(indexOfPlaylist);
playlists->insertAt(tempPlay,indexOfPlaylist);
std::cout << titleIn + " by " + artist + " was added to the playlist: " + nameIn << std::endl;
}
}
void Library::displayArtist(std::string artistIn) {
int count = 0;
std::cout << "Songs by " + artistIn + ": " << std::endl;
for (int i =0; i < songs->itemCount(); i++){
if (artistIn == songs->getValueAt(i).getArtist()){
count += 1;
std::cout << "\t"+songs->getValueAt(i).toString() << std::endl;
}
}
if (count ==0){
std::cout << "\tNo songs by that artist were found" << std::endl;
}
}
void Library::displaySong(std::string artistIn, std::string titleIn) {
int index = songs->findArtistandTitle(songs->itemCount(),artistIn,titleIn);
if (index != -1){
std::cout << songs->getValueAt(index).toString() << std::endl;
}
else{
std::cout << "\tSong was not in the Library" << std::endl;
}
}
void Library::displayPlaylist(std::string nameIn) {
int index = playlists->find(nameIn);
if (index != -1){
std::cout << nameIn + " Duration: " + playlists->getValueAt(index).calcDuration() << std::endl;
std::cout << playlists->getValueAt(index).toString()<<std::endl;
}
else{
std::cout << "Playlist does not exist" << std::endl;
}
}
void Library::playNext(std::string nameIn) {
int index = playlists->find(nameIn);
if (index != -1) {
Playlist tempPlay = playlists->getValueAt(index);
std::string msg = tempPlay.playNext();
playlists->removeValueAt(index);
playlists->insertAt(tempPlay, index);
if (msg == "Playlist is empty") {
playlists->removeValueAt(index);
std::cout << "Playlist is already empty, it will now be removed" << std::endl;
} else {
Song temp = Song(msg);
int songindex = songs->findArtistandTitle(songs->itemCount(), temp.getArtist(), temp.getTitle());
std::cout << "Song being played: " + songs->getValueAt(songindex).toString() << std::endl;
Song real = songs->getValueAt(songindex);
real.incrementPlayCount();
songs->removeValueAt(songindex);
songs->insertAt(real, songindex);
if (playlists->getValueAt(index).isEmpty()) {
playlists->removeValueAt(index);
}
}
}
else{
std::cout<< "Playlist with this name does not exist" <<std::endl;
}
}
void Library::newRandomPlaylist(std::string nameIn, std::string duration) {
if (playlists->find(nameIn) == -1){
Playlist randPlaylist = Playlist(nameIn);
std::stringstream splitter (duration);
std::string words;
int count = 0;
int maxDuration = 0;
if (splitter){
getline(splitter, words, ':');
while (splitter){
int temp = stoi(words);
// it to the integer x
if(count == 0) {
count++;
maxDuration += temp * 60 * 60;
}
else if(count == 1) {
count++;
maxDuration += temp * 60;
}
else{
maxDuration += temp;
}
getline(splitter, words, ':');
}
}
int failCount = 0;
int runningDuration = 0;
while (failCount != songs->itemCount()*2){
int randIndex = genRandInt(0, songs->itemCount()-1);
Song randSong = songs->getValueAt(randIndex);
if (randPlaylist.find(randSong.getArtist(),randSong.getTitle()) != -1){
failCount++;
}
else{
if (runningDuration + randSong.getDuration() > maxDuration){
failCount ++;
}
else{
failCount = 0;
runningDuration += randSong.getDuration();
randPlaylist.addSong(randSong.toStringtoFile());
}
}
}
playlists->insertAtEnd(randPlaylist);
std::cout << "a random playlist was created. Playlist: " + nameIn << std::endl;
}
else{
std::cout << "Playlist with this name already exists" << std::endl;
}
}
void Library::addSongToLibrary(std::string songIn) {
if (songs->isEmpty()){
songs->insertAtFront(Song(songIn));
}
else {
Song temp = Song(songIn);
if (songs->findArtistandTitle(songs->itemCount(), temp.getArtist(), temp.getTitle()) == -1) {
songs->insertAtAlphabetized(Song(songIn));
} else {
std::cout << Song(songIn).toString() << " is already in the library" << std::endl;
}
}
}
void Library::removeSongFromLibrary(std::string songIn) {
Song temp = Song(songIn);
int indexSong = songs->findArtistandTitle(songs->itemCount(), temp.getArtist(), temp.getTitle());
if(indexSong != -1) {
songs->removeValueAt(indexSong);
}else{
std::cout << Song(songIn).toString() << " is not in the library" << std::endl;
}
}
void Library::help(){
std::cout << "help \n\t Provides a summary of all available commands." << std::endl;
std::cout << "library \n\t Display all songs in alphabetical order by artist (within artist alphabetical by song)" << std::endl;
std::cout << "artist<artist> \n\tDisplay all songs for the given artist." << std::endl;
std::cout << "song<artist,title> \n\tDisplay all information for the given song." <<std::endl;
std::cout << "import<filename>\n\tAdd all songs from the given file to the library." <<std::endl;
std::cout << "discontinue<filename>\n\tRemove all songs from the given file from the library. Also remove these songs from any playlist in which they occur." <<std::endl;
std::cout << "playlists\n\tDisplay the names of all playlists and their durations." << std::endl;
std::cout << "playlist<name>\n\tDisplay all songs left in the given playlist, and the duration (time it will take to play the remaining songs)" << std::endl;
std::cout << "new<name>\n\tMake a new empty playlist with the given name." <<std::endl;
std::cout << "add<name,artist,title>\n\tAdd the given song to the end of the given playlist" << std::endl;
std::cout << "remove<name,artist,title>\n\tremove the given song from the playlist" << std::endl;
std::cout << "playnext<name>\n\tPrint all information about the next song to be played from the given playlist to the screen." <<std::endl;
std::cout << "newrandom<name,duration>\n\tMake a new playlist with the given name, and populate it with a random group of songs that do not repeat. \n\tInput duration like 0:12:2 if the desired length of the playlist was 0 hours, 12 minutes, and 2 seconds" << std::endl;
std::cout << "quit\n\tSave the library and all playlists and terminate execution." << std::endl;
}