Parkmanne/Customer.cpp

99 lines
3.0 KiB
C++
Raw Permalink Normal View History

2019-07-02 00:18:52 +00:00
#include "headers/Customer.h"
2019-07-08 20:57:09 +00:00
2019-07-02 00:18:52 +00:00
// constructors
2019-07-08 20:57:09 +00:00
Customer::Customer(string name_, string password_, Vehicle_type vehicle_, string telephone_)
: id{auto_increment_db() + 1},
name{name_},
password{hash_password(password_)},
vehicle{vehicle_},
telephone{telephone_} {
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_,
vector<Park_time> instances, string telephone_)
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},
telephone{telephone_} {}
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);
}
// 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);
}
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() {
2019-07-08 21:31:10 +00:00
string statement{"insert into Customer values (, '', '', ,'');"};
2019-07-02 00:18:52 +00:00
// after ( = 28)
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));
2019-07-08 21:31:10 +00:00
// cout << statement;
2019-07-02 00:18:52 +00:00
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 = '' where id = '';";
statement.insert(87, to_string(id));
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
// cout << statement;
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;
}