Compare commits
4 commits
2c5368c621
...
2fb72f1701
Author | SHA1 | Date | |
---|---|---|---|
2fb72f1701 | |||
051ed3e730 | |||
9c624065bb | |||
b69531ced4 |
13 changed files with 85 additions and 31 deletions
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
Article::Article() : Entity() {}
|
Article::Article() : Entity() {}
|
||||||
|
|
||||||
Article::Article(std::shared_ptr<Seller> sellerPtr) : Entity() { sellerPtr_ = sellerPtr; }
|
// Article::Article(std::shared_ptr<Seller> sellerPtr) : Entity() { sellerPtr_ = sellerPtr; }
|
||||||
|
|
||||||
void Article::setArticleNo(int articleNo) { articleNo_ = articleNo; }
|
void Article::setArticleNo(int articleNo) { articleNo_ = articleNo; }
|
||||||
|
|
||||||
|
@ -10,12 +10,14 @@ void Article::setPrice(int price) { price_ = price; }
|
||||||
|
|
||||||
void Article::setDescription(const std::string& description) { description_ = description; }
|
void Article::setDescription(const std::string& description) { description_ = description; }
|
||||||
|
|
||||||
void Article::setSale(std::shared_ptr<Sale> salePtr) { salePtr_ = salePtr; }
|
void Article::setSale(Sale* salePtr) { salePtr_ = salePtr; }
|
||||||
|
|
||||||
|
void Article::setSeller(Seller* sellerPtr) { sellerPtr_ = sellerPtr; }
|
||||||
|
|
||||||
bool Article::isSold() { return salePtr_ ? true : false; }
|
bool Article::isSold() { return salePtr_ ? true : false; }
|
||||||
|
|
||||||
std::string Article::getDescription() { return description_; }
|
std::string Article::getDescription() { return description_; }
|
||||||
|
|
||||||
Seller* Article::getSeller() { return sellerPtr_.get(); }
|
Seller* Article::getSeller() { return sellerPtr_; }
|
||||||
|
|
||||||
int Article::getPrice() { return price_; }
|
int Article::getPrice() { return price_; }
|
|
@ -15,21 +15,22 @@ class Article : public Entity
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Article();
|
Article();
|
||||||
Article(std::shared_ptr<Seller> sellerPtr);
|
//Article(std::shared_ptr<Seller> sellerPtr);
|
||||||
void setArticleNo(int articleNo);
|
void setArticleNo(int articleNo);
|
||||||
void setPrice(int price);
|
void setPrice(int price);
|
||||||
void setDescription(const std::string& description);
|
void setDescription(const std::string& description);
|
||||||
bool isSold();
|
bool isSold();
|
||||||
void setSale(std::shared_ptr<Sale> salePtr);
|
void setSale(Sale* salePtr);
|
||||||
void setSeller(std::shared_ptr<Seller> sellerPtr);
|
//void setSeller(std::shared_ptr<Seller> sellerPtr);
|
||||||
|
void setSeller(Seller* sellerPtr);
|
||||||
|
|
||||||
std::string getDescription();
|
std::string getDescription();
|
||||||
Seller* getSeller();
|
Seller* getSeller();
|
||||||
int getPrice();
|
int getPrice();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<Seller> sellerPtr_{};
|
Seller* sellerPtr_{};
|
||||||
std::shared_ptr<Sale> salePtr_{};
|
Sale* salePtr_{};
|
||||||
int articleNo_{};
|
int articleNo_{};
|
||||||
int price_{};
|
int price_{};
|
||||||
std::string description_{};
|
std::string description_{};
|
||||||
|
|
|
@ -174,7 +174,7 @@ unsigned int Database::storeSellers(std::vector<Seller>& sellers)
|
||||||
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":last_name"),
|
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":last_name"),
|
||||||
seller.getLastName().c_str(), -1, nullptr);
|
seller.getLastName().c_str(), -1, nullptr);
|
||||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":num_offered_articles"),
|
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":num_offered_articles"),
|
||||||
seller.getNumberOfOfferedArticles());
|
seller.numArticlesOffered());
|
||||||
|
|
||||||
retCode = sqlite3_step(stmt);
|
retCode = sqlite3_step(stmt);
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ void Entity::createUuid()
|
||||||
uuid_ = generator();
|
uuid_ = generator();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Entity::createUuidFromString(const std::string& uuidString)
|
void Entity::setUuidFromString(const std::string& uuidString)
|
||||||
{
|
{
|
||||||
boost::uuids::string_generator generator{};
|
boost::uuids::string_generator generator{};
|
||||||
uuid_ = generator(uuidString);
|
uuid_ = generator(uuidString);
|
||||||
|
|
|
@ -14,7 +14,7 @@ class Entity
|
||||||
virtual ~Entity() = 0;
|
virtual ~Entity() = 0;
|
||||||
const boost::uuids::uuid& getUuid() const { return uuid_; };
|
const boost::uuids::uuid& getUuid() const { return uuid_; };
|
||||||
void createUuid();
|
void createUuid();
|
||||||
void createUuidFromString(const std::string& uuidString);
|
void setUuidFromString(const std::string& uuidString);
|
||||||
virtual State getState() const;
|
virtual State getState() const;
|
||||||
void setState(State state) { state_ = state; }
|
void setState(State state) { state_ = state; }
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,15 @@
|
||||||
|
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
|
||||||
|
void Sale::addArticle(std::shared_ptr<Article> articlePtr)
|
||||||
|
{
|
||||||
|
articlePtr->setSale(this);
|
||||||
|
articles_.push_back(articlePtr);
|
||||||
|
}
|
||||||
|
|
||||||
int Sale::sumInCents()
|
int Sale::sumInCents()
|
||||||
{
|
{
|
||||||
int test = std::accumulate(articles_.begin(), articles_.end(), 0,
|
int sum = std::accumulate(articles_.begin(), articles_.end(), 0,
|
||||||
[](int a, Article* b) { return a + b->getPrice(); });
|
[](int a, std::shared_ptr<Article> b) { return a + b->getPrice(); });
|
||||||
return test;
|
return sum;
|
||||||
}
|
}
|
|
@ -13,9 +13,10 @@ class Sale : public Entity
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
int sumInCents();
|
int sumInCents();
|
||||||
|
void addArticle(std::shared_ptr<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<Article*> articles_{};
|
std::vector<std::shared_ptr<Article>> articles_{};
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -3,13 +3,13 @@
|
||||||
Seller::Seller() : Entity() {}
|
Seller::Seller() : Entity() {}
|
||||||
|
|
||||||
Seller::Seller(const std::string& firstName, const std::string& lastName, int sellerNo,
|
Seller::Seller(const std::string& firstName, const std::string& lastName, int sellerNo,
|
||||||
int numberOfArticles)
|
int numArticlesOffered)
|
||||||
: Entity()
|
: Entity()
|
||||||
{
|
{
|
||||||
firstName_ = firstName;
|
firstName_ = firstName;
|
||||||
lastName_ = lastName;
|
lastName_ = lastName;
|
||||||
sellerNo_ = sellerNo;
|
sellerNo_ = sellerNo;
|
||||||
numberOfOfferedArticles_ = numberOfArticles;
|
numArticlesOffered_ = numArticlesOffered;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; }
|
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::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)
|
||||||
|
{
|
||||||
void Seller::addArticle(std::shared_ptr<Article> article) { articles_.push_back(article); }
|
article->setSeller(this);
|
||||||
|
articles_.push_back(article);
|
||||||
|
}
|
||||||
|
|
||||||
std::string Seller::getFirstName() const { return firstName_; }
|
std::string Seller::getFirstName() const { return firstName_; }
|
||||||
|
|
||||||
|
@ -43,4 +45,8 @@ std::vector<Article*> Seller::getArticles(bool onlySold) const
|
||||||
return articles;
|
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()); }
|
|
@ -15,24 +15,25 @@ class Seller : public Entity
|
||||||
public:
|
public:
|
||||||
Seller();
|
Seller();
|
||||||
Seller(const std::string& firstName, const std::string& lastName, int sellerNo = 0,
|
Seller(const std::string& firstName, const std::string& lastName, int sellerNo = 0,
|
||||||
int numberOfArticles = 0);
|
int numArticlesOffered = 0);
|
||||||
|
|
||||||
void setSellerNo(int sellerNo);
|
void setSellerNo(int sellerNo);
|
||||||
void setFirstName(const std::string& firstName);
|
void setFirstName(const std::string& firstName);
|
||||||
void setLastName(const std::string& lastName);
|
void setLastName(const std::string& lastName);
|
||||||
void setNumberOfOfferedArticles(int number);
|
void setNumArticlesOffered(int number);
|
||||||
void addArticle(std::shared_ptr<Article> article);
|
void addArticle(std::shared_ptr<Article> article);
|
||||||
|
|
||||||
std::string getFirstName() const;
|
std::string getFirstName() const;
|
||||||
std::string getLastName() const;
|
std::string getLastName() const;
|
||||||
int getSellerNo() const;
|
int getSellerNo() const;
|
||||||
int getNumberOfOfferedArticles() const;
|
int numArticlesOffered() const;
|
||||||
size_t soldArticles() const;
|
int numArticlesSold() const;
|
||||||
std::vector<Article*> getArticles(bool onlySold = false) const;
|
//int numArticlesTotal() const;
|
||||||
|
std::vector<Article*> getArticles(bool onlySold = true) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int sellerNo_{-1};
|
int sellerNo_{-1};
|
||||||
int numberOfOfferedArticles_{};
|
int numArticlesOffered_{};
|
||||||
std::string firstName_{};
|
std::string firstName_{};
|
||||||
std::string lastName_{};
|
std::string lastName_{};
|
||||||
std::vector<std::shared_ptr<Article>> articles_{};
|
std::vector<std::shared_ptr<Article>> articles_{};
|
||||||
|
|
|
@ -11,3 +11,7 @@ add_test(NAME Article COMMAND ${CMAKE_BINARY_DIR}/bin/test_article)
|
||||||
add_executable(test_database test_database.cpp)
|
add_executable(test_database test_database.cpp)
|
||||||
target_link_libraries(test_database core Boost::unit_test_framework stdc++fs)
|
target_link_libraries(test_database core Boost::unit_test_framework stdc++fs)
|
||||||
add_test(NAME Database COMMAND ${CMAKE_BINARY_DIR}/bin/test_database)
|
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)
|
||||||
|
|
|
@ -16,6 +16,6 @@ BOOST_AUTO_TEST_CASE(check_is_sold)
|
||||||
BOOST_TEST(article.isSold() == false);
|
BOOST_TEST(article.isSold() == false);
|
||||||
|
|
||||||
auto salePtr = std::make_shared<Sale>();
|
auto salePtr = std::make_shared<Sale>();
|
||||||
article.setSale(salePtr);
|
article.setSale(salePtr.get());
|
||||||
BOOST_TEST(article.isSold() == true);
|
BOOST_TEST(article.isSold() == true);
|
||||||
}
|
}
|
33
test/test_sale.cpp
Normal file
33
test/test_sale.cpp
Normal 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);
|
||||||
|
}
|
|
@ -34,6 +34,6 @@ BOOST_AUTO_TEST_CASE(with_article) {
|
||||||
auto article = std::make_shared<Article>();
|
auto article = std::make_shared<Article>();
|
||||||
article->setDescription("Test article");
|
article->setDescription("Test article");
|
||||||
seller.addArticle(article);
|
seller.addArticle(article);
|
||||||
BOOST_TEST(seller.getArticles().at(0)->getDescription() == "Test article");
|
BOOST_TEST(seller.getArticles(false).at(0)->getDescription() == "Test article");
|
||||||
BOOST_TEST(seller.soldArticles() == 0);
|
BOOST_TEST(seller.numArticlesSold() == 0);
|
||||||
}
|
}
|
Loading…
Reference in a new issue