More on basket

This commit is contained in:
Martin Brodbeck 2018-07-22 20:10:22 +02:00
parent d0f0d604ed
commit ee8c059441
9 changed files with 183 additions and 7 deletions

View file

@ -34,6 +34,16 @@ int Marketplace::getNextSellerNo()
return (*iter)->getSellerNo() + 1;
}
int Marketplace::getNextArticleNo()
{
auto iter = std::max_element(
sellers_.begin(), sellers_.end(),
[](const auto& a, const auto& b) -> bool { return a->getMaxArticleNo() < b->getMaxArticleNo(); });
if (iter == sellers_.end())
return 1;
return (*iter)->getMaxArticleNo() + 1;
}
int Marketplace::getNumSellersDelete()
{
int count = std::count_if(sellers_.begin(), sellers_.end(),
@ -72,4 +82,10 @@ void Marketplace::finishCurrentSale()
}
sales_.push_back(std::move(sale));
basket_.clear();
}
BasketVec& Marketplace::getBasket()
{
return basket_;
}

View file

@ -24,9 +24,13 @@ class Marketplace
void storeToDb(bool onlyDelete = false);
void loadFromDb();
SellersVec& getSellers();
int getNextSellerNo();
int getNextArticleNo();
int getNumSellersDelete();
BasketVec& getBasket();
void sortSellers();
Seller* findSellerWithSellerNo(int sellerNo);
void addArticleToBasket(std::unique_ptr<Article> article);

View file

@ -46,6 +46,15 @@ int Seller::numArticlesSold() const { return static_cast<int>(getArticles(true).
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(),

View file

@ -32,6 +32,7 @@ class Seller : public Entity
int numArticlesSold() const;
// int numArticlesTotal() const;
std::vector<Article*> getArticles(bool onlySold = true) const;
int getMaxArticleNo() const;
friend bool operator<(const Seller& li, const Seller& re);
friend bool operator<(const std::unique_ptr<Seller>& li, const std::unique_ptr<Seller>& re);