refacturing

This commit is contained in:
Martin Brodbeck 2018-07-12 14:39:08 +02:00
parent 051ed3e730
commit 2fb72f1701
6 changed files with 60 additions and 15 deletions

View File

@ -4,6 +4,7 @@
void Sale::addArticle(std::shared_ptr<Article> articlePtr)
{
articlePtr->setSale(this);
articles_.push_back(articlePtr);
}

View File

@ -3,13 +3,13 @@
Seller::Seller() : Entity() {}
Seller::Seller(const std::string& firstName, const std::string& lastName, int sellerNo,
int numberOfArticles)
int numArticlesOffered)
: Entity()
{
firstName_ = firstName;
lastName_ = lastName;
sellerNo_ = sellerNo;
numberOfOfferedArticles_ = numberOfArticles;
numArticlesOffered_ = numArticlesOffered;
}
void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; }
@ -18,11 +18,13 @@ inline void Seller::setFirstName(const std::string& firstName) { firstName_ = fi
inline void Seller::setLastName(const std::string& lastName) { lastName_ = lastName; }
inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArticles_ = number; }
inline void Seller::setNumArticlesOffered(int number) { numArticlesOffered_ = number; }
int Seller::getNumberOfOfferedArticles() const { return static_cast<int>(articles_.size()); }
void Seller::addArticle(std::shared_ptr<Article> article) { articles_.push_back(article); }
void Seller::addArticle(std::shared_ptr<Article> article)
{
article->setSeller(this);
articles_.push_back(article);
}
std::string Seller::getFirstName() const { return firstName_; }
@ -43,4 +45,8 @@ std::vector<Article*> Seller::getArticles(bool onlySold) const
return articles;
}
size_t Seller::soldArticles() const { return getArticles(true).size(); }
int Seller::numArticlesSold() const { return static_cast<int>(getArticles(true).size()); }
int Seller::numArticlesOffered() const { return numArticlesOffered_; }
//int Seller::numArticlesTotal() const { return static_cast<int>(getArticles().size()); }

View File

@ -15,24 +15,25 @@ class Seller : public Entity
public:
Seller();
Seller(const std::string& firstName, const std::string& lastName, int sellerNo = 0,
int numberOfArticles = 0);
int numArticlesOffered = 0);
void setSellerNo(int sellerNo);
void setFirstName(const std::string& firstName);
void setLastName(const std::string& lastName);
void setNumberOfOfferedArticles(int number);
void setNumArticlesOffered(int number);
void addArticle(std::shared_ptr<Article> article);
std::string getFirstName() const;
std::string getLastName() const;
int getSellerNo() const;
int getNumberOfOfferedArticles() const;
size_t soldArticles() const;
std::vector<Article*> getArticles(bool onlySold = false) const;
int numArticlesOffered() const;
int numArticlesSold() const;
//int numArticlesTotal() const;
std::vector<Article*> getArticles(bool onlySold = true) const;
private:
int sellerNo_{-1};
int numberOfOfferedArticles_{};
int numArticlesOffered_{};
std::string firstName_{};
std::string lastName_{};
std::vector<std::shared_ptr<Article>> articles_{};

View File

@ -11,3 +11,7 @@ add_test(NAME Article COMMAND ${CMAKE_BINARY_DIR}/bin/test_article)
add_executable(test_database test_database.cpp)
target_link_libraries(test_database core Boost::unit_test_framework stdc++fs)
add_test(NAME Database COMMAND ${CMAKE_BINARY_DIR}/bin/test_database)
add_executable(test_sale test_sale.cpp)
target_link_libraries(test_sale core Boost::unit_test_framework)
add_test(NAME Sale COMMAND ${CMAKE_BINARY_DIR}/bin/test_sale)

33
test/test_sale.cpp Normal file
View File

@ -0,0 +1,33 @@
#define BOOST_TEST_MODULE sale
#include "../src/core/sale.h"
#include <vector>
#include <boost/test/included/unit_test.hpp>
BOOST_AUTO_TEST_CASE(articles_sum)
{
const int NUM_ARTICLES = 30;
Seller seller("Max", "Mustermann", 1, NUM_ARTICLES);
std::vector<std::shared_ptr<Article>> articles{};
Sale sale{};
for(int i = 0; i < NUM_ARTICLES; ++i)
{
auto art = std::make_shared<Article>();
art->setPrice((i+1) * 10);
articles.push_back(art);
seller.addArticle(art);
}
for(int i = 0; i < 10; ++i)
{
sale.addArticle(articles.at(i));
}
BOOST_TEST(sale.sumInCents() == 550);
BOOST_TEST(seller.getArticles(true).size() == 10);
}

View File

@ -34,6 +34,6 @@ BOOST_AUTO_TEST_CASE(with_article) {
auto article = std::make_shared<Article>();
article->setDescription("Test article");
seller.addArticle(article);
BOOST_TEST(seller.getArticles().at(0)->getDescription() == "Test article");
BOOST_TEST(seller.soldArticles() == 0);
BOOST_TEST(seller.getArticles(false).at(0)->getDescription() == "Test article");
BOOST_TEST(seller.numArticlesSold() == 0);
}