From 085cd5af0832a4fea04fc7f1eb164f9d7c73a795 Mon Sep 17 00:00:00 2001 From: TinyAtoms Date: Sat, 29 Jun 2019 23:22:58 -0300 Subject: [PATCH] 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>}