Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cassert>
  4. #include <vector>
  5. #include <string>
  6.  
  7. #include "employees.h"
  8. #include "bin_manip.h"
  9.  
  10. std::ostream& operator<<(std::ostream& os, const Employee& emp) {
  11. return emp.print(os);
  12. }
  13.  
  14. std::istream& operator>>(std::istream& ist, Employee& emp) {
  15. return emp.read(ist);
  16. }
  17.  
  18. std::ofstream& operator<<(std::ofstream& ofs, const Employee& emp) {
  19. return emp.print(ofs);
  20. }
  21.  
  22. std::ifstream& operator>>(std::ifstream& ifs, Employee& emp) {
  23. return emp.read(ifs);
  24. }
  25.  
  26. std::ostream& Developer::print(std::ostream& os) const {
  27. os << "Developer" << '\n';
  28. os << "Name: " << name_ << '\n';
  29. os << "Base Salary: " << base_salary_ << '\n';
  30. return os << "Has bonus: " << (has_bonus_ ? '+' : '-') << std::endl;
  31. }
  32.  
  33. std::istream& Developer::read(std::istream& ist) {
  34. return ist >> name_ >> base_salary_ >> has_bonus_;
  35. }
  36.  
  37. std::ofstream& Developer::print(std::ofstream& ofs) const {
  38. ofs << write_le_int32(1) << write_str(name_)
  39. << write_le_int32(base_salary_) << write_bool(has_bonus_);
  40. return ofs;
  41. }
  42.  
  43. std::ifstream& Developer::read(std::ifstream& ifs) {
  44. ifs >> read_str(name_) >> read_le_int32(base_salary_) >> read_bool(has_bonus_);
  45. return ifs;
  46. }
  47.  
  48. std::ostream& SalesManager::print(std::ostream& os) const {
  49. os << "Sales Manager" << '\n';
  50. os << "Name: " << name_ << '\n';
  51. os << "Base Salary: " << base_salary_ << '\n';
  52. os << "Sold items: " << sold_nm_ << '\n';
  53. return os << "Item price: " << price_ << std::endl;
  54. }
  55.  
  56. std::istream& SalesManager::read(std::istream& ist) {
  57. return ist >> name_ >> base_salary_ >> sold_nm_ >> price_;
  58. }
  59.  
  60. std::ofstream& SalesManager::print(std::ofstream& ofs) const {
  61. s << write_le_int32(2) << write_str(name_) << write_le_int32(base_salary_)
  62. << write_le_int32(sold_nm_) << write_le_int32(price_);
  63. return s;
  64. }
  65.  
  66. std::ifstream& SalesManager::read(std::ifstream& ifs) {
  67. ifs >> read_str(name_) >> read_le_int32(base_salary_)
  68. >> read_le_int32(sold_nm_) >> read_le_int32(price_);
  69. return ifs;
  70. }
  71.  
  72. std::ostream& EmployeesArray::print(std::ostream& os) const {
  73. for (int i = 0; i < (int)employees_.size(); i++) {
  74. assert(employees_[i] != nullptr);
  75. os << i + 1 << ". " << (*employees_[i]);
  76. }
  77. os << "== Total salary: " << total_salary() << std::endl << std::endl;
  78. return os;
  79. }
  80.  
  81. std::ostream& operator<<(std::ostream& os, const EmployeesArray& emps) {
  82. return emps.print(os);
  83. }
  84.  
  85. std::ofstream& EmployeesArray::print(std::ofstream& ofs) const {
  86. ofs << write_le_int32(employees_.size());
  87. for (int i = 0; i < (int)employees_.size(); i++) {
  88. ofs << (*employees_[i]);
  89. }
  90. return ofs;
  91. }
  92.  
  93. std::ofstream& operator<<(std::ofstream& ofs, const EmployeesArray& emps) {
  94. return emps.print(ofs);
  95. }
  96.  
  97. void EmployeesArray::add(Employee *emp) {
  98. employees_.push_back(emp);
  99. }
  100.  
  101. std::ifstream& EmployeesArray::read(std::ifstream& ifs) {
  102. int32_t size_array;
  103. ifs >> read_le_int32(size_array);
  104. for (int i = 0; i < size_array; i++) {
  105. int32_t type;
  106. ifs >> read_le_int32(type);
  107. Employee *current_employee;
  108. if (type == 1) {
  109. current_employee = new Developer();
  110. } else {
  111. current_employee = new SalesManager();
  112. }
  113. ifs >> (*current_employee);
  114. add(current_employee);
  115. }
  116. return ifs;
  117. }
  118.  
  119. std::ifstream& operator>>(std::ifstream& ifs, EmployeesArray& emp) {
  120. return emp.read(ifs);
  121. }
  122.  
  123. int32_t EmployeesArray::total_salary() const {
  124. int32_t sum = 0;
  125. for (int i = 0; i < (int)employees_.size(); i++) {
  126. sum += employees_[i]->salary();
  127. }
  128. return sum;
  129. }
  130.  
  131. EmployeesArray::~EmployeesArray() {
  132. for (int i = 0; i < (int)employees_.size(); i++) {
  133. delete employees_[i];
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement