-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics.h
108 lines (89 loc) · 2.69 KB
/
statistics.h
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
#ifndef STATISTICS_H
#define STATISTICS_H
#include <common.h>
#include <options.h>
#include <request.h>
#include <string>
#include <sstream>
class TPetrolStatistics
{
public:
TPetrolStatistics() = default;
TPetrolStatistics(TPetrol petrol)
: Petrol_(std::move(petrol))
{ }
void OnPetrolSold(TTime time, double amount)
{
Events_.emplace_back(time, amount);
}
double PetrolSold(TTime startTime, TTime endTime) const
{
double sold = 0;
for (const auto& event : Events_) {
if (event.Time >= startTime && event.Time <= endTime) {
sold += event.Amount;
}
}
return sold;
}
private:
struct TPetrolSoldEvent
{
TPetrolSoldEvent(TTime time, double amount)
: Time(time)
, Amount(amount)
{ }
TTime Time;
double Amount;
};
std::vector<TPetrolSoldEvent> Events_;
TPetrol Petrol_;
};
class TStatistics
{
public:
TStatistics() = default;
TStatistics(TOptions options)
: Options_(std::move(options))
{
PetrolStatistics_.resize(Options_.Petrols.size());
for (size_t index = 0; index < PetrolStatistics_.size(); ++index) {
PetrolStatistics_[index] = TPetrolStatistics{Options_.Petrols[index]};
}
}
void OnPetrolSold(const TRequest& request)
{
++CarsProcessed_;
TotalProfit_ += Options_.Petrols[request.Type].GetPrice() * request.Amount;
PetrolStatistics_[request.Type].OnPetrolSold(request.ProcessTime, request.Amount);
}
void OnCarRejected()
{
++CarsRejected_;
}
int GetCarsProcessed() const
{
return CarsProcessed_;
}
std::string Print(TTime time) const
{
std::stringstream result;
result << "Time: " << PrintTime(time) << std::endl;
result << "Cars processed: " << CarsProcessed_ << std::endl;
result << "Cars rejected: " << CarsRejected_ << std::endl;
result << "Total profit: " << TotalProfit_ << std::endl;
for (size_t petrolIndex = 0; petrolIndex < PetrolStatistics_.size(); ++petrolIndex) {
result << "Petrol " << petrolIndex + 1 << ":" << std::endl;
result << "Total sold: " << PetrolStatistics_[petrolIndex].PetrolSold(0, Week) << std::endl;
result << "Sold during last day: " << PetrolStatistics_[petrolIndex].PetrolSold(std::max<int>(0, time - Day), time) << std::endl;
}
return result.str();
}
private:
TOptions Options_;
std::vector<TPetrolStatistics> PetrolStatistics_;
int CarsProcessed_ = 0;
int CarsRejected_ = 0;
double TotalProfit_ = 0;
};
#endif // STATISTICS_H