Parkmanne/Customer.cpp
TinyAtoms 85ba50c5d6 ??
2019-06-26 22:20:06 -03:00

101 lines
3.2 KiB
C++

#include "headers/Customer.h"
#include <iostream>
// constructors
Customer::Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db):
name{name_}, verhicle{verhicle_},
card_code{gen_cardcode()}
{
id = auto_increment_db(db) + 1;
save_db(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}
{
}
// clock methods ====================================================================================
/*
creert een park_time object met start time= nu, en voegt t toe aan een vector.
*/
void Customer::clock_in(int s_id) {
Park_time pt{id, s_id};
park_instances.push_back(pt);
}
// edit de laatste park_time obj in de vector zodat de end_time = 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() {
std::cout << "NAME: " << name << " card code: " << card_code << "\n";
std::cout << "-------------------------------------------------\n";
for (auto& i : park_instances) {
// TODO: need some logic to only include from this month
std::cout << i;
}
std::cout << "-------------------------------------------------\n\n";
}
//================================================================================================
// functions that interact with the database
void Customer::save_db(SQLite::Database& database) {
string statement{"insert into Customer values (, '', '', );"};
// after ( = 28)
statement.insert(38, std::to_string(int(verhicle)));
statement.insert(36, card_code);
statement.insert(32, name);
statement.insert(29, "null");
SQLite::Transaction transaction(database);
database.exec(statement);
transaction.commit();
}
void Customer::update_db(SQLite::Database& database) {
string statement = "UPDATE Customer SET name = \"\", card_code = \"\" where id = '';";
statement.insert(58, std::to_string(id));
statement.insert(44, card_code);
statement.insert(28, name);
// std::cout << statement; TODO: set some logging here
database.exec(statement);
}
void Customer::delete_db(SQLite::Database& database) {
string statement = "delete from Customer where id= ;";
statement.insert(statement.length() - 2, std::to_string(id));
// std::cout << statement;
SQLite::Transaction transaction(database);
database.exec(statement);
transaction.commit();
}
int Customer::auto_increment_db(SQLite::Database& database) {
SQLite::Statement max_id(database, "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;
}