Main
unknown
c_cpp
2 months ago
8.0 kB
3
Indexable
#include <iostream> #include <fstream> #include <stdexcept> #include <memory> #include "Employee.h" #include <vector> using namespace std; int main(int argc, char* argv[]) { // Check if a filename was provided as a command-line argument if (argc != 2) { cerr << "Usage: " << argv[0] << " <XMLFile>" << endl; return 1; } const string filename = argv[1]; ifstream xmlFile(filename); if (!xmlFile) { cerr << "Error: Could not open file " << filename << endl; return 1; } else { vector<unique_ptr<Employee>> employees; try { cout << "Employee data successfully loaded from XML:" << endl; // Processing each employee in the XML file and displaying their info or throwing erros if the file has issues while (!xmlFile.eof()) { Employee* employee = Employee::fromXML(xmlFile); if (employee) { employees.push_back(unique_ptr<Employee>(employee)); employee->display(cout); cout << endl; } } cout << "Finished processing all employees.\n" << endl; // Writing my employees to employee.bin ofstream binaryFile("employee.bin", ios::binary | ios::out); if (!binaryFile) { cerr << "Error: Could not open employee.bin for writing." << endl; return 1; } for (const auto& emp : employees) { emp->write(binaryFile); } binaryFile.close(); cout << "Employee data successfully written to employee.bin." << endl; employees.clear(); // Walking my file and reading back the employee records that's in it ifstream readFile("employee.bin", ios::binary | ios::in); if (!readFile) { cerr << "Error: Could not open employee.bin for reading." << endl; return 1; } while (true) { Employee* employee = Employee::read(readFile); if (employee == nullptr) { break; // End of file } employees.push_back(unique_ptr<Employee>(employee)); } cout << "Finished reading employee data from employee.bin." << endl; // Looping through my vector and printing in XML format for each of my employees for (const auto& emp : employees) { emp->toXML(cout); cout << endl; } // Search for the employee with id 12345 int searchId = 12345; Employee* foundEmployee = nullptr; for (const auto& emp : employees) { if (emp->id == searchId) { foundEmployee = emp.get(); break; } } if (foundEmployee) { cout << "Found the employee matching the id: " << searchId << endl; foundEmployee->display(cout); } else { cerr << "Error: Employee with ID " << searchId << " not found." << endl; } // Setting the new salary for the employee I searched and found if (foundEmployee) { foundEmployee->salary = 150000.00; cout << "Updated Salary: " << foundEmployee->salary << endl; } cout << endl; // Storing the modified employee in the binary file again ofstream modifiedEmployeeInBinFile("employee.bin", ios::binary | ios::out | ios::trunc); if (!modifiedEmployeeInBinFile) { cerr << "Error: Could not open employee.bin for writing." << endl; return 1; } for (const auto& emp : employees) { emp->write(modifiedEmployeeInBinFile); } modifiedEmployeeInBinFile.close(); cout << "Employee data successfully written back to employee.bin." << endl; // Retrieve the employee again by id 12345 to confirm the salary was succesfully updated ifstream retrieveEmployeeAgain("employee.bin", ios::binary | ios::in); if (!retrieveEmployeeAgain) { cerr << "Error: Could not open employee.bin for reading." << endl; return 1; } // Clearing my vector before I re-read it employees.clear(); while (true) { Employee* employee = Employee::read(retrieveEmployeeAgain); if (employee == nullptr) { break; // End of file } employees.push_back(unique_ptr<Employee>(employee)); } retrieveEmployeeAgain.close(); // Searching for employee with id 12345 and displaying their updated salary Employee* updatedEmployee = nullptr; for (const auto& emp : employees) { if (emp->id == 12345) { updatedEmployee = emp.get(); break; } } // Printing the updated salary after we've retrived it! if (updatedEmployee) { cout << "Updated Salary after retrieval: " << updatedEmployee->salary << endl; } else { cerr << "Error: Employee with ID 12345 not found after update." << endl; } cout << endl; cout << "Printing the Employee data with the updated salary." << endl; for (const auto& emp : employees) { if (emp->id == 12345) { emp->display(std::cout); } } cout << endl; // Creating a new Employee object Employee* newEmployee = new Employee(); newEmployee->id = 1840; // New unique id newEmployee->name = "Tyler Perkins"; newEmployee->salary = 180000.00; newEmployee->address = ("Somewhere Dr."); newEmployee->city = "Draper"; newEmployee->state = "Utah"; newEmployee->country = "USA"; newEmployee->phone = "801-699-3941"; employees.push_back(unique_ptr<Employee>(newEmployee)); // Storing my new employee in the file ofstream storeNewEmployee("employee.bin", ios::binary | ios::out | ios::app); if (!storeNewEmployee) { cerr << "Error: Could not open employee.bin for writing." << endl; return 1; } // Writing my new employee to the file newEmployee->write(storeNewEmployee); storeNewEmployee.close(); cout << "New employee stored in employee.bin." << endl; // Retrieving my new employee by their id int newEmployeeId = 1840; Employee* retrievedNewEmployee = nullptr; for (const auto& emp : employees) { if (emp->id == newEmployeeId) { retrievedNewEmployee = emp.get(); break; } } // Display the data for the new employee that I created if (retrievedNewEmployee) { cout << "Retrieved New Employee:" << endl; retrievedNewEmployee->display(cout); } else { cerr << "Error: New employee with ID " << newEmployeeId << " not found." << endl; } } catch (const runtime_error& e) { cerr << e.what() << endl; return 1; } } return 0; }
Editor is loading...
Leave a Comment