From f11ebc6a9c1cbbb7b80c44b21490d0734c551d18 Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Wed, 26 Jun 2019 15:12:23 -0300 Subject: [PATCH] customer can be saved into db --- CMakeLists.txt | 2 +- Customer.cpp | 47 ++++++++++++++++++++++++++++++++------------- data.cpp | 14 ++++++++++++++ headers/Customer.h | 26 +++++++++++++------------ headers/data.h | 6 ++++++ main.cpp | 16 ++++++--------- test.db3 | Bin 16384 -> 16384 bytes 7 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 data.cpp create mode 100644 headers/data.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b455a7d..b83da79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,7 @@ include_directories( ) -add_executable(park main.cpp Customer.cpp headers/Customer.h Park_spot.cpp headers/Park_spot.h Park_time.cpp headers/Park_time.h) +add_executable(park main.cpp data.cpp headers/data.h Customer.cpp headers/Customer.h Park_spot.cpp headers/Park_spot.h Park_time.cpp headers/Park_time.h) target_link_libraries(park SQLiteCpp diff --git a/Customer.cpp b/Customer.cpp index a77d2c3..aa1d49a 100644 --- a/Customer.cpp +++ b/Customer.cpp @@ -2,29 +2,21 @@ #include // moet aangepast worden om een verhicle_type toe te voegen -Customer::Customer(int id_, string name_) - : id { id_ } - , name { name_ } -{ -} +Customer::Customer(int id_, string name_, Verhicle_type verhicle_) : id{id_}, name{name_}, verhicle{verhicle_}, card_code{gen_cardcode()} {} /* creert een park_time object met start time= nu, en voegt t toe aan een vector. */ -void Customer::clock_in( int s_id) -{ +void Customer::clock_in(int s_id) { Park_time pt{id, s_id}; park_instances.push_back(pt); } // edit de laatste park_time obj in de vector zodat de end_time = now. -void Customer::clock_out(int s_id){ - park_instances[park_instances.size()-1].clock_out(id, s_id); -} - +void Customer::clock_out(int s_id) { park_instances[park_instances.size() - 1].clock_out(id, s_id); } // monthly report generation. moet nog een manier vinden om af te bakenen. -void Customer::gen_monthly(){ +void Customer::gen_monthly() { std::cout << "NAME: " << name << " card code: " << card_code << "\n"; std::cout << "-------------------------------------------------\n"; for (auto& i : park_instances) { @@ -32,4 +24,33 @@ void Customer::gen_monthly(){ std::cout << i; } std::cout << "-------------------------------------------------\n\n"; -} \ No newline at end of file +} + +void Customer::update_db(SQLite::Database& database) { + string statement{"insert into Customer values (, '', '', )"}; + // after ( = 28) + statement.insert(38, std::to_string(int(verhicle))); + statement.insert(36, card_code); + statement.insert(32, name); + statement.insert(29, std::to_string(id)); + std::cout << statement; + SQLite::Transaction transaction(database); + database.exec(statement); + transaction.commit(); +} + +// + +// used to generate random card codes that will be used to authenticate users. +// they represent contactless rf cards that users will use to authenticate +std::mt19937 mt(time(0)); +std::uniform_int_distribution dist(65, 127); + +string Customer::gen_cardcode() { + string code; + for (int i = 0; i < 20; i++) { + char letter = char(dist(mt)); + code += letter; + } + return code; +} diff --git a/data.cpp b/data.cpp new file mode 100644 index 0000000..f8d1fa3 --- /dev/null +++ b/data.cpp @@ -0,0 +1,14 @@ +#include "headers/data.h" + +namespace data { + +SQLite::Database start_db() { + SQLite::Database db("test.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); + + db.exec("create table if not exists Customer (id integer primary key, name text, card_code varchar(20), verhicle int)"); + db.exec("create table if not exists Park_spot (id integer primary key, taken boolean, customer_id int)"); + db.exec("create table if not exists Park_time (id integer primary key, customer_id int, spot_id int, start real, end real, duration real)"); + + return db; +} +} // namespace data \ No newline at end of file diff --git a/headers/Customer.h b/headers/Customer.h index 85d82be..99a6a08 100644 --- a/headers/Customer.h +++ b/headers/Customer.h @@ -1,15 +1,17 @@ #ifndef CUSTOMER_H #define CUSTOMER_H #pragma once - -#include -#include +#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" #include "Park_time.h" +#include +#include +#include +#include -using std::vector; using std::string; +using std::vector; -// enum type is basically een manier om categories te representen als een integer in the background, maar om t in code +// enum type is basically een manier om categories te representen als een integer in the background, maar om t in code // aan te geven als de actual category. enum class Verhicle_type { small = 1, @@ -22,16 +24,16 @@ db repr of Customer int id (not null, auto increment) string name (not nulll) string card_code (not null) -Dit moet nog verandert worden. +Dit moet nog verandert worden. -card code zou eigenlijk een randomly generated string moeten zijn, die je bv. op een ndf card zou opslaan en zo zou +card code zou eigenlijk een randomly generated string moeten zijn, die je bv. op een ndf card zou opslaan en zo zou authenticaten bij je parking spot. We kunnen dit ipv of samen met een password gebruiken. clock in en out creeert en compleet een park_time object. Voegt het toe aan een vector. */ class Customer { -public: + public: int id; string name; string card_code; @@ -39,13 +41,13 @@ public: void clock_out(int s_id); // void gen_weekly(); TODO: this void gen_monthly(); - Customer(int id_, string name_); + Customer(int id_, string name_, Verhicle_type verhicle_); + void update_db(SQLite::Database& database); -private: + private: Verhicle_type verhicle; vector park_instances; + string gen_cardcode(); }; - - #endif // CUSTOMER_H \ No newline at end of file diff --git a/headers/data.h b/headers/data.h new file mode 100644 index 0000000..5595cdf --- /dev/null +++ b/headers/data.h @@ -0,0 +1,6 @@ +#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" +#include "Customer.h" +namespace data { +SQLite::Database start_db(); + +} diff --git a/main.cpp b/main.cpp index 30b6468..1e89a51 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,5 @@ #include "headers/Park_spot.h" -#include "thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" +#include "headers/data.h" #include #include // to make pausing work, not sure if i need chrono, or this, or both #include @@ -30,18 +30,11 @@ a wait function where 1 sec represents 1 hour irl. using std::cout; int main() { - SQLite::Database db("test.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); - db.exec("create table if not exists Customer (id integer primary key, name text, card_code varchar(20), verhicle int)"); - db.exec("create table if not exists Park_spot (id integer primary key, taken boolean, customer_id int)"); - db.exec("create table if not exists Park_time (id integer primary key, customer_id int, spot_id int, start real, end real, duration real)"); - - SQLite::Transaction transaction(db); - db.exec("insert into Customer values (NULL, 'sagar ram', 'aqwsderfgtaqwsderfgt', 2)"); - transaction.commit(); + SQLite::Database db = data::start_db(); + // search example SQLite::Statement get_sagar(db, "select * from Customer where name like '%sagar%' "); - while (get_sagar.executeStep()) { int id = get_sagar.getColumn(0); string name = get_sagar.getColumn(1); @@ -49,4 +42,7 @@ int main() { int car = get_sagar.getColumn(3); cout << id << "," << name << "," << code << "," << car << std::endl; } + + Customer sagar{15, "Ramsaransing", Verhicle_type::medium}; + sagar.update_db(db); } diff --git a/test.db3 b/test.db3 index f4eddd502bb251d8c4412a283c501a32b67cf407..20d9c2c4295eb3a0fc6ec90d39e7fb61cf4d888a 100644 GIT binary patch delta 125 zcmZo@U~Fh$oFL7}J5k1&k#}Rl5`GCTzHSEo+x*-3C-Ud;yYnmY{pGvCx0$bdv!Xyg zpOzXwD}$;jV^Cslabi(oUU6n#dS$M2aY1r?xLZO_Y)C{KyaBrAyAoGU+B0RZThBlQ3P delta 51 zcmZo@U~Fh$oFL7}F;T{ukz-@R5`I2bes>1`+x*-3C-Ud;yKfd$Fy&`bW}W