// This program processes queries about the MP3 files available on various computers. // Each computer is identified by a unique id (a string). Information about the computer, // including connection speed and a list of MP3's are stored in the computer class object. #include #include #include #include #include #include #include "mp3.h" #include "computer.h" typedef map MP3_map; // since the names of maps tend to be rather long. void add_computer(MP3_map& mp3_system); // prototypes of helper functions void print_mp3s_on_computer(const MP3_map& mp3_system); void find_song(const MP3_map& mp3_system); void find_songs_by_artist(const MP3_map& mp3_system); int main() { // the mp3 database MP3_map mp3_system; bool quit = false; // process the input char option; while (cin >> option) { switch (option) { case 'a': add_computer(mp3_system); break; case 'p': print_mp3s_on_computer(mp3_system); break; case 'f': find_song(mp3_system); break; case 'F': find_songs_by_artist(mp3_system); break; case 'q': return 1; break; default: cout << "Error: " << option << " is not a valid command" << endl;; break; } } } // Add a computer to the map of MP3's void add_computer(MP3_map& mp3_system) { string computer_id, line; double speed; // read id & speed cin >> computer_id >> speed; getline(cin, line); // read the rest of that line // Create a new computer & read its MP3's Computer new_computer(speed); do { string artist,title; getline(cin, artist); if (artist == "") break; getline(cin, title); new_computer.add_mp3(artist, title); } while (true); // Try to add the computer using the map insert function. pair result_pair = mp3_system.insert(make_pair(computer_id, new_computer)); // make sure the computer isn't already there! assert(result_pair.second); cout << "Computer " << computer_id << " added." << endl; } // Print the MP3's for a given computer. void print_mp3s_on_computer(const MP3_map& mp3_system) { string computer_id, line; // read id cin >> computer_id; getline(cin, line); // Find the computer MP3_map::const_iterator c_itr = mp3_system.find(computer_id); if (c_itr == mp3_system.end()) { cout << "Computer " << computer_id << " is not in the system." << endl; } else { cout << "Songs found on " << computer_id << ":\n"; c_itr->second.print_mp3s(); } } // Find all computers with a particular song. void find_song(const MP3_map& mp3_system) { string line, artist, title; getline(cin, line); // read the rest of request line // get the artist & title getline(cin, artist); getline(cin, title); // Iterate through the map, looking for the song. bool found = false; string computer_id; double speed; MP3_map::const_iterator c_itr; for (c_itr = mp3_system.begin(); c_itr != mp3_system.end(); c_itr++) { MP3 request; if (c_itr->second.has_song(artist, title, request)) { if (!found || c_itr->second.getSpeed() > speed) { found = true; computer_id = c_itr->first; speed = c_itr->second.getSpeed(); } } } if (found) cout << "Fastest download for \"" << title << "\" by " << artist << " from " << computer_id << endl; else cout << "No computer found with \"" << title << "\" by " << artist << endl; } // Find and output information about all songs by a given artist. void find_songs_by_artist(const MP3_map& mp3_system) { string line, artist; getline(cin, line); // read the rest of request line // Get the artist. getline(cin, artist); // you need to write this for lab }