Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. #include "employees.h"
  2. #include "bin.h"
  3. #include <string>
  4.  
  5.  
  6. std::istream &Employee::scan(std::istream &in) const {
  7.     in >>_name >> (int&)_base_salary;
  8.     return in;
  9. }
  10.  
  11. std::ifstream &Employee::scan(std::ifstream &in) const {
  12.     read_cstring r_nam(_name, sizeof(_name));
  13.     read_le_int32 r_sal(_base_salary);
  14.     in >> r_nam >> r_sal;
  15.     return in;
  16. }
  17.  
  18. std::ostream &Employee::print(std::ostream &out) const {
  19.     out << _typ << "\n" << "Name: " << _name << "\n"
  20.         << "Base Salary: " << _base_salary << "\n";
  21.     return out;
  22. }
  23.  
  24. std::ofstream &Employee::print(std::ofstream &out) const {
  25.     write_cstring w_nam(_name);
  26.     write_le_int32 w_sal(_base_salary);
  27.     out << w_nam << w_sal;
  28.     return out;
  29. }
  30.  
  31.  
  32. std::istream &Developer::scan(std::istream &in) const {
  33.     Employee::scan(in) >> (bool&)_has_bonus;
  34.     return in;
  35. }
  36.  
  37. std::ifstream &Developer::scan(std::ifstream &in) const {
  38.     Employee::scan(in);
  39.     read_bool r_bon(_has_bonus);
  40.     in >> r_bon;
  41.     return in;
  42. }
  43.  
  44. std::ostream & Developer::print(std::ostream &out) const {
  45.     Employee::print(out) << "Has bonus: " << (_has_bonus ? "+" : "-") << "\n";
  46.     return out;
  47. }
  48.  
  49. std::ofstream &Developer::print(std::ofstream &out) const {
  50.     write_le_int32 w_typ(DEVELOPER);
  51.     out << w_typ;
  52.     Employee::print(out);
  53.     write_bool w_bon(_has_bonus);
  54.     out << w_bon;
  55.     return out;
  56. }
  57.  
  58.  
  59. std::istream &SalesManager::scan(std::istream &in) const {
  60.     Employee::scan(in) >> (int&)_sold_nm >> (int&)_price;
  61.     return in;
  62. }
  63.  
  64. std::ifstream &SalesManager::scan(std::ifstream &in) const {
  65.     Employee::scan(in);
  66.     read_le_int32 r_sol(_sold_nm);
  67.     read_le_int32 r_pri(_price);
  68.     in >> r_sol >> r_pri;
  69.     return in;
  70. }
  71.  
  72. std::ostream &SalesManager::print(std::ostream &out) const {
  73.     Employee::print(out)
  74.         << "Sold items: " << _sold_nm << "\n"
  75.         << "Item price: " << _price << "\n";
  76.     return out;
  77. }
  78.  
  79. std::ofstream &SalesManager::print(std::ofstream &out) const {
  80.     write_le_int32 w_typ(SALES_MANAGER);
  81.     out << w_typ;
  82.     Employee::print(out);
  83.     write_le_int32 w_sol(_sold_nm);
  84.     write_le_int32 w_pri(_price);
  85.     out << w_sol << w_pri;
  86.     return out;
  87. }
  88.  
  89.  
  90. std::istream &operator>>(std::istream &in, Employee &obj) {
  91.     return obj.scan(in);
  92. }
  93.  
  94. std::ifstream &operator>>(std::ifstream &in, Employee &obj) {
  95.     return obj.scan(in);
  96. }
  97.  
  98. std::ostream &operator<<(std::ostream &out, const Employee &obj) {
  99.     return obj.print(out);
  100. }
  101.  
  102. std::ofstream &operator<<(std::ofstream &out, const Employee &obj) {
  103.     return obj.print(out);
  104. }
  105.  
  106.  
  107. void EmployeesArray::add(Employee *obj) {
  108.     _employees.push_back(obj);
  109. }
  110.  
  111. void EmployeesArray::add(EmployeesArray &arr) {
  112.     for (size_t i = 0; i < arr._employees.size(); i++) {
  113.         add(arr._employees[i]);
  114.     }
  115.     arr._employees.clear();
  116. }
  117.  
  118. int EmployeesArray::total_salary() const {
  119.     int total_salary = 0;
  120.     for (size_t i = 0; i < _employees.size(); i++) {
  121.         total_salary += _employees[i]->salary();
  122.     }
  123.     return total_salary;
  124. }
  125.  
  126.  
  127. std::ifstream &operator>>(std::ifstream &in, EmployeesArray &arr) {
  128.     int32_t N = 0;
  129.     read_le_int32 r_N(N);
  130.     in >> r_N;
  131.     for (int i = 0; i < N; i++) {
  132.         int32_t t = 0;
  133.         read_le_int32 r_t(t);
  134.         in >> r_t;
  135.         Employee *obj = init(t);
  136.         in >> *obj;
  137.         arr.add(obj);
  138.     }
  139.     return in;
  140. }
  141.  
  142. std::ostream &operator<<(std::ostream &out, const EmployeesArray &arr) {
  143.     for (size_t i = 0; i < arr._employees.size(); i++) {
  144.         out << i + 1 << ". " << *(arr._employees[i]);
  145.     }
  146.     out << "== Total salary: " << arr.total_salary() << "\n\n";
  147.     return out;
  148. }
  149.  
  150. std::ofstream &operator<<(std::ofstream &out, const EmployeesArray &arr) {
  151.     write_le_int32 w_N(arr._employees.size());
  152.     out << w_N;
  153.     for (size_t i = 0; i < arr._employees.size(); i++) {
  154.         out << arr._employees[i];
  155.     }
  156.     return out;
  157. }
  158.  
  159.  
  160. Employee *init(int32_t t) {
  161.     Employee *obj;
  162.     switch(t) {
  163.         case 1:
  164.             obj = new Developer();
  165.             break;
  166.         case 2:
  167.             obj = new SalesManager();
  168.             break;
  169.         default:
  170.             exit(-1);
  171.     }
  172.     return obj;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement