Created
November 21, 2018 08:12
-
-
Save onsah/0a4e1ed9940bd998710ad3b386156fd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MAC.h" | |
#include <iostream> | |
MAC::MAC() : musicAlbums{nullptr}, | |
noOfMusicAlbums{0}, | |
_cap{0} | |
{ | |
} | |
MAC::~MAC() | |
{ | |
if (_cap) | |
delete[] musicAlbums; | |
} | |
MAC::MAC(const MAC &macToCopy) : musicAlbums{new MusicAlbum[macToCopy.noOfMusicAlbums]}, | |
noOfMusicAlbums{macToCopy.noOfMusicAlbums}, | |
_cap{macToCopy.noOfMusicAlbums} | |
{ | |
for (int i = 0; i < noOfMusicAlbums; ++i) | |
musicAlbums[i] = macToCopy.musicAlbums[i]; | |
} | |
void MAC::operator=(const MAC &macToCopy) | |
{ | |
noOfMusicAlbums = macToCopy.noOfMusicAlbums; | |
reserve(noOfMusicAlbums); | |
for (int i = 0; i < noOfMusicAlbums; ++i) | |
musicAlbums[i] = macToCopy.musicAlbums[i]; | |
} | |
bool MAC::addMusicAlbum(const std::string maArtist, const std::string maTitle, const int maYear) | |
{ | |
//std::cout << "size: " << noOfMusicAlbums << " capacity: " << capacity() << '\n'; | |
MusicAlbum album(maArtist, maTitle, maYear); | |
if (hasMusicAlbum(album)) | |
return false; | |
if (noOfMusicAlbums == capacity()) | |
grow(); | |
musicAlbums[noOfMusicAlbums] = album; | |
noOfMusicAlbums += 1; | |
return true; | |
} | |
bool MAC::removeMusicAlbum(const std::string maArtist, const std::string maTitle) | |
{ | |
MusicAlbum album(maArtist, maTitle); | |
int found = findMusicAlbum(album); | |
if (found != -1) { | |
for (int i = found + 1; i < noOfMusicAlbums; ++i) | |
musicAlbums[i - 1] = musicAlbums[i]; | |
noOfMusicAlbums -= 1; | |
} | |
return found != -1; | |
} | |
int MAC::getMusicAlbums(MusicAlbum *&allMusicAlbums) | |
{ | |
allMusicAlbums = new MusicAlbum[noOfMusicAlbums]; | |
for (int i = 0; i < noOfMusicAlbums; ++i) | |
allMusicAlbums[i] = musicAlbums[i]; | |
return noOfMusicAlbums; | |
} | |
bool MAC::addSong(const std::string maArtist, const std::string maTitle, | |
const std::string sName, const int sMins, | |
const int sSecs) | |
{ | |
MusicAlbum album(maArtist, maTitle); | |
int found = findMusicAlbum(album); | |
//std::cout << "found: " << found << '\n'; | |
if (found != -1) { | |
Song song(sName, sMins, sSecs); | |
return musicAlbums[found].addSong(song); | |
} else { | |
return false; | |
} | |
} | |
bool MAC::removeSongs(const std::string maArtist, | |
const std::string maTitle) | |
{ | |
MusicAlbum album(maArtist, maTitle); | |
int found = findMusicAlbum(album); | |
if (found != -1) | |
musicAlbums[found].clear(); | |
return found != -1; | |
} | |
void MAC::calculateAvgMusicAlbumLength(int &minutes, int &seconds) | |
{ | |
if (noOfMusicAlbums == 0) | |
return; | |
minutes = 0; | |
seconds = 0; | |
for (int i = 0; i < noOfMusicAlbums; ++i) { | |
int tempmins, tempsecs; | |
musicAlbums[i].calculateMusicAlbumLength(tempmins, tempsecs); | |
minutes += tempmins; | |
seconds += tempsecs; | |
} | |
int totalsecs = ((minutes * 60) + seconds) / noOfMusicAlbums; | |
minutes = totalsecs / SECONDS_IN_MINUTE; | |
seconds = totalsecs % SECONDS_IN_MINUTE; | |
} | |
int MAC::capacity() const | |
{ | |
return _cap; | |
} | |
bool MAC::hasMusicAlbum(const MusicAlbum &musicAlbum) const | |
{ | |
return findMusicAlbum(musicAlbum) != -1; | |
} | |
int MAC::findMusicAlbum(const MusicAlbum &musicAlbum) const | |
{ | |
for (int i = 0; i < noOfMusicAlbums; ++i) { | |
if (musicAlbums[i] == musicAlbum) | |
return i; | |
} | |
return -1; | |
} | |
void MAC::grow() | |
{ | |
if (capacity() != 0) | |
reserve(capacity() * 2); | |
else | |
reserve(4); | |
} | |
void MAC::resize(int new_size, bool copy_old) | |
{ | |
reserve(new_size, copy_old); | |
noOfMusicAlbums = new_size; | |
} | |
void MAC::reserve(int new_cap, bool copy_old) | |
{ | |
if (new_cap <= _cap) | |
return; | |
//std::cout << "reserving albums: " << new_cap << '\n'; | |
MusicAlbum *new_albums = new MusicAlbum[new_cap]; | |
if (copy_old) { | |
for (int i = 0; i < noOfMusicAlbums; ++i) | |
new_albums[i] = musicAlbums[i]; | |
} | |
if (musicAlbums) | |
delete[] musicAlbums; | |
_cap = new_cap; | |
musicAlbums = new_albums; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __MAC_H | |
#define __MAC_H | |
#include <string> | |
#include "Song.h" | |
#include "MusicAlbum.h" | |
class MAC | |
{ | |
public: | |
MAC(); | |
~MAC(); | |
MAC(const MAC &macToCopy); | |
void operator=(const MAC &right); | |
bool addMusicAlbum(const std::string maArtist, | |
const std::string maTitle, | |
const int maYear); | |
bool removeMusicAlbum(const std::string maArtist, | |
const std::string maTitle); | |
int getMusicAlbums(MusicAlbum *&allMusicAlbums); | |
bool addSong(const std::string maArtist, const std::string maTitle, | |
const std::string sName, const int sMins, | |
const int sSecs); | |
bool removeSongs(const std::string maArtist, | |
const std::string maTitle); | |
void calculateAvgMusicAlbumLength(int &minutes, | |
int &seconds); | |
int capacity() const; | |
private: | |
static const int SECONDS_IN_MINUTE = 60; | |
bool hasMusicAlbum(const MusicAlbum& musicAlbum) const; | |
int findMusicAlbum(const MusicAlbum& musicAlbum) const; | |
void grow(); | |
void resize(int new_size, bool copy_old = true); | |
void reserve(int new_cap, bool copy_old = true); | |
MusicAlbum *musicAlbums; | |
int noOfMusicAlbums; | |
int _cap; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MusicAlbum.h" | |
#include <iostream> | |
MusicAlbum::MusicAlbum(const std::string maArtist, | |
const std::string maTitle, | |
const int maYear) : artist{maArtist}, | |
title{maTitle}, | |
year{maYear}, | |
songs{nullptr}, | |
noSongs{0}, | |
_cap{0} | |
{ | |
} | |
MusicAlbum::~MusicAlbum() | |
{ | |
if (_cap) | |
delete[] songs; | |
} | |
MusicAlbum::MusicAlbum(const MusicAlbum &right) : songs{nullptr} | |
{ | |
*this = right; | |
} | |
void MusicAlbum::operator=(const MusicAlbum &right) | |
{ | |
artist = right.artist; | |
title = right.title; | |
year = right.year; | |
noSongs = right.noSongs; | |
reserve(noSongs, false); | |
for (int i = 0; i < noSongs; ++i) | |
songs[i] = right.songs[i]; | |
} | |
bool MusicAlbum::operator==(const MusicAlbum &right) const | |
{ | |
return artist == right.artist && | |
title == right.title; | |
} | |
std::string MusicAlbum::getMusicAlbumArtist() const | |
{ | |
return artist; | |
} | |
std::string MusicAlbum::getMusicAlbumTitle() const | |
{ | |
return title; | |
} | |
int MusicAlbum::getMusicAlbumYear() const | |
{ | |
return year; | |
} | |
void MusicAlbum::calculateMusicAlbumLength(int &minutes, int &seconds) const | |
{ | |
minutes = 0; | |
seconds = 0; | |
for (int i = 0; i < noSongs; ++i) { | |
const Song& s = songs[i]; | |
minutes += s.getMins(); | |
seconds += s.getSecs(); | |
} | |
minutes += seconds / SECONDS_IN_MINUTE; | |
seconds %= SECONDS_IN_MINUTE; | |
} | |
bool MusicAlbum::addSong(const Song &song) | |
{ | |
if (hasSong(song)) | |
return false; | |
//std::cout << "size: " << noSongs << " capacity: " << capacity() << '\n'; | |
if (noSongs == capacity()) | |
grow(); | |
songs[noSongs] = song; | |
noSongs += 1; | |
return true; | |
} | |
int MusicAlbum::capacity() const | |
{ | |
return _cap; | |
} | |
bool MusicAlbum::hasSong(const Song& song) const | |
{ | |
for (int i = 0; i < noSongs; ++i) { | |
if (songs[i] == song) | |
return true; | |
} | |
return false; | |
} | |
void MusicAlbum::grow() | |
{ | |
if (capacity() != 0) | |
reserve(capacity() * 2); | |
else | |
reserve(4); | |
} | |
void MusicAlbum::reserve(int new_cap, bool copy_old) | |
{ | |
if (new_cap <= _cap) | |
return; | |
/* std::cout << "reserving songs: " << new_cap << '\n' | |
<< "noSongs: " << noSongs << '\n'; */ | |
Song *new_songs = new Song[new_cap]; | |
if (copy_old) { | |
for (int i = 0; i < noSongs; ++i) | |
new_songs[i] = songs[i]; | |
} | |
if (_cap != 0) | |
delete[] songs; | |
_cap = new_cap; | |
songs = new_songs; | |
} | |
void MusicAlbum::clear() | |
{ | |
noSongs = 0; | |
} | |
void MusicAlbum::reset_capacity() | |
{ | |
if (_cap) | |
delete[] songs; | |
songs = nullptr; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __MUSIC_ALBUM_H | |
#define __MUSIC_ALBUM_H | |
#include <string> | |
#include "Song.h" | |
class MusicAlbum | |
{ | |
public: | |
MusicAlbum(const std::string maArtist = "", | |
const std::string maTitle = "", | |
const int maYear = 0); | |
~MusicAlbum(); | |
MusicAlbum(const MusicAlbum &maToCopy); | |
void operator=(const MusicAlbum &right); | |
bool operator==(const MusicAlbum &right) const; | |
std::string getMusicAlbumArtist() const; | |
std::string getMusicAlbumTitle() const; | |
int getMusicAlbumYear() const; | |
void calculateMusicAlbumLength(int &minutes, int &seconds) const; | |
bool addSong(const Song &song); | |
int capacity() const; | |
void clear(); | |
void reset_capacity(); | |
private: | |
static const int SECONDS_IN_MINUTE = 60; | |
bool hasSong(const Song& song) const; | |
void grow(); | |
void reserve(int size, bool copy_old = true); | |
std::string artist; | |
std::string title; | |
int year; | |
Song *songs; | |
int noSongs; | |
int _cap; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Song.h" | |
Song::Song(const std::string sName, | |
const int sMins, | |
const int sSecs) : name{sName}, | |
mins{sMins}, | |
secs{sSecs} | |
{ | |
} | |
Song::~Song() | |
{ /* */ | |
} | |
Song::Song(const Song &songToCopy) : name{songToCopy.name}, | |
mins{songToCopy.mins}, | |
secs{songToCopy.secs} | |
{ | |
} | |
void Song::operator=(const Song &songToCopy) | |
{ | |
this->name = songToCopy.name; | |
this->mins = songToCopy.mins; | |
this->secs = songToCopy.secs; | |
} | |
bool Song::operator==(const Song &right) const | |
{ | |
return name == right.name && | |
mins == right.mins && | |
secs == right.secs; | |
} | |
std::string Song::getName() const | |
{ | |
return name; | |
} | |
int Song::getMins() const | |
{ | |
return mins; | |
} | |
int Song::getSecs() const | |
{ | |
return secs; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __SONG_H | |
#define __SONG_H | |
#include <string> | |
class Song | |
{ | |
public: | |
Song( const std::string sName = "", | |
const int sMins = 0, | |
const int sSecs = 0); | |
~Song(); | |
Song(const Song &songToCopy); | |
void operator=(const Song &right); | |
bool operator==(const Song &right) const; | |
std::string getName() const; | |
int getMins() const; | |
int getSecs() const; | |
private: | |
std::string name; | |
int mins; | |
int secs; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment