SHARE
TWEET

Untitled

a guest Mar 26th, 2020 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ConsoleHandler.h"
  2. #include <iostream>
  3. #include <Position.h>
  4. #include <Programmer.h>
  5. #include <Analyzer.h>
  6. #include <Lead.h>
  7. #include <vector>
  8. #include <list>
  9. #include <memory>
  10. #include <algorithm>
  11.  
  12. ConsoleHandler::ConsoleHandler()
  13. {
  14.     //ctor
  15. }
  16.  
  17. ConsoleHandler::~ConsoleHandler()
  18. {
  19.     //dtor
  20. }
  21.  
  22.  
  23. std::list<std::unique_ptr<Employee>> ConsoleHandler::readInput()
  24. {
  25.     // unique pointers so I can use runtime polymorphism and access virtual methods in derived classes
  26.     std::list<std::unique_ptr<Employee>> employeeList;
  27.     int employeesFilled = 0;
  28.     std::string currentInput;
  29.  
  30.     std::cout << "Please input information about the company. \n";
  31.     std::cout << "You will have to input more than one worker(s). The input continues until \"END\" is received. \n";
  32.  
  33.     while (currentInput != "END"){
  34.         addEmployee(employeeList);
  35.         employeesFilled++;
  36.         std::cin >> currentInput;
  37.     }
  38.  
  39.     return employeeList;
  40. }
  41.  
  42.  void ConsoleHandler::print(const std::list<std::unique_ptr<Employee>>& printableList){
  43.     for (const auto& it : printableList)   std::cout << *it << std::string(" ");
  44. }
  45.  
  46.  void ConsoleHandler::addEmployee(std::list<std::unique_ptr<Employee>>& printableList){
  47.         std::string currentInput, positionName, employeeName, employeeAddress, employeePIN, employeeLeader, employeeStartDate;
  48.         int positionLevel;
  49.  
  50.         std::cout << "Please input this employee's position: (programmer, analyzer, lead) -- they are case sensitive \n";
  51.         std::cin >> positionName;
  52.  
  53.         // can be improved with regex
  54.         // white spaces not trimmed
  55.         while (positionName != "programmer" && positionName != "analyzer" && positionName != "lead"){
  56.             std::cout << "The position is invalid. Please try again. \n";
  57.             std::cin >> positionName;
  58.         }
  59.  
  60.         std::cout << "Please input this employee's position level: \n ";
  61.         std::cin >> positionLevel;
  62.  
  63.         std::cout << "Please input this employee's full name: \n";
  64.         std::cin >> employeeName;
  65.  
  66.         std::cout << "Please input this employee's address: \n";
  67.         std::cin >> employeeAddress;
  68.  
  69.         std::cout << "Please input this employee's PIN: \n";
  70.         std::cin >> employeePIN;
  71.  
  72.         std::cout << "Please input this employee's start date: \n";
  73.         std::cin >> employeeStartDate;
  74.  
  75.         std::cout << "Please input this employee's leader: \n";
  76.         std::cin >> employeeLeader;
  77.  
  78.         Position position(positionName, positionLevel);
  79.  
  80.         std::string projectName, customerEmail;
  81.         std::list<std::string> analyzer_customerEmails;
  82.  
  83.         if (position.getPositionName() == "programmer"){
  84.             std::cout << "Please input the project's name this programmer is working on: \n";
  85.             std::cin >> projectName;
  86.  
  87.             try {
  88.             printableList.emplace_back(std::make_unique<Programmer>(employeeName, employeeAddress, employeePIN, employeeStartDate,
  89.                              employeeLeader, position, projectName));
  90.             } catch (const std::exception& e){
  91.                 std::cout << "Unable to create Employee. \n";
  92.             }
  93.            // employeeList.emplace_back(Programmer(employeeName, employeeAddress, employeePIN, employeeStartDate, employeeLeader, position, projectName));
  94.         } else if (position.getPositionName() == "analyzer" || position.getPositionName() == "lead"){
  95.             std::cout << "Please input the project's name this analyzer is working on: \n";
  96.             std::cin >> projectName;
  97.  
  98.             std::cout << "Please input the customers' emails. The iteration continues until \"END\" is inputted. \n";
  99.             std::cin >> customerEmail;
  100.  
  101.             while (customerEmail != "END"){
  102.                 analyzer_customerEmails.emplace_back(std::string(customerEmail));
  103.                 std::cin >> customerEmail;
  104.             }
  105.  
  106.             try {
  107.                 printableList.emplace_back(std::make_unique<Analyzer>(employeeName, employeeAddress, employeePIN, employeeStartDate,
  108.                               employeeLeader, position, projectName, analyzer_customerEmails));
  109.             } catch (const std::exception& e){
  110.                 std::cout << "Unable to create Employee. \n";
  111.             }
  112.         }
  113.  }
  114.  
  115. void ConsoleHandler::printEmployeeInfo(const std::list<std::unique_ptr<Employee>>& printableList, const std::string& name){
  116.     for (const auto& it : printableList){
  117.         if (it->getFullName() == name) {
  118.             std::cout << *it << std::string(" ");
  119.             return;
  120.         }
  121.     }
  122.     std::cout << "Unable to find username. \n";
  123. }
  124.  
  125. void ConsoleHandler::sackEmployee(std::list<std::unique_ptr<Employee>>& printableList, const std::string& name){
  126.      for (const auto& it : printableList){
  127.         if (it->getFullName() == name) {
  128.             printableList.remove(it);
  129.             return;
  130.         }
  131.     }
  132.  
  133.     std::cout << "Unable to find username";
  134. }
  135.  
  136. void ConsoleHandler::printEmployeesByPosition(const std::list<std::unique_ptr<Employee>>& printableList, const std::string& pos){
  137.         for (const auto& it : printableList){
  138.          if (it->getPositionName() == pos) {
  139.             std::cout << *it << std::string(" ");
  140.         }
  141.     }
  142. }
  143.  
  144. void ConsoleHandler::changeEmployeeInfo(Employee& employee, const std::string& modify_field, const std::string& modify_value){
  145.     // thank you C++ for not supporting switch case for strings
  146.     if (modify_field == "name") employee.setFullName(modify_value);
  147.     else if (modify_field == "address") employee.setAddress(modify_value);
  148.     else if (modify_field == "PIN") employee.setPIN(modify_value);
  149.     else if (modify_field == "startDate") employee.setStartDate(modify_value);
  150.     else if (modify_field == "leader") employee.setLeader(modify_value);
  151.     else if (modify_field == "position"){
  152.         int posLevel;
  153.         std::cout << "Please input position level: ";
  154.         std::cin >> posLevel;
  155.  
  156.         employee.setPosition(Position(modify_field, posLevel));
  157.     }
  158.  
  159.     else if (modify_field == "project"){
  160.         // could throw exception depending whether its derived or base class
  161.         employee.setProjectName(modify_value);
  162.     } else if (modify_field == "email"){
  163.         std::string action;
  164.         std::cin >> action;
  165.         std::cout << "Would you like to remove or add emails or modify existing emails? Input ADD or REMOVE or MODIFY \n";
  166.  
  167.         // kind of memory consuming to create a new instance rather than use a pointer; might fix later
  168.         std::list<std::string> customers = employee.getCustomerEmails();
  169.         customers.emplace_back(modify_value);
  170.  
  171.         if (action == "ADD") customers.emplace_back(modify_value);
  172.         else if (action == "REMOVE") customers.remove(modify_value);
  173.         else if (action == "MODIFY") {
  174.             std::string newValue;
  175.             std::cout << "Please add the new value for " << modify_value << "\n";
  176.  
  177.             /* we need to grab the index of the value, remove it and insert the new one at this index, but we won't be doing
  178.             it at this point */
  179.             customers.remove(modify_value);
  180.             customers.emplace_back(newValue);
  181.         }
  182.  
  183.         employee.setCustomerEmails(customers);
  184.     }
  185. }
  186.  
  187. void ConsoleHandler::createSquad(std::list<std::unique_ptr<Squad>>& squads, const std::list<std::unique_ptr<Employee>>& printableList){
  188.     std::string squadName, leaderName, projectName;
  189.     std::list<std::string> empsToAdd;
  190.     bool isLeaderValid = false;
  191.     bool isCurrentEmployeeValid = false;
  192.  
  193.     std::cout << "Please input the name of the squad: \n";
  194.     std::cin >> squadName;
  195.  
  196.     std::cout << "Please input the name of the leader: \n";
  197.     std::cin >> leaderName;
  198.  
  199.     /*
  200.         1. Iterate through all employees to find whether the leader is an active employee.
  201.         2. Iterate with an inner cycle through all employees again to see whether the employee we are adding is
  202.         already inside a squad (every employee can have one squad only)
  203.  
  204.     */
  205.  
  206.    for (auto& it : printableList){
  207.     if (it->getFullName() == leaderName){
  208.         isLeaderValid = true;
  209.         std::cout << "Please input the name of the project: \n";
  210.         std::cin >> projectName; // no check to see if it's valid
  211.  
  212.         std::string inputNext, employeeName;
  213.         std::cout << "Press any key to continue. \n";
  214.         std::cin >> inputNext;
  215.  
  216.         while (inputNext != "END"){
  217.             std::cout << "Please input the name of the employees in this squad. Filling continues until END is submitted.";
  218.             std::cin >> employeeName;
  219.  
  220.             for (auto& it2 : printableList){
  221.                 if (it2 != it && it2->getFullName() == employeeName && !it2->getSquad().empty()){
  222.                     isCurrentEmployeeValid = true;
  223.                     it2->setSquad(squadName);
  224.                     empsToAdd.emplace_back(it2->getFullName());
  225.                 }
  226.             }
  227.  
  228.             if (!isCurrentEmployeeValid) {
  229.                 std::cout << "Unable to find an Employee with such name. Please try again. \n";
  230.                 continue;
  231.             }
  232.  
  233.             std::cin >> inputNext;
  234.         }
  235.  
  236.         try{
  237.         Squad squad(projectName,
  238.                      it->getFullName(),
  239.                      projectName,
  240.                       empsToAdd);
  241.  
  242.         squads.emplace_back(std::make_unique<Squad>(squad));
  243.         } catch (const std::exception& e){
  244.             std::cout << "Unable to create squad. \n";
  245.         }
  246.     }
  247.    }
  248.  
  249.    if (!isLeaderValid) std::cout << "Unable to find an employee with such name. \n";
  250. }
  251.  
  252. void ConsoleHandler::changeEmployeeSquad(std::list<std::unique_ptr<Squad>>& squads, std::list<std::unique_ptr<Employee>>& employees){
  253.     std::string employeeName, newSquad;
  254.     bool isSquadValid;
  255.  
  256.     std::cout << "Please input the name of the employee you would like to change the squad of: \n";
  257.     std::cin >> employeeName;
  258.  
  259.     for (const auto& empIterator : employees) {
  260.         if (empIterator->getFullName() == employeeName){
  261.              std::cout << "Please input the name of the squad you would like this employee to receive: \n";
  262.              std::cin >> newSquad;
  263.  
  264.              for (const auto& squadIterator : squads){
  265.                 if (squadIterator->getSquadName() == newSquad){
  266.                     isSquadValid = true;
  267.                     empIterator->setSquad(squadIterator->getSquadName());
  268.                 }
  269.              }
  270.         }
  271.     }
  272.  
  273.     if (!isSquadValid) std::cout << "Invalid name of squad. \n";
  274. }
  275.  
  276. void ConsoleHandler::deleteSquad(std::list<std::unique_ptr<Squad>>& squads, std::list<std::unique_ptr<Employee>>& employee){
  277.     std::string squadToDelete;
  278.     std::cout << "Please input the name of the squad you wish to delete: \n";
  279.  
  280.     std::cin >> squadToDelete;
  281.     bool isSquadValid = false;
  282.  
  283.     for (const auto& sq : squads){
  284.         if (sq->getSquadName() == squadToDelete){
  285.             isSquadValid = true;
  286.             squads.remove(sq);
  287.         }
  288.     }
  289.  
  290.     if (!isSquadValid) {
  291.         std::cout << "Invalid name of squad. \n";
  292.         return;
  293.     }
  294.  
  295.     for (const auto& emp : employee){
  296.         if (emp->getSquad() == squadToDelete){
  297.             emp->setSquad("NOSQUAD");
  298.         }
  299.     }
  300. }
  301.  
  302. void ConsoleHandler::printSquads(std::list<std::unique_ptr<Squad>>& squads){
  303.     for (const auto& it : squads){
  304.         std::cout << "Squad name: " << it->getSquadName() << "\n";
  305.         std::cout << "Leader name " << it->getLeaderName() << "\n";
  306.         std::cout << "Project name: " <<it->getProject() << "\n";
  307.  
  308.         std::cout << "Employees: \n";
  309.  
  310.         for (const auto& emp : it->getEmployees()){
  311.             std::cout << emp << "\n";
  312.         }
  313.         std::cout << "\n";
  314.     }
  315. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top