-
Notifications
You must be signed in to change notification settings - Fork 0
/
IncomeManager.cpp
155 lines (121 loc) · 4.26 KB
/
IncomeManager.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
#include "IncomeManager.h"
Income IncomeManager::getNewIncomeData()
{
Income newIncome;
char selection;
newIncome.setIncomeId(getNewIncomeId());
newIncome.setUserId(LOGGED_USER_ID);
system("cls");
cout << "Czy przychod dotyczy dnia dzisiejszego? t - Tak, n - nie" << endl;
while(true)
{
selection = getch();
if(selection == 't')
{
newIncome.setDate(AuxiliaryMethods::getCurrentUnixTime());
break;
}
else if(selection == 'n')
{
cout << "Podaj date przychodu w formacie rrrr-mm-dd: ";
newIncome.setDate(AuxiliaryMethods::getCorrectDate());
break;
}
else
cout << "Blad! Wybierz odpowiedni znak." << endl;
}
cout << "Podaj czego dotyczy przychod: ";
newIncome.setItem(AuxiliaryMethods::getLine());
cout << "Okresl wysokosc przychodu: ";
newIncome.setAmount(AuxiliaryMethods::getFloat());
return newIncome;
}
int IncomeManager::getNewIncomeId()
{
if(incomes.empty())
return 1;
else
return incomeFile.getLastIncomeId() + 1;
}
void IncomeManager::displayIncomeHeader()
{
cout << "-----------------------Lista przychodow-----------------------" << endl;
cout << left << setw(4) << "id"
<< left << setw(35)<< "Przedmiot"
<< left << setw(15)<< "Data"
<< left << setw(30)<< "Przychod" << endl;
}
void IncomeManager::displayIncomesVector(vector<Income> &incomes)
{
for(int i = 0; i < incomes.size(); i++)
cout << left << setw(4) << incomes[i].getIncomeId()
<< left << setw(35)<< incomes[i].getItem()
<< left << setw(15)<< AuxiliaryMethods::unixTimeToStringDate(incomes[i].getDate())
<< left << setw(30)<< setprecision(2) << incomes[i].getAmount() << endl;
}
float IncomeManager::getTotalIncomesValueInVector(vector<Income> &incomes)
{
float totalIncome = 0;
for(int i = 0; i < incomes.size(); i++)
totalIncome += incomes[i].getAmount();
return totalIncome;
}
void IncomeManager::addIncome()
{
Income newIncome = getNewIncomeData();
incomes.push_back(newIncome);
incomeFile.appendIncomeToFile(newIncome);
AuxiliaryMethods::timedMessage("Przychod dodany pomyslnie!");
}
float IncomeManager::displayCurrentMonthIncomes()
{
time_t currentTime = AuxiliaryMethods::getCurrentUnixTime();
time_t currentMonthfirstDayUnixTime = AuxiliaryMethods::getLastMonthLastDayUnixTime();
vector<Income> currentMonthIncomes;
for(int i = 0; i < incomes.size(); i++)
{
if(incomes[i].getDate() >= currentMonthfirstDayUnixTime &&
incomes[i].getDate() <= currentTime)
{
currentMonthIncomes.push_back(incomes[i]);
}
}
sort(currentMonthIncomes.begin(), currentMonthIncomes.end(), greater <>());
displayIncomeHeader();
displayIncomesVector(currentMonthIncomes);
return getTotalIncomesValueInVector(currentMonthIncomes);
}
float IncomeManager::displayLastMonthIncomes()
{
time_t lastMonthFirstDayUnixTime = AuxiliaryMethods::getLastMonthFirstDayUnixTime();
time_t lastMonthLastDayUnixTime = AuxiliaryMethods::getLastMonthLastDayUnixTime();
vector<Income> lastMonthIncomes;
for(int i = 0; i < incomes.size(); i++)
{
if(incomes[i].getDate() >= lastMonthFirstDayUnixTime &&
incomes[i].getDate() <= lastMonthLastDayUnixTime)
{
lastMonthIncomes.push_back(incomes[i]);
}
}
sort(lastMonthIncomes.begin(), lastMonthIncomes.end(), greater <>());
displayIncomeHeader();
displayIncomesVector(lastMonthIncomes);
return getTotalIncomesValueInVector(lastMonthIncomes);
}
float IncomeManager::displayChosenPeriodIncomes(time_t beginPeriodUnixTime, time_t endPeriodUnixTime)
{
vector<Income> lastMonthIncomes;
for(int i = 0; i < incomes.size(); i++)
{
if(incomes[i].getDate() >= beginPeriodUnixTime &&
incomes[i].getDate() <= endPeriodUnixTime)
{
lastMonthIncomes.push_back(incomes[i]);
}
}
sort(lastMonthIncomes.begin(), lastMonthIncomes.end(), greater <>());
displayIncomeHeader();
displayIncomesVector(lastMonthIncomes);
return getTotalIncomesValueInVector(lastMonthIncomes);
}