//------------------------------------------------------------------- // File :library.C // Author :Alicia Thorsen // Course :CS1129 // // Implementation of Library class. //------------------------------------------------------------------- #include "library.h" #include #include #include using namespace std; // ------------------------------------------------------------------- // Precondition :None. // Postcondition :The music library is loaded. // ------------------------------------------------------------------- void Library::loadLibrary() { string input; // Open input file. inputFile.open(LIBRARY_FILE); // Read first line of input. getline(inputFile, input); // Read in all songs and mark each song as not deleted. while (!inputFile.eof()) { //Song title. songs[libSize].setTitle(input); // Album. getline(inputFile, input); songs[libSize].setAlbum(input); // Artist. getline(inputFile, input); songs[libSize].setArtist(input); // Genre. getline(inputFile, input); songs[libSize].setGenre(input); // Mark as not deleted. songs[libSize].setDeleted(false); // Increase lib size. ++libSize; // Read next line. May not exist. getline(inputFile, input); } // Mark end of non-deleted songs. libEnd = libSize; // Close file stream. inputFile.close(); } // end loadLibrary(). // ------------------------------------------------------------------- // Precondition :None. // Postcondition :MyPod logo is displayed. // ------------------------------------------------------------------- void Library::showLogo() { cout << "\n\n"; cout << "# # ###### \n"; cout << "## ## # # # # #### ##### \n"; cout << "# # # # # # # # # # # # \n"; cout << "# # # # ###### # # # # \n"; cout << "# # # # # # # # \n"; cout << "# # # # # # # # \n"; cout << "# # # # #### ##### \n\n"; } // end showLogo(). // ------------------------------------------------------------------- // Precondition :None. // Postcondition :Returns the user's unvalidated menu choice. // ------------------------------------------------------------------- char Library::showMenu() { string choice; cout << "\nMyPod Music Library Menu\n"; cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"; cout << "~ Add a new song\n"; cout << "~ Delete a song\n"; cout << "~ View library\n"; cout << "~ Info\n"; cout << "~ Save\n"; cout << "~ Quit\n\n"; cout << "Enter the first letter of your choice: "; getline(cin, choice); return choice[0]; } // end menu(). // ------------------------------------------------------------------- // Precondition :The library has been loaded. // Postcondition :The library is displayed. // ------------------------------------------------------------------- void Library::displayLib() { // Calculate width of display table. int tblWidth = MAX_ID + MAX_TITLE + MAX_ALBUM + MAX_ARTIST + MAX_GENRE; // Align output left. cout.setf(ios::left); cout << "\nMUSIC LIBRARY\n\n"; cout << setw(MAX_ID) << "ID " << " | " << setw(MAX_TITLE) << "TITLE" << " | " << setw(MAX_ALBUM) << "ALBUM" << " | " << setw(MAX_ARTIST) << "ARTIST" << " | " << "GENRE" << endl; for (int i = 0; i < tblWidth; ++i) cout << "-"; // Display all songs (except deleted ones). for (int i = 0; i < libEnd; ++i) { if (!songs[i].isDeleted()) { cout << endl << setw(MAX_ID) << i << " | " << setw(MAX_TITLE) << songs[i].getTitle() << " | " << setw(MAX_ALBUM) << songs[i].getAlbum() << " | " << setw(MAX_ARTIST) << songs[i].getArtist() << " | " << songs[i].getGenre(); } } cout << endl; } // end displayLib(). // ------------------------------------------------------------------- // Precondition :libSize contains the size of the library. // Postcondition :Library statistics are displayed. // ------------------------------------------------------------------- void Library::showLibStats() { cout << "\nLIBRARY STATS\n\n"; cout << "Songs = " << libSize << endl; cout << "Capacity = " << MAX_LIB_SIZE << endl; cout << "Available = " << (MAX_LIB_SIZE - libSize) << endl; cout << "Version = 1.0" << endl; } // end showLibStats(). // ------------------------------------------------------------------- // Precondition :The library has been loaded. // Postcondition :If there is space, a new song is added to the library. // ------------------------------------------------------------------- void Library::addSong() { string input; int index = 0; cout << "\nADD A NEW SONG\n"; // Check if library is full. if (libSize == MAX_LIB_SIZE) { cout << "Sorry the library is full. You must delete a song before you can " << "add another.\n"; return; } // Find first available slot. while (!songs[index].isDeleted()) index++; // Add song at location found. cout << "\nSong title: "; getline(cin, input); songs[index].setTitle(input); cout << "Album: "; getline(cin, input); songs[index].setAlbum(input); cout << "Artist: "; getline(cin, input); songs[index].setArtist(input); cout << "Genre: "; getline(cin, input); songs[index].setGenre(input); // Mark slot as occupied. songs[index].setDeleted(false); // Increment library size to account for new song. ++libSize; // If song was added at the end, increment end of library marker. if (index == libEnd) ++libEnd; cout << "\nSong was successfully added.\n"; } // end addSong(). // ------------------------------------------------------------------- // Precondition :Library has been loaded. // Postcondition :A song may be deleted. // ------------------------------------------------------------------- void Library::deleteSong() { int index = 0; cout << "\nDELETE A SONG\n"; displayLib(); // Get song to delete by id. cout << "\nEnter the ID of the song you would like to delete: "; cin >> index; cin.ignore(); // Check if song id is valid. if ((index < 0) || (index >= libEnd) || songs[index].isDeleted()) { cout << "\nError: Invalid Song ID. Delete cancelled." << endl; return; } // Mark song as deleted. songs[index].setDeleted(true); --libSize; // Update end of library marker. if (index == libEnd - 1) --libEnd; cout << "\nSong " << index << " was successfully deleted." << endl; } // end deleteSong(). // ------------------------------------------------------------------- // Precondition :The library has been loaded. // Postcondition :Changes may be saved. // ------------------------------------------------------------------- void Library::save() { // Open output file in overwrite mode. outputFile.open(LIBRARY_FILE); // Write all songs which were not marked as deleted. for (int i = 0, count = 0; i < libEnd; ++i) { if (!songs[i].isDeleted()) { outputFile << songs[i].getTitle() << endl << songs[i].getAlbum() << endl << songs[i].getArtist() << endl << songs[i].getGenre(); if (count < libSize - 1) outputFile << endl; ++count; } } cout << "\nMusic Library has been saved.\n"; // Close file. outputFile.close(); } // end save(). // ------------------------------------------------------------------- // Precondition :The library has been loaded. // Postcondition :May save the library and returns whether or not the user // wants to quit. // ------------------------------------------------------------------- bool Library::quit() { string wantToQuit, saveChanges; // Double check user wants to quit. cout << "\nAre you sure you want to quit? (y/n) ~ "; getline(cin, wantToQuit); if ((wantToQuit[0] != 'y') && (wantToQuit[0] != 'Y')) return true; // keepGoing = true; // Find out if changes should be saved. cout << "Save your changes before quitting? (y/n) ~ "; getline(cin, saveChanges); if ((saveChanges[0] == 'y') || (saveChanges[0] == 'Y')) save(); else cout << "\nMusic Library was not saved." << endl; return false; // keepGoing = false; } // end quit().