-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncomeGroupClass.cpp
More file actions
167 lines (130 loc) · 5.35 KB
/
Copy pathIncomeGroupClass.cpp
File metadata and controls
167 lines (130 loc) · 5.35 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
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
// Tax distribution algorithm — a tax distributing program
// Copyright (C) 2026 Martin Herchel
// This file is part of Tax distribution algorithm.
// Tax distribution algorithm is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// Tax distribution algorithm is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with Tax distribution algorithm. If not, see <https://www.gnu.org/licenses/>.
#ifndef IncomeGroupClass_cpp
#define IncomeGroupClass_cpp
#include "IncomeGroupClass.h"
#include <stdexcept>
// Private
// Private functions
void IncomeGroup::SetTaxOfIndividual(const long double taxOfIndividual)
{
if (taxOfIndividual < 0)
throw std::invalid_argument{"Tax of an individual cannot be smaller than 0."};
this->taxOfIndividual = taxOfIndividual;
}
void IncomeGroup::SetTaxOfGroup(const long double taxOfGroup)
{
if (taxOfGroup < 0)
throw std::invalid_argument{"Tax of a group cannot be smaller than 0."};
this->taxOfGroup = taxOfGroup;
}
constexpr long double IncomeGroup::CheckIncomeOfIndividual(const long double incomeOfIndividual)
{
if (incomeOfIndividual < 0)
throw std::invalid_argument{"Income of an individual cannot be smaller than 0."};
return incomeOfIndividual;
}
constexpr long double IncomeGroup::CheckIncomeOfGroup(const long double incomeOfGroup)
{
if (incomeOfGroup < 0)
throw std::invalid_argument{"Income of a group cannot be smaller than 0."};
return incomeOfGroup;
}
constexpr long long int IncomeGroup::CheckIndexOfStartOfGroup(const long long int indexOfStartOfGroup)
{
if (indexOfStartOfGroup < 0)
throw std::invalid_argument{"IndexOfStartOfGroup cannot be smaller than 0."};
return indexOfStartOfGroup;
}
constexpr long long int IncomeGroup::CheckGroupSize(const long long int groupSize)
{
if (groupSize <= 0)
throw std::invalid_argument{"GroupSize cannot be 0 or smaller."};
return groupSize;
}
constexpr long double IncomeGroup::CalculateIncomeOfIndividual(const long double incomeOfGroup, const long long int groupSize)
{
return CheckIncomeOfIndividual(incomeOfGroup / groupSize);
}
constexpr long double IncomeGroup::CalculateIncomeOfGroup(const long double incomeOfIndividual, const long long int groupSize)
{
return CheckIncomeOfGroup(incomeOfIndividual * groupSize);
}
constexpr LimitedPercentage IncomeGroup::CalculateIncomePercentile(const long long int indexOfStartOfGroup, const long long int sizeOfSortedIncome)
{
if (indexOfStartOfGroup >= sizeOfSortedIncome || indexOfStartOfGroup < 0)
throw std::out_of_range{"Index is out of the range of sortedIncome."};
return (sizeOfSortedIncome == 1) ? LimitedPercentage(1.0L * 100) : LimitedPercentage(1.0L * indexOfStartOfGroup / (sizeOfSortedIncome - 1) * 100);
}
constexpr LimitedPercentage IncomeGroup::CalculateIncomePercentile(const long long int indexOfStartOfGroup, const std::vector<long double> &sortedIncome)
{
return CalculateIncomePercentile(indexOfStartOfGroup, sortedIncome.size());
}
// Public
// Constructors
IncomeGroup::IncomeGroup(const long double incomeOfIndividual, const long long int indexOfStartOfGroup, const long long int groupSize, const long long int sizeOfSortedIncomes)
: incomeOfIndividual{CheckIncomeOfIndividual(incomeOfIndividual)},
indexOfStartOfGroup{CheckIndexOfStartOfGroup(indexOfStartOfGroup)},
groupSize{CheckGroupSize(groupSize)},
incomePercentile{CalculateIncomePercentile(indexOfStartOfGroup, sizeOfSortedIncomes)},
incomeOfGroup{CalculateIncomeOfGroup(incomeOfIndividual, groupSize)}
{
this->taxOfIndividual = 0;
CalculateAndSetTaxOfGroup();
}
// Destructor
IncomeGroup::~IncomeGroup()
{
}
// Public functions
constexpr long double IncomeGroup::IncomeOfIndividual() const
{
return incomeOfIndividual;
}
constexpr long double IncomeGroup::IncomeOfGroup() const
{
return incomeOfGroup;
}
constexpr long long int IncomeGroup::IndexOfStartOfGroup() const
{
return indexOfStartOfGroup;
}
constexpr long long int IncomeGroup::GroupSize() const
{
return groupSize;
}
constexpr LimitedPercentage IncomeGroup::IncomePercentile() const
{
return incomePercentile;
}
long double IncomeGroup::TaxOfIndividual() const
{
return taxOfIndividual;
}
void IncomeGroup::SetTaxOfIndividualAndUpdateGroup(const long double taxOfIndividual)
{
SetTaxOfIndividual(taxOfIndividual);
CalculateAndSetTaxOfGroup();
}
long double IncomeGroup::TaxOfGroup() const
{
return taxOfGroup;
}
void IncomeGroup::SetTaxOfGroupAndUpdateIndividual(const long double taxOfGroup)
{
SetTaxOfGroup(taxOfGroup);
CalculateAndSetTaxOfIndividual();
}
void IncomeGroup::CalculateAndSetTaxOfIndividual()
{
SetTaxOfIndividual(TaxOfGroup() / GroupSize());
}
void IncomeGroup::CalculateAndSetTaxOfGroup()
{
SetTaxOfGroup(TaxOfIndividual() * GroupSize());
}
#endif