-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApprovalSystem.cpp
More file actions
127 lines (107 loc) · 3.29 KB
/
Copy pathApprovalSystem.cpp
File metadata and controls
127 lines (107 loc) · 3.29 KB
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
// ===========================================================================
// ApprovalSystem.cpp
// ===========================================================================
#include <iostream>
#include <string>
#include <memory>
#include <limits>
class Role
{
public:
virtual ~Role() {}
virtual double getApprovalLimit() const = 0;
};
class EmployeeRole : public Role
{
public:
virtual double getApprovalLimit() const override
{
return 1000;
}
};
class TeamManagerRole : public Role
{
public:
virtual double getApprovalLimit() const override
{
return 10000;
}
};
class DepartmentManagerRole : public Role
{
public:
virtual double getApprovalLimit() const override
{
return 100000;
}
};
class CEORole : public Role
{
public:
virtual double getApprovalLimit() const override
{
return std::numeric_limits<double>::max();
}
};
class Expense
{
private:
double m_amount;
std::string m_description;
public:
Expense(double const amount, std::string description)
: m_amount{ amount }, m_description{ description } {}
double getAmount() const noexcept { return m_amount; }
std::string getDescription() const noexcept { return m_description; }
};
class Employee
{
private:
std::string m_name;
std::unique_ptr<Role> m_ownRole;
std::weak_ptr<Employee> m_directManager;
public:
explicit Employee(const std::string& name, std::unique_ptr<Role> ownrole)
: m_name{ name }, m_ownRole{ std::move(ownrole) } {}
void setDirectManager(std::shared_ptr<Employee> manager)
{
m_directManager = manager;
}
void approve(const Expense& e)
{
if (e.getAmount() <= m_ownRole->getApprovalLimit()) {
std::cout
<< m_name << " approved expense '" << e.getDescription()
<< "', cost=" << e.getAmount() << std::endl;
}
else if (std::shared_ptr<Employee> manager; (manager = m_directManager.lock()) != nullptr) {
manager->approve(e);
}
}
};
void approval_system()
{
std::unique_ptr<Role> role1 = std::make_unique<EmployeeRole>();
std::shared_ptr<Employee> cliff =
std::make_shared<Employee>("Cliff Booth", std::move(role1));
std::unique_ptr<Role> role2 = std::make_unique<TeamManagerRole>();
std::shared_ptr<Employee> rick =
std::make_shared<Employee>("Rick Dalton", std::move(role2));
std::unique_ptr<Role> role3 = std::make_unique<DepartmentManagerRole>();
std::shared_ptr<Employee> randy =
std::make_shared<Employee>("Randy Miller", std::move(role3));
std::unique_ptr<Role> role4 = std::make_unique<CEORole>();
std::shared_ptr<Employee> marvin =
std::make_shared<Employee>("Marvin Swartz", std::move(role4));
cliff->setDirectManager(rick);
rick->setDirectManager(randy);
randy->setDirectManager(marvin);
// employee 'cliff' gets all bills
cliff->approve(Expense{ 500, "Magazins" });
cliff->approve(Expense{ 5000, "Hotel Accomodation" });
cliff->approve(Expense{ 50000, "Conference costs" });
cliff->approve(Expense{ 200000, "New Truck" });
}
// ===========================================================================
// End-of-File
// ===========================================================================