Parkmanne/Customer.cpp
TinyAtoms bf17b82c2e Added constructors to construct from db info
This is in no way finished. CHECK TODOs!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2019-06-30 00:37:18 -03:00

99 lines
3.0 KiB
C++

#include "headers/Customer.h"
// constructors
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<Park_time> instances)
: name{name_},
card_code{card_code_},
verhicle{verhicle_},
park_instances{instances} {}
Customer::~Customer() { update_db(); }
// clock in/out methods
// ====================================================================================
/*
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 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() {
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
cout << i;
}
cout << "-------------------------------------------------\n\n";
}
//================================================================================================
// functions that interact with the database
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(32, name);
statement.insert(29, to_string(id));
SQLite::Transaction transaction(data::db);
data::db.exec(statement);
transaction.commit();
}
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(28, name);
data::db.exec(statement);
}
void Customer::delete_db() {
string statement = "delete from Customer where id= ;";
statement.insert(statement.length() - 2, to_string(id));
SQLite::Transaction transaction(data::db);
data::db.exec(statement);
transaction.commit();
}
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);
max_id.reset();
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;
}