#include #include #include #include #include #include using namespace std; char getMenu(); void printMenu(); const string unknown = "UNK"; class Refugee{ public: Refugee(); Refugee(string, string, char, int, string); string getLast(); string getFirst(); void setLast(string); void setFirst(string); unsigned int getAge(); void setAge(unsigned int); char getGender(); void setGender(char); string getCamp(); void setCamp(string); string nameMatch(string, string); bool ageMatch(int, char); private: string lastName; string firstName; char gender; unsigned int age; string camp; }; Refugee::Refugee(){ lastName = ""; firstName = ""; gender = ' '; age = 0; camp = ""; } Refugee::Refugee(string newFirst, string newLast, char newGender, int newAge, string newCamp){ lastName = newLast; firstName = newFirst; setAge(newAge); setGender(newGender); camp = newCamp; } string Refugee::getLast(){ return lastName; } string Refugee::getFirst(){ return firstName; } void Refugee::setLast(string newLast){ lastName = newLast; } void Refugee::setFirst(string newFirst){ firstName = newFirst; } unsigned int Refugee::getAge(){ return age; } void Refugee::setAge(unsigned int newAge){ if (newAge > 120){ cout << "Age not set, outside of normal range of 0 to 120" << endl; } else { age = newAge; } } char Refugee::getGender(){ return gender; } void Refugee::setGender(char newGender){ switch (newGender){ case 'm': case 'M': gender = 'M'; break; case 'f': case 'F': gender = 'F'; break; default: cout << "Gender must be M or F, cannot set " << newGender << endl; } } string Refugee::getCamp(){ return camp; }; void Refugee::setCamp(string newCamp){ camp = newCamp; }; string Refugee::nameMatch(string seekFirst, string seekLast){ if ((firstName != unknown) && (lastName != unknown) && (firstName == seekFirst) && (lastName == seekLast)){ return "Exact"; } if ((seekFirst == unknown) && (lastName == seekLast)){ return "Last"; } if ((seekLast == unknown) && (firstName == seekFirst)){ return "First"; } return "None"; }; bool Refugee::ageMatch(int seekAge, char seekGender){ if (gender == seekGender){ if ((age >= seekAge - 1) && (age <= seekAge + 1)){ return true; } } return false; }; class Operation { public: Operation(); void loadFile(string); void showRefugees(); void showCamp(string); void nameSearch(string, string); void childSearch(int, char); int totalRefugees(); private: vector refugees; }; Operation::Operation(){ // Sometimes no initialization is needed. // Like now. } void Operation::loadFile(string filename){ string firstName; string lastName; char refugeeGender; int refugeeAge; string campName; ifstream refugeeFile; refugeeFile.open(filename.c_str()); if (!refugeeFile){ cout << "Unable to open file " << filename << endl; return; } int count = 0; string header; getline(refugeeFile, header); while (refugeeFile >> firstName >> lastName >> refugeeGender >> refugeeAge >> campName){ count++; Refugee currentRefugee(firstName, lastName, refugeeGender, refugeeAge, campName); refugees.push_back(currentRefugee); } cout << "Loaded " << count << " refugees" << endl; } void Operation::showRefugees(){ for (int i = 0; i < refugees.size(); i++){ cout << setw(15) << refugees[i].getFirst() << setw(15) << refugees[i].getLast() << setw(4) << refugees[i].getGender() << setw(4) << refugees[i].getAge() << setw(15) << refugees[i].getCamp() << endl; } } void Operation::showCamp(string campName){ for (int i=0; i < refugees.size(); i++){ if (refugees[i].getCamp() == campName){ cout << setw(15) << refugees[i].getFirst() << setw(15) << refugees[i].getLast() << setw(4) << refugees[i].getGender() << setw(4) << refugees[i].getAge() << setw(15) << refugees[i].getCamp() << endl; } } } int Operation::totalRefugees(){ return refugees.size(); } void Operation::nameSearch(string first, string last){ string retval; for (int i = 0; i < refugees.size(); i++){ retval = refugees[i].nameMatch(first,last); if (retval != "None"){ cout << setw(15) << retval << ":" << setw(15) << refugees[i].getFirst() << setw(15) << refugees[i].getLast() << setw(4) << refugees[i].getGender() << setw(4) << refugees[i].getAge() << setw(15) << refugees[i].getCamp() << endl; } } } void Operation::childSearch(int age, char gender){ for (int i = 0; i < refugees.size(); i++){ if (refugees[i].ageMatch(age, gender)){ cout << setw(15) << refugees[i].getFirst() << setw(15) << refugees[i].getLast() << setw(4) << refugees[i].getGender() << setw(4) << refugees[i].getAge() << setw(15) << refugees[i].getCamp() << endl; } } } // int main(){ // // A main to test the Operation class // Operation rescue; // string filename; // cout << "Enter the name of the test file from project 3." << endl; // cout << "It will need to be in the same directory as this program." << endl; // cout << "Enter a filename to read: "; // getline(cin, filename); // cout << "This should print: Loaded 19 refugees" << endl; // rescue.loadFile(filename); // cout << "The 19 refugees should print below:" << endl; // rescue.showRefugees(); // cout << "Now 7 refugees from camp Alpha should be below" << endl; // rescue.showCamp("Alpha"); // cout << "The next line should print 19:" << endl; // cout << "Total refugees: " << rescue.totalRefugees() << endl; // cout << "The next call should print out one First name match" << endl; // rescue.nameSearch("Anejo","UNK"); // cout << "The next call should return three possible matches" << endl; // rescue.childSearch(2, 'F'); // } // int main(){ // // A main to test the refugee class // // create a refugee with no initial values; // Refugee bob; // bob.setFirst("Bob"); // bob.setLast("Refugee"); // bob.setAge(400); // bob.setAge(-10); // bob.setAge(20); // bob.setGender('W'); // bob.setGender('m'); // bob.setCamp("Alpha"); // cout << bob.getFirst() << " " // << bob.getLast() << " " // << bob.getAge() << " " // << bob.getGender() << " " // << bob.getCamp() << endl; // cout << "You should have had three error messages, then" // << " printed: Bob Refugee 20 M Alpha" << endl; // Refugee ben ("Ben", "Refugee", 25, 'm', "Bravo"); // cout << ben.getFirst() << " " // << ben.getLast() << " " // << ben.getAge() << " " // << ben.getGender() << " " // << ben.getCamp() << endl; // cout << "You should have had no error messages, then" // << " printed: Ben Refugee 25 M Alpha" << endl; // cout << "The next line should print 'Exact'" << endl; // cout << ben.nameMatch("Ben", "Refugee") << endl; // cout << "The next line should print 'First'" << endl; // cout << ben.nameMatch("Ben", "UNK") << endl; // cout << "The next line should print 'Last'" << endl; // cout << ben.nameMatch("UNK", "Refugee") << endl; // cout << "The next line should print 'None'" << endl; // cout << ben.nameMatch("Sam", "Dog") << endl; // Refugee child ("UNK", "UNK", 2, 'm', "Bravo"); // cout << "This should print 1: " << child.ageMatch(3, 'M') << endl; // cout << "This should print 1: " << child.ageMatch(1, 'M') << endl; // cout << "This should print 0: " << child.ageMatch(4, 'M') << endl; // cout << "This should print 0: " << child.ageMatch(2, 'F') << endl; // return 0; // } int main(){ char choice; bool finished = false; string filename; string campName; string firstName; string lastName; int searchAge; char searchGender; cout << fixed << setprecision(2); Operation operation; while (!finished){ choice = getMenu(); switch (choice){ case 'l': case 'L': cout << "Enter name of file to load: "; getline(cin, filename); operation.loadFile(filename); break; case 'S': case 's': cout << "Enter the first name to search for: "; getline(cin, firstName); cout << "Enter the last name to search for: "; getline(cin, lastName); operation.nameSearch(firstName, lastName); break; case 'A': case 'a': cout << "Enter the age to search for: "; cin >> searchAge; cout << "Enter the gender to search for (F/M): "; cin >> searchGender; operation.childSearch(searchAge, searchGender); break; case 'C': case 'c': cout << "Enter the name of the camp to show: "; getline(cin, campName); operation.showCamp(campName); break; case 'P': case 'p': operation.showRefugees(); break; case 'T': case 't': cout << "Total refugees: " << operation.totalRefugees() << endl; break; case 'Q': case 'q': finished = true; break; default: cout << "Unknown menu selection." << endl; break; } } } char getMenu(){ char choice; cout << endl << endl; cout << setw(45) << "Add a camp list" << setw (10) << "L" << endl; cout << setw(45) << "Search for refugee by name" << setw(10) << "S" << endl; cout << setw(45) << "Search for child by age and gender" << setw (10) << "A" << endl; cout << setw(45) << "Show all refugees at a camp" << setw (10) << "C" << endl; cout << setw(45) << "Count of refugees" << setw (10) << "T" << endl; cout << setw(45) << "Print all refugees" << setw (10) << "P" << endl; cout << setw(45) << "Exit the program" << setw (10) << "Q" << endl; cout << endl; cout << "Please enter your choice: "; cin >> choice; string trash; getline (cin,trash); return choice; cout << endl << endl; }