diff --git a/src/core/sale.cpp b/src/core/sale.cpp index 92b0140..d15e286 100644 --- a/src/core/sale.cpp +++ b/src/core/sale.cpp @@ -8,9 +8,19 @@ void Sale::addArticle(std::shared_ptr
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 sum = std::accumulate(articles_.begin(), articles_.end(), 0, - [](int a, std::shared_ptr
b) { return a + b->getPrice(); }); + [](int a, std::shared_ptr
b) { return a + b->getPrice(); }); return sum; } \ No newline at end of file diff --git a/src/core/sale.h b/src/core/sale.h index 11253bd..0c38e51 100644 --- a/src/core/sale.h +++ b/src/core/sale.h @@ -14,6 +14,8 @@ class Sale : public Entity public: int sumInCents(); void addArticle(std::shared_ptr
articlePtr); + void removeArticle(const Article* articlePtr); + private: boost::posix_time::ptime systemTime_{boost::posix_time::second_clock::local_time()}; std::vector> articles_{}; diff --git a/test/test_sale.cpp b/test/test_sale.cpp index 0ee1bed..7ef6bc4 100644 --- a/test/test_sale.cpp +++ b/test/test_sale.cpp @@ -30,4 +30,15 @@ BOOST_AUTO_TEST_CASE(articles_sum) BOOST_TEST(sale.sumInCents() == 550); BOOST_TEST(seller.getArticles(true).size() == 10); +} + +BOOST_AUTO_TEST_CASE(remove_article) { + auto art = std::make_shared
(); + Sale sale{}; + + BOOST_TEST(art->isSold() == false); + sale.addArticle(art); + BOOST_TEST(art->isSold() == true); + sale.removeArticle(art.get()); + BOOST_TEST(art->isSold() == false); } \ No newline at end of file