From bf17b82c2e61685ccfffbad5d3fbf12d365ca302 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sun, 30 Jun 2019 00:37:18 -0300 Subject: [PATCH] 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); - - }