kima2/src/core/seller.cpp

111 lines
3.2 KiB
C++
Raw Normal View History

2018-07-09 16:06:27 +02:00
#include "seller.h"
#include "utils.h"
2018-07-09 16:06:27 +02:00
#include <iomanip>
2018-07-30 09:50:54 +02:00
#include <numeric>
#include <sstream>
2022-07-07 15:21:46 +02:00
Seller::Seller(const std::string &firstName, const std::string &lastName, int sellerNo,
2018-07-25 09:31:17 +02:00
int numArticlesOffered)
2019-10-06 10:47:15 +02:00
: EntityInt(sellerNo)
2018-07-09 16:06:27 +02:00
{
2022-07-07 15:21:46 +02:00
m_firstName = firstName;
m_lastName = lastName;
m_numArticlesOffered = numArticlesOffered;
2018-07-09 16:06:27 +02:00
}
2019-10-06 10:47:15 +02:00
void Seller::setSellerNo(int seller_no) { setId(seller_no); }
2018-07-09 16:06:27 +02:00
2022-07-07 15:21:46 +02:00
void Seller::setFirstName(const std::string &firstName) { m_firstName = firstName; }
2018-07-11 11:53:46 +02:00
2022-07-07 15:21:46 +02:00
void Seller::setLastName(const std::string &lastName) { m_lastName = lastName; }
2018-07-11 11:53:46 +02:00
2022-07-07 15:21:46 +02:00
void Seller::setNumArticlesOffered(int number) { m_numArticlesOffered = number; }
2018-07-11 11:53:46 +02:00
2018-07-20 11:52:26 +02:00
void Seller::addArticle(std::unique_ptr<Article> article)
2018-07-12 14:39:08 +02:00
{
article->setSeller(this);
2022-07-07 15:21:46 +02:00
m_articles.push_back(std::move(article));
2018-07-12 14:39:08 +02:00
}
2018-07-11 15:59:38 +02:00
2022-07-07 15:21:46 +02:00
std::string Seller::getFirstName() const { return m_firstName; }
2018-07-11 15:59:38 +02:00
2022-07-07 15:21:46 +02:00
std::string Seller::getLastName() const { return m_lastName; }
2018-07-11 15:59:38 +02:00
2019-10-06 10:47:15 +02:00
int Seller::getSellerNo() const { return getId(); }
2018-07-11 13:25:39 +02:00
2018-10-12 11:49:52 +02:00
std::string Seller::getSellerNoAsString() const
2018-10-12 11:47:03 +02:00
{
std::stringstream selNoStr;
2018-10-12 11:49:52 +02:00
2022-07-07 15:03:39 +02:00
selNoStr << std::setfill('0') << std::setw(3) << m_id;
2018-10-12 11:47:03 +02:00
2018-10-12 11:49:52 +02:00
return selNoStr.str();
;
2018-10-12 11:47:03 +02:00
}
2022-07-07 15:21:46 +02:00
std::vector<Article *> Seller::getArticles(bool onlySold) const
2018-07-11 13:25:39 +02:00
{
2022-07-07 15:21:46 +02:00
std::vector<Article *> articles;
for (const auto &article : m_articles) {
2018-07-12 08:26:03 +02:00
if (onlySold && article->isSold()) {
articles.push_back(article.get());
2018-07-11 13:25:39 +02:00
} else if (!onlySold) {
articles.push_back(article.get());
2018-07-11 12:54:10 +02:00
}
}
2018-07-11 13:25:39 +02:00
return articles;
}
2022-07-07 15:21:46 +02:00
Article *Seller::getArticleByUuid(const std::string &uuidString)
2018-07-25 09:31:17 +02:00
{
2022-07-07 15:21:46 +02:00
auto iter = std::find_if(m_articles.begin(), m_articles.end(), [&uuidString](const auto &art) {
2018-07-25 09:31:17 +02:00
return art->getUuidAsString() == uuidString;
});
2022-07-07 15:21:46 +02:00
if (iter == m_articles.end())
2018-07-25 09:31:17 +02:00
return nullptr;
return (*iter).get();
}
2018-07-12 14:39:08 +02:00
int Seller::numArticlesSold() const { return static_cast<int>(getArticles(true).size()); }
2022-07-07 15:21:46 +02:00
int Seller::numArticlesOffered() const { return m_numArticlesOffered; }
2018-07-12 14:39:08 +02:00
2018-07-25 09:31:17 +02:00
int Seller::getMaxArticleNo() const
{
auto iter = std::max_element(
2022-07-07 15:21:46 +02:00
m_articles.begin(), m_articles.end(),
[](const auto &a, const auto &b) -> bool { return a->getArticleNo() < b->getArticleNo(); });
if (iter == m_articles.end())
2018-07-22 20:10:22 +02:00
return 0;
return (*iter)->getArticleNo();
}
2018-07-13 13:54:02 +02:00
void Seller::cleanupArticles()
{
2022-07-07 15:21:46 +02:00
m_articles.erase(std::remove_if(m_articles.begin(), m_articles.end(),
[](const auto &article) {
2018-07-13 13:54:02 +02:00
return article->getState() == Article::State::DELETE;
2018-07-16 15:30:24 +02:00
}),
2022-07-07 15:21:46 +02:00
m_articles.end());
2018-07-13 13:54:02 +02:00
2022-07-07 15:21:46 +02:00
for (auto &article : m_articles) {
2018-07-13 13:54:02 +02:00
article->setState(Article::State::OK);
}
}
2018-07-30 09:50:54 +02:00
int Seller::sumInCents()
{
2022-07-07 15:21:46 +02:00
int sum = std::accumulate(m_articles.begin(), m_articles.end(), 0,
[](int a, const auto &b) { return a + b->getPrice(); });
2018-07-30 09:50:54 +02:00
return sum;
}
std::string Seller::sumAsString() { return formatCentAsEuroString(sumInCents()); }
2018-07-30 09:50:54 +02:00
2022-07-07 15:21:46 +02:00
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)
2018-07-18 09:00:46 +02:00
{
2022-07-07 15:03:39 +02:00
return li->m_id < re->m_id;
2019-10-04 14:05:19 +02:00
}