diff --git a/CMakeLists.txt b/CMakeLists.txt index 62ade13..7f77bd2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,12 +13,12 @@ add_executable(park main.cpp data.cpp headers/data.h - #[[Customer.cpp + Customer.cpp headers/Customer.h Park_spot.cpp headers/Park_spot.h Park_time.cpp - headers/Park_time.h]] + headers/Park_time.h ) diff --git a/old/Customer.cpp b/Customer.cpp similarity index 75% rename from old/Customer.cpp rename to Customer.cpp index 67de740..b88e7df 100644 --- a/old/Customer.cpp +++ b/Customer.cpp @@ -2,12 +2,12 @@ #include // constructors -Customer::Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db): +Customer::Customer(string name_, Verhicle_type verhicle_): name{name_}, verhicle{verhicle_}, card_code{gen_cardcode()} { - id = auto_increment_db(db) + 1; - save_db(db); + id = auto_increment_db() + 1; + save_db(); } Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, vector instances) @@ -47,38 +47,38 @@ void Customer::gen_monthly() { //================================================================================================ // functions that interact with the database -void Customer::save_db(SQLite::Database& database) { +void Customer::save_db() { 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, "null"); - SQLite::Transaction transaction(database); - database.exec(statement); + SQLite::Transaction transaction(data::db); + data::db.exec(statement); transaction.commit(); } -void Customer::update_db(SQLite::Database& database) { - string statement = "UPDATE Customer SET name = \"\", card_code = \"\" where id = '';"; +void Customer::update_db() { + 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); + data::db.exec(statement); } -void Customer::delete_db(SQLite::Database& database) { +void Customer::delete_db() { 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); + SQLite::Transaction transaction(data::db); + data::db.exec(statement); transaction.commit(); } -int Customer::auto_increment_db(SQLite::Database& database) { - SQLite::Statement max_id(database, "select max(id) from Customer;"); +int Customer::auto_increment_db() { + SQLite::Statement max_id(data::db, "select max(id) from Customer;"); int id = 0; max_id.executeStep(); id = max_id.getColumn(0); diff --git a/old/Park_spot.cpp b/Park_spot.cpp similarity index 100% rename from old/Park_spot.cpp rename to Park_spot.cpp diff --git a/old/Park_time.cpp b/Park_time.cpp similarity index 51% rename from old/Park_time.cpp rename to Park_time.cpp index 26f319b..e5cd542 100644 --- a/old/Park_time.cpp +++ b/Park_time.cpp @@ -8,7 +8,9 @@ Park_time::Park_time(int c_id, int s_id) , spot_id { s_id } , duration { 0 } , start { high_resolution_clock::now() } + , id {auto_increment_db() + 1} { + save_db(); } void Park_time::clock_out(int c_id, int s_id) @@ -26,6 +28,7 @@ void Park_time::clock_out(int c_id, int s_id) if (!duration) { end = high_resolution_clock::now(); duration = duration_cast(end - start).count(); // use mins later + update_db(); } else { std::cout << "Already clocked out. Something is wrong \n"; @@ -44,13 +47,47 @@ std::ostream& operator<<(std::ostream& os, const Park_time & pt){ return os; } -void Park_time::debug() { + +int Park_time::start_to_int(){ auto start_to_epoch = start.time_since_epoch(); auto start_value = std::chrono::duration_cast(start_to_epoch); int start_seconds = start_value.count(); + return start_seconds; +} - auto end_to_epoch = end.time_since_epoch(); - auto end_value = std::chrono::duration_cast(start_to_epoch); - int end_seconds = end_value.count(); - std::cout << "<" << start_seconds << "-" << end_seconds << ">" ; +// db funcs ----------------------------------------------------------------------------- + + + +void Park_time::save_db() { + string statement{"insert into Park_time values ( , , , , , );"}; + statement.insert(41, "NULL"); + statement.insert(39, "NULL"); + statement.insert(37, to_string(start_to_int()) ); + statement.insert(35, to_string(spot_id)); + statement.insert(33, to_string(customer_id)); + statement.insert(31, to_string(id)); + SQLite::Transaction transaction(data::db); + data::db.exec(statement); + transaction.commit(); +} + +void Park_time::update_db() { + string statement = "UPDATE Park_time SET end = , duration = where id = '';"; + statement.insert(53, std::to_string(id)); + statement.insert(40, to_string(duration)); + statement.insert(27, to_string(start_to_int() + duration)); + std::cout << statement; // TODO: set some logging here + data::db.exec(statement); +} + + +// to get id on first save to db +int Park_time::auto_increment_db() { + SQLite::Statement max_id(data::db, "select max(id) from Park_time;"); + int id = 0; + max_id.executeStep(); + id = max_id.getColumn(0); + max_id.reset(); + return id; } \ No newline at end of file diff --git a/data.cpp b/data.cpp index f8d1fa3..42e09a7 100644 --- a/data.cpp +++ b/data.cpp @@ -7,7 +7,7 @@ SQLite::Database start_db() { 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)"); + db.exec("create table if not exists Park_time (id integer primary key, customer_id int, spot_id int, start int, end int, duration int)"); return db; } diff --git a/old/headers/Customer.h b/headers/Customer.h similarity index 83% rename from old/headers/Customer.h rename to headers/Customer.h index f235ed9..c2a112a 100644 --- a/old/headers/Customer.h +++ b/headers/Customer.h @@ -7,6 +7,7 @@ #include #include #include +#include "data.h" using std::string; using std::vector; @@ -28,7 +29,7 @@ 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(string name_, Verhicle_type verhicle_); 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 @@ -39,8 +40,8 @@ class Customer { 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 update_db(); + void delete_db(); // void gen_weekly(); TODO: this void gen_monthly(); @@ -51,8 +52,8 @@ class Customer { string gen_cardcode(); - void save_db(SQLite::Database& database); - int auto_increment_db(SQLite::Database& database); + void save_db(); + int auto_increment_db(); }; #endif // CUSTOMER_H \ No newline at end of file diff --git a/old/headers/Park_spot.h b/headers/Park_spot.h similarity index 100% rename from old/headers/Park_spot.h rename to headers/Park_spot.h diff --git a/old/headers/Park_time.h b/headers/Park_time.h similarity index 78% rename from old/headers/Park_time.h rename to headers/Park_time.h index a98d9c8..130e144 100644 --- a/old/headers/Park_time.h +++ b/headers/Park_time.h @@ -4,10 +4,13 @@ #include #include +#include +#include "data.h" using namespace std::chrono; - +using std::string; +using std::to_string; /* db repr of Park_time int id (not null, auto increment) @@ -28,14 +31,18 @@ public: int spot_id; int duration; Park_time(int c_id, int s_id); + // Park_time(int c_id, int s_id ); void clock_out(int c_id, int s_id); friend std::ostream& operator<<(std::ostream& os, const Park_time & pt); - void debug(); private: high_resolution_clock::time_point start; high_resolution_clock::time_point end; - //TODO: discuss pros cons of using chrono, ctime, or 3th party lib + void save_db(); + void update_db(); + int auto_increment_db(); // helper + int start_to_int(); // helper + }; diff --git a/headers/data.h b/headers/data.h index 1f7aff5..e28404b 100644 --- a/headers/data.h +++ b/headers/data.h @@ -6,7 +6,6 @@ namespace data { SQLite::Database start_db(); static SQLite::Database db = start_db(); - } #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 6df10b8..4a5b372 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include "thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" #include "headers/data.h" +#include "headers/Park_spot.h" #include #include #include @@ -23,12 +24,12 @@ De client clockt in en uit bij een spot. using std::cout; int main() { - SQLite::Statement test(data::db, "SELECT * FROM Customer WHERE id > 2"); - while (test.executeStep()){ - int id = test.getColumn(0); - std::string name = test.getColumn(1); - cout << id << ", " << name << std::endl; + class Customer sagar{"nonsense", Verhicle_type::medium}; + sagar.update_db(); - } + Park_spot p1{1}; + p1.clock(&sagar); + p1.clock(&sagar); + sagar.gen_monthly(); } diff --git a/test.db3 b/test.db3 index 10a2d99..c44648e 100644 Binary files a/test.db3 and b/test.db3 differ