|
| 1 | +#ifndef DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANSERVICE_H |
| 2 | +#define DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANSERVICE_H |
| 3 | + |
| 4 | +#include <domain/ElectricityReading.h> |
| 5 | +#include <domain/PricePlan.h> |
| 6 | +#include <service/MeterReadingService.h> |
| 7 | +#include <vector> |
| 8 | +#include <ctime> |
| 9 | +#include <optional> |
| 10 | +#include <map> |
| 11 | +#include <string> |
| 12 | +#include <configuration.h> |
| 13 | + |
| 14 | +class PricePlanService { |
| 15 | +public: |
| 16 | + using time_point_type = std::chrono::time_point<std::chrono::system_clock>; |
| 17 | + |
| 18 | + std::optional<std::map<std::string, float>> getConsumptionCostOfElectricityReadingsForEachPricePlan(std::string smartMeterId){ |
| 19 | + std::optional<std::vector<ElectricityReading>> electricityReadings = meterReadingService.getReadings(smartMeterId); |
| 20 | + if (!electricityReadings.has_value()) { |
| 21 | + return {}; |
| 22 | + } |
| 23 | + |
| 24 | + std::map<std::string, float> consumptionCostOfElectricityReadingsForEachPricePlan; |
| 25 | + for(auto pricePlan:pricePlans){ |
| 26 | + consumptionCostOfElectricityReadingsForEachPricePlan.insert(std::make_pair(pricePlan.getPlanName(), calculateCost(electricityReadings.value(), pricePlan))); |
| 27 | + } |
| 28 | + return consumptionCostOfElectricityReadingsForEachPricePlan; |
| 29 | + } |
| 30 | + |
| 31 | + PricePlanService(std::vector<PricePlan> pricePlans, MeterReadingService meterReadingService) : |
| 32 | + pricePlans(pricePlans), meterReadingService(meterReadingService) {} |
| 33 | + |
| 34 | +private: |
| 35 | + const std::vector<PricePlan> pricePlans; |
| 36 | + MeterReadingService meterReadingService; |
| 37 | + |
| 38 | + int calculateCost(std::vector<ElectricityReading> electricityReadings, PricePlan pricePlan) { |
| 39 | + float average = calculateAverageReading(electricityReadings); |
| 40 | + float timeElapsed = calculateTimeElapsed(electricityReadings); |
| 41 | + |
| 42 | + float averagedCost = round(average / timeElapsed); |
| 43 | + return averagedCost * pricePlan.getUnitRate(); |
| 44 | + } |
| 45 | + |
| 46 | + float calculateAverageReading(std::vector<ElectricityReading> electricityReadings) { |
| 47 | + float summedReadings = 0; |
| 48 | + for (ElectricityReading electricityReading : electricityReadings) { |
| 49 | + summedReadings += electricityReading.getReading(); |
| 50 | + } |
| 51 | + |
| 52 | + return round(summedReadings / electricityReadings.size()); |
| 53 | + } |
| 54 | + |
| 55 | + float calculateTimeElapsed(std::vector<ElectricityReading> electricityReadings) { |
| 56 | + ElectricityReading first = *electricityReadings.begin(); |
| 57 | + ElectricityReading last = *electricityReadings.begin(); |
| 58 | + std::vector<ElectricityReading>::iterator it; |
| 59 | + for(it = electricityReadings.begin(); it != electricityReadings.end(); it++){ |
| 60 | + if (it->getTime() < first.getTime()){ |
| 61 | + first = *it; |
| 62 | + } |
| 63 | + if (it->getTime() > first.getTime()){ |
| 64 | + last = *it; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + std::chrono::steady_clock::duration duration = std::chrono::duration_cast<std::chrono::hours>( |
| 69 | + last.getTime() - first.getTime()); |
| 70 | + return duration.count(); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +#endif // DEVELOPER_JOYOFENERGY_CPP_BEAST_PRICEPLANSERVICE_H |
0 commit comments