-
Notifications
You must be signed in to change notification settings - Fork 0
/
Library.cpp
210 lines (182 loc) · 5.59 KB
/
Library.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
//Created by Elon Skolnik on 11/28/18.
//Stores a list of all the songs and a list of playlists on which users can perform basic operations.
#include <iostream>
#include "Library.h"
#include "Song.h"
#include "SongList.h"
#include "Playlist.h"
//Constructor
Library::Library(int playlistCapacity){
songList = new SongList(10);
playlists = new Playlist*[10];
playlistsize=10;
playlistCount = 0;
this->playlistCapacity = playlistCapacity;
}
//Destructor
Library::~Library(){
delete songList;
delete[] playlists;
}
void Library::doubleCapacity(){
Playlist** newArray = new Playlist*[this->playlistCapacity*2];
for(int i = 0; i < playlistCount; i++){
newArray[i] = playlists[i];
}
delete[] playlists;
playlists = newArray;
playlistCapacity *= 2;
}
void Library::importSong(std::string title, std::string artist, float duration){
Song* newSong = new Song(title, artist, duration);
songList->addSong(newSong);
}
void Library::newPlaylist(std::string name){
if(playlistCapacity <= playlistCount-1) {
doubleCapacity();
}
Playlist* newPlaylist = new Playlist(name);
playlists[playlistCount] = newPlaylist;
playlistCount++;
}
//creates a new playlist of the given length composed of randomly selected songs
void Library::genRandomPlaylist(std::string name, float duration){
if(playlistCapacity <= playlistCount-1) {
doubleCapacity();
}
bool isThere, check;
bool full = false;
Playlist* newPlaylist = new Playlist(name);
//generate a random index and pick the song of that index in the list
int randNum = rand() % songList->itemCount() + 1;
Song* songToAdd = songList->getValueAt(randNum);
newPlaylist->addSong(songToAdd);
std::string added[100] = {};
added[0] = songToAdd->getTitle();
int addedLength = 1;
//keep going until it reaches the given duration
while(newPlaylist->calcDuration() < duration && !full) {
isThere = false;
check = false;
//keeps going until it finds a song that wasn't used already
while (!check) {
int randNum = rand() % songList->itemCount() + 1;
songToAdd = songList->getValueAt(randNum);
for (int i = 0; i < addedLength; i++) {
if (songToAdd->getTitle().compare(added[i]) == 0)
isThere = true;
}
if (!isThere)
check = true;
if (addedLength == songList->itemCount()){
check = true;
full = true;
}
}
if ((newPlaylist->calcDuration() + songToAdd->getDuration()) < duration) {
newPlaylist->addSong(songToAdd);
std::cout<<"song added" <<std::endl;
added[addedLength] = songToAdd->getTitle();
addedLength++;
}
if(full){
std::cout<<"Ran out of unique songs in the current library" <<std::endl;
}
}
playlists[playlistCount] = newPlaylist;
playlistCount++;
}
//removes the given song from the library of songs
void Library::discontinue(std::string name, std::string artist){
songList->removeSong(name, artist);
}
//save the current library to a file
void Library::saveLib(){
//todo
}
//return info about all the songs in the library
std::string Library::songsInfo(){
if(songList->isEmpty()){
return "No songs are in the current library";
}
return songList->toString();
}
//return info for all songs by the given artist
std::string Library::artistInfo(std::string artist){
return songList->findArtist(artist);
}
//return the names of all playlists and their durations
std::string Library::playlistsInfo(){
if(playlistCount < 0){
throw std::out_of_range("No playlists are in the current library");
}
std::string info = "{";
for(int i = 0; i < playlistCount; i++){
info += playlists[i]->getTitle() + ": ";
info += ", " + playlists[i]->getInfo() + "\n";
if(i < playlistCount -1){
info += ", ";
}
else{
info += "}";
}
}
return info;
}
//return the contents of the given playlist
std::string Library::playlistInfo(std::string title){
int index = -1;
for (int i = 0; i < playlistCount; i++){
if(playlists[i]->getTitle() == title){
index = i;
}
}
if(index < 0){
return "{}";
}
return playlists[index]->getInfo();
}
Playlist** Library::getPlaylist(){
return playlists;
}
Playlist* Library::findPlaylist(std::string title){
int index = -1;
for(int i = 0; i < playlistCount; i++){
if(title == playlists[i]->getTitle()){
index = i;
}
}
if(index < 0){
return nullptr;
}
return playlists[index];
}
Song* Library::findSong(std::string title, std::string artist){
return songList->getValueAt(songList->findSong(title, artist));
}
void Library::emptyLibrary(){
songList->clearList();
for (int x=0; x<playlistCount;x++){
delete playlists[x];
}
delete playlists;
}
void Library::removeFromPlaylist(int index, std::string title, std::string artist){
playlists[index]->removeSong(title, artist);
}
void Library::addToPlaylist(int index, Song* SongToAdd){
playlists[index]->addSong(SongToAdd);
}
void Library::deletePlaylist(std::string title){
int index = -1;
for(int i = 0; i < playlistCount; i++){
if(title == playlists[i]->getTitle()){
index = i;
}
}
delete playlists[index];
for (int i = index; i < playlistCount; i++){
playlists[i] = playlists[i+1];
}
playlistCount--;
}