metalni

OOP Labs 7 Employee

Jun 2nd, 2020
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. //abstract Employee
  7. class Employee{
  8.     protected:
  9.         char name[50];
  10.         int age;
  11.         int exp;
  12.     public:
  13.         Employee(){
  14.             strcpy(this->name, "None");
  15.             this->age = 0;
  16.             this->exp = 0;
  17.         }
  18.         Employee(const char * name, const int age, const int exp){
  19.             strcpy(this->name, name);
  20.             this->age = age;
  21.             this->exp = exp;
  22.         }
  23.         virtual ~Employee(){}
  24.         virtual const double bonus() = 0;
  25.         virtual const double plata() = 0;
  26.         const int getAge(){
  27.             return this->age;
  28.         }
  29. };
  30.  
  31. //Derived classes
  32. class SalaryEmployee : public Employee{
  33.     private:
  34.         double baseSalary;
  35.     public:
  36.         SalaryEmployee(){
  37.             this->baseSalary = 0.0;
  38.         }
  39.         SalaryEmployee(const char * name, const int age, const int exp, const double baseSalary) : Employee(name, age, exp){
  40.             this->baseSalary = baseSalary;
  41.         }
  42.         ~SalaryEmployee(){}
  43.         const double bonus(){
  44.             return (this->baseSalary * this->exp) / 100;
  45.         }
  46.         const double plata(){
  47.             return this->baseSalary + this->bonus();
  48.         }
  49. };
  50.  
  51. class HourlyEmployee : public Employee{
  52.     private:
  53.         int totalHours;
  54.         double salaryPerHour;
  55.     public:
  56.         HourlyEmployee(){
  57.             this->totalHours = 0;
  58.             this->salaryPerHour = 0.0;
  59.         }
  60.         HourlyEmployee(const char * name, const int age, const int exp, const int totalHours, const double salaryPerHour) : Employee(name, age, exp){
  61.             this->totalHours = totalHours;
  62.             this->salaryPerHour = salaryPerHour;
  63.         }
  64.         ~HourlyEmployee(){}
  65.         const double bonus(){
  66.             if(this->totalHours > 320){
  67.                 return (this->totalHours - 320) * (this->salaryPerHour * 0.5);
  68.             } else
  69.                 return 0.0;
  70.         }
  71.         const double plata(){
  72.             return (this->totalHours * this->salaryPerHour) + this->bonus();
  73.         }
  74. };
  75.  
  76. class Freelancer  : public Employee{
  77.     private:
  78.         int doneProjects;
  79.         double * money;
  80.     public:
  81.         Freelancer (){
  82.             this->doneProjects = 0;
  83.             this->money = new double[0];
  84.         }
  85.         Freelancer (const char * name, const int age, const int exp, const int doneProjects, const double * money) : Employee(name, age, exp){
  86.             this->doneProjects = doneProjects;
  87.             this->money = new double[this->doneProjects];
  88.             for(int i=0; i<this->doneProjects; i++)
  89.                 this->money[i] = money[i];
  90.         }
  91.         ~Freelancer (){
  92.             delete [] this->money;
  93.         }
  94.         const double bonus(){
  95.             if(this->doneProjects > 5){
  96.                 return (this->doneProjects - 5) * 1000;
  97.             } else
  98.                 return 0.0;
  99.         }
  100.         const double plata(){
  101.             double totalSalary = 0.0;
  102.             for(int i=0; i<this->doneProjects; i++)
  103.                 totalSalary += this->money[i];
  104.             return totalSalary + this->bonus();
  105.         }
  106. };
  107.  
  108. bool operator==(Employee *left, Employee &right){
  109.     if((left->getAge() == right.getAge())&&left->bonus() == right.bonus())
  110.         return true;
  111.     else
  112.         return false;
  113. }
  114.  
  115. //class Company
  116. class Company{
  117.     private:
  118.         char name[50];
  119.         int noEmployees;
  120.         Employee ** emps;
  121.     public:
  122.         Company(){
  123.             strcpy(this->name, "None");
  124.             this->noEmployees = 0;
  125.             this->emps = new Employee *[0];
  126.         }
  127.         Company(const char * name){
  128.             strcpy(this->name, name);
  129.             this->noEmployees = 0;
  130.             this->emps = new Employee *[0];
  131.         }
  132.         ~Company(){
  133.             //delete [] this->emps;
  134.         }
  135.         Company &operator += (Employee * orig){
  136.             Employee ** tmp = new Employee *[this->noEmployees+1];
  137.             for(int i=0; i<this->noEmployees; i++)
  138.                 tmp[i] = this->emps[i];
  139.            
  140.             tmp[this->noEmployees++] = orig;
  141.             delete [] this->emps;
  142.             this->emps = tmp;
  143.  
  144.             return *this;
  145.         }
  146.         double vkupnaPlata(){
  147.             double totalSalary = 0.0;
  148.             for(int i=0; i<this->noEmployees; i++)
  149.                 totalSalary += this->emps[i]->plata();
  150.             return totalSalary;
  151.         }
  152.         double filtriranaPlata(Employee *emp){
  153.             double totalFiltered = 0.0;
  154.             for(int i=0; i<this->noEmployees; i++){
  155.                 if(this->emps[i] == *emp)
  156.                     totalFiltered += this->emps[i]->plata();
  157.             }
  158.             return totalFiltered;
  159.         }
  160.         const void pecatiRabotnici(){
  161.             int salaryEmps = 0; int hourlyEmps = 0; int freelancerEmps = 0;
  162.             for(int i=0; i<this->noEmployees; i++){
  163.                 SalaryEmployee *a = dynamic_cast<SalaryEmployee *>(this->emps[i]);
  164.                 HourlyEmployee *b = dynamic_cast<HourlyEmployee *>(this->emps[i]);
  165.                 Freelancer *c = dynamic_cast<Freelancer *>(this->emps[i]);
  166.                 if(a)
  167.                     salaryEmps++;
  168.                 else if(b)
  169.                     hourlyEmps++;
  170.                 else if(c)
  171.                     freelancerEmps++;
  172.             }
  173.             cout << "Vo kompanijata " << this->name << " rabotat:" << endl;
  174.             cout << "Salary employees: " << salaryEmps << endl;
  175.             cout << "Hourly employees: " << hourlyEmps << endl;
  176.             cout << "Freelancers: " << freelancerEmps << endl;
  177.         }
  178. };
  179.  
  180. //main
  181. int main() {
  182.  
  183. char name[50];
  184. cin >> name;
  185. Company c(name);
  186.  
  187. int n;
  188. cin >> n;
  189.  
  190. char employeeName[50];
  191. int age;
  192. int experience;
  193. int type;
  194.  
  195. for (int i=0; i <n; ++i) {
  196.   cin >> type;
  197.   cin >> employeeName >> age >> experience;
  198.  
  199.   if (type == 1) {
  200.     int basicSalary;
  201.     cin >> basicSalary;
  202.     c += new SalaryEmployee(employeeName, age, experience, basicSalary);
  203.   }
  204.  
  205.   else if (type == 2) {
  206.     int hoursWorked;
  207.     int hourlyPay;
  208.     cin >> hoursWorked >> hourlyPay;
  209.     c += new HourlyEmployee(employeeName, age, experience, hoursWorked, hourlyPay);
  210.   }
  211.  
  212.   else {
  213.     int numProjects;
  214.     cin >> numProjects;
  215.     double projects[10];
  216.     for (int i=0; i < numProjects; ++i) {
  217.       cin >> projects[i];
  218.     }
  219.     c += new Freelancer(employeeName, age, experience, numProjects, projects);
  220.   }
  221. }
  222.  
  223. c.pecatiRabotnici();
  224. cout << "Vkupnata plata e: " << c.vkupnaPlata() << endl;
  225. Employee * emp = new HourlyEmployee("Petre_Petrov",31,6,340,80);
  226. cout << "Filtriranata plata e: " << c.filtriranaPlata(emp);
  227.  
  228. delete emp;
  229. }
Add Comment
Please, Sign In to add comment