std::list<Scientist> ScientistRepository::search(std::string searchTerm) { QSqlDatabase db = databaseConnect(); QSqlQuery query(db); query.exec(QString::fromStdString("Select * from Scientists where Name like '%" + searchTerm + "%'")); std::list<Scientist> scientists = std::list<Scientist>(); while(query.next()) { Scientist a = Scientist(); a.name = query.value("Name").toString().toStdString(); a.dateOfBirth = query.value("DateOfBirth").toString().toStdString(); a.dateOfDeath = query.value("DateofDeath").toString().toStdString(); a.gender = query.value("Gender").toString().toStdString(); scientists.push_back(a); } return scientists; // // Naive search implementation, finds the case sensitive substring in the name and returns first match // for(std::list<Scientist>::iterator iter = scientistList.begin(); iter != scientistList.end(); iter++) { // if(iter->name.find(searchTerm) != std::string::npos) { // return new Scientist(*iter); // } // } // return NULL; }
ScientistRepository::ScientistRepository(std::string fname) { filename = fname; delimiter = '\t'; std::ifstream scientistFile; try { scientistFile.open(filename.c_str(),std::ios::in); } catch(...) { // Ignore the error, the file is non existent and will be created next time we save. } scientistList = std::list<Scientist>(); if(scientistFile.is_open()) { std::string lineToRead = ""; // Load all records into memory while(std::getline(scientistFile,lineToRead)) { Scientist scientist = Scientist(); std::vector<std::string> fields = util::split(lineToRead,delimiter); scientist.name = fields.at(0); scientist.dateOfBirth = fields.at(1); scientist.dateOfDeath = fields.at(2); scientist.gender = fields.at(3); scientistList.push_back(scientist); } scientistFile.close(); } }
std::list<Scientist> ScientistRepository::deepCopy() { std::list<Scientist> outList = std::list<Scientist>(); for(std::list<Scientist>::iterator iter = scientistList.begin(); iter != scientistList.end(); iter++) { outList.push_back(Scientist(*iter)); } return outList; }
void ScientistComputerConnectionsRepository::populateScientistList(std::list<Scientist> &scientistList, QSqlQuery query) { while(query.next()){ Scientist s = Scientist(); s.setId(query.value("ID").toInt()); s.setName(query.value("Name").toString().toStdString()); s.setDateOfBirth(query.value("DateOfBirth").toString().toStdString()); s.setDateOfDeath(query.value("DateOfDeath").toString().toStdString()); s.setGender(query.value("Gender").toInt()); scientistList.push_back(s); } }
std::list<Scientist> ScientistRepository::list() { std::list<Scientist> scientists = std::list<Scientist>(); QSqlDatabase db = databaseConnect(); QSqlQuery query(db); query.exec("SELECT * FROM Scientists"); while(query.next()){ Scientist a = Scientist(); a.name = query.value("Name").toString().toStdString(); a.dateOfBirth = query.value("DateOfBirth").toString().toStdString(); a.dateOfDeath = query.value("DateofDeath").toString().toStdString(); a.gender = query.value("Gender").toString().toStdString(); scientists.push_back(a); } return scientists; }
std::list<Scientist> ScientistRepository::list(int col, int mod) { QSqlDatabase db = databaseConnect(); QSqlQuery query(db); string sorter; switch(col) { case 1: if (mod == 1) { sorter = "Select * from Scientists order by Name ASC"; } else if (mod == 2) { sorter = "Select * from Scientists order by Name DESC"; } break; case 2: if (mod == 1) { sorter = "Select * from Scientists order by DateOfBirth ASC"; } else if (mod == 2) { sorter = "Select * from Scientists order by DateOfBirth DESC"; } break; case 3: if (mod == 1) { sorter = "Select * from Scientists order by DateOfDeath ASC"; } else if (mod == 2) { sorter = "Select * from Scientists order by DateOfDeath DESC"; } break; case 4: if (mod == 1) { sorter = "Select * from Scientists order by Gender ASC"; } else if (mod == 2) { sorter = "Select * from Scientists order by Gender DESC"; } break; } query.exec(QString::fromStdString(sorter)); std::list<Scientist> scientists = std::list<Scientist>(); while(query.next()) { Scientist a = Scientist(); a.name = query.value("Name").toString().toStdString(); a.dateOfBirth = query.value("DateOfBirth").toString().toStdString(); a.dateOfDeath = query.value("DateofDeath").toString().toStdString(); a.gender = query.value("Gender").toString().toStdString(); scientists.push_back(a); } return scientists; }
void ConsoleUI::add_scientist() { Scientist s = Scientist(); string firstname_, lastname_, gender_, birth_, death_; clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Name of Scientist: [FirstName LastName]" << endl; cout << endl << " -> "; cin >> firstname_ >> lastname_; clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Gender of scientist: \n(1) male \n(2) female" << endl; cout << endl << " -> "; cin >> gender_; while ((gender_ != "1") && (gender_ != "2")) { clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Invalid input! Please try again:" << endl; cout << "Gender of scientist: \n(1) male \n(2) female" << endl; cout << endl << " -> "; cin >> gender_; } clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Birth year of scientist:" << endl; cout << endl << " -> "; cin >> birth_; while (!is_digits(birth_)) { clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Invalid input! Please try again:" << endl; cout << "Birth year of scientist:" << endl; cout << endl << " -> "; cin >> birth_; } clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "Scientist's Year of death: " << endl; cout << endl << " -> "; cin >> death_; while (!is_digits(death_) || (atoi(death_.c_str()) < atoi(birth_.c_str()))) { clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; if (!is_digits(death_)) { cout << "Invalid input! Please try again:" << endl; } else if (atoi(death_.c_str()) < atoi(birth_.c_str())) { cout << "Year of death should be AFTER year of birth! Please ry again:" << endl; } cout << "Scientist's Year of death:" << endl; cout << endl << " -> "; cin >> death_; } if (gender_ == "1") { s.set_gender("Male"); } else if (gender_ == "2") { s.set_gender("Female"); } fix_string(firstname_); fix_string(lastname_); s.set_firstname(firstname_); s.set_lastname(lastname_); s.set_birth(birth_); s.set_death(death_); dbService.addS(s); clear_screen(); cout << readFileToString("../templates/commands.txt") << endl; cout << "\t\t " << "Scientist has been added to the list!\n\n\n\n\n\n\n" << endl; }