remove article from sale

This commit is contained in:
Martin Brodbeck 2018-07-13 10:12:16 +02:00
parent 2fb72f1701
commit 91357d7c7f
3 changed files with 24 additions and 1 deletions

View file

@ -8,9 +8,19 @@ void Sale::addArticle(std::shared_ptr<Article> articlePtr)
articles_.push_back(articlePtr); articles_.push_back(articlePtr);
} }
void Sale::removeArticle(const Article* articlePtr)
{
auto it = std::find_if(articles_.begin(), articles_.end(),
[&articlePtr](auto art) { return art.get() == articlePtr; });
if (it != articles_.end()) {
(*it)->setSale(nullptr);
articles_.erase(it);
}
}
int Sale::sumInCents() int Sale::sumInCents()
{ {
int sum = std::accumulate(articles_.begin(), articles_.end(), 0, int sum = std::accumulate(articles_.begin(), articles_.end(), 0,
[](int a, std::shared_ptr<Article> b) { return a + b->getPrice(); }); [](int a, std::shared_ptr<Article> b) { return a + b->getPrice(); });
return sum; return sum;
} }

View file

@ -14,6 +14,8 @@ class Sale : public Entity
public: public:
int sumInCents(); int sumInCents();
void addArticle(std::shared_ptr<Article> articlePtr); void addArticle(std::shared_ptr<Article> articlePtr);
void removeArticle(const Article* articlePtr);
private: private:
boost::posix_time::ptime systemTime_{boost::posix_time::second_clock::local_time()}; boost::posix_time::ptime systemTime_{boost::posix_time::second_clock::local_time()};
std::vector<std::shared_ptr<Article>> articles_{}; std::vector<std::shared_ptr<Article>> articles_{};

View file

@ -31,3 +31,14 @@ BOOST_AUTO_TEST_CASE(articles_sum)
BOOST_TEST(sale.sumInCents() == 550); BOOST_TEST(sale.sumInCents() == 550);
BOOST_TEST(seller.getArticles(true).size() == 10); BOOST_TEST(seller.getArticles(true).size() == 10);
} }
BOOST_AUTO_TEST_CASE(remove_article) {
auto art = std::make_shared<Article>();
Sale sale{};
BOOST_TEST(art->isSold() == false);
sale.addArticle(art);
BOOST_TEST(art->isSold() == true);
sale.removeArticle(art.get());
BOOST_TEST(art->isSold() == false);
}