#include #include #include "SongT.h" #include "ArtistT.h" #include "SongListT.h" #include "ArtistListT.h" #include "ProjectConstants.h" using namespace std; void ReadData(SongListT & songs, ArtistListT & artists); void PrintSongWithArtist(const SongT & song, const ArtistListT & artists); void PrintSongsWithArtist(const SongListT & songs, const ArtistListT & artists); void PrintArtistsWithSongs(const ArtistListT & artists, const SongListT & songs); void PrintSongsByArtist(const ArtistT & artist, const SongListT & songs); void PrintArtistWithSongs(const ArtistT & artist, const SongListT & songs); int main() { SongListT songs; ArtistListT artists; ReadData(songs, artists); songs.Sort(); artists.SortByName(); cout << endl << endl; cout << "The list of songs" << endl; cout << "-----------------------------------------------" << endl; PrintSongsWithArtist(songs, artists); cout << endl << endl; cout << "The list of artists" << endl; cout << "-----------------------------------------------" << endl; PrintArtistsWithSongs(artists, songs); return 0; } void BreakLine(string line, string & name, string & title) { size_t pos; pos = line.find(','); title = line.substr(0,pos); name = line.substr(pos+1); } SongT NewSong(string title, SongListT & songs) { SongT song; bool ok; if (not songs.ValidSongTitle(title)) { song.Title(title); song.ID(songs.Size()); ok = songs.InsertSong(song); if (not ok) { cout << "Could not insert " << title << " too many songs." << endl; } } else { song = songs.GetSongByTitle(title); } return song; } ArtistT NewArtist(string name, ArtistListT & artists){ bool ok; ArtistT artist; if (not artists.ValidArtistName(name)) { artist.Name(name); artist.ID(artists.Size()); ok = artists.InsertArtist(artist); if (not ok) { cout << "Could not insert " << name << " too many artists." << endl; } } else { artist = artists.GetArtistByName(name); } return artist; } void UpdateSongAndArtist(SongT & song, ArtistT & artist, SongListT & songs, ArtistListT & artists){ bool ok; if (artist.ID() != -1 and song.ID()!= -1) { song.ArtistID(artist.ID()); songs.UpdateSong(song); ok = artist.AddSongID(song.ID()); if (not ok) { cout << "Could not insert song " << song.Title() << " for " << artist.Name() << " maximum songs exceeded." << endl; } artists.UpdateArtist(artist); } } void ReadData(SongListT & songs, ArtistListT & artists) { ifstream inFile; string line; string name, title; SongT song; ArtistT artist; inFile.open("Songs.csv"); getline(inFile, line); while(inFile) { BreakLine(line, name, title); song = NewSong(title, songs); artist = NewArtist(name, artists); UpdateSongAndArtist(song, artist, songs, artists); getline(inFile, line); } return; } void PrintArtistsWithSongs(const ArtistListT & artists, const SongListT & songs){ int i; cout << "There are a total of " << artists.Size() << " artists." << endl; for(i =0; i < artists.Size(); i++) { PrintArtistWithSongs(artists.GetArtistByIndex(i), songs); cout << endl; } return; } void PrintSongsByArtist(const ArtistT & artist, const SongListT & songs) { int i; int songID; if (artist.SongCount() > 0) { cout <<"\tSongs : " << endl ; for(i =0; i < artist.SongCount();i++) { songID = artist.SongID(i); if (not songs.ValidID(songID) ) { cout << "\t\tUNKNOWN" << endl; } else { cout << "\t\t" << songs.GetSongByID(songID).Title() << endl; } } cout << endl; } return; } void PrintArtistWithSongs(const ArtistT & artist, const SongListT & songs) { cout << "Artist Name: " << artist.Name() << endl; cout << "\tSong Count: " << artist.SongCount() << endl; PrintSongsByArtist(artist, songs); return; } void PrintSongsWithArtist(const SongListT & songs, const ArtistListT & artists){ int i; cout << "There are a total of " << songs.Size() << " songs." << endl; for(i =0; i < songs.Size(); i++) { PrintSongWithArtist(songs.GetSongByIndex(i), artists); } return; } void PrintSongWithArtist(const SongT & song, const ArtistListT & artists) { string artist; if (artists.ValidID(song.ArtistID())) { artist = artists.GetArtistByID(song.ArtistID()).Name(); } else { artist = "UNKNOWN"; } cout << song.Title() << " by " << artist<< endl; return; }