almost all internals done after i fix segfault

This commit is contained in:
MassiveAtoms 2019-07-01 14:23:15 -03:00
parent 450ffc9588
commit 5fa84e866f
10 changed files with 127 additions and 96 deletions

View File

@ -1,20 +1,20 @@
#include "headers/Customer.h" #include "headers/Customer.h"
// constructors // constructors
Customer::Customer(string name_, Verhicle_type verhicle_) Customer::Customer(string name_, string password_, Verhicle_type verhicle_)
: name{name_}, verhicle{verhicle_}, card_code{gen_cardcode()} { : name{name_}, verhicle{verhicle_}, password{hash_password(password)} {
id = auto_increment_db() + 1; id = auto_increment_db() + 1;
save_db(); save_db();
} }
Customer::Customer(int id_, string name_, string card_code_, Customer::Customer(int id_, string name_, string password_,
Verhicle_type verhicle_, vector<Park_time> instances) Verhicle_type verhicle_, vector<Park_time> instances)
: name{name_}, :id{id_},
card_code{card_code_}, name{name_},
password{password_},
verhicle{verhicle_}, verhicle{verhicle_},
park_instances{instances} {} park_instances{instances} {}
Customer::~Customer() { update_db(); }
// clock in/out methods // clock in/out methods
// ==================================================================================== // ====================================================================================
@ -33,7 +33,7 @@ void Customer::clock_out(int s_id) {
// report gen // report gen
void Customer::gen_monthly() { void Customer::gen_monthly() {
cout << "NAME: " << name << " card code: " << card_code << "\n"; cout << "NAME: " << name << "\n";
cout << "-------------------------------------------------\n"; cout << "-------------------------------------------------\n";
for (auto& i : park_instances) { for (auto& i : park_instances) {
// TODO: need some logic to only include from this month. scratch that, // 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 (, '', '', );"}; string statement{"insert into Customer values (, '', '', );"};
// after ( = 28) // after ( = 28)
statement.insert(38, to_string(int(verhicle))); statement.insert(38, to_string(int(verhicle)));
statement.insert(36, card_code); statement.insert(36, password);
statement.insert(32, name); statement.insert(32, name);
statement.insert(29, to_string(id)); statement.insert(29, to_string(id));
SQLite::Transaction transaction(data::db); SQLite::Transaction transaction(data::db);
@ -62,7 +62,7 @@ void Customer::update_db() {
string statement = string statement =
"UPDATE Customer SET name = '', card_code = '' where id = '';"; "UPDATE Customer SET name = '', card_code = '' where id = '';";
statement.insert(58, to_string(id)); statement.insert(58, to_string(id));
statement.insert(44, card_code); statement.insert(44, password);
statement.insert(28, name); statement.insert(28, name);
data::db.exec(statement); data::db.exec(statement);
} }
@ -84,15 +84,4 @@ int Customer::auto_increment_db() {
return id; return id;
} }
// random helpers=============================================================
std::mt19937 mt(time(0));
std::uniform_int_distribution<int> 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;
}

View File

@ -13,7 +13,6 @@ Park_spot::Park_spot(Customer* parked_, int id_, bool taken_)
taken{taken_} // TODO: think about how init parked? 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 // clock in en out, calls de juist(in/out) van de customer aan de hand van
// internal state van taken // internal state van taken
@ -39,10 +38,10 @@ void Park_spot::update_db() {
statement.insert(63, to_string(id)); statement.insert(63, to_string(id));
if (taken) { if (taken) {
statement.insert(49, to_string(parked->id)); statement.insert(49, to_string(parked->id));
statement.insert(30, "true"); statement.insert(30, "1");
} else { } else {
statement.insert(49, "NULL"); statement.insert(49, "NULL");
statement.insert(30, "false"); statement.insert(30, "0");
} }
data::db.exec(statement); data::db.exec(statement);
} }
@ -52,7 +51,7 @@ void Park_spot::save_db() {
string statement{"insert into Park_spot values ( , , );"}; string statement{"insert into Park_spot values ( , , );"};
// after ( = 28) // after ( = 28)
statement.insert(34, "NULL"); statement.insert(34, "NULL");
statement.insert(32, "false"); statement.insert(32, "0");
statement.insert(30, to_string(id)); statement.insert(30, to_string(id));
SQLite::Transaction transaction(data::db); SQLite::Transaction transaction(data::db);
data::db.exec(statement); data::db.exec(statement);

View File

@ -1,6 +1,7 @@
#include "headers/Query.h" #include "headers/Query.h"
vector<Park_time> query_Parktime_for_customer(int cid) {
vector<Park_time> query_parktimes_for_customer(int cid) {
/* /*
This is needed to initialize the park_instances for the customer constructor This is needed to initialize the park_instances for the customer constructor
that is supposed to create a customer from data in the db. that is supposed to create a customer from data in the db.
@ -31,20 +32,67 @@ vector<Customer> query_customer_with_name(string name) {
2. multiple customers could be returned with the same name. 2. multiple customers could be returned with the same name.
*/ */
vector<Customer> result; vector<Customer> result;
SQLite::Statement query(data::db, SQLite::Statement query(
"SELECT * FROM Customer WHERE name = '?';"); data::db,
"SELECT id, name, password, verhicle FROM Customer WHERE name = ?;");
query.bind(1, name); query.bind(1, name);
while (query.executeStep()) { 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<Park_time> instances)
int id = query.getColumn(0); int id = query.getColumn(0);
string name = query.getColumn(1); string name_ = query.getColumn(1);
string password = query.getColumn(2); string password = query.getColumn(2);
// int verhicle = query.getColumn(3); // cast to verhicle int verhicle = query.getColumn(3); // cast to verhicle
vector<Park_time> park_instances = query_Parktime_for_customer(id); vector<Park_time> park_instances = query_parktimes_for_customer(id);
// result.push_back(Customer{id, name, password, Verhicle_type(verhicle), park_instances}); result.push_back(Customer{
id, name_, password, Verhicle_type(verhicle), park_instances});
} }
return result; 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_time> park_instances = query_parktimes_for_customer(id);
Customer result{
id, name, password, Verhicle_type(verhicle), park_instances};
// DEBUG
cout << "{" << result.id << "," <<result.password <<"," << int(verhicle) << "}\n";
return result;
}
}
void query_all_parking_spots() {
SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE 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;
}

View File

@ -2,19 +2,18 @@
namespace data { namespace data {
SQLite::Database SQLite::Database start_db() {
start_db() {
SQLite::Database db("test.db3", SQLite::Database db("test.db3",
SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE); SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
while (sodium_init()< 0){ while (sodium_init() < 0) {
std::cout << "SODIUM NOT WORKING"; std::cout << "SODIUM NOT WORKING";
} }
db.exec( db.exec(
"create table if not exists Customer (id integer primary key, name " "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( db.exec(
"create table if not exists Park_spot (id integer primary key, taken " "create table if not exists Park_spot (id integer primary key, taken "
"boolean, customer_id int)"); "int, customer_id int)");
db.exec( db.exec(
"create table if not exists Park_time (id integer primary key, " "create table if not exists Park_time (id integer primary key, "
"customer_id int, spot_id int, start int, end int, duration int)"); "customer_id int, spot_id int, start int, end int, duration int)");

View File

@ -4,8 +4,6 @@
#include "Park_time.h" #include "Park_time.h"
#include "data.h" #include "data.h"
#include <random>
#include <vector> #include <vector>
using std::vector; using std::vector;
@ -24,16 +22,15 @@ park_time object. Voegt het toe aan een vector.
class Customer { class Customer {
public: 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<Park_time> instances);
~Customer();
int id; int id;
string name; 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<Park_time> instances);
void clock_in(int s_id); void clock_in(int s_id);
void clock_out(int s_id); void clock_out(int s_id);
@ -45,10 +42,13 @@ class Customer {
private: private:
Verhicle_type verhicle; Verhicle_type verhicle;
vector<Park_time> park_instances; vector<Park_time> park_instances;
string gen_cardcode();
void save_db(); void save_db();
int auto_increment_db(); int auto_increment_db();
}; };
static vector<Customer> 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 #endif // CUSTOMER_H

View File

@ -17,17 +17,13 @@ class Park_spot {
Customer* parked; Customer* parked;
Park_spot(); Park_spot();
Park_spot(Customer* parked_, int id_, bool taken_); Park_spot(Customer* parked_, int id_, bool taken_);
~Park_spot(); void clock(Customer* c_customer);
void
clock(Customer* c_customer);
private: private:
void void save_db();
save_db(); void update_db();
void void delete_db();
update_db(); int auto_increment_db();
void
delete_db();
int
auto_increment_db();
}; };
static vector<Park_spot> parking_spots; // to save the parking spots in memory

View File

@ -4,11 +4,15 @@
#include "Park_spot.h" #include "Park_spot.h"
#include <exception>
#include <array> #include <array>
vector<Park_time> query_Parktime_for_customer(int cid); vector<Park_time> query_parktimes_for_customer(int cid);
vector<Customer> query_customer_with_name(string name); vector<Customer> 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 #endif // CUSTOMER_H

View File

@ -3,7 +3,6 @@
#include <array> #include <array>
#include <thread> #include <thread>
/* /*
Code strucure like this: Code strucure like this:
class declarations zijn in /headers/class_naam.h, en definitions van de member 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. 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. 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}); std::this_thread::sleep_for(seconds{sec});
} }
int main() { Customer* get_customer_ptr_for_parkspot(int id);
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);
vector<Customer> test = query_customer_with_name("query"); int main() {
if (!test.size()){ query_all_parking_spots();
cout << "No customers with this name found;";
} Customer p0 = query_customer_with_name("Shaquile")[0];
else if (test.size()==1) { Customer p1 = query_customer_with_name("Sagar Ramsaransing")[0];
cout << "1 customer found;"; Customer p2 = query_customer_with_name("Joshua karto")[0];
} Customer p3 = query_customer_with_name("Stefan udit")[0];
else {
cout << "MORE customers found?"; 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);
} }

BIN
old_test.db3 Normal file

Binary file not shown.

BIN
test.db3

Binary file not shown.