Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main.cpp
- #include <iostream>
- #include <string>
- #include "Song.h"
- #include "MusicLibrary.h"
- using namespace std;
- int main()
- {
- string filename;
- int numsongs;
- char mode;
- cout << "Enter number of Songs " << endl;
- cin >> numsongs;
- cout << "Enter filename with information about the songs" << endl;
- cin >> filename;
- cout << "Enter mode of operation (r : play Random, l: play PlayList, b: play both ) " << endl;
- cin >> mode;
- MusicLibrary mylibrary(numsongs);
- mylibrary.readSongsFromFile(filename);
- if (mode == 'r' || mode == 'b')
- {
- mylibrary.playRandom();
- }
- if (mode == 'l' || mode == 'b')
- {
- for (int i = numsongs - 1; i >= 0; i--)
- {
- mylibrary.addSongToPlayList(i);
- }
- mylibrary.playPlaylist();
- }
- return 0;
- }
- ----------------------------------------------------------------------------------------------------------------------
- Song.cpp:
- #include <iostream>
- #include "Song.h"
- void Song::Play() {
- cout << "Playing " << Title << " by " << Artist << " " << PlayTime << " seconds" << endl;
- }
- Song::Song() {
- PlayTime = 0;
- Year = 0;
- }
- Song::Song(string title, string artist, string album, int year, int playTime) {
- Title = title;
- Artist = artist;
- Album = album;
- Year = year;
- PlayTime = playTime;
- }
- void Song::setTitle(string title) {
- Title = title;
- }
- string Song::getTitle() {
- return Title;
- }
- void Song::setArtist(string artist) {
- Artist = artist;
- }
- string Song::getArtist() {
- return Artist;
- }
- void Song::setAlbum(string album) {
- Album = album;
- }
- string Song::getAlbum() {
- return Album;
- }
- void Song::setYear(int year) {
- Year = year;
- }
- int Song::getYear() {
- return Year;
- }
- void Song::setPlayTime(int playTime) {
- PlayTime = playTime;
- }
- int Song::getPlayTime() {
- return PlayTime;
- }
- bool Song::operator==(Song &rhs) {
- return ((this->Title == rhs.Title) && (this->Artist == rhs.Artist) && (this->Album == rhs.Album));
- }
- ------------------------------------------------------------------------------------------------------------------
- Song.h:
- #pragma once
- #include <string>
- using namespace std;
- class Song {
- private:
- string Title;
- string Artist;
- string Album;
- int Year;
- int PlayTime;
- public:
- Song();
- Song(string title, string artist, string album, int year, int playTime);
- void setTitle(string title);
- string getTitle();
- void setArtist(string artist);
- string getArtist();
- void setAlbum(string album);
- string getAlbum();
- void setYear(int year);
- int getYear();
- void setPlayTime(int playTime);
- int getPlayTime();
- bool operator==(Song &rhs);
- void Play();
- };
- ---------------------------------------------------------------------------------------------------------------------
- MusicLibrary.cpp:
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include "MusicLibrary.h"
- MusicLibrary::MusicLibrary(int maxsongs) {
- maxSongs = maxsongs;
- mySongs = new Song[maxSongs];
- playList = &mySongs;
- numSongs = 0;
- numSongsPlayList = 0;
- }
- MusicLibrary::MusicLibrary(MusicLibrary &other) {
- maxSongs = other.maxSongs;
- numSongs = other.numSongs;
- mySongs = other.mySongs;
- numSongsPlayList = other.numSongsPlayList;
- playList = other.playList;
- }
- MusicLibrary::~MusicLibrary() {
- delete[] mySongs;
- delete[] playList;
- }
- int MusicLibrary::getnumSongs() {
- return numSongs;
- }
- int MusicLibrary::getmaxSongs() {
- return maxSongs;
- }
- int MusicLibrary::getnumSongsPlayList() {
- return numSongsPlayList;
- }
- bool MusicLibrary::addSong(string title, string artist, string album, int year, int time) {
- if (numSongs == maxSongs) {
- cout << "Could not add song to library. Library is full" << endl;
- return false;
- }
- mySongs[numSongs].setTitle(title);
- mySongs[numSongs].setArtist(artist);
- mySongs[numSongs].setAlbum(album);
- mySongs[numSongs].setYear(year);
- mySongs[numSongs].setPlayTime(time);
- numSongs++;
- cout << numSongs << " Added " << mySongs[numSongs].getTitle() << endl;
- return true;
- }
- bool MusicLibrary::addSong(Song &song) {
- if (numSongs == maxSongs) {
- cout << "Could not add song to library. Library is full" << endl;
- return false;
- }
- mySongs[numSongs] = song;
- numSongs++;
- return true;
- }
- void MusicLibrary::readSongsFromFile(string filename) {
- ifstream input;
- input.open(filename);
- bool cont = true;
- if (input.is_open()) {
- string line;
- while (getline(input, line) && cont) {
- string title, artist, album;
- string s_year, s_time;
- int year;
- int time;
- istringstream inSS(line);
- getline(inSS, title, ',');
- getline(inSS, artist, ',');
- getline(inSS, album, ',');
- getline(inSS, s_year, ',');
- getline(inSS, s_time);
- year = stoi(s_year);
- time = stoi(s_time);
- cout << title << endl;
- cont = addSong(title, artist, album, year, time);
- };
- }
- else {
- cout << "could not open file " << filename << endl;
- }
- }
- void MusicLibrary::playRandom() {
- int x = 0;
- int y = numSongs - 1;
- while(x > y) {
- mySongs[x].Play();
- mySongs[y - x].Play();
- x++;
- y--;
- }
- }
- bool MusicLibrary::addSongToPlayList(int pos) {
- if(numSongsPlayList == maxSongs) {
- cout << "Playlist is full" << endl;
- return false;
- }
- if((pos < 0) || (pos > numSongs)) {
- cout << "Invalid song" << endl;
- return false;
- }
- numSongsPlayList = numSongsPlayList + 1;
- *playList[numSongsPlayList] = mySongs[pos];
- return true;
- }
- void MusicLibrary::playPlaylist()
- {
- for(int i = 0; i < numSongsPlayList; i++) {
- playList[i]->Play();
- }
- }
- -------------------------------------------------------------------------------------------------------------------------
- MusicLibrary.h:
- #pragma once
- #include <string>
- #include "Song.h"
- using namespace std;
- class MusicLibrary {
- private:
- int maxSongs;
- int numSongs; // number of Songs in library
- Song *mySongs; // dynamic array storing all Songs
- int numSongsPlayList; // number of Songs in Playlist
- Song **playList; // dynamic array of pointers to Songs
- public:
- MusicLibrary(int maxsongs);
- MusicLibrary(MusicLibrary &other);
- ~MusicLibrary();
- int getnumSongs();
- int getmaxSongs();
- int getnumSongsPlayList();
- bool addSong(string title, string artist, string album, int year, int time);
- bool addSong(Song &song);
- void readSongsFromFile(string filename);
- bool addSongToPlayList(int pos);
- void playRandom();
- void playPlaylist();
- };
- --------------------------------------------------------------------------------------------------------------------------
- songs.txt:
- Zoo Station, U2, Achtung Baby, 1991, 203
- Youngblood, 5 Seconds of Summer, Youngblood, 2018, 311
- Money for Nothing, Dire Straits, Brothers in Arms, 1986, 501
- Summer of 69, Bryan Adams, Reckless, 1984, 178
- Livin on a Prayer, Bon Jovi, Slippery when Wet, 1986, 241
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement