kima2/src/core/seller.cpp

111 lines
3.1 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>
2018-07-11 11:53: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
{
2018-07-10 12:51:03 +02:00
firstName_ = firstName;
lastName_ = lastName;
2018-07-12 14:39:08 +02:00
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
2018-07-16 15:30:24 +02:00
void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; }
2018-07-11 11:53:46 +02:00
2018-07-16 15:30:24 +02:00
void Seller::setLastName(const std::string& lastName) { lastName_ = lastName; }
2018-07-11 11:53:46 +02:00
2018-07-16 15:30:24 +02:00
void Seller::setNumArticlesOffered(int number) { 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);
2018-07-20 11:52:26 +02:00
articles_.push_back(std::move(article));
2018-07-12 14:39:08 +02:00
}
2018-07-11 15:59:38 +02:00
std::string Seller::getFirstName() const { return firstName_; }
std::string Seller::getLastName() const { return lastName_; }
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
2019-10-06 10:47:15 +02:00
selNoStr << std::setfill('0') << std::setw(3) << 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
}
std::vector<Article*> Seller::getArticles(bool onlySold) const
2018-07-11 13:25:39 +02:00
{
std::vector<Article*> articles;
2018-07-20 11:52:26 +02:00
for (const auto& article : 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;
}
2018-07-25 09:31:17 +02:00
Article* Seller::getArticleByUuid(const std::string& uuidString)
{
auto iter = std::find_if(articles_.begin(), articles_.end(), [&uuidString](const auto& art) {
return art->getUuidAsString() == uuidString;
});
if (iter == articles_.end())
return nullptr;
return (*iter).get();
}
2018-07-12 14:39:08 +02:00
int Seller::numArticlesSold() const { return static_cast<int>(getArticles(true).size()); }
int Seller::numArticlesOffered() const { return numArticlesOffered_; }
2018-07-25 09:31:17 +02:00
int Seller::getMaxArticleNo() const
{
auto iter = std::max_element(
2018-07-22 20:10:22 +02:00
articles_.begin(), articles_.end(),
[](const auto& a, const auto& b) -> bool { return a->getArticleNo() < b->getArticleNo(); });
if (iter == articles_.end())
return 0;
return (*iter)->getArticleNo();
}
2018-07-13 13:54:02 +02:00
void Seller::cleanupArticles()
{
2018-07-16 15:30:24 +02:00
articles_.erase(std::remove_if(articles_.begin(), articles_.end(),
2018-07-20 11:52:26 +02:00
[](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
}),
articles_.end());
2018-07-13 13:54:02 +02:00
for (auto& article : articles_) {
article->setState(Article::State::OK);
}
}
2018-07-30 09:50:54 +02:00
int Seller::sumInCents()
{
int sum = std::accumulate(articles_.begin(), articles_.end(), 0,
[](int a, const auto& b) { return a + b->getPrice(); });
return sum;
}
std::string Seller::sumAsString() { return formatCentAsEuroString(sumInCents()); }
2018-07-30 09:50:54 +02:00
2019-10-06 10:47:15 +02:00
bool operator<(const Seller& li, const Seller& re) { return li.id_ < re.id_; }
2018-07-18 09:00:46 +02:00
bool operator<(const std::unique_ptr<Seller>& li, const std::unique_ptr<Seller>& re)
{
2019-10-06 10:47:15 +02:00
return li->id_ < re->id_;
2019-10-04 14:05:19 +02:00
}