Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cassert>
- #include <vector>
- #include <string>
- #include "employees.h"
- #include "bin_manip.h"
- std::ostream& operator<<(std::ostream& os, const Employee& emp) {
- return emp.print(os);
- }
- std::istream& operator>>(std::istream& ist, Employee& emp) {
- return emp.read(ist);
- }
- std::ofstream& operator<<(std::ofstream& ofs, const Employee& emp) {
- return emp.print(ofs);
- }
- std::ifstream& operator>>(std::ifstream& ifs, Employee& emp) {
- return emp.read(ifs);
- }
- std::ostream& Developer::print(std::ostream& os) const {
- os << "Developer" << '\n';
- os << "Name: " << name_ << '\n';
- os << "Base Salary: " << base_salary_ << '\n';
- return os << "Has bonus: " << (has_bonus_ ? '+' : '-') << std::endl;
- }
- std::istream& Developer::read(std::istream& ist) {
- return ist >> name_ >> base_salary_ >> has_bonus_;
- }
- std::ofstream& Developer::print(std::ofstream& ofs) const {
- ofs << write_le_int32(1) << write_str(name_)
- << write_le_int32(base_salary_) << write_bool(has_bonus_);
- return ofs;
- }
- std::ifstream& Developer::read(std::ifstream& ifs) {
- ifs >> read_str(name_) >> read_le_int32(base_salary_) >> read_bool(has_bonus_);
- return ifs;
- }
- std::ostream& SalesManager::print(std::ostream& os) const {
- os << "Sales Manager" << '\n';
- os << "Name: " << name_ << '\n';
- os << "Base Salary: " << base_salary_ << '\n';
- os << "Sold items: " << sold_nm_ << '\n';
- return os << "Item price: " << price_ << std::endl;
- }
- std::istream& SalesManager::read(std::istream& ist) {
- return ist >> name_ >> base_salary_ >> sold_nm_ >> price_;
- }
- std::ofstream& SalesManager::print(std::ofstream& ofs) const {
- s << write_le_int32(2) << write_str(name_) << write_le_int32(base_salary_)
- << write_le_int32(sold_nm_) << write_le_int32(price_);
- return s;
- }
- std::ifstream& SalesManager::read(std::ifstream& ifs) {
- ifs >> read_str(name_) >> read_le_int32(base_salary_)
- >> read_le_int32(sold_nm_) >> read_le_int32(price_);
- return ifs;
- }
- std::ostream& EmployeesArray::print(std::ostream& os) const {
- for (int i = 0; i < (int)employees_.size(); i++) {
- assert(employees_[i] != nullptr);
- os << i + 1 << ". " << (*employees_[i]);
- }
- os << "== Total salary: " << total_salary() << std::endl << std::endl;
- return os;
- }
- std::ostream& operator<<(std::ostream& os, const EmployeesArray& emps) {
- return emps.print(os);
- }
- std::ofstream& EmployeesArray::print(std::ofstream& ofs) const {
- ofs << write_le_int32(employees_.size());
- for (int i = 0; i < (int)employees_.size(); i++) {
- ofs << (*employees_[i]);
- }
- return ofs;
- }
- std::ofstream& operator<<(std::ofstream& ofs, const EmployeesArray& emps) {
- return emps.print(ofs);
- }
- void EmployeesArray::add(Employee *emp) {
- employees_.push_back(emp);
- }
- std::ifstream& EmployeesArray::read(std::ifstream& ifs) {
- int32_t size_array;
- ifs >> read_le_int32(size_array);
- for (int i = 0; i < size_array; i++) {
- int32_t type;
- ifs >> read_le_int32(type);
- Employee *current_employee;
- if (type == 1) {
- current_employee = new Developer();
- } else {
- current_employee = new SalesManager();
- }
- ifs >> (*current_employee);
- add(current_employee);
- }
- return ifs;
- }
- std::ifstream& operator>>(std::ifstream& ifs, EmployeesArray& emp) {
- return emp.read(ifs);
- }
- int32_t EmployeesArray::total_salary() const {
- int32_t sum = 0;
- for (int i = 0; i < (int)employees_.size(); i++) {
- sum += employees_[i]->salary();
- }
- return sum;
- }
- EmployeesArray::~EmployeesArray() {
- for (int i = 0; i < (int)employees_.size(); i++) {
- delete employees_[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement