diff --git a/CMakeLists.txt b/CMakeLists.txt index afc421a..44b98f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,10 @@ add_executable(Sim_C__ src/services/Cost/BillCharge.h src/services/CostHistory.cpp src/services/CostHistory.h + src/services/Cost/ServiceCharge.cpp + src/services/Cost/ServiceCharge.h + src/helper/StringOperations.cpp + src/helper/StringOperations.h ) find_package(doctest CONFIG REQUIRED) find_package(spdlog CONFIG REQUIRED) diff --git a/src/helper/StringOperations.cpp b/src/helper/StringOperations.cpp new file mode 100644 index 0000000..86c3159 --- /dev/null +++ b/src/helper/StringOperations.cpp @@ -0,0 +1,16 @@ +// +// Created by StanislausCichocki on 13.03.2025. +// + +#include "StringOperations.h" + +void StringOperations::splitString(const std::string &input, const std::string &delimiter, std::string &before, +std::string &after) { + if (const size_t pos = input.find(delimiter); pos != std::string::npos) { + before = input.substr(0, pos); + after = input.substr(pos + delimiter.length()); + } else { + before = input; + after = ""; + } +} diff --git a/src/helper/StringOperations.h b/src/helper/StringOperations.h new file mode 100644 index 0000000..72e6551 --- /dev/null +++ b/src/helper/StringOperations.h @@ -0,0 +1,17 @@ +// +// Created by StanislausCichocki on 13.03.2025. +// + +#ifndef STRINGOPERATIONS_H +#define STRINGOPERATIONS_H +#include + + +class StringOperations { +public: + static void splitString(const std::string &input, const std::string &delimiter, std::string &before, std::string &after); +}; + + + +#endif //STRINGOPERATIONS_H diff --git a/src/model/Energy_Tariff.h b/src/model/Energy_Tariff.h index d1ac8ad..2f870a4 100644 --- a/src/model/Energy_Tariff.h +++ b/src/model/Energy_Tariff.h @@ -10,7 +10,8 @@ class Energy_Tariff { public: float consumption_tariff{0.0f}; float generation_tariff{0.0f}; - float bill_charge; + float bill_charge{0.0f}; + float service_charge{0.0f}; }; diff --git a/src/services/Cost/BillCharge.cpp b/src/services/Cost/BillCharge.cpp index abb073e..507efac 100644 --- a/src/services/Cost/BillCharge.cpp +++ b/src/services/Cost/BillCharge.cpp @@ -7,5 +7,5 @@ void BillCharge::apply(std::unique_ptr& building, std::unique_ptr& community) { const std::shared_ptr& valuesWith = building->cost()->get_cost_values_with(); - valuesWith->add_cost_point("Bill charge",1.0,community->energy_tariff().bill_charge); + valuesWith->add_cost_point("Bill charge",1.0f,community->energy_tariff().bill_charge); } diff --git a/src/services/Cost/ServiceCharge.cpp b/src/services/Cost/ServiceCharge.cpp new file mode 100644 index 0000000..f18a07d --- /dev/null +++ b/src/services/Cost/ServiceCharge.cpp @@ -0,0 +1,12 @@ +// +// Created by StanislausCichocki on 13.03.2025. +// + +#include "ServiceCharge.h" + +void ServiceCharge::apply(std::unique_ptr &building, std::unique_ptr &community) { + const std::shared_ptr& valuesWith = building->cost()->get_cost_values_with(); + const std::shared_ptr& values = building->values(); + + valuesWith->add_cost_point("Service Charge",values->consumption_from_community_sum(),community->energy_tariff().service_charge); +} diff --git a/src/services/Cost/ServiceCharge.h b/src/services/Cost/ServiceCharge.h new file mode 100644 index 0000000..5b058ce --- /dev/null +++ b/src/services/Cost/ServiceCharge.h @@ -0,0 +1,17 @@ +// +// Created by StanislausCichocki on 13.03.2025. +// + +#ifndef SERVICECHARGE_H +#define SERVICECHARGE_H +#include "../../interfaces/ICostComponent.h" + + +class ServiceCharge : public ICostComponent{ +public: + void apply(std::unique_ptr &building, std::unique_ptr &community) override; +}; + + + +#endif //SERVICECHARGE_H diff --git a/src/services/CostHistory.h b/src/services/CostHistory.h index b637e5a..f5fffd7 100644 --- a/src/services/CostHistory.h +++ b/src/services/CostHistory.h @@ -7,35 +7,35 @@ #include #include #include +#include +#include "../helper/StringOperations.h" class CostHistory { -public: +private: struct CostPoint { std::string name; - double value; - double amount; + float value; + float amount; - CostPoint(std::string name, double value, double amount) + CostPoint(std::string name, const float value, const float amount) : name(std::move(name)), value(value), amount(amount) {} - double total() const { + float total() const { return value * amount; } }; - -private: - std::vector costPoints; - + std::vector costPoints{}; + std::unordered_map TotalByCategory{}; public: - void add_cost_point(const std::string &name, double value, double amount) { + void add_cost_point(const std::string &name, float value, float amount) { if (amount==0.0) return; costPoints.emplace_back(name, value, amount); } - double total_cost() const { - return std::accumulate(costPoints.begin(), costPoints.end(), 0.0, - [](double sum, const CostPoint &point) { + float total_cost() const { + return std::accumulate(costPoints.begin(), costPoints.end(), 0.0f, + [](const float sum, const CostPoint &point) { return sum + point.total(); }); } @@ -43,6 +43,16 @@ public: const std::vector &get_cost_points() const { return costPoints; } + std::unordered_map calculateCategories() { + TotalByCategory.clear(); + if (costPoints.empty()) return {}; + for (const CostPoint& point : costPoints) { + const size_t pos = point.name.find(' '); + std::string result = (pos != std::string::npos) ? point.name.substr(0, pos) : point.name; + TotalByCategory[result] += point.total(); + } + return TotalByCategory; + } };