Parkmanne/Customer.cpp

104 lines
3.0 KiB
C++
Raw Normal View History

2019-06-27 01:20:06 +00:00
#include "headers/Customer.h"
#include <iostream>
// constructors
2019-06-30 00:50:36 +00:00
Customer::Customer(string name_, Verhicle_type verhicle_):
2019-06-27 01:20:06 +00:00
name{name_}, verhicle{verhicle_},
card_code{gen_cardcode()}
{
2019-06-30 00:50:36 +00:00
id = auto_increment_db() + 1;
save_db();
2019-06-27 01:20:06 +00:00
}
2019-06-30 01:48:49 +00:00
2019-06-27 01:20:06 +00:00
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}
{
}
2019-06-30 01:48:49 +00:00
// clock in/out methods ====================================================================================
2019-06-27 01:20:06 +00:00
/*
2019-06-30 01:48:49 +00:00
Create a p_time object with start=now and adds to vector
2019-06-27 01:20:06 +00:00
*/
void Customer::clock_in(int s_id) {
Park_time pt{id, s_id};
park_instances.push_back(pt);
}
2019-06-30 01:48:49 +00:00
// edit last p_time object so end=now
2019-06-27 01:20:06 +00:00
void Customer::clock_out(int s_id) { park_instances[park_instances.size() - 1].clock_out(id, s_id); }
2019-06-30 01:48:49 +00:00
2019-06-27 01:20:06 +00:00
// 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";
}
2019-06-30 01:48:49 +00:00
2019-06-27 01:20:06 +00:00
//================================================================================================
// functions that interact with the database
2019-06-30 00:50:36 +00:00
void Customer::save_db() {
2019-06-27 01:20:06 +00:00
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);
2019-06-30 01:30:47 +00:00
statement.insert(29, to_string(id));
2019-06-30 00:50:36 +00:00
SQLite::Transaction transaction(data::db);
data::db.exec(statement);
2019-06-27 01:20:06 +00:00
transaction.commit();
}
2019-06-30 00:50:36 +00:00
void Customer::update_db() {
string statement = "UPDATE Customer SET name = '', card_code = '' where id = '';";
2019-06-27 01:20:06 +00:00
statement.insert(58, std::to_string(id));
statement.insert(44, card_code);
statement.insert(28, name);
// std::cout << statement; TODO: set some logging here
2019-06-30 00:50:36 +00:00
data::db.exec(statement);
2019-06-27 01:20:06 +00:00
}
2019-06-30 00:50:36 +00:00
void Customer::delete_db() {
2019-06-27 01:20:06 +00:00
string statement = "delete from Customer where id= ;";
statement.insert(statement.length() - 2, std::to_string(id));
// std::cout << statement;
2019-06-30 00:50:36 +00:00
SQLite::Transaction transaction(data::db);
data::db.exec(statement);
2019-06-27 01:20:06 +00:00
transaction.commit();
}
2019-06-30 00:50:36 +00:00
int Customer::auto_increment_db() {
SQLite::Statement max_id(data::db, "select max(id) from Customer;");
2019-06-27 01:20:06 +00:00
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;
}