-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path13_homework_01_answer.cpp
229 lines (194 loc) · 4.47 KB
/
13_homework_01_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <bits/stdc++.h>
using namespace std;
class Payable {
public:
virtual double GetAmountToPay() const = 0;
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;
class VaidationRule {
public:
virtual bool ValidateRule(const Invoice* const invoice) = 0;
virtual ~VaidationRule() {}
};
class InvoiceValidator {
protected:
vector<VaidationRule*> rules;
public:
virtual bool ValidateInvoice(const Invoice* const invoice) {
cout << "Validator\n";
for (const auto rule_ptr : rules)
rule_ptr->ValidateRule(invoice); // ask child: inverse of control
}
virtual ~InvoiceValidator() {
}
};
class Invoice: public Payable {
private:
int invoice_id = -1;
vector<Item*> items;
InvoiceValidator* validator; // doesn't know which validator/rule
public:
Invoice(InvoiceValidator* validator) :
validator(validator) {
}
void AddItem(Item* item) {
items.push_back(item);
}
virtual double GetAmountToPay() const override {
cout << "Invoice\n";
validator->ValidateInvoice(this);
double sum = 0;
for (const Item* item_ptr : items)
sum += item_ptr->GetPrice();
return sum;
}
};
class TaxesVaidationRule: public VaidationRule {
public:
virtual bool ValidateRule(const Invoice* const invoice) {
cout << "Tax Validation rule\n";
return true;
}
};
class SuppliersDealsVaidationRule: public VaidationRule {
public:
virtual bool ValidateRule(const Invoice* const invoice) {
cout << "Suppliers deals validation rule\n";
return true;
}
};
class MandatoryInvoiceValidator: public InvoiceValidator {
public:
MandatoryInvoiceValidator() { // could be singleton
rules.push_back(new TaxesVaidationRule());
}
};
class CompleteInvoiceValidator: public InvoiceValidator {
public:
CompleteInvoiceValidator() { // could be singleton
rules.push_back(new TaxesVaidationRule());
rules.push_back(new SuppliersDealsVaidationRule());
}
};
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:
bool is_use_complete_validations = true;
InvoiceValidator* validator = nullptr;
Payroll payroll;
public:
Company() {
if (is_use_complete_validations)
validator = new CompleteInvoiceValidator();
else
validator = new MandatoryInvoiceValidator();
}
void Run() {
payroll.AddPayable(new Volunteer());
payroll.AddPayable(new HourlyEmployee());
payroll.AddPayable(new SalariedEmployee());
payroll.AddPayable(new CommisionSalariedEmployee());
Invoice* invoice = new Invoice(validator);
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
Validator
Tax Validation rule
Suppliers deals validation rule
*/