107 lines
No EOL
3.2 KiB
C++
107 lines
No EOL
3.2 KiB
C++
#include "seller.h"
|
|
|
|
#include <numeric>
|
|
#include <sstream>
|
|
#include <iomanip>
|
|
|
|
Seller::Seller(const std::string& firstName, const std::string& lastName, int sellerNo,
|
|
int numArticlesOffered)
|
|
: Entity()
|
|
{
|
|
firstName_ = firstName;
|
|
lastName_ = lastName;
|
|
sellerNo_ = sellerNo;
|
|
numArticlesOffered_ = numArticlesOffered;
|
|
}
|
|
|
|
void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; }
|
|
|
|
void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; }
|
|
|
|
void Seller::setLastName(const std::string& lastName) { lastName_ = lastName; }
|
|
|
|
void Seller::setNumArticlesOffered(int number) { numArticlesOffered_ = number; }
|
|
|
|
void Seller::addArticle(std::unique_ptr<Article> article)
|
|
{
|
|
article->setSeller(this);
|
|
articles_.push_back(std::move(article));
|
|
}
|
|
|
|
std::string Seller::getFirstName() const { return firstName_; }
|
|
|
|
std::string Seller::getLastName() const { return lastName_; }
|
|
|
|
int Seller::getSellerNo() const { return sellerNo_; }
|
|
|
|
std::vector<Article*> Seller::getArticles(bool onlySold) const
|
|
{
|
|
std::vector<Article*> articles;
|
|
for (const auto& article : 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(articles_.begin(), articles_.end(), [&uuidString](const auto& art) {
|
|
return art->getUuidAsString() == uuidString;
|
|
});
|
|
if (iter == articles_.end())
|
|
return nullptr;
|
|
return (*iter).get();
|
|
}
|
|
|
|
int Seller::numArticlesSold() const { return static_cast<int>(getArticles(true).size()); }
|
|
|
|
int Seller::numArticlesOffered() const { return numArticlesOffered_; }
|
|
|
|
int Seller::getMaxArticleNo() const
|
|
{
|
|
auto iter = std::max_element(
|
|
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();
|
|
}
|
|
|
|
void Seller::cleanupArticles()
|
|
{
|
|
articles_.erase(std::remove_if(articles_.begin(), articles_.end(),
|
|
[](const auto& article) {
|
|
return article->getState() == Article::State::DELETE;
|
|
}),
|
|
articles_.end());
|
|
|
|
for (auto& article : articles_) {
|
|
article->setState(Article::State::OK);
|
|
}
|
|
}
|
|
|
|
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()
|
|
{
|
|
std::stringstream sumStream;
|
|
sumStream << std::right << std::setw(10) << std::showbase << std::put_money(sumInCents(), false);
|
|
return sumStream.str();
|
|
}
|
|
|
|
// int Seller::numArticlesTotal() const { return static_cast<int>(getArticles().size()); }
|
|
|
|
bool operator<(const Seller& li, const Seller& re) { return li.sellerNo_ < re.sellerNo_; }
|
|
bool operator<(const std::unique_ptr<Seller>& li, const std::unique_ptr<Seller>& re)
|
|
{
|
|
return li->sellerNo_ < re->sellerNo_;
|
|
} |