Parkmanne/Customer.cpp

100 lines
3.1 KiB
C++
Raw Normal View History

2019-07-02 00:18:52 +00:00
#include "headers/Customer.h"
// constructors
2019-07-23 14:17:55 +00:00
Customer::Customer(string name_, string password_, Vehicle_type vehicle_, string telephone_,
int role_)
2019-07-08 20:57:09 +00:00
: id{auto_increment_db() + 1},
name{name_},
password{hash_password(password_)},
vehicle{vehicle_},
telephone{telephone_},
role{role_} {
2019-07-02 00:18:52 +00:00
save_db();
}
2019-07-08 20:57:09 +00:00
Customer::Customer(int id_, string name_, string password_, Vehicle_type vehicle_,
2019-07-22 13:16:07 +00:00
vector<Park_time> instances, string telephone_, int role_)
2019-07-06 16:32:00 +00:00
: id{id_},
name{name_},
2019-07-02 00:18:52 +00:00
password{password_},
2019-07-06 14:52:01 +00:00
vehicle{vehicle_},
2019-07-08 20:57:09 +00:00
park_instances{instances},
2019-07-22 13:16:07 +00:00
telephone{telephone_},
role{role_} {}
2019-07-02 00:18:52 +00:00
// 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);
}
2019-07-23 14:17:55 +00:00
// edit last p_time object in park_instances so end=now
2019-07-02 00:18:52 +00:00
void Customer::clock_out(int s_id) {
park_instances[park_instances.size() - 1].clock_out(id, s_id);
}
2019-07-06 16:32:00 +00:00
bool Customer::parked() {
2019-07-08 20:57:09 +00:00
if (!park_instances.size()) {
2019-07-06 16:32:00 +00:00
return false;
}
if ((park_instances[park_instances.size() - 1].duration)) {
// if duration of the last parktime == 0, meaning
// that the customer has not clocked out
return false;
2019-07-08 20:57:09 +00:00
} else {
2019-07-06 16:32:00 +00:00
return true;
}
}
2019-07-08 20:57:09 +00:00
int Customer::parked_at() { return park_instances[park_instances.size() - 1].spot_id; }
2019-07-02 00:18:52 +00:00
//================================================================================================
// functions that interact with the database
void Customer::save_db() {
string statement{"insert into Customer values (, '', '', ,'', );"};
statement.insert(43, to_string(role));
2019-07-08 21:31:10 +00:00
statement.insert(41, telephone);
2019-07-06 14:52:01 +00:00
statement.insert(38, to_string(int(vehicle)));
2019-07-02 00:18:52 +00:00
statement.insert(36, password);
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 =
2019-07-08 20:57:09 +00:00
"UPDATE Customer SET name = '', password = '', "
"vehicle = '', telephone = '', role = '' where id = '';";
2019-07-22 01:13:34 +00:00
statement.insert(98, to_string(id));
statement.insert(84, to_string(role));
2019-07-08 20:57:09 +00:00
statement.insert(73, telephone);
statement.insert(57, to_string(int(vehicle)));
statement.insert(43, password);
2019-07-02 00:18:52 +00:00
statement.insert(28, name);
2019-07-08 21:31:10 +00:00
data::db.exec(statement);
2019-07-02 00:18:52 +00:00
}
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;
}