Billinghistory Categories

This commit is contained in:
StanislausCichocki
2025-03-13 15:38:10 +01:00
parent 04c16b63ce
commit 86b255a140
8 changed files with 92 additions and 15 deletions

View File

@ -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)

View File

@ -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 = "";
}
}

View File

@ -0,0 +1,17 @@
//
// Created by StanislausCichocki on 13.03.2025.
//
#ifndef STRINGOPERATIONS_H
#define STRINGOPERATIONS_H
#include <string>
class StringOperations {
public:
static void splitString(const std::string &input, const std::string &delimiter, std::string &before, std::string &after);
};
#endif //STRINGOPERATIONS_H

View File

@ -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};
};

View File

@ -7,5 +7,5 @@
void BillCharge::apply(std::unique_ptr<Building::Base>& building, std::unique_ptr<Community>& community)
{
const std::shared_ptr<CostHistory>& 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);
}

View File

@ -0,0 +1,12 @@
//
// Created by StanislausCichocki on 13.03.2025.
//
#include "ServiceCharge.h"
void ServiceCharge::apply(std::unique_ptr<Building::Base> &building, std::unique_ptr<Community> &community) {
const std::shared_ptr<CostHistory>& valuesWith = building->cost()->get_cost_values_with();
const std::shared_ptr<Building::Simulation_Values>& values = building->values();
valuesWith->add_cost_point("Service Charge",values->consumption_from_community_sum(),community->energy_tariff().service_charge);
}

View File

@ -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::Base> &building, std::unique_ptr<Community> &community) override;
};
#endif //SERVICECHARGE_H

View File

@ -7,35 +7,35 @@
#include <numeric>
#include <string>
#include <vector>
#include <unordered_map>
#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<CostPoint> costPoints;
std::vector<CostPoint> costPoints{};
std::unordered_map<std::string, float> 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<CostPoint> &get_cost_points() const {
return costPoints;
}
std::unordered_map<std::string, float> 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;
}
};