Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "employees.h"
- #include "bin.h"
- #include <string>
- std::istream &Employee::scan(std::istream &in) const {
- in >>_name >> (int&)_base_salary;
- return in;
- }
- std::ifstream &Employee::scan(std::ifstream &in) const {
- read_cstring r_nam(_name, sizeof(_name));
- read_le_int32 r_sal(_base_salary);
- in >> r_nam >> r_sal;
- return in;
- }
- std::ostream &Employee::print(std::ostream &out) const {
- out << _typ << "\n" << "Name: " << _name << "\n"
- << "Base Salary: " << _base_salary << "\n";
- return out;
- }
- std::ofstream &Employee::print(std::ofstream &out) const {
- write_cstring w_nam(_name);
- write_le_int32 w_sal(_base_salary);
- out << w_nam << w_sal;
- return out;
- }
- std::istream &Developer::scan(std::istream &in) const {
- Employee::scan(in) >> (bool&)_has_bonus;
- return in;
- }
- std::ifstream &Developer::scan(std::ifstream &in) const {
- Employee::scan(in);
- read_bool r_bon(_has_bonus);
- in >> r_bon;
- return in;
- }
- std::ostream & Developer::print(std::ostream &out) const {
- Employee::print(out) << "Has bonus: " << (_has_bonus ? "+" : "-") << "\n";
- return out;
- }
- std::ofstream &Developer::print(std::ofstream &out) const {
- write_le_int32 w_typ(DEVELOPER);
- out << w_typ;
- Employee::print(out);
- write_bool w_bon(_has_bonus);
- out << w_bon;
- return out;
- }
- std::istream &SalesManager::scan(std::istream &in) const {
- Employee::scan(in) >> (int&)_sold_nm >> (int&)_price;
- return in;
- }
- std::ifstream &SalesManager::scan(std::ifstream &in) const {
- Employee::scan(in);
- read_le_int32 r_sol(_sold_nm);
- read_le_int32 r_pri(_price);
- in >> r_sol >> r_pri;
- return in;
- }
- std::ostream &SalesManager::print(std::ostream &out) const {
- Employee::print(out)
- << "Sold items: " << _sold_nm << "\n"
- << "Item price: " << _price << "\n";
- return out;
- }
- std::ofstream &SalesManager::print(std::ofstream &out) const {
- write_le_int32 w_typ(SALES_MANAGER);
- out << w_typ;
- Employee::print(out);
- write_le_int32 w_sol(_sold_nm);
- write_le_int32 w_pri(_price);
- out << w_sol << w_pri;
- return out;
- }
- std::istream &operator>>(std::istream &in, Employee &obj) {
- return obj.scan(in);
- }
- std::ifstream &operator>>(std::ifstream &in, Employee &obj) {
- return obj.scan(in);
- }
- std::ostream &operator<<(std::ostream &out, const Employee &obj) {
- return obj.print(out);
- }
- std::ofstream &operator<<(std::ofstream &out, const Employee &obj) {
- return obj.print(out);
- }
- void EmployeesArray::add(Employee *obj) {
- _employees.push_back(obj);
- }
- void EmployeesArray::add(EmployeesArray &arr) {
- for (size_t i = 0; i < arr._employees.size(); i++) {
- add(arr._employees[i]);
- }
- arr._employees.clear();
- }
- int EmployeesArray::total_salary() const {
- int total_salary = 0;
- for (size_t i = 0; i < _employees.size(); i++) {
- total_salary += _employees[i]->salary();
- }
- return total_salary;
- }
- std::ifstream &operator>>(std::ifstream &in, EmployeesArray &arr) {
- int32_t N = 0;
- read_le_int32 r_N(N);
- in >> r_N;
- for (int i = 0; i < N; i++) {
- int32_t t = 0;
- read_le_int32 r_t(t);
- in >> r_t;
- Employee *obj = init(t);
- in >> *obj;
- arr.add(obj);
- }
- return in;
- }
- std::ostream &operator<<(std::ostream &out, const EmployeesArray &arr) {
- for (size_t i = 0; i < arr._employees.size(); i++) {
- out << i + 1 << ". " << *(arr._employees[i]);
- }
- out << "== Total salary: " << arr.total_salary() << "\n\n";
- return out;
- }
- std::ofstream &operator<<(std::ofstream &out, const EmployeesArray &arr) {
- write_le_int32 w_N(arr._employees.size());
- out << w_N;
- for (size_t i = 0; i < arr._employees.size(); i++) {
- out << arr._employees[i];
- }
- return out;
- }
- Employee *init(int32_t t) {
- Employee *obj;
- switch(t) {
- case 1:
- obj = new Developer();
- break;
- case 2:
- obj = new SalesManager();
- break;
- default:
- exit(-1);
- }
- return obj;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement