kima2/src/core/seller.cpp

111 lines
3.2 KiB
C++

#include "seller.h"
#include "utils.h"
#include <iomanip>
#include <numeric>
#include <sstream>
Seller::Seller(const std::string &firstName, const std::string &lastName, int sellerNo,
int numArticlesOffered)
: EntityInt(sellerNo)
{
m_firstName = firstName;
m_lastName = lastName;
m_numArticlesOffered = numArticlesOffered;
}
void Seller::setSellerNo(int seller_no) { setId(seller_no); }
void Seller::setFirstName(const std::string &firstName) { m_firstName = firstName; }
void Seller::setLastName(const std::string &lastName) { m_lastName = lastName; }
void Seller::setNumArticlesOffered(int number) { m_numArticlesOffered = number; }
void Seller::addArticle(std::unique_ptr<Article> article)
{
article->setSeller(this);
m_articles.push_back(std::move(article));
}
std::string Seller::getFirstName() const { return m_firstName; }
std::string Seller::getLastName() const { return m_lastName; }
int Seller::getSellerNo() const { return getId(); }
std::string Seller::getSellerNoAsString() const
{
std::stringstream selNoStr;
selNoStr << std::setfill('0') << std::setw(3) << m_id;
return selNoStr.str();
;
}
std::vector<Article *> Seller::getArticles(bool onlySold) const
{
std::vector<Article *> articles;
for (const auto &article : m_articles) {
if (onlySold && article->isSold()) {
articles.push_back(article.get());
} else if (!onlySold) {
articles.push_back(article.get());
}
}
return articles;
}
Article *Seller::getArticleByUuid(const std::string &uuidString)
{
auto iter = std::find_if(m_articles.begin(), m_articles.end(), [&uuidString](const auto &art) {
return art->getUuidAsString() == uuidString;
});
if (iter == m_articles.end())
return nullptr;
return (*iter).get();
}
int Seller::numArticlesSold() const { return static_cast<int>(getArticles(true).size()); }
int Seller::numArticlesOffered() const { return m_numArticlesOffered; }
int Seller::getMaxArticleNo() const
{
auto iter = std::max_element(
m_articles.begin(), m_articles.end(),
[](const auto &a, const auto &b) -> bool { return a->getArticleNo() < b->getArticleNo(); });
if (iter == m_articles.end())
return 0;
return (*iter)->getArticleNo();
}
void Seller::cleanupArticles()
{
m_articles.erase(std::remove_if(m_articles.begin(), m_articles.end(),
[](const auto &article) {
return article->getState() == Article::State::DELETE;
}),
m_articles.end());
for (auto &article : m_articles) {
article->setState(Article::State::OK);
}
}
int Seller::sumInCents()
{
int sum = std::accumulate(m_articles.begin(), m_articles.end(), 0,
[](int a, const auto &b) { return a + b->getPrice(); });
return sum;
}
std::string Seller::sumAsString() { return formatCentAsEuroString(sumInCents()); }
bool operator<(const Seller &li, const Seller &re) { return li.m_id < re.m_id; }
bool operator<(const std::unique_ptr<Seller> &li, const std::unique_ptr<Seller> &re)
{
return li->m_id < re->m_id;
}