From 77919e4ce035ad9f46e46a5fb8093cdb59c0b298 Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Wed, 26 Jun 2019 23:32:06 -0300 Subject: [PATCH] Customer almost done. need to make ctor that can make Customer from db --- .clang-format | 6 ++++- Customer.cpp | 64 ++++++++++++++++++++++++++++++++++++++------- headers/Customer.h | 23 +++++++++------- main.cpp | 15 +++-------- test.db3 | Bin 16384 -> 16384 bytes 5 files changed, 76 insertions(+), 32 deletions(-) diff --git a/.clang-format b/.clang-format index ca8e432..182267a 100644 --- a/.clang-format +++ b/.clang-format @@ -5,4 +5,8 @@ IndentWidth: 4 Language: Cpp PointerAlignment: Left -ColumnLimit: 160 \ No newline at end of file +ColumnLimit: 120 +AllowAllArgumentsOnNextLine: true +BreakConstructorInitializersStyle : BCIS_AfterColon +ConstructorInitializerAllOnOneLineOrOnePerLine: true +Cpp11BracedListStyle: true diff --git a/Customer.cpp b/Customer.cpp index aa1d49a..fdd1408 100644 --- a/Customer.cpp +++ b/Customer.cpp @@ -1,9 +1,27 @@ #include "headers/Customer.h" #include -// moet aangepast worden om een verhicle_type toe te voegen -Customer::Customer(int id_, string name_, Verhicle_type verhicle_) : id{id_}, name{name_}, verhicle{verhicle_}, card_code{gen_cardcode()} {} +// constructors +Customer::Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db): + name{name_}, verhicle{verhicle_}, + card_code{gen_cardcode()} +{ + id = auto_increment_db(db) + 1; + save_db(db); +} +Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, vector instances) + : name{name_}, + card_code{card_code_}, + verhicle{verhicle_}, + park_instances{instances} +{ + +} + + + +// clock methods ==================================================================================== /* creert een park_time object met start time= nu, en voegt t toe aan een vector. */ @@ -15,7 +33,7 @@ void Customer::clock_in(int s_id) { // 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); } -// monthly report generation. moet nog een manier vinden om af te bakenen. +// report gen void Customer::gen_monthly() { std::cout << "NAME: " << name << " card code: " << card_code << "\n"; std::cout << "-------------------------------------------------\n"; @@ -26,23 +44,49 @@ void Customer::gen_monthly() { std::cout << "-------------------------------------------------\n\n"; } -void Customer::update_db(SQLite::Database& database) { - string statement{"insert into Customer values (, '', '', )"}; +//================================================================================================ +// functions that interact with the database + +void Customer::save_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; + statement.insert(29, "null"); SQLite::Transaction transaction(database); database.exec(statement); transaction.commit(); } -// +void Customer::update_db(SQLite::Database& database) { + string statement = "UPDATE Customer SET name = \"\", card_code = \"\" where id = '';"; + statement.insert(58, std::to_string(id)); + statement.insert(44, card_code); + statement.insert(28, name); + // std::cout << statement; TODO: set some logging here + database.exec(statement); +} -// used to generate random card codes that will be used to authenticate users. -// they represent contactless rf cards that users will use to authenticate +void Customer::delete_db(SQLite::Database& database) { + string statement = "delete from Customer where id= ;"; + statement.insert(statement.length() - 2, std::to_string(id)); + // std::cout << statement; + SQLite::Transaction transaction(database); + database.exec(statement); + transaction.commit(); +} + +int Customer::auto_increment_db(SQLite::Database& database) { + SQLite::Statement max_id(database, "select max(id) from Customer;"); + int id = 0; + max_id.executeStep(); + id = max_id.getColumn(0); + max_id.reset(); + return id; +} + +// random helpers============================================================= std::mt19937 mt(time(0)); std::uniform_int_distribution dist(65, 127); diff --git a/headers/Customer.h b/headers/Customer.h index 99a6a08..f235ed9 100644 --- a/headers/Customer.h +++ b/headers/Customer.h @@ -20,13 +20,7 @@ enum class Verhicle_type { }; /* -db repr of Customer -int id (not null, auto increment) -string name (not nulll) -string card_code (not null) -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 is een randomly generated string moeten zijn, die je bv. op een nfc 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. @@ -34,20 +28,31 @@ clock in en out creeert en compleet een park_time object. Voegt het toe aan een class Customer { public: + Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db); + Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, + vector instances); // needed to construct from db + // potentially: add a destructor that calls update_db() before being destroyed int id; string name; string card_code; + void clock_in(int s_id); void clock_out(int s_id); + + void update_db(SQLite::Database& database); + void delete_db(SQLite::Database& database); + // void gen_weekly(); TODO: this void gen_monthly(); - Customer(int id_, string name_, Verhicle_type verhicle_); - void update_db(SQLite::Database& database); private: Verhicle_type verhicle; vector park_instances; + string gen_cardcode(); + + void save_db(SQLite::Database& database); + int auto_increment_db(SQLite::Database& database); }; #endif // CUSTOMER_H \ No newline at end of file diff --git a/main.cpp b/main.cpp index 1e89a51..2959e4c 100644 --- a/main.cpp +++ b/main.cpp @@ -32,17 +32,8 @@ using std::cout; int main() { 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); - string code = get_sagar.getColumn(2); - int car = get_sagar.getColumn(3); - cout << id << "," << name << "," << code << "," << car << std::endl; - } - - Customer sagar{15, "Ramsaransing", Verhicle_type::medium}; + // see implementation of update_db, save_db and delete_db of customer to see how queries and statements work + Customer sagar{"Ramsaransing", Verhicle_type::medium, db}; sagar.update_db(db); + sagar.delete_db(db); } diff --git a/test.db3 b/test.db3 index 20d9c2c4295eb3a0fc6ec90d39e7fb61cf4d888a..dc45c7ae446e4c382034c1a7009018a1e5793301 100644 GIT binary patch literal 16384 zcmeI%KaAVf9S3mf?6VsK?zIV8qYVNJ30(MEqbS^F$y7-s^^f{Pi4rMF6!paa|H(&6 z6!jNQhXUE!F`d#eTQhXXn5_l61nAIo>zJ(>Gez%g*Yyk1dTf)&gTP1fqu+b{^2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