From 0ad460346cc18ed1b52d0b38adefe34b35525379 Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Fri, 20 Jul 2018 22:07:42 +0200 Subject: [PATCH] Find seller by sellerno --- src/core/marketplace.cpp | 13 ++++++++++++- src/core/marketplace.h | 1 + src/gui/mainwindow.cpp | 31 +++++++++++++++++++++++++++++++ src/gui/mainwindow.h | 13 +++++++------ src/gui/mainwindow.ui | 4 ++-- 5 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/core/marketplace.cpp b/src/core/marketplace.cpp index 5e46bf3..ded680f 100644 --- a/src/core/marketplace.cpp +++ b/src/core/marketplace.cpp @@ -41,4 +41,15 @@ int Marketplace::getNumSellersDelete() return count; } -void Marketplace::sortSellers() { std::sort(sellers_.begin(), sellers_.end()); } \ No newline at end of file +void Marketplace::sortSellers() { std::sort(sellers_.begin(), sellers_.end()); } + +Seller* Marketplace::findSellerWithSellerNo(int sellerNo) +{ + auto iter = + std::find_if(sellers_.begin(), sellers_.end(), [sellerNo](const auto& a) { + return a->getSellerNo() == sellerNo; + }); + if (iter == sellers_.end()) + return nullptr; + return (*iter).get(); +} \ No newline at end of file diff --git a/src/core/marketplace.h b/src/core/marketplace.h index 9d76168..139579f 100644 --- a/src/core/marketplace.h +++ b/src/core/marketplace.h @@ -26,6 +26,7 @@ class Marketplace int getNextSellerNo(); int getNumSellersDelete(); void sortSellers(); + Seller* findSellerWithSellerNo(int sellerNo); private: SellersVec sellers_; diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index a0ad153..4146441 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -2,6 +2,8 @@ #include "sellerdialog.h" +#include + constexpr int STATUSBAR_TIMEOUT = 5000; MainWindow::MainWindow() @@ -11,6 +13,8 @@ MainWindow::MainWindow() connect(ui_.actionQuit, &QAction::triggered, qApp, QApplication::quit); connect(ui_.actionEditSeller, &QAction::triggered, this, &MainWindow::on_actionEditSeller_triggered); + connect(ui_.sellerNoEdit, &QLineEdit::returnPressed, this, + &MainWindow::on_sellerNoEdit_checkSellerNo); marketplace_ = std::make_unique(); marketplace_->loadFromDb(); @@ -31,4 +35,31 @@ void MainWindow::on_actionEditSeller_triggered() statusBar()->showMessage("Änderungen an den Verkäufer-Stammdaten verworfen!", STATUSBAR_TIMEOUT); } +} + +void MainWindow::on_sellerNoEdit_checkSellerNo() +{ + using std::regex, std::regex_match, std::smatch; + + auto inputText = ui_.sellerNoEdit->text().toStdString(); + + if (inputText.empty()) { + + return; + } + + regex pattern{R"(\d{1,5})"}; + smatch result; + if (!regex_match(inputText, result, pattern)) { + return; + } + int sellerNo = std::stoi(result[0]); + + auto seller = marketplace_->findSellerWithSellerNo(sellerNo); + if (seller) { + std::cout << "!!! Seller gefunden: " << seller->getFirstName() << "\n"; + // TODO: Warenkorb füllen + } + + ui_.sellerNoEdit->clear(); } \ No newline at end of file diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index e9826b4..d6f5160 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -13,15 +13,16 @@ class MainWindow : public QMainWindow { Q_OBJECT - public: + public: MainWindow(); - Marketplace* getMarketplace() {return marketplace_.get();} + Marketplace* getMarketplace() { return marketplace_.get(); } - private: - void on_actionEditSeller_triggered(); + private: + void on_actionEditSeller_triggered(); + void on_sellerNoEdit_checkSellerNo(); - Ui::MainWindow ui_; - std::unique_ptr marketplace_; + Ui::MainWindow ui_; + std::unique_ptr marketplace_; }; #endif \ No newline at end of file diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index 1d09eae..44fe7db 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -52,10 +52,10 @@ - + - + Bezahlt!