code cleanup
This commit is contained in:
parent
710b4cf9fb
commit
9fd29d588f
24 changed files with 60 additions and 101 deletions
|
@ -4,12 +4,8 @@
|
|||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
// Article::Article() : Entity() {}
|
||||
|
||||
Article::Article(int price) : price_(price) {}
|
||||
|
||||
// Article::Article(std::shared_ptr<Seller> sellerPtr) : Entity() { sellerPtr_ = sellerPtr; }
|
||||
|
||||
void Article::setArticleNo(int articleNo) { articleNo_ = articleNo; }
|
||||
|
||||
void Article::setPrice(int price) { price_ = price; }
|
||||
|
@ -29,10 +25,7 @@ Sale* Article::getSale() { return salePtr_; }
|
|||
|
||||
int Article::getPrice() const { return price_; }
|
||||
|
||||
std::string Article::getPriceAsString() const
|
||||
{
|
||||
return formatCentAsEuroString(price_);
|
||||
}
|
||||
std::string Article::getPriceAsString() const { return formatCentAsEuroString(price_); }
|
||||
|
||||
int Article::getArticleNo() const { return articleNo_; }
|
||||
|
||||
|
|
|
@ -12,7 +12,8 @@
|
|||
class CsvReader
|
||||
{
|
||||
public:
|
||||
static std::size_t readSellersFromFile(const std::filesystem::path& filePath, Marketplace* market);
|
||||
static std::size_t readSellersFromFile(const std::filesystem::path& filePath,
|
||||
Marketplace* market);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -136,10 +136,7 @@ void Database::updateDbToVer2()
|
|||
endTransaction();
|
||||
}
|
||||
|
||||
void Database::updateDbToVer3()
|
||||
{
|
||||
newDb();
|
||||
}
|
||||
void Database::updateDbToVer3() { newDb(); }
|
||||
|
||||
void Database::init()
|
||||
{
|
||||
|
@ -274,8 +271,7 @@ unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& seller
|
|||
if (retCode != SQLITE_OK)
|
||||
throw std::runtime_error(sqlite3_errmsg(db_));
|
||||
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":id"),
|
||||
seller->getId());
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":id"), seller->getId());
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_no"),
|
||||
seller->getSellerNo());
|
||||
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":first_name"),
|
||||
|
@ -298,14 +294,13 @@ unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& seller
|
|||
} else if (seller->getState() == Seller::State::DELETE) {
|
||||
count += static_cast<int>(seller->getArticles(false).size());
|
||||
|
||||
retCode =
|
||||
sqlite3_prepare_v2(db_, "DELETE FROM sellers WHERE seller_no = :id", -1, &stmt, nullptr);
|
||||
retCode = sqlite3_prepare_v2(db_, "DELETE FROM sellers WHERE seller_no = :id", -1,
|
||||
&stmt, nullptr);
|
||||
|
||||
if (retCode != SQLITE_OK)
|
||||
throw std::runtime_error(sqlite3_errmsg(db_));
|
||||
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":id"),
|
||||
seller->getId());
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":id"), seller->getId());
|
||||
|
||||
retCode = sqlite3_step(stmt);
|
||||
|
||||
|
@ -363,7 +358,7 @@ unsigned int Database::storeArticles(std::vector<Article*> articles)
|
|||
boost::uuids::to_string(article->getUuid()).c_str(), -1,
|
||||
SQLITE_TRANSIENT);
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_id"),
|
||||
article->getSeller()->getId());
|
||||
article->getSeller()->getId());
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":source_no"),
|
||||
article->getSourceNo());
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":article_no"),
|
||||
|
@ -399,7 +394,7 @@ unsigned int Database::storeArticles(std::vector<Article*> articles)
|
|||
boost::uuids::to_string(article->getUuid()).c_str(), -1,
|
||||
SQLITE_TRANSIENT);
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_id"),
|
||||
article->getSeller()->getId());
|
||||
article->getSeller()->getId());
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":source_no"),
|
||||
article->getSourceNo());
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":article_no"),
|
||||
|
@ -592,8 +587,7 @@ unsigned int Database::loadSellers(std::vector<std::unique_ptr<Seller>>& sellers
|
|||
if (retCode != SQLITE_OK)
|
||||
throw std::runtime_error(sqlite3_errmsg(db_));
|
||||
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_id"),
|
||||
seller->getId());
|
||||
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_id"), seller->getId());
|
||||
|
||||
retCode = sqlite3_step(stmt);
|
||||
|
||||
|
|
|
@ -3,13 +3,13 @@
|
|||
|
||||
class Entity
|
||||
{
|
||||
public:
|
||||
public:
|
||||
enum class State { NEW, UPDATE, DELETE, OK };
|
||||
virtual ~Entity() = default;
|
||||
void setState(State state) { state_ = state; }
|
||||
virtual State getState() const;
|
||||
|
||||
private:
|
||||
private:
|
||||
State state_{State::NEW};
|
||||
};
|
||||
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
#include "entityint.h"
|
||||
|
||||
EntityInt::EntityInt(int id) {
|
||||
id_ = id;
|
||||
}
|
||||
EntityInt::EntityInt(int id) { id_ = id; }
|
||||
|
||||
void EntityInt::setId(int id) {
|
||||
id_ = id;
|
||||
}
|
||||
void EntityInt::setId(int id) { id_ = id; }
|
||||
|
|
|
@ -17,11 +17,6 @@ void EntityUuid::setUuidFromString(const std::string& uuidString)
|
|||
uuid_ = generator(uuidString);
|
||||
}
|
||||
|
||||
void EntityUuid::setSourceNo(int sourceNo) { sourceNo_ = sourceNo; }
|
||||
|
||||
void EntityUuid::setSourceNo(int sourceNo) {
|
||||
sourceNo_ = sourceNo;
|
||||
}
|
||||
|
||||
int EntityUuid::getSourceNo() const {
|
||||
return sourceNo_;
|
||||
}
|
||||
int EntityUuid::getSourceNo() const { return sourceNo_; }
|
||||
|
|
|
@ -12,7 +12,8 @@
|
|||
class ExcelReader
|
||||
{
|
||||
public:
|
||||
static std::size_t readSellersFromFile(const std::filesystem::path& filePath, Marketplace* market);
|
||||
static std::size_t readSellersFromFile(const std::filesystem::path& filePath,
|
||||
Marketplace* market);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -61,7 +61,8 @@ std::size_t JsonUtil::importSellers(const std::filesystem::path& filePath, Marke
|
|||
return market->getSellers().size() - 1; // minus 1 because we don't count the "special" seller
|
||||
}
|
||||
|
||||
void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo)
|
||||
void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* market,
|
||||
int cashPointNo)
|
||||
{
|
||||
json root;
|
||||
std::ofstream file(filePath);
|
||||
|
@ -94,7 +95,8 @@ void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* m
|
|||
file << root.dump(4) << std::endl;
|
||||
}
|
||||
|
||||
void JsonUtil::importSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo)
|
||||
void JsonUtil::importSales(const std::filesystem::path& filePath, Marketplace* market,
|
||||
int cashPointNo)
|
||||
{
|
||||
std::ifstream file(filePath);
|
||||
json jsonValues = json::parse(file);
|
||||
|
|
|
@ -3,16 +3,18 @@
|
|||
|
||||
#include "marketplace.h"
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
class JsonUtil
|
||||
{
|
||||
public:
|
||||
static void exportSellers(const std::filesystem::path& filePath, Marketplace* market);
|
||||
static std::size_t importSellers(const std::filesystem::path& filePath, Marketplace* market);
|
||||
static void exportSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo);
|
||||
static void importSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo);
|
||||
static void exportSales(const std::filesystem::path& filePath, Marketplace* market,
|
||||
int cashPointNo);
|
||||
static void importSales(const std::filesystem::path& filePath, Marketplace* market,
|
||||
int cashPointNo);
|
||||
};
|
||||
|
||||
#endif
|
|
@ -3,11 +3,11 @@
|
|||
#include "utils.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
@ -104,20 +104,10 @@ Seller* Marketplace::findSellerWithSellerNo(int sellerNo)
|
|||
return (*iter).get();
|
||||
}
|
||||
|
||||
/*
|
||||
Seller* Marketplace::findSellerWithUuid(const std::string& uuid)
|
||||
{
|
||||
auto iter = std::find_if(sellers_.begin(), sellers_.end(),
|
||||
[uuid](const auto& a) { return a->getUuidAsString() == uuid; });
|
||||
if (iter == sellers_.end())
|
||||
return nullptr;
|
||||
return (*iter).get();
|
||||
}
|
||||
*/
|
||||
|
||||
void Marketplace::addArticleToBasket(std::unique_ptr<Article> article)
|
||||
{
|
||||
basket_.insert(basket_.begin(), std::move(article)); // article to the beginning of the basket vector
|
||||
basket_.insert(basket_.begin(),
|
||||
std::move(article)); // article to the beginning of the basket vector
|
||||
}
|
||||
|
||||
size_t Marketplace::basketSize() { return basket_.size(); }
|
||||
|
@ -189,8 +179,9 @@ void Marketplace::exportReportToCSV(const fs::path& filePath, int feeInPercent,
|
|||
<< escapeCsvValue(seller->getFirstName(), delimiter) << delimiter
|
||||
<< seller->numArticlesOffered() << delimiter << seller->numArticlesSold() << delimiter
|
||||
<< escapeCsvValue(seller->sumAsString(), delimiter) << delimiter
|
||||
<< escapeCsvValue(paymentAsString(seller->sumInCents(), feeInPercent, maxFeeInEuro * 100),
|
||||
delimiter)
|
||||
<< escapeCsvValue(
|
||||
paymentAsString(seller->sumInCents(), feeInPercent, maxFeeInEuro * 100),
|
||||
delimiter)
|
||||
<< "\n";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
#ifndef MARKETPLACE_H
|
||||
#define MARKETPLACE_H
|
||||
|
||||
#include "database.h"
|
||||
#include "article.h"
|
||||
#include "database.h"
|
||||
#include "sale.h"
|
||||
#include "seller.h"
|
||||
|
||||
|
|
|
@ -13,8 +13,6 @@ ArticlesVec& Sale::getArticles() { return articles_; }
|
|||
|
||||
void Sale::removeArticle(const Article* articlePtr)
|
||||
{
|
||||
/* auto it = std::find_if(articles_.begin(), articles_.end(),
|
||||
[&articlePtr](auto art) { return art.get() == articlePtr; }); */
|
||||
auto it = std::find(articles_.begin(), articles_.end(), articlePtr);
|
||||
|
||||
if (it != articles_.end()) {
|
||||
|
|
|
@ -32,7 +32,6 @@ class Seller : public EntityInt
|
|||
std::string getSellerNoAsString() const;
|
||||
int numArticlesOffered() const;
|
||||
int numArticlesSold() const;
|
||||
// int numArticlesTotal() const;
|
||||
std::vector<Article*> getArticles(bool onlySold = true) const;
|
||||
Article* getArticleByUuid(const std::string& uuidString);
|
||||
int getMaxArticleNo() const;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
#include "utils.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
#include <numeric>
|
||||
#include <algorithm>
|
||||
|
||||
std::string formatCentAsEuroString(const int cent, int width)
|
||||
{
|
||||
|
@ -51,23 +51,24 @@ std::string& ltrim(std::string& str, const std::string& chars)
|
|||
str.erase(0, str.find_first_not_of(chars));
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
std::string& rtrim(std::string& str, const std::string& chars)
|
||||
{
|
||||
str.erase(str.find_last_not_of(chars) + 1);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
std::string& trim(std::string& str, const std::string& chars)
|
||||
{
|
||||
return ltrim(rtrim(str, chars), chars);
|
||||
}
|
||||
|
||||
bool case_insensitive_match(std::string s1, std::string s2) {
|
||||
//convert s1 and s2 into lower case strings
|
||||
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
|
||||
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
|
||||
if(s1.compare(s2) == 0)
|
||||
return true; //The strings are same
|
||||
return false; //not matched
|
||||
bool case_insensitive_match(std::string s1, std::string s2)
|
||||
{
|
||||
// convert s1 and s2 into lower case strings
|
||||
transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
|
||||
transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
|
||||
if (s1.compare(s2) == 0)
|
||||
return true; // The strings are same
|
||||
return false; // not matched
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue