code cleanup

This commit is contained in:
Martin Brodbeck 2019-10-07 14:08:01 +02:00
parent 710b4cf9fb
commit 9fd29d588f
24 changed files with 60 additions and 101 deletions

View file

@ -4,12 +4,8 @@
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
// Article::Article() : Entity() {}
Article::Article(int price) : price_(price) {} 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::setArticleNo(int articleNo) { articleNo_ = articleNo; }
void Article::setPrice(int price) { price_ = price; } void Article::setPrice(int price) { price_ = price; }
@ -29,10 +25,7 @@ Sale* Article::getSale() { return salePtr_; }
int Article::getPrice() const { return price_; } int Article::getPrice() const { return price_; }
std::string Article::getPriceAsString() const std::string Article::getPriceAsString() const { return formatCentAsEuroString(price_); }
{
return formatCentAsEuroString(price_);
}
int Article::getArticleNo() const { return articleNo_; } int Article::getArticleNo() const { return articleNo_; }

View file

@ -12,7 +12,8 @@
class CsvReader class CsvReader
{ {
public: 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 #endif

View file

@ -136,10 +136,7 @@ void Database::updateDbToVer2()
endTransaction(); endTransaction();
} }
void Database::updateDbToVer3() void Database::updateDbToVer3() { newDb(); }
{
newDb();
}
void Database::init() void Database::init()
{ {
@ -274,8 +271,7 @@ unsigned int Database::storeSellers(std::vector<std::unique_ptr<Seller>>& seller
if (retCode != SQLITE_OK) if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_)); throw std::runtime_error(sqlite3_errmsg(db_));
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":id"), sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":id"), seller->getId());
seller->getId());
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_no"), sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_no"),
seller->getSellerNo()); seller->getSellerNo());
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":first_name"), 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) { } else if (seller->getState() == Seller::State::DELETE) {
count += static_cast<int>(seller->getArticles(false).size()); count += static_cast<int>(seller->getArticles(false).size());
retCode = retCode = sqlite3_prepare_v2(db_, "DELETE FROM sellers WHERE seller_no = :id", -1,
sqlite3_prepare_v2(db_, "DELETE FROM sellers WHERE seller_no = :id", -1, &stmt, nullptr); &stmt, nullptr);
if (retCode != SQLITE_OK) if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_)); throw std::runtime_error(sqlite3_errmsg(db_));
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":id"), sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":id"), seller->getId());
seller->getId());
retCode = sqlite3_step(stmt); 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, boost::uuids::to_string(article->getUuid()).c_str(), -1,
SQLITE_TRANSIENT); SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_id"), 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"), sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":source_no"),
article->getSourceNo()); article->getSourceNo());
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":article_no"), 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, boost::uuids::to_string(article->getUuid()).c_str(), -1,
SQLITE_TRANSIENT); SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_id"), 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"), sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":source_no"),
article->getSourceNo()); article->getSourceNo());
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":article_no"), 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) if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_)); throw std::runtime_error(sqlite3_errmsg(db_));
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_id"), sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_id"), seller->getId());
seller->getId());
retCode = sqlite3_step(stmt); retCode = sqlite3_step(stmt);

View file

@ -3,13 +3,13 @@
class Entity class Entity
{ {
public: public:
enum class State { NEW, UPDATE, DELETE, OK }; enum class State { NEW, UPDATE, DELETE, OK };
virtual ~Entity() = default; virtual ~Entity() = default;
void setState(State state) { state_ = state; } void setState(State state) { state_ = state; }
virtual State getState() const; virtual State getState() const;
private: private:
State state_{State::NEW}; State state_{State::NEW};
}; };

View file

@ -1,9 +1,5 @@
#include "entityint.h" #include "entityint.h"
EntityInt::EntityInt(int id) { EntityInt::EntityInt(int id) { id_ = id; }
id_ = id;
}
void EntityInt::setId(int id) { void EntityInt::setId(int id) { id_ = id; }
id_ = id;
}

View file

@ -17,11 +17,6 @@ void EntityUuid::setUuidFromString(const std::string& uuidString)
uuid_ = generator(uuidString); uuid_ = generator(uuidString);
} }
void EntityUuid::setSourceNo(int sourceNo) { sourceNo_ = sourceNo; }
void EntityUuid::setSourceNo(int sourceNo) { int EntityUuid::getSourceNo() const { return sourceNo_; }
sourceNo_ = sourceNo;
}
int EntityUuid::getSourceNo() const {
return sourceNo_;
}

View file

@ -12,7 +12,8 @@
class ExcelReader class ExcelReader
{ {
public: 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 #endif

View file

@ -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 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; json root;
std::ofstream file(filePath); std::ofstream file(filePath);
@ -94,7 +95,8 @@ void JsonUtil::exportSales(const std::filesystem::path& filePath, Marketplace* m
file << root.dump(4) << std::endl; 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); std::ifstream file(filePath);
json jsonValues = json::parse(file); json jsonValues = json::parse(file);

View file

@ -3,16 +3,18 @@
#include "marketplace.h" #include "marketplace.h"
#include <string>
#include <filesystem> #include <filesystem>
#include <string>
class JsonUtil class JsonUtil
{ {
public: public:
static void exportSellers(const std::filesystem::path& filePath, Marketplace* market); static void exportSellers(const std::filesystem::path& filePath, Marketplace* market);
static std::size_t importSellers(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 exportSales(const std::filesystem::path& filePath, Marketplace* market,
static void importSales(const std::filesystem::path& filePath, Marketplace* market, int cashPointNo); int cashPointNo);
static void importSales(const std::filesystem::path& filePath, Marketplace* market,
int cashPointNo);
}; };
#endif #endif

View file

@ -3,11 +3,11 @@
#include "utils.h" #include "utils.h"
#include <algorithm> #include <algorithm>
#include <filesystem>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
#include <numeric> #include <numeric>
#include <sstream> #include <sstream>
#include <filesystem>
namespace fs = std::filesystem; namespace fs = std::filesystem;
@ -104,20 +104,10 @@ Seller* Marketplace::findSellerWithSellerNo(int sellerNo)
return (*iter).get(); 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) 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(); } 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 << escapeCsvValue(seller->getFirstName(), delimiter) << delimiter
<< seller->numArticlesOffered() << delimiter << seller->numArticlesSold() << delimiter << seller->numArticlesOffered() << delimiter << seller->numArticlesSold() << delimiter
<< escapeCsvValue(seller->sumAsString(), delimiter) << delimiter << escapeCsvValue(seller->sumAsString(), delimiter) << delimiter
<< escapeCsvValue(paymentAsString(seller->sumInCents(), feeInPercent, maxFeeInEuro * 100), << escapeCsvValue(
delimiter) paymentAsString(seller->sumInCents(), feeInPercent, maxFeeInEuro * 100),
delimiter)
<< "\n"; << "\n";
} }
} }

View file

@ -1,8 +1,8 @@
#ifndef MARKETPLACE_H #ifndef MARKETPLACE_H
#define MARKETPLACE_H #define MARKETPLACE_H
#include "database.h"
#include "article.h" #include "article.h"
#include "database.h"
#include "sale.h" #include "sale.h"
#include "seller.h" #include "seller.h"

View file

@ -13,8 +13,6 @@ ArticlesVec& Sale::getArticles() { return articles_; }
void Sale::removeArticle(const Article* articlePtr) 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); auto it = std::find(articles_.begin(), articles_.end(), articlePtr);
if (it != articles_.end()) { if (it != articles_.end()) {

View file

@ -32,7 +32,6 @@ class Seller : public EntityInt
std::string getSellerNoAsString() const; std::string getSellerNoAsString() const;
int numArticlesOffered() const; int numArticlesOffered() const;
int numArticlesSold() const; int numArticlesSold() const;
// int numArticlesTotal() const;
std::vector<Article*> getArticles(bool onlySold = true) const; std::vector<Article*> getArticles(bool onlySold = true) const;
Article* getArticleByUuid(const std::string& uuidString); Article* getArticleByUuid(const std::string& uuidString);
int getMaxArticleNo() const; int getMaxArticleNo() const;

View file

@ -1,8 +1,8 @@
#include "utils.h" #include "utils.h"
#include <algorithm>
#include <iomanip> #include <iomanip>
#include <numeric> #include <numeric>
#include <algorithm>
std::string formatCentAsEuroString(const int cent, int width) 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)); str.erase(0, str.find_first_not_of(chars));
return str; return str;
} }
std::string& rtrim(std::string& str, const std::string& chars) std::string& rtrim(std::string& str, const std::string& chars)
{ {
str.erase(str.find_last_not_of(chars) + 1); str.erase(str.find_last_not_of(chars) + 1);
return str; return str;
} }
std::string& trim(std::string& str, const std::string& chars) std::string& trim(std::string& str, const std::string& chars)
{ {
return ltrim(rtrim(str, chars), chars); return ltrim(rtrim(str, chars), chars);
} }
bool case_insensitive_match(std::string s1, std::string s2) { 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); // convert s1 and s2 into lower case strings
transform(s2.begin(), s2.end(), s2.begin(), ::tolower); transform(s1.begin(), s1.end(), s1.begin(), ::tolower);
if(s1.compare(s2) == 0) transform(s2.begin(), s2.end(), s2.begin(), ::tolower);
return true; //The strings are same if (s1.compare(s2) == 0)
return false; //not matched return true; // The strings are same
return false; // not matched
} }

View file

@ -1,8 +1,8 @@
#include "basketmodel.h" #include "basketmodel.h"
#include <QFont> #include <QFont>
#include <QSettings>
#include <QFontDatabase> #include <QFontDatabase>
#include <QSettings>
BasketModel::BasketModel(Marketplace* market, QObject* parent) BasketModel::BasketModel(Marketplace* market, QObject* parent)
: QAbstractTableModel(parent), marketplace_(market) : QAbstractTableModel(parent), marketplace_(market)
@ -22,7 +22,7 @@ QVariant BasketModel::data(const QModelIndex& index, int role) const
QFont myFont; QFont myFont;
QFont myFixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); QFont myFixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
if (myFixedFont.fixedPitch() == false) { if (myFixedFont.fixedPitch() == false) {
myFixedFont.setFamily("monospace"); myFixedFont.setFamily("monospace");
} }

View file

@ -7,7 +7,7 @@
class BasketModel : public QAbstractTableModel class BasketModel : public QAbstractTableModel
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit BasketModel(Marketplace* market, QObject* parent = nullptr); explicit BasketModel(Marketplace* market, QObject* parent = nullptr);
@ -15,15 +15,12 @@ class BasketModel : public QAbstractTableModel
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override; virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override; virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
//virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
//virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
//virtual bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
void addArticle(Seller* seller, int price, const std::string& desc); void addArticle(Seller* seller, int price, const std::string& desc);
void finishSale(); void finishSale();
void cancelSale(); void cancelSale();
virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
signals: signals:
void basketDataChanged(); void basketDataChanged();
private: private:

View file

@ -19,14 +19,6 @@ int main(int argc, char* argv[])
QCoreApplication::setOrganizationDomain("rustysoft.de"); QCoreApplication::setOrganizationDomain("rustysoft.de");
QCoreApplication::setApplicationName("kima2"); QCoreApplication::setApplicationName("kima2");
// Make sure that only one instance of the application runs
// Of course the uuid used here is unique to this application
// (commented out in favour of SingleApplication)
/*QSharedMemory shared("26297455-946a-4607-bfb7-5025a5f1e136");
if (!shared.create(512, QSharedMemory::ReadWrite)) {
exit(0);
}*/
QTranslator qTranslator; QTranslator qTranslator;
QLocale german(QLocale::German); QLocale german(QLocale::German);
#ifdef __linux__ #ifdef __linux__

View file

@ -29,7 +29,7 @@ class MainWindow : public QMainWindow
void onCancelAllArticlesButtonClicked(bool checked); void onCancelAllArticlesButtonClicked(bool checked);
void onAboutQt(); void onAboutQt();
void onAbout(); void onAbout();
virtual bool eventFilter(QObject *target, QEvent *event) override; virtual bool eventFilter(QObject* target, QEvent* event) override;
protected: protected:
virtual void closeEvent(QCloseEvent* event) override; virtual void closeEvent(QCloseEvent* event) override;

View file

@ -33,7 +33,4 @@ void PriceDialog::accept()
} }
} }
void PriceDialog::setForceDesc(bool force) void PriceDialog::setForceDesc(bool force) { forceDesc_ = force; }
{
forceDesc_ = force;
}

View file

@ -24,7 +24,8 @@ class ReportDialog : public QDialog
void onReportViewSelectionChanged(const QItemSelection& selected, void onReportViewSelectionChanged(const QItemSelection& selected,
const QItemSelection& deselected); const QItemSelection& deselected);
private : Ui::ReportDialog ui_; private:
Ui::ReportDialog ui_;
Marketplace* market_; Marketplace* market_;
std::unique_ptr<ReportModel> model_; std::unique_ptr<ReportModel> model_;
}; };

View file

@ -12,7 +12,7 @@ class SaleModel : public QAbstractItemModel
public: public:
explicit SaleModel(Marketplace* market, QObject* parent = nullptr); explicit SaleModel(Marketplace* market, QObject* parent = nullptr);
virtual QModelIndex index(int row, int column, virtual QModelIndex index(int row, int column,
const QModelIndex& parent = QModelIndex()) const override; const QModelIndex& parent = QModelIndex()) const override;
virtual QModelIndex parent(const QModelIndex& index) const override; virtual QModelIndex parent(const QModelIndex& index) const override;
virtual QVariant data(const QModelIndex& index, int role) const override; virtual QVariant data(const QModelIndex& index, int role) const override;
virtual int rowCount(const QModelIndex& parent) const override; virtual int rowCount(const QModelIndex& parent) const override;

View file

@ -26,9 +26,6 @@ QVariant SellerModel::data(const QModelIndex& index, int role) const
Seller* seller = marketplace_->getSellers().at(index.row()).get(); Seller* seller = marketplace_->getSellers().at(index.row()).get();
/* if (seller->getState() == Seller::State::DELETE)
return QVariant();
*/
switch (index.column()) { switch (index.column()) {
case 0: case 0:
return seller->getId(); return seller->getId();

View file

@ -7,7 +7,7 @@
class SellerModel : public QAbstractTableModel class SellerModel : public QAbstractTableModel
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit SellerModel(Marketplace* market, QObject* parent = nullptr); explicit SellerModel(Marketplace* market, QObject* parent = nullptr);
@ -16,7 +16,8 @@ class SellerModel : public QAbstractTableModel
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override; virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
virtual Qt::ItemFlags flags(const QModelIndex& index) const override; virtual Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override; virtual bool setData(const QModelIndex& index, const QVariant& value,
int role = Qt::EditRole) override;
virtual bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; virtual bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override; virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;

View file

@ -36,7 +36,8 @@ class PosPrinter
void printHeader(const std::string& commune = "Musterhausen"); void printHeader(const std::string& commune = "Musterhausen");
void printTest(); void printTest();
void printSaleReceipt(Sale* sale, const std::string& commune = "Dettingen"); void printSaleReceipt(Sale* sale, const std::string& commune = "Dettingen");
void printSellerReceipt(Seller* seller, const int percent, const int maxFeeInCent, const std::string& commune = "Dettingen"); void printSellerReceipt(Seller* seller, const int percent, const int maxFeeInCent,
const std::string& commune = "Dettingen");
bool isValid(); bool isValid();
struct Command { struct Command {