-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy path12_homework_03_answer.cpp
161 lines (135 loc) · 2.76 KB
/
12_homework_03_answer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <bits/stdc++.h>
using namespace std;
class Payable {
public:
virtual double GetAmountToPay() const {}
virtual ~Payable() {
}
};
class StaffMember: public Payable {
private:
string name;
string address;
public:
};
class Employee: public StaffMember {
private:
int day_to_pay;
public:
};
class HourlyEmployee: public Employee {
private:
int total_working_hours;
double salary_per_hour;
public:
virtual double GetAmountToPay() const override {
cout << "HourlyEmployee\n";
return total_working_hours * salary_per_hour;
}
};
class SalariedEmployee: public Employee {
private:
double monthly_salary;
public:
virtual double GetAmountToPay() const override {
cout << "SalariedEmployee\n";
return monthly_salary;
}
};
class CommisionSalariedEmployee: public SalariedEmployee {
private:
double commision_rate;
double current_month_sales;
public:
virtual double GetAmountToPay() const override {
cout << "CommisionSalariedEmployee\n";
return SalariedEmployee::GetAmountToPay() + current_month_sales * commision_rate;
}
};
class Volunteer: public StaffMember {
private:
int current_payment;
public:
virtual double GetAmountToPay() const override {
return current_payment;
}
};
class Item {
private:
string desc;
double price;
int quantity;
public:
double GetPrice() const {
return price * quantity;
}
};
class Book: public Item {
private:
string author;
};
class Food: public Item {
private:
string expiration_date;
};
class Invoice: public Payable {
private:
int invoice_id = -1;
vector<Item*> items;
public:
void AddItem(Item* item) {
items.push_back(item);
}
virtual double GetAmountToPay() const override {
cout << "Invoice\n";
double sum = 0;
for (const Item* item_ptr : items)
sum += item_ptr->GetPrice();
return sum;
}
};
class Payroll {
private:
vector<Payable*> payables;
public:
void AddPayable(Payable* payable) {
payables.push_back(payable); // we should add copy
}
void Pay() {
cout << "Payroll::Pay\n";
;
for (const auto payable : payables)
payable->GetAmountToPay();
}
};
class Company {
private:
Payroll payroll;
public:
Company() {
}
void Run() {
payroll.AddPayable(new Volunteer());
payroll.AddPayable(new HourlyEmployee());
payroll.AddPayable(new SalariedEmployee());
payroll.AddPayable(new CommisionSalariedEmployee());
Invoice* invoice = new Invoice();
invoice->AddItem(new Book());
invoice->AddItem(new Food());
payroll.AddPayable(invoice);
payroll.Pay();
}
};
int main() {
Company company;
company.Run();
return 0;
}
/*
Payroll::Pay
HourlyEmployee
SalariedEmployee
CommisionSalariedEmployee
SalariedEmployee
Invoice
*/