From a6061196267af211a6ab29f1f81404a67736a0ad Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sat, 29 Jun 2019 19:59:48 -0300 Subject: [PATCH 1/7] WIP refractoring --- CMakeLists.txt | 12 +++++++++++- headers/data.h | 8 +++++++- main.cpp | 24 +++++++++--------------- Customer.cpp => old/Customer.cpp | 0 Park_spot.cpp => old/Park_spot.cpp | 0 Park_time.cpp => old/Park_time.cpp | 0 {headers => old/headers}/Customer.h | 0 {headers => old/headers}/Park_spot.h | 0 {headers => old/headers}/Park_time.h | 0 9 files changed, 27 insertions(+), 17 deletions(-) rename Customer.cpp => old/Customer.cpp (100%) rename Park_spot.cpp => old/Park_spot.cpp (100%) rename Park_time.cpp => old/Park_time.cpp (100%) rename {headers => old/headers}/Customer.h (100%) rename {headers => old/headers}/Park_spot.h (100%) rename {headers => old/headers}/Park_time.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index c278d3c..62ade13 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,17 @@ include_directories( ) -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) +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]] + ) diff --git a/headers/data.h b/headers/data.h index 5595cdf..1f7aff5 100644 --- a/headers/data.h +++ b/headers/data.h @@ -1,6 +1,12 @@ +#ifndef DATA_H +#define DATA_H +#pragma once #include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" -#include "Customer.h" 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 cc0b796..6df10b8 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,8 @@ -#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 +#include /* Code strucure like this: @@ -19,22 +19,16 @@ record die zegt dat een customer voor x tijd geparkeert heeft bij spot x, enz. De client clockt in en uit bij een spot. */ -void Wait(int sec) -/* -a wait function where 1 sec represents 1 hour irl. - */ -{ - std::this_thread::sleep_for(seconds{sec}); -} 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; + + } - SQLite::Database db = data::start_db(); - // see implementation of update_db, save_db and delete_db of customer to see how queries and statements work - Customer sagar{"Sagar Winston Ramsaransing", Verhicle_type::medium, db}; - sagar.update_db(db); - cout << "THIS WRKS"; - // sagar.delete_db(db); } diff --git a/Customer.cpp b/old/Customer.cpp similarity index 100% rename from Customer.cpp rename to old/Customer.cpp diff --git a/Park_spot.cpp b/old/Park_spot.cpp similarity index 100% rename from Park_spot.cpp rename to old/Park_spot.cpp diff --git a/Park_time.cpp b/old/Park_time.cpp similarity index 100% rename from Park_time.cpp rename to old/Park_time.cpp diff --git a/headers/Customer.h b/old/headers/Customer.h similarity index 100% rename from headers/Customer.h rename to old/headers/Customer.h diff --git a/headers/Park_spot.h b/old/headers/Park_spot.h similarity index 100% rename from headers/Park_spot.h rename to old/headers/Park_spot.h diff --git a/headers/Park_time.h b/old/headers/Park_time.h similarity index 100% rename from headers/Park_time.h rename to old/headers/Park_time.h From 0eee67d3d94dd4bdc77a9d90268b62b403bd3312 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sat, 29 Jun 2019 21:50:36 -0300 Subject: [PATCH 2/7] park time can be saved to db now --- CMakeLists.txt | 4 +-- old/Customer.cpp => Customer.cpp | 28 ++++++++-------- old/Park_spot.cpp => Park_spot.cpp | 0 old/Park_time.cpp => Park_time.cpp | 47 ++++++++++++++++++++++++--- data.cpp | 2 +- {old/headers => headers}/Customer.h | 11 ++++--- {old/headers => headers}/Park_spot.h | 0 {old/headers => headers}/Park_time.h | 13 ++++++-- headers/data.h | 1 - main.cpp | 13 ++++---- test.db3 | Bin 16384 -> 16384 bytes 11 files changed, 82 insertions(+), 37 deletions(-) rename old/Customer.cpp => Customer.cpp (75%) rename old/Park_spot.cpp => Park_spot.cpp (100%) rename old/Park_time.cpp => Park_time.cpp (51%) rename {old/headers => headers}/Customer.h (83%) rename {old/headers => headers}/Park_spot.h (100%) rename {old/headers => headers}/Park_time.h (78%) 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 10a2d99dfe155a1442be647bb5adf13bdfde3cb1..c44648e656df65c1ee69b8f02d9ce8fe8d1c3da0 100644 GIT binary patch delta 462 zcmZo@U~Fh$oFL68Fj2-?Sb#y#M1hxqfq|K?iGjbFUzo3Hv!H?iA6FwEGrPFFJY!?_ zh;5*vz|$XcJQvb^Mw;DkV* z_`rZV@7m-7CM8xBHO{#)eqKTTZef0jLB*l9k&#uQ*-T0-C~8t`J(B8!Dr!soL{Z}%pX8E|=p0v`wg|5D32vpf@LQ+ PBRc~pBM>k#v#sZc1)j^C@t?~~=qvG8oY_+-a_S1Xpez z3g^E*f91Hs3r+whfD^z8-~@02I02jhP5>u>6Tk_4`x1Egy(?$8Zr?6Gj8kRq={*Iw zQfm883sZuYkPd>iON0x=7N{*Mam1+$|+h zm_Dtf3P?*pSG>C=JkYF2^eZ#_)FAoWB{^}v6imL~u zc^G=S5`1Zd7n|bQt=C>FK5Sg}SA9JGC+m~vdV8|k`&;NA(+jH-nJOA;`&#oT+XFj8E-%Wwf z7$@+5p1_UY7w)|Ipx9UZL;*^W*nw$!R-m_afr_VNcN6XyDPElv3fKSo=Bv-S#IjRg z3E-{|ktUYD1j@7}4mC50tYO^=x-4|RLtJgdD@$<+SY39O9Mz#G{2Dq3ag&BV$oyJm z*3;baHX+tP5TL6dZZueQm!jo&_7V$biX0974s<_4+(KMFOqfIW7sO>ud*CYvMa(YSTO-btn7&x6_Js+k%b5$dD^`T<^%3VfJ(o8UL*C3tPM=D3_FzEwQ^bXK z9?pH%54%-MjJQnHDHFOA#0iWsG4?6d5Q)=Uzc{Lb#iJof9Jnh5lcJ?mfytx~B@_-5? zA*;5VyTrfk_ghWz255R?cy--_sA@y!G7FnCi!8Te#yrq=A)}$w@cas=T%C z=ks>8=9Zz$5hqB^jWekn7%mJ}vDbCYwhSGExaFccuW4#R8T`mGnA$9I;0c~yAWn0H zi5YH~HRm;@>PWR40-W$pi-=Q_I2*ewMbr0AT^7V%JA;2nSO11MaZl>C5j9C2UeElx zKlTh8x<4Y$CtA`d6{0TVE#l>HFX}))x$*JIr--X43n>sctD5Na8r0D2)ssv20pe&< z)`wYBTTwX|&)mS*h6HTy9mMq`E02!I(HV4qXjJVx=yP~bZoTc3+H|wye!Z=4){2XzS+wXC< zwcYDrx?X^|mRoD+K0;ijwkBJ3{!k@@jzxoH6ihVe-bI`N>S~)zcI}Ot@p{AJ_^=Kg zKpd!33U4p^S)F1!f>ntc0|mM!;%r0P(WJeRDbeE^O;GFAF30ye;wnkGXA-gFk#v7~ zP-BwG7svJrUmy;&7Ro52_@>dW2pe+AA9@^gpCB%owsdn}*?N6-xVDVWmL43SdmnKf z#;^28n}%7LRf4@Y6FPeU-P?#`bho^y1Oez7+s%0&>K%?%H#~*mPN2V1l{apa5~a zQ;NTN<@?3snx6ifym~7-xNDyPCJXXdM1Kpnym;2Qu zQl$|kdm_j8ryA?Q2ZzFYw;vP}#Z*8ED83SBiKYX?OrMq@+CGc!pPd%U4~p#NK_fdj zPNYHU*~e3xuW*MQIgwkleqE5IN>-y!?qBDWCk{^t5pM z)u#i57Xv=gzH#-q{p$w)#+4UACoh7EFM#0t|M5qEyx;_I0yqJj08RiWfD^z8-~@02 yI02jhP5>wH4Nd^R|9^uoEu>6Tk^PPT=1iLY|la From c058783a1154e0fb7423983ec8a30bc9579ce6f3 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sat, 29 Jun 2019 22:30:47 -0300 Subject: [PATCH 3/7] ALL objects can be saved to db now --- Customer.cpp | 2 +- Park_spot.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++-- Park_time.cpp | 1 - headers/Park_spot.h | 6 ++++- main.cpp | 21 +++++++++++++---- test.db3 | Bin 16384 -> 16384 bytes 6 files changed, 76 insertions(+), 10 deletions(-) diff --git a/Customer.cpp b/Customer.cpp index b88e7df..28afbf8 100644 --- a/Customer.cpp +++ b/Customer.cpp @@ -53,7 +53,7 @@ void Customer::save_db() { statement.insert(38, std::to_string(int(verhicle))); statement.insert(36, card_code); statement.insert(32, name); - statement.insert(29, "null"); + statement.insert(29, to_string(id)); SQLite::Transaction transaction(data::db); data::db.exec(statement); transaction.commit(); diff --git a/Park_spot.cpp b/Park_spot.cpp index 929a7d4..55243f2 100644 --- a/Park_spot.cpp +++ b/Park_spot.cpp @@ -1,9 +1,10 @@ #include "headers/Park_spot.h" -Park_spot::Park_spot(int id_){ +Park_spot::Park_spot(){ parked = nullptr; - id = id_; + id = auto_increment_db() + 1; taken = false; + save_db(); } // clock in en out, calls de juist(in/out) van de customer aan de hand van internal state van taken @@ -12,10 +13,61 @@ void Park_spot::clock(Customer* c_customer){ parked = c_customer; taken = true; parked->clock_in(id); + update_db(); } else{ taken = false; parked->clock_out(id); parked = nullptr; + update_db(); } +} + + +// --------------------- db functs + +void Park_spot::update_db() { + string statement = "UPDATE Park_spot SET taken = '', customer_id = '' where id = '';"; + // 29, 48, 62 + statement.insert(63, to_string(id)); + if (taken){ + statement.insert(49, to_string(parked->id)); + statement.insert(30, "true"); + } + else { + statement.insert(49, "NULL"); + statement.insert(30, "false"); + } + data::db.exec(statement); +} + + +void Park_spot::save_db() { + //(int id, bool taken, int customer_id) + string statement{"insert into Park_spot values ( , , );"}; + // after ( = 28) + statement.insert(34, "NULL"); + statement.insert(32, "false"); + statement.insert(30, to_string(id)); + SQLite::Transaction transaction(data::db); + data::db.exec(statement); + transaction.commit(); +} + +void Park_spot::delete_db() { + string statement = "delete from Park_spot where id= ;"; + statement.insert(statement.length() - 2, std::to_string(id)); + // std::cout << statement; + SQLite::Transaction transaction(data::db); + data::db.exec(statement); + transaction.commit(); +} + +int Park_spot::auto_increment_db() { + SQLite::Statement max_id(data::db, "select max(id) from Park_spot;"); + 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/Park_time.cpp b/Park_time.cpp index e5cd542..84069fb 100644 --- a/Park_time.cpp +++ b/Park_time.cpp @@ -77,7 +77,6 @@ void Park_time::update_db() { 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); } diff --git a/headers/Park_spot.h b/headers/Park_spot.h index 4085152..86b9da0 100644 --- a/headers/Park_spot.h +++ b/headers/Park_spot.h @@ -15,7 +15,11 @@ class Park_spot { int id; bool taken; Customer* parked; //TODO: think about memory management - Park_spot(int id_); + Park_spot(); void clock(Customer* c_customer); private: + void save_db(); + void update_db(); + void delete_db(); + int auto_increment_db(); }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4a5b372..f83c0ba 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,8 @@ #include #include +#include + /* Code strucure like this: class declarations zijn in /headers/class_naam.h, en definitions van de member @@ -20,16 +22,25 @@ record die zegt dat een customer voor x tijd geparkeert heeft bij spot x, enz. De client clockt in en uit bij een spot. */ +void Wait(int sec) +/* +a wait function where 1 sec represents 1 hour irl. + */ +{ + std::this_thread::sleep_for(seconds{sec}); +} + + using std::cout; int main() { - class Customer sagar{"nonsense", Verhicle_type::medium}; + class Customer sagar{"Sagar Ramsaransing", Verhicle_type::medium}; sagar.update_db(); + Park_spot p1; + p1.clock(&sagar); + Wait(2); + p1.clock(&sagar); - Park_spot p1{1}; - p1.clock(&sagar); - p1.clock(&sagar); - sagar.gen_monthly(); } diff --git a/test.db3 b/test.db3 index c44648e656df65c1ee69b8f02d9ce8fe8d1c3da0..649423a0e0cade26f7a187d6d955b49f7faf155b 100644 GIT binary patch delta 143 zcmZo@U~Fh$oFL7}IZ?)$k#l3h5`H#D{!0w}mo^J3oZz?6VPs`6G-V7(sVw#?^{h;Z3QF?GWa4FDU|{6`$iV**sQWp;BrhWigSco~ oVoq_YU#O1{MEV^A|2v@c6Mi89Ms@~HP8Jr%SP6kCKs=2J0Fx;uSpWb4 delta 410 zcmZo@U~Fh$oFL68Fj2;tQD9@j5`H0eeijD)JN!HNr|=i>`|xYBDE%A?w^GeDn^9}Z_ za$!(2mBK!ND1)*d93`~8Tdc* zKjpv9f0BPYFp!q=dkV0!Gcab%7 From 97ed3150ecfdac94e5c42b20914c448e751c27a4 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sat, 29 Jun 2019 22:48:49 -0300 Subject: [PATCH 4/7] can save to db --- Customer.cpp | 11 +++++++---- Park_spot.cpp | 6 ++++++ headers/Customer.h | 14 ++++++-------- headers/Park_time.h | 16 +++++----------- main.cpp | 2 +- test.db3 | Bin 16384 -> 16384 bytes 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/Customer.cpp b/Customer.cpp index 28afbf8..db6bcd2 100644 --- a/Customer.cpp +++ b/Customer.cpp @@ -10,6 +10,7 @@ Customer::Customer(string name_, Verhicle_type verhicle_): save_db(); } + Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, vector instances) : name{name_}, card_code{card_code_}, @@ -20,19 +21,20 @@ Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhi } - -// clock methods ==================================================================================== +// clock in/out methods ==================================================================================== /* -creert een park_time object met start time= nu, en voegt t toe aan een vector. +Create a p_time object with start=now and adds to vector */ 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. +// edit last p_time object so end=now void Customer::clock_out(int s_id) { park_instances[park_instances.size() - 1].clock_out(id, s_id); } + + // report gen void Customer::gen_monthly() { std::cout << "NAME: " << name << " card code: " << card_code << "\n"; @@ -44,6 +46,7 @@ void Customer::gen_monthly() { std::cout << "-------------------------------------------------\n\n"; } + //================================================================================================ // functions that interact with the database diff --git a/Park_spot.cpp b/Park_spot.cpp index 55243f2..d17fbb4 100644 --- a/Park_spot.cpp +++ b/Park_spot.cpp @@ -1,5 +1,9 @@ #include "headers/Park_spot.h" + + +// constructors + Park_spot::Park_spot(){ parked = nullptr; id = auto_increment_db() + 1; @@ -7,6 +11,8 @@ Park_spot::Park_spot(){ save_db(); } + + // clock in en out, calls de juist(in/out) van de customer aan de hand van internal state van taken void Park_spot::clock(Customer* c_customer){ if (!taken){ diff --git a/headers/Customer.h b/headers/Customer.h index c2a112a..ecfd5d8 100644 --- a/headers/Customer.h +++ b/headers/Customer.h @@ -12,12 +12,12 @@ 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 -// aan te geven als de actual category. +// will make it easy to represent it in the database while making it easy to use while programming enum class Verhicle_type { - small = 1, - medium = 2, - large = 3, + bike = 1, + small_car = 2, + suv = 3, + pickup = 4 }; /* @@ -43,15 +43,13 @@ class Customer { void update_db(); void delete_db(); - // void gen_weekly(); TODO: this - void gen_monthly(); + void gen_monthly(); // remove, make it a function in data private: Verhicle_type verhicle; vector park_instances; string gen_cardcode(); - void save_db(); int auto_increment_db(); }; diff --git a/headers/Park_time.h b/headers/Park_time.h index 130e144..8093a13 100644 --- a/headers/Park_time.h +++ b/headers/Park_time.h @@ -12,26 +12,20 @@ using namespace std::chrono; using std::string; using std::to_string; /* -db repr of Park_time -int id (not null, auto increment) -int customer_id (not null) (many to one or something like that) -int spot_id (not null, many to one or something like that) -int duration -datetime start (not null) -datetime end -Dit is gewoon een record van hoe lang, wie en waar iemand parkeert. Basically, een component van -de internal state van customer. + +Record of who parked at what park_spot and at what time. */ class Park_time { public: + Park_time(int c_id, int s_id); + // Park_time(int c_id, int s_id ); int id; int customer_id; 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); diff --git a/main.cpp b/main.cpp index f83c0ba..ffd73db 100644 --- a/main.cpp +++ b/main.cpp @@ -35,7 +35,7 @@ a wait function where 1 sec represents 1 hour irl. using std::cout; int main() { - class Customer sagar{"Sagar Ramsaransing", Verhicle_type::medium}; + class Customer sagar{"Sagar Ramsaransing", Verhicle_type::bike}; sagar.update_db(); Park_spot p1; p1.clock(&sagar); diff --git a/test.db3 b/test.db3 index 649423a0e0cade26f7a187d6d955b49f7faf155b..47a4ff193bbe21a9484f1da7018abce97b4d9f2a 100644 GIT binary patch delta 256 zcmZo@U~Fh$oFL68I#I@%QFLR%5`Hdb{t5>EOZ-duD>e%XWbsF7GqW-nnsNpwrY9CD z1SRGcCl)2<6=&w9R~0))*El7{d**u-I0Y5DWjICVX*1za98{6+T~p^3SnBTQ6YrHB zALHtiJNc5l5zxBJ4E!JYAMsz_EU0jlUyhfVg+W|2EitD!)i2b?hnES3JNcu&Fi^`e z2L5;axA>0%UATkaN{E@Afsv7gg^`&#Rzl<`5Fcj}VuFe>F~v#<{sH2DOahGT44j-m Nb+HlxQ-F9H69E4JMREWD delta 91 zcmZo@U~Fh$oFL7}IZ?)$k#l3h5`H#D{!0w}mo^J39O9oCz&81kyb)07BLn|Oh|mPp j$shHFfkN*X_}@W=l!OEr*%>%FSy&ijB?P7b@iZm?@hln5 From 085cd5af0832a4fea04fc7f1eb164f9d7c73a795 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sat, 29 Jun 2019 23:22:58 -0300 Subject: [PATCH 5/7] heavily simplified imports and using directives --- Customer.cpp | 23 ++++++++++++----------- Park_spot.cpp | 15 +++++++++------ Park_time.cpp | 16 +++++++++------- headers/Customer.h | 17 ++++++++--------- headers/Park_spot.h | 3 ++- headers/Park_time.h | 3 +++ main.cpp | 10 +--------- test.db3 | Bin 16384 -> 16384 bytes 8 files changed, 44 insertions(+), 43 deletions(-) diff --git a/Customer.cpp b/Customer.cpp index db6bcd2..2f07331 100644 --- a/Customer.cpp +++ b/Customer.cpp @@ -1,5 +1,4 @@ #include "headers/Customer.h" -#include // constructors Customer::Customer(string name_, Verhicle_type verhicle_): @@ -20,6 +19,10 @@ Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhi } +Customer::~Customer(){ + update_db(); +} + // clock in/out methods ==================================================================================== /* @@ -37,13 +40,13 @@ void Customer::clock_out(int s_id) { park_instances[park_instances.size() - 1].c // report gen void Customer::gen_monthly() { - std::cout << "NAME: " << name << " card code: " << card_code << "\n"; - std::cout << "-------------------------------------------------\n"; + cout << "NAME: " << name << " card code: " << card_code << "\n"; + cout << "-------------------------------------------------\n"; for (auto& i : park_instances) { - // TODO: need some logic to only include from this month - std::cout << i; + // TODO: need some logic to only include from this month. scratch that, need to remove gen monthly + cout << i; } - std::cout << "-------------------------------------------------\n\n"; + cout << "-------------------------------------------------\n\n"; } @@ -53,7 +56,7 @@ void Customer::gen_monthly() { void Customer::save_db() { string statement{"insert into Customer values (, '', '', );"}; // after ( = 28) - statement.insert(38, std::to_string(int(verhicle))); + statement.insert(38, to_string(int(verhicle))); statement.insert(36, card_code); statement.insert(32, name); statement.insert(29, to_string(id)); @@ -64,17 +67,15 @@ void Customer::save_db() { void Customer::update_db() { string statement = "UPDATE Customer SET name = '', card_code = '' where id = '';"; - statement.insert(58, std::to_string(id)); + statement.insert(58, to_string(id)); statement.insert(44, card_code); statement.insert(28, name); - // std::cout << statement; TODO: set some logging here data::db.exec(statement); } void Customer::delete_db() { string statement = "delete from Customer where id= ;"; - statement.insert(statement.length() - 2, std::to_string(id)); - // std::cout << statement; + statement.insert(statement.length() - 2, to_string(id)); SQLite::Transaction transaction(data::db); data::db.exec(statement); transaction.commit(); diff --git a/Park_spot.cpp b/Park_spot.cpp index d17fbb4..29ab878 100644 --- a/Park_spot.cpp +++ b/Park_spot.cpp @@ -4,13 +4,17 @@ // constructors -Park_spot::Park_spot(){ - parked = nullptr; - id = auto_increment_db() + 1; - taken = false; +Park_spot::Park_spot() + : parked{nullptr}, + id{auto_increment_db()+1}, + taken{false} { save_db(); } +Park_spot::~Park_spot(){ + update_db(); +} + // clock in en out, calls de juist(in/out) van de customer aan de hand van internal state van taken @@ -62,8 +66,7 @@ void Park_spot::save_db() { void Park_spot::delete_db() { string statement = "delete from Park_spot where id= ;"; - statement.insert(statement.length() - 2, std::to_string(id)); - // std::cout << statement; + statement.insert(statement.length() - 2, to_string(id)); SQLite::Transaction transaction(data::db); data::db.exec(statement); transaction.commit(); diff --git a/Park_time.cpp b/Park_time.cpp index 84069fb..b93c0ef 100644 --- a/Park_time.cpp +++ b/Park_time.cpp @@ -1,6 +1,4 @@ #include"headers/Park_time.h" -#include -#include Park_time::Park_time(int c_id, int s_id) @@ -13,15 +11,19 @@ Park_time::Park_time(int c_id, int s_id) save_db(); } +Park_time::~Park_time(){ + update_db(); +} + void Park_time::clock_out(int c_id, int s_id) { if (c_id != customer_id) { - std::cout << "wrong customer id, you are at the wrong location"; + cout << "wrong customer id, you are at the wrong location"; return; } if (s_id != spot_id) { - std::cout << "Wrong spot id, you're at the wrong location"; + cout << "Wrong spot id, you're at the wrong location"; return; } @@ -31,7 +33,7 @@ void Park_time::clock_out(int c_id, int s_id) update_db(); } else { - std::cout << "Already clocked out. Something is wrong \n"; + cout << "Already clocked out. Something is wrong \n"; } } @@ -50,7 +52,7 @@ std::ostream& operator<<(std::ostream& os, const Park_time & pt){ int Park_time::start_to_int(){ auto start_to_epoch = start.time_since_epoch(); - auto start_value = std::chrono::duration_cast(start_to_epoch); + auto start_value = duration_cast(start_to_epoch); int start_seconds = start_value.count(); return start_seconds; } @@ -74,7 +76,7 @@ void Park_time::save_db() { void Park_time::update_db() { string statement = "UPDATE Park_time SET end = , duration = where id = '';"; - statement.insert(53, std::to_string(id)); + statement.insert(53, to_string(id)); statement.insert(40, to_string(duration)); statement.insert(27, to_string(start_to_int() + duration)); data::db.exec(statement); diff --git a/headers/Customer.h b/headers/Customer.h index ecfd5d8..cb1c7f7 100644 --- a/headers/Customer.h +++ b/headers/Customer.h @@ -1,15 +1,14 @@ #ifndef CUSTOMER_H #define CUSTOMER_H #pragma once -#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" -#include "Park_time.h" -#include -#include -#include -#include -#include "data.h" -using std::string; + +#include "Park_time.h" +#include "data.h" +#include +#include + + using std::vector; // will make it easy to represent it in the database while making it easy to use while programming @@ -32,7 +31,7 @@ class Customer { 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 + ~Customer(); int id; string name; string card_code; diff --git a/headers/Park_spot.h b/headers/Park_spot.h index 86b9da0..b5b7757 100644 --- a/headers/Park_spot.h +++ b/headers/Park_spot.h @@ -14,8 +14,9 @@ class Park_spot { public: int id; bool taken; - Customer* parked; //TODO: think about memory management + Customer* parked; Park_spot(); + ~Park_spot(); void clock(Customer* c_customer); private: void save_db(); diff --git a/headers/Park_time.h b/headers/Park_time.h index 8093a13..4f33791 100644 --- a/headers/Park_time.h +++ b/headers/Park_time.h @@ -5,12 +5,14 @@ #include #include #include +#include #include "data.h" using namespace std::chrono; using std::string; using std::to_string; +using std::cout; /* @@ -21,6 +23,7 @@ class Park_time { public: Park_time(int c_id, int s_id); // Park_time(int c_id, int s_id ); + ~Park_time(); int id; int customer_id; int spot_id; diff --git a/main.cpp b/main.cpp index ffd73db..6af922b 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,4 @@ -#include "thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" -#include "headers/data.h" #include "headers/Park_spot.h" -#include -#include -#include #include @@ -31,16 +26,13 @@ a wait function where 1 sec represents 1 hour irl. } - -using std::cout; - int main() { class Customer sagar{"Sagar Ramsaransing", Verhicle_type::bike}; sagar.update_db(); Park_spot p1; p1.clock(&sagar); Wait(2); - p1.clock(&sagar); + // p1.clock(&sagar); } diff --git a/test.db3 b/test.db3 index 47a4ff193bbe21a9484f1da7018abce97b4d9f2a..794b7ed5405e4c884d64f9048169da04dee03634 100644 GIT binary patch delta 249 zcmZo@U~Fh$oFL7pI8nx#QE_9!5`I2bemMsIOZ-duEBL+nEOZ-duD>f@CWbrd;Gf!5K7oR*)z8ENV pnSuW!|0Dj(K&cu06DR0SzN~Ku6h6kl|Bn9_|1qer!sKK2Rsh}}9X0>} From bf17b82c2e61685ccfffbad5d3fbf12d365ca302 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sun, 30 Jun 2019 00:37:18 -0300 Subject: [PATCH 6/7] Added constructors to construct from db info This is in no way finished. CHECK TODOs!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --- .clang-format | 20 +++++++++++++----- Customer.cpp | 42 +++++++++++++++++--------------------- Park_spot.cpp | 38 +++++++++++++++-------------------- Park_time.cpp | 49 ++++++++++++++++++++++++--------------------- data.cpp | 18 ++++++++++++----- headers/Customer.h | 26 +++++++++++------------- headers/Park_spot.h | 24 +++++++++++++--------- headers/Park_time.h | 25 ++++++++++------------- headers/data.h | 5 +++-- main.cpp | 13 ++++++------ 10 files changed, 136 insertions(+), 124 deletions(-) diff --git a/.clang-format b/.clang-format index 182267a..64937c3 100644 --- a/.clang-format +++ b/.clang-format @@ -1,12 +1,22 @@ BasedOnStyle: LLVM IndentWidth: 4 -#------------- + +#--- cpp Language: Cpp - PointerAlignment: Left -ColumnLimit: 120 -AllowAllArgumentsOnNextLine: true -BreakConstructorInitializersStyle : BCIS_AfterColon +ColumnLimit: 80 +AlignAfterOpenBracket: Align +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +BinPackArguments: false +BreakConstructorInitializers: BeforeColon ConstructorInitializerAllOnOneLineOrOnePerLine: true Cpp11BracedListStyle: true +IncludeBlocks: Regroup + +#--- \ No newline at end of file diff --git a/Customer.cpp b/Customer.cpp index 2f07331..85948ac 100644 --- a/Customer.cpp +++ b/Customer.cpp @@ -1,30 +1,23 @@ #include "headers/Customer.h" // constructors -Customer::Customer(string name_, Verhicle_type verhicle_): - name{name_}, verhicle{verhicle_}, - card_code{gen_cardcode()} -{ +Customer::Customer(string name_, Verhicle_type verhicle_) + : name{name_}, verhicle{verhicle_}, card_code{gen_cardcode()} { id = auto_increment_db() + 1; save_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} {} -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} -{ +Customer::~Customer() { update_db(); } -} - -Customer::~Customer(){ - update_db(); -} - - -// clock in/out methods ==================================================================================== +// clock in/out methods +// ==================================================================================== /* Create a p_time object with start=now and adds to vector */ @@ -34,22 +27,22 @@ void Customer::clock_in(int s_id) { } // edit last p_time object so end=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); +} // report gen void Customer::gen_monthly() { cout << "NAME: " << name << " card code: " << card_code << "\n"; cout << "-------------------------------------------------\n"; for (auto& i : park_instances) { - // TODO: need some logic to only include from this month. scratch that, need to remove gen monthly + // TODO: need some logic to only include from this month. scratch that, + // need to remove gen monthly cout << i; } cout << "-------------------------------------------------\n\n"; } - //================================================================================================ // functions that interact with the database @@ -66,7 +59,8 @@ void Customer::save_db() { } void Customer::update_db() { - string statement = "UPDATE Customer SET name = '', card_code = '' where id = '';"; + string statement = + "UPDATE Customer SET name = '', card_code = '' where id = '';"; statement.insert(58, to_string(id)); statement.insert(44, card_code); statement.insert(28, name); diff --git a/Park_spot.cpp b/Park_spot.cpp index 29ab878..5c982fc 100644 --- a/Park_spot.cpp +++ b/Park_spot.cpp @@ -1,31 +1,28 @@ #include "headers/Park_spot.h" - - // constructors Park_spot::Park_spot() - : parked{nullptr}, - id{auto_increment_db()+1}, - taken{false} { + : parked{nullptr}, id{auto_increment_db() + 1}, taken{false} { save_db(); } +Park_spot::Park_spot(Customer parked_, int id_, bool taken_) + : parked{nullptr}, + id{id_}, + taken{taken_} // TODO: think about how init parked? +{} -Park_spot::~Park_spot(){ - update_db(); -} +Park_spot::~Park_spot() { update_db(); } - - -// clock in en out, calls de juist(in/out) van de customer aan de hand van internal state van taken -void Park_spot::clock(Customer* c_customer){ - if (!taken){ +// clock in en out, calls de juist(in/out) van de customer aan de hand van +// internal state van taken +void Park_spot::clock(Customer* c_customer) { + if (!taken) { parked = c_customer; taken = true; parked->clock_in(id); update_db(); - } - else{ + } else { taken = false; parked->clock_out(id); parked = nullptr; @@ -33,25 +30,22 @@ void Park_spot::clock(Customer* c_customer){ } } - // --------------------- db functs void Park_spot::update_db() { - string statement = "UPDATE Park_spot SET taken = '', customer_id = '' where id = '';"; - // 29, 48, 62 + string statement = + "UPDATE Park_spot SET taken = '', customer_id = '' where id = '';"; statement.insert(63, to_string(id)); - if (taken){ + if (taken) { statement.insert(49, to_string(parked->id)); statement.insert(30, "true"); - } - else { + } else { statement.insert(49, "NULL"); statement.insert(30, "false"); } data::db.exec(statement); } - void Park_spot::save_db() { //(int id, bool taken, int customer_id) string statement{"insert into Park_spot values ( , , );"}; diff --git a/Park_time.cpp b/Park_time.cpp index b93c0ef..963a53d 100644 --- a/Park_time.cpp +++ b/Park_time.cpp @@ -1,22 +1,27 @@ -#include"headers/Park_time.h" - +#include "headers/Park_time.h" Park_time::Park_time(int c_id, int s_id) - : customer_id { c_id } - , spot_id { s_id } - , duration { 0 } - , start { high_resolution_clock::now() } - , id {auto_increment_db() + 1} -{ + : customer_id{c_id}, + spot_id{s_id}, + duration{0}, + start{high_resolution_clock::now()}, + id{auto_increment_db() + 1} { save_db(); } -Park_time::~Park_time(){ - update_db(); +Park_time::Park_time(int id_, int customer_id_, int spot_id_, int start_, + int duration_) + : id{id_}, + customer_id{customer_id_}, + spot_id{spot_id_}, + duration{duration_} { + start = time_point(seconds(start_)); + end = time_point(seconds(start_ + duration_)); } -void Park_time::clock_out(int c_id, int s_id) -{ +Park_time::~Park_time() { update_db(); } + +void Park_time::clock_out(int c_id, int s_id) { if (c_id != customer_id) { cout << "wrong customer id, you are at the wrong location"; @@ -29,7 +34,8 @@ 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 + duration = + duration_cast(end - start).count(); // use mins later update_db(); } else { @@ -37,8 +43,7 @@ void Park_time::clock_out(int c_id, int s_id) } } - -std::ostream& operator<<(std::ostream& os, const Park_time & pt){ +std::ostream& operator<<(std::ostream& os, const Park_time& pt) { std::time_t start_ = system_clock::to_time_t(pt.start); std::time_t end_ = system_clock::to_time_t(pt.end); os << "- - - - - - - - - - - - - - - - - - - -\n"; @@ -49,23 +54,21 @@ std::ostream& operator<<(std::ostream& os, const Park_time & pt){ return os; } - -int Park_time::start_to_int(){ +int Park_time::start_to_int() { auto start_to_epoch = start.time_since_epoch(); auto start_value = duration_cast(start_to_epoch); int start_seconds = start_value.count(); return start_seconds; } -// db funcs ----------------------------------------------------------------------------- - - +// 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(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)); @@ -75,14 +78,14 @@ void Park_time::save_db() { } void Park_time::update_db() { - string statement = "UPDATE Park_time SET end = , duration = where id = '';"; + string statement = + "UPDATE Park_time SET end = , duration = where id = '';"; statement.insert(53, to_string(id)); statement.insert(40, to_string(duration)); statement.insert(27, to_string(start_to_int() + duration)); 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;"); diff --git a/data.cpp b/data.cpp index 42e09a7..7176131 100644 --- a/data.cpp +++ b/data.cpp @@ -2,12 +2,20 @@ namespace data { -SQLite::Database start_db() { - SQLite::Database db("test.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); +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 int, end int, duration int)"); + 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 int, end int, duration int)"); return db; } diff --git a/headers/Customer.h b/headers/Customer.h index cb1c7f7..fb6e449 100644 --- a/headers/Customer.h +++ b/headers/Customer.h @@ -2,35 +2,33 @@ #define CUSTOMER_H #pragma once - #include "Park_time.h" #include "data.h" + #include #include - using std::vector; -// will make it easy to represent it in the database while making it easy to use while programming -enum class Verhicle_type { - bike = 1, - small_car = 2, - suv = 3, - pickup = 4 -}; +// will make it easy to represent it in the database while making it easy to use +// while programming +enum class Verhicle_type { bike = 1, small_car = 2, suv = 3, pickup = 4 }; /* -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. +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. */ class Customer { public: Customer(string name_, Verhicle_type verhicle_); - Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, - vector instances); // needed to construct from db + Customer(int id_, string name_, // needed to construct from db + string card_code_, + Verhicle_type verhicle_, // TODO: how init. p_time instances? + vector instances); ~Customer(); int id; string name; diff --git a/headers/Park_spot.h b/headers/Park_spot.h index b5b7757..597737d 100644 --- a/headers/Park_spot.h +++ b/headers/Park_spot.h @@ -1,26 +1,32 @@ #include "Customer.h" - /* db representation: int id not null bool taken not null int customer_id (null) (many to one, foreign key, whatever) -Dit representeert een parkeerplaats. Het heeft als internal state alleen dat t bezet is of niet. +Dit representeert een parkeerplaats. Het heeft als internal state alleen dat t +bezet is of niet. */ class Park_spot { - public: + public: int id; bool taken; Customer* parked; Park_spot(); ~Park_spot(); - void clock(Customer* c_customer); - private: - void save_db(); - void update_db(); - void delete_db(); - int auto_increment_db(); + void + clock(Customer* c_customer); + + private: + void + save_db(); + void + update_db(); + void + delete_db(); + int + auto_increment_db(); }; \ No newline at end of file diff --git a/headers/Park_time.h b/headers/Park_time.h index 4f33791..49ab9ea 100644 --- a/headers/Park_time.h +++ b/headers/Park_time.h @@ -2,17 +2,17 @@ #define PARK_TIME_H #pragma once -#include -#include -#include -#include #include "data.h" +#include +#include +#include +#include using namespace std::chrono; +using std::cout; using std::string; using std::to_string; -using std::cout; /* @@ -20,9 +20,10 @@ Record of who parked at what park_spot and at what time. */ class Park_time { -public: + public: Park_time(int c_id, int s_id); - // Park_time(int c_id, int s_id ); + Park_time(int id_, int customer_id_, int spot_id_, int start_, + int duration_); ~Park_time(); int id; int customer_id; @@ -30,19 +31,15 @@ public: int duration; void clock_out(int c_id, int s_id); - friend std::ostream& operator<<(std::ostream& os, const Park_time & pt); + friend std::ostream& operator<<(std::ostream& os, const Park_time& pt); -private: + private: high_resolution_clock::time_point start; high_resolution_clock::time_point end; void save_db(); void update_db(); int auto_increment_db(); // helper - int start_to_int(); // helper - + int start_to_int(); // helper }; - - - #endif // Park_time \ No newline at end of file diff --git a/headers/data.h b/headers/data.h index e28404b..2684f60 100644 --- a/headers/data.h +++ b/headers/data.h @@ -3,9 +3,10 @@ #pragma once #include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" namespace data { -SQLite::Database start_db(); +SQLite::Database +start_db(); static SQLite::Database db = start_db(); -} +} // namespace data #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 6af922b..0c03f7f 100644 --- a/main.cpp +++ b/main.cpp @@ -17,7 +17,8 @@ record die zegt dat een customer voor x tijd geparkeert heeft bij spot x, enz. De client clockt in en uit bij een spot. */ -void Wait(int sec) +void +Wait(int sec) /* a wait function where 1 sec represents 1 hour irl. */ @@ -25,14 +26,14 @@ a wait function where 1 sec represents 1 hour irl. std::this_thread::sleep_for(seconds{sec}); } - -int main() { - class Customer sagar{"Sagar Ramsaransing", Verhicle_type::bike}; +int +main() { + class Customer sagar { + "Sagar Ramsaransing", Verhicle_type::bike + }; sagar.update_db(); Park_spot p1; p1.clock(&sagar); Wait(2); // p1.clock(&sagar); - - } From c5763ed82657be05af64389e1826da25b9cac22b Mon Sep 17 00:00:00 2001 From: MassiveAtoms Date: Sun, 30 Jun 2019 05:32:01 -0300 Subject: [PATCH 7/7] small bugfix --- Park_spot.cpp | 3 ++- headers/Park_spot.h | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Park_spot.cpp b/Park_spot.cpp index 5c982fc..edb30d6 100644 --- a/Park_spot.cpp +++ b/Park_spot.cpp @@ -6,7 +6,8 @@ Park_spot::Park_spot() : parked{nullptr}, id{auto_increment_db() + 1}, taken{false} { save_db(); } -Park_spot::Park_spot(Customer parked_, int id_, bool taken_) + +Park_spot::Park_spot(Customer* parked_, int id_, bool taken_) : parked{nullptr}, id{id_}, taken{taken_} // TODO: think about how init parked? diff --git a/headers/Park_spot.h b/headers/Park_spot.h index 597737d..e5dc5d7 100644 --- a/headers/Park_spot.h +++ b/headers/Park_spot.h @@ -16,6 +16,7 @@ class Park_spot { bool taken; Customer* parked; Park_spot(); + Park_spot(Customer* parked_, int id_, bool taken_); ~Park_spot(); void clock(Customer* c_customer);