int main() { Employee alice = {"alice", -123}; Employee bob; Employee es[] = { alice, bob }; fire(&bob); cout << alice.getName() << endl; cout << alice.getSalary() << endl; Employee * charlie = new Employee(); // delete charlie; fire(charlie); charlie->setName("Charlie"); Employee * dan = new Employee("Dan", 234432); cout << dan->getName() << " $" << dan->getSalary() << endl; //delete charlie; //delete dan; bob.setSalary(-234); bob.setName("Bob"); cout << bob.getSalary() << endl; }
ptree unparse(const Employee& employee) { ptree tree; tree.put("name", employee.getName()); tree.put("address", employee.getAddress()); tree.put("salary", employee.getSalary()); return tree; }
/* Main Function, follows these steps: 1. get filename 2. read in xml records and create an employee by calling Employee::fromXML(steam); 3. cout each employee object using display() 4. write employee objects to a new file 'employee.bin' 5. clear employees vector 6. iterate through new file and reload employees vector with employee records 7. print out each employee record as xml. toXML() 8. search for employee 12345. retrieve() 9. change salary of retrieved employee to 150,000.0 10. write employee back to the file 11. retrieve the same employee again and print its salary 12. create a new employee with my choice of data!! such freedom 13. store my new employee. store() 14. retrieve and display my new employee */ int main(int argc, char* argv[]) { // Check for arg if (argv[1] == nullptr) { //throw a fit here cout << "no arguments passed... noob" << endl; return 0; } // Step 1: get file name and open file fstream file; file.open(argv[1]); if (file.fail()) { cout << "File could not open... exiting" << endl; return 0; } vector<unique_ptr<Employee>> employees; // init employees vector // Step 2: read in employee objects from xml file while (!file.eof()) { try { Employee * emp = Employee::fromXML(file); if (emp != nullptr) { employees.push_back(unique_ptr<Employee>(emp)); } } catch (runtime_error e) { cout << e.what() << endl; } } // Step 3: display each employee record for (int i = 0; i < employees.size(); i++) { employees[i]->display(cout); } // Step 4: write employee objects to a new file 'employee.bin' fstream outFile("employee.bin", ios::out | ios::binary | ios::trunc); if (!outFile.fail() && !outFile.bad()) { for (int i = 0; i < employees.size(); i++) { employees[i]->write(outFile); } } outFile.close(); // Step 5: clear employees vector employees.clear(); // Step 6: iterate through new file and reload employees vector with employee records fstream inFile("employee.bin", ios::in | ios::out | ios::binary); if (!inFile.fail() && !inFile.bad()) { Employee* emp = Employee::read(inFile); while (emp->validateEmployeeData()) { employees.push_back(unique_ptr<Employee>(emp)); emp = Employee::read(inFile); } } // Step 7: print out each employee record as xml.toXML() for (int i = 0; i < employees.size(); i++) { employees[i]->toXML(cout); } // Step 8: search for employee 12345. retrieve() int idToSearch = 12345; Employee* emp = Employee::retrieve(inFile, idToSearch); // Step 9: change salary of retrieved employee to 150, 000.0 double newSalary = 150000; if (emp != nullptr) { emp->setSalary(newSalary); } // Step 10: write employee back to the file emp->store(inFile); // Step 11: retrieve the same employee again and print its salary emp = Employee::retrieve(inFile, idToSearch); cout << "Employees new salary: " << emp->getSalary() << endl; // Step 12: create a new employee with my choice of data!!such freedom Employee* goku = new Employee(9001, "Goku", "out in the woods", "Somewhere", "NA", "NA", "1-800-OVER-9000", 9001); // Step 13: store my new employee.store() goku->store(inFile); // Step 14: retrieve and display my new employee goku = Employee::retrieve(inFile, 9001); goku->display(cout); // We're Done!!! huzzah! inFile.close(); delete goku; employees.clear(); cin.get(); }
bool CompareBySalary::operator()(const Employee& a , const Employee& b) const { return a.getSalary() < b.getSalary(); }
bool equalSalary(const Employee &lv, const Employee &rv) { return lv.getSalary() == rv.getSalary(); }
//if two employees have equal salaries and experience, they compare equal bool operator==(const Employee &lv, const Employee &rv) { return ( ( lv.getExperience() == rv.getExperience() ) && ( lv.getSalary() == rv.getSalary() ) ); }
int main(int argc, char** argv) { //Check for arg if(argc < 2) error("no file name provided. Exiting."); //Verify file exists std::filebuf buffer; if(!buffer.open(argv[1],std::ios::in)) error("Error opening file. Verify it exists."); std::istream in(&buffer); //Verify tag structure if(!pass1(&in)) error("mismatched tag brackets. \nPlease verify correct structure."); std::filebuf buffer2; buffer2.open(argv[1],std::ios::in); std::istream in2(&buffer2); if(!pass2(&in2)) error("Mismatched tags. Please verify all tags have matching close tag"); std::filebuf buffer3; buffer3.open(argv[1],std::ios::in); std::istream in3(&buffer3); std::vector<Employee*> employees; //Read in Employees to XML Employee* e; while(!in3.eof()) { char c = in3.peek(); if(isspace(c) || c == EOF) { in3.get(); continue; } //std::cout << in3.peek() << std::endl; // std::cout << in3.eof() << std::endl; e = Employee::fromXML(in3); if(e == nullptr) error("Missing or incorrect employee attribute."); employees.push_back(e); } //delete e; buffer3.close(); //Print out employees to std::cout for(int i=0; i < employees.size(); i++) { employees[i]->display(std::cout); std::cout << std::endl; std::cout.flush(); } std::cout << std::endl; //Print out employees to file // //this is stupid and kind of hack-y but I did it for a good reason. //When opening my fstream below, opening with the 'in' and 'out' params //consistently failed. I presume because when doing so fstream tries to //open an istream from a non-existent file. // //So instead I open, create file, close, and reopen with in,out, and binary. std::fstream fs(OUTPUT_FILE,std::ios::out); fs.close(); fs.open(OUTPUT_FILE,std::ios::in | std::ios::out | std::ios::binary); if(fs.fail()) error("Failed to open fstream."); for(int i = 0; i < employees.size(); i++) { employees[i]->write(fs); fs << "\n"; // delete employees[i]; } fs.flush(); //clear vector employees.clear(); //reset fstream to beginning of file - for whatever reason seekg only wored once. //had to close and reopen for each ensuing operation./ refreshFstream(fs); //repopulate vector with calls to read (from employee.bin...correct???) while(!fs.eof()) { if(isspace(fs.peek())) { fs.get(); continue; } Employee* e = Employee::read(fs); if(e != nullptr) employees.push_back(e); } //print to cout XML representation of employees for(int i = 0; i < employees.size(); i++) { employees[i]->toXML(std::cout); std::cout << "\n"; // delete employees[i]; } std::cout << std::endl; //reset fstream to beginning of file fs.clear(); fs.seekg(0,fs.beg); //find employee 12345 and print out e = Employee::retrieve(fs,12345); std::cout << "Found:" << std::endl; e->display(std::cout); std::cout << std::endl; //update salary e->setSalary(150000.0); //store in XML file refreshFstream(fs); e->store(fs); // delete e; //Retrieve and print salary refreshFstream(fs); e = Employee::retrieve(fs,12345); std::cout << e->getSalary() << "\n" << std::endl; // delete e; //is it awesome or pathetic that I knew these lyrics? I'll let you be the judge. Employee* my_e = new Employee(1987,std::string("Rick Astley"),std::string("Never Gonna Give You Up street"),std::string("Never Gonna Let You Down city"),std::string("Never Gonna Run Around and Desert You state"),std::string("England"),std::string("1-888-RICKROLLED"),1000.0); refreshFstream(fs); my_e->store(fs); fs.flush(); //reset fs for retrieval and printing of Mr. Astley refreshFstream(fs); e = Employee::retrieve(fs,1987); e->display(std::cout); std::cout << std::endl; // delete e; fs.close(); // delete my_e; // delete e; }