-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.cpp
More file actions
136 lines (124 loc) · 4.09 KB
/
Collection.cpp
File metadata and controls
136 lines (124 loc) · 4.09 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
#include "Collection.h"
//Default constructor
Collection::Collection() {
albums = std::vector<Album *>();
}
//Constructor that takes a vector of Album pointers as parameter.
Collection::Collection(std::vector<Album *> albums) {
this->albums = std::move(albums);
}
const std::vector<Album *> &Collection::getAlbums() const {
return albums;
}
//Adds an album to the collection and returns a pointer to the album.
Album *Collection::addAlbum(Album *albPointer) {
this->albums.push_back(albPointer);
return this->albums[albums.size() - 1];
}
Duration Collection::pinkFloyd() {
std::vector<Track> allTracks;
//Find all Pink Floyd albums and add all tracks belonging to Pink Floyd albums to allTracks vector.
for (int i = 0; i < albums.size(); i++) {
if (albums[i]->getArtist() == "Pink Floyd") {
for (auto &t : albums[i]->getTracks()) {
allTracks.push_back(t);
}
}
}
//Add all Pink Floyd tracks to a blank duration object
Duration totDuration = Duration();
for (int j = 0; j < allTracks.size(); j++) {
totDuration.add(allTracks.at(j).getDuration());
}
std::cout << "---The Total Duration Of All Pink Floyd Albums is:---" << std::endl;
return totDuration;
}
Track Collection::longestTrack() {
std::vector<Track> allTracks;
//Add all tracks in collection to a vector allTracks.
for (int i = 0; i < albums.size(); i++) {
for (auto &t : albums[i]->getTracks()) {
allTracks.push_back(t);
}
}
//Iterate over the vector to find the longest Track.
Track largestTrack = allTracks.at(0);
for (int j = 1; j < allTracks.size(); j++) {
if (allTracks.at(j).getDuration() > largestTrack.getDuration()) {
largestTrack = allTracks.at(j);
}
}
std::cout << "\n---The Longest Track Is:---" << std::endl;
return largestTrack;
}
//Finds the album with the largest number of tracks.
Album Collection::mostTracks() {
Album mostTracks = Album();
for (auto &a : albums) {
if (a->getTracks().size() > mostTracks.getTracks().size()) {
mostTracks = *a;
}
}
std::cout << "\n---The Album With The Most Tracks Is:---" << std::endl;
return mostTracks;
}
//Insertion sort to arrange a collection alphabetically.
void Collection::sort() {
int i, j;
Album *albumKeyP;
for (i = 1; i < albums.size(); i++) {
albumKeyP = albums[i];
j = i - 1;
while (j >= 0 && *this->albums[j] > *albumKeyP) {
this->albums[j + 1] = this->albums[j];
j = j - 1;
}
this->albums[j + 1] = albumKeyP;
}
}
std::ostream &operator<<(std::ostream &Str, const Collection &collection) {
Str << "Collection: \n";
for (auto &t : collection.albums) {
Str << *t << "\n";
}
return Str;
}
std::istream &operator>>(std::istream &is, Collection &collectionObj) {
Album album;
Album *albumPointer;
std::vector<Album *> albumVec;
std::string line;
while (getline(is, line)) {
std::istringstream iss(line);
//Album detected
if (isalpha(line[0])) {
iss >> album;
albumPointer = new Album(album.getArtist(), album.getAlbumTitle());
albumPointer = collectionObj.addAlbum(albumPointer);
}
//Track detected.
if (isdigit(line[0])) {
Track track;
iss >> track;
albumPointer->addTrack(track);
}
}
return is;
}
void Collection::testHarness() {
std::cout << "\n=---Collection Test Harness---=" << std::endl;
Track track = Track("Last Train Home", "00:03:22");
Track track2 = Track("Uber Everywhere", "00:03:59");
std::vector<Track> tracks;
tracks.push_back(track);
tracks.push_back(track2);
Album *album = new Album("Zebra", "An Album");
album->addTrack(track);
Album *album2 = new Album("Lima", "Also An Album");
album2->addTrack(track2);
Collection albumCol = Collection();
albumCol.addAlbum(album);
albumCol.addAlbum(album2);
albumCol.sort();
std::cout << albumCol << std::endl;
}