diff --git a/Customer.cpp b/Customer.cpp index 85948ac..d156f3e 100644 --- a/Customer.cpp +++ b/Customer.cpp @@ -1,20 +1,20 @@ #include "headers/Customer.h" // constructors -Customer::Customer(string name_, Verhicle_type verhicle_) - : name{name_}, verhicle{verhicle_}, card_code{gen_cardcode()} { +Customer::Customer(string name_, string password_, Verhicle_type verhicle_) + : name{name_}, verhicle{verhicle_}, password{hash_password(password)} { id = auto_increment_db() + 1; save_db(); } -Customer::Customer(int id_, string name_, string card_code_, +Customer::Customer(int id_, string name_, string password_, Verhicle_type verhicle_, vector instances) - : name{name_}, - card_code{card_code_}, + :id{id_}, + name{name_}, + password{password_}, verhicle{verhicle_}, park_instances{instances} {} -Customer::~Customer() { update_db(); } // clock in/out methods // ==================================================================================== @@ -33,7 +33,7 @@ void Customer::clock_out(int s_id) { // report gen void Customer::gen_monthly() { - cout << "NAME: " << name << " card code: " << card_code << "\n"; + cout << "NAME: " << name << "\n"; cout << "-------------------------------------------------\n"; for (auto& i : park_instances) { // TODO: need some logic to only include from this month. scratch that, @@ -50,7 +50,7 @@ void Customer::save_db() { string statement{"insert into Customer values (, '', '', );"}; // after ( = 28) statement.insert(38, to_string(int(verhicle))); - statement.insert(36, card_code); + statement.insert(36, password); statement.insert(32, name); statement.insert(29, to_string(id)); SQLite::Transaction transaction(data::db); @@ -62,7 +62,7 @@ void Customer::update_db() { string statement = "UPDATE Customer SET name = '', card_code = '' where id = '';"; statement.insert(58, to_string(id)); - statement.insert(44, card_code); + statement.insert(44, password); statement.insert(28, name); data::db.exec(statement); } @@ -84,15 +84,4 @@ int Customer::auto_increment_db() { return id; } -// random helpers============================================================= -std::mt19937 mt(time(0)); -std::uniform_int_distribution dist(65, 127); -string Customer::gen_cardcode() { - string code; - for (int i = 0; i < 20; i++) { - char letter = char(dist(mt)); - code += letter; - } - return code; -} diff --git a/Park_spot.cpp b/Park_spot.cpp index edb30d6..5798683 100644 --- a/Park_spot.cpp +++ b/Park_spot.cpp @@ -13,7 +13,6 @@ Park_spot::Park_spot(Customer* parked_, int id_, bool taken_) taken{taken_} // TODO: think about how init parked? {} -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 @@ -39,10 +38,10 @@ void Park_spot::update_db() { statement.insert(63, to_string(id)); if (taken) { statement.insert(49, to_string(parked->id)); - statement.insert(30, "true"); + statement.insert(30, "1"); } else { statement.insert(49, "NULL"); - statement.insert(30, "false"); + statement.insert(30, "0"); } data::db.exec(statement); } @@ -52,7 +51,7 @@ void Park_spot::save_db() { string statement{"insert into Park_spot values ( , , );"}; // after ( = 28) statement.insert(34, "NULL"); - statement.insert(32, "false"); + statement.insert(32, "0"); statement.insert(30, to_string(id)); SQLite::Transaction transaction(data::db); data::db.exec(statement); @@ -74,4 +73,4 @@ int Park_spot::auto_increment_db() { id = max_id.getColumn(0); max_id.reset(); return id; -} \ No newline at end of file +} diff --git a/Query.cpp b/Query.cpp index 6cda385..5a2617a 100644 --- a/Query.cpp +++ b/Query.cpp @@ -1,6 +1,7 @@ #include "headers/Query.h" -vector query_Parktime_for_customer(int cid) { + +vector query_parktimes_for_customer(int cid) { /* This is needed to initialize the park_instances for the customer constructor that is supposed to create a customer from data in the db. @@ -31,20 +32,67 @@ vector query_customer_with_name(string name) { 2. multiple customers could be returned with the same name. */ vector result; - SQLite::Statement query(data::db, - "SELECT * FROM Customer WHERE name = '?';"); + SQLite::Statement query( + data::db, + "SELECT id, name, password, verhicle FROM Customer WHERE name = ?;"); query.bind(1, name); while (query.executeStep()) { - // (id integer primary key, name text, card_code varchar(20), verhicle - // int) (int id_, string name_, string card_code_, Verhicle_type - // verhicle_, vector instances) - int id = query.getColumn(0); - string name = query.getColumn(1); + string name_ = query.getColumn(1); string password = query.getColumn(2); - // int verhicle = query.getColumn(3); // cast to verhicle - vector park_instances = query_Parktime_for_customer(id); - // result.push_back(Customer{id, name, password, Verhicle_type(verhicle), park_instances}); + int verhicle = query.getColumn(3); // cast to verhicle + vector park_instances = query_parktimes_for_customer(id); + result.push_back(Customer{ + id, name_, password, Verhicle_type(verhicle), park_instances}); } return result; +} + +Customer query_customer_with_id(int id) { + /* do not call this function if you are not certain a customer with this id + exists. + // the only legitimate caller of this function is query_parkspot_x + // there is no error handling in this function + // for when this function doesn't find the customer with this id !!!! + */ + + SQLite::Statement query(data::db, "SELECT * FROM Customer WHERE id = ?;"); + query.bind(1, id); + while (query.executeStep()) { + string name = query.getColumn(1); + string password = query.getColumn(2); + int verhicle = query.getColumn(3); // cast to verhicle + vector park_instances = query_parktimes_for_customer(id); + Customer result{ + id, name, password, Verhicle_type(verhicle), park_instances}; + // DEBUG + cout << "{" << result.id << "," < ?;"); + query.bind(1, 0); + while (query.executeStep()) { + int id = query.getColumn(0); + int taken = query.getColumn(1); + int cid = query.getColumn(2); + park_customers.push_back(query_customer_with_id(cid)); + parking_spots.push_back( + Park_spot{get_customer_ptr_for_parkspot(cid), id, bool(taken)}); + } +} + +Customer* get_customer_ptr_for_parkspot(int id) { + if (!id) { + return nullptr; + } + for (int i = 0; i < park_customers.size(); i++) { + if (park_customers[i].id == id) { + + return &park_customers[i]; + } + } + return nullptr; } \ No newline at end of file diff --git a/data.cpp b/data.cpp index a7286b8..16610c1 100644 --- a/data.cpp +++ b/data.cpp @@ -2,19 +2,18 @@ namespace data { -SQLite::Database -start_db() { +SQLite::Database start_db() { SQLite::Database db("test.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); - while (sodium_init()< 0){ + while (sodium_init() < 0) { std::cout << "SODIUM NOT WORKING"; } db.exec( "create table if not exists Customer (id integer primary key, name " - "text, card_code varchar(20), verhicle int)"); + "text, password text, verhicle int)"); db.exec( "create table if not exists Park_spot (id integer primary key, taken " - "boolean, customer_id int)"); + "int, 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)"); diff --git a/headers/Customer.h b/headers/Customer.h index fb6e449..c05934d 100644 --- a/headers/Customer.h +++ b/headers/Customer.h @@ -4,8 +4,6 @@ #include "Park_time.h" #include "data.h" - -#include #include using std::vector; @@ -24,16 +22,15 @@ park_time object. Voegt het toe aan een vector. class Customer { public: - Customer(string name_, Verhicle_type verhicle_); - 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; - string card_code; - + string password; + Customer(string name_, string password_, Verhicle_type verhicle_); + Customer(int id_, string name_, // needed to construct from db + string password_, + Verhicle_type verhicle_, // TODO: how init. p_time instances? + vector instances); void clock_in(int s_id); void clock_out(int s_id); @@ -45,10 +42,13 @@ class Customer { private: Verhicle_type verhicle; vector park_instances; - - string gen_cardcode(); void save_db(); int auto_increment_db(); }; +static vector park_customers; // save the customers that are parked in here +// parking_spot uses pointers, so it's better to save the parked customers here +// where we know they'll be destroyed at the end of this scope, instead of too early +// and end up with dangling pointers + #endif // CUSTOMER_H \ No newline at end of file diff --git a/headers/Park_spot.h b/headers/Park_spot.h index e5dc5d7..be1788c 100644 --- a/headers/Park_spot.h +++ b/headers/Park_spot.h @@ -17,17 +17,13 @@ class Park_spot { Customer* parked; Park_spot(); Park_spot(Customer* parked_, int id_, bool taken_); - ~Park_spot(); - void - clock(Customer* c_customer); + 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 + void save_db(); + void update_db(); + void delete_db(); + int auto_increment_db(); +}; + +static vector parking_spots; // to save the parking spots in memory \ No newline at end of file diff --git a/headers/Query.h b/headers/Query.h index c618d31..33fb173 100644 --- a/headers/Query.h +++ b/headers/Query.h @@ -4,11 +4,15 @@ #include "Park_spot.h" -#include #include -vector query_Parktime_for_customer(int cid); +vector query_parktimes_for_customer(int cid); + vector query_customer_with_name(string name); +Customer query_customer_with_id(int id); +Customer* get_customer_ptr_for_parkspot(int id); + +void query_all_parking_spots(); // used for initializing the parking spots at start of the program #endif // CUSTOMER_H \ No newline at end of file diff --git a/main.cpp b/main.cpp index c6a9b89..c9ec230 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,6 @@ #include #include - /* Code strucure like this: class declarations zijn in /headers/class_naam.h, en definitions van de member @@ -19,6 +18,9 @@ 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. @@ -27,35 +29,29 @@ a wait function where 1 sec represents 1 hour irl. std::this_thread::sleep_for(seconds{sec}); } +Customer* get_customer_ptr_for_parkspot(int id); + int main() { - class Customer sagar { - "query", Verhicle_type::bike - }; - sagar.update_db(); - Park_spot p1; - p1.clock(&sagar); // in - Wait(2); - p1.clock(&sagar); - Wait(2); - p1.clock(&sagar); // in - Wait(2); - p1.clock(&sagar); - Wait(2); - p1.clock(&sagar); // in - Wait(2); - p1.clock(&sagar); - Wait(2); + query_all_parking_spots(); - vector test = query_customer_with_name("query"); - if (!test.size()){ - cout << "No customers with this name found;"; - } - else if (test.size()==1) { - cout << "1 customer found;"; - } - else { - cout << "MORE customers found?"; - } + Customer p0 = query_customer_with_name("Shaquile")[0]; + Customer p1 = query_customer_with_name("Sagar Ramsaransing")[0]; + Customer p2 = query_customer_with_name("Joshua karto")[0]; + Customer p3 = query_customer_with_name("Stefan udit")[0]; + + parking_spots[2].clock(&p1); + Wait(2); + parking_spots[2].clock(&p1); + Wait(1); + parking_spots[0].clock(&p2); + Wait(1); + parking_spots[1].clock(&p3); + Wait(1); + parking_spots[0].clock(&p2); + parking_spots[1].clock(&p3); + Wait(1); + parking_spots[1].clock(&p3); -} \ No newline at end of file +} + diff --git a/old_test.db3 b/old_test.db3 new file mode 100644 index 0000000..108a7ad Binary files /dev/null and b/old_test.db3 differ diff --git a/test.db3 b/test.db3 index 126855f..b4fd627 100644 Binary files a/test.db3 and b/test.db3 differ