so far, still working
This commit is contained in:
parent
cf1cfdfd79
commit
601f6c92bc
@ -17,12 +17,12 @@ add_executable(park
|
|||||||
encrypt.cpp
|
encrypt.cpp
|
||||||
headers/encrypt.h
|
headers/encrypt.h
|
||||||
|
|
||||||
# Customer.cpp
|
Customer.cpp
|
||||||
# headers/Customer.h
|
headers/Customer.h
|
||||||
# Park_spot.cpp
|
Park_spot.cpp
|
||||||
# headers/Park_spot.h
|
headers/Park_spot.h
|
||||||
# Park_time.cpp
|
Park_time.cpp
|
||||||
# headers/Park_time.h
|
headers/Park_time.h
|
||||||
# Query.cpp
|
# Query.cpp
|
||||||
# headers/Query.h
|
# headers/Query.h
|
||||||
)
|
)
|
||||||
|
87
Customer.cpp
Normal file
87
Customer.cpp
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
#include "headers/Customer.h"
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
Customer::Customer(string name_, string password_, Verhicle_type verhicle_)
|
||||||
|
: name{name_}, verhicle{verhicle_}, password{hash_password(password_)} {
|
||||||
|
id = auto_increment_db() + 1;
|
||||||
|
save_db();
|
||||||
|
}
|
||||||
|
|
||||||
|
Customer::Customer(int id_, string name_, string password_,
|
||||||
|
Verhicle_type verhicle_, vector<Park_time> instances)
|
||||||
|
:id{id_},
|
||||||
|
name{name_},
|
||||||
|
password{password_},
|
||||||
|
verhicle{verhicle_},
|
||||||
|
park_instances{instances} {}
|
||||||
|
|
||||||
|
|
||||||
|
// 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 << "\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, 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 =
|
||||||
|
"UPDATE Customer SET name = '', card_code = '' where id = '';";
|
||||||
|
statement.insert(58, to_string(id));
|
||||||
|
statement.insert(44, password);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
76
Park_spot.cpp
Normal file
76
Park_spot.cpp
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
#include "headers/Park_spot.h"
|
||||||
|
|
||||||
|
// constructors
|
||||||
|
|
||||||
|
Park_spot::Park_spot()
|
||||||
|
: parked_customer{0}, id{auto_increment_db() + 1}, taken{false} {
|
||||||
|
save_db();
|
||||||
|
}
|
||||||
|
|
||||||
|
Park_spot::Park_spot(int id_, bool taken_, Customer& parked)
|
||||||
|
: parked_customer{parked.id},
|
||||||
|
id{id_},
|
||||||
|
taken{taken_} // TODO: think about how init parked?
|
||||||
|
{}
|
||||||
|
|
||||||
|
|
||||||
|
// clock in en out, calls de juist(in/out) van de customer aan de hand van
|
||||||
|
// internal state van taken
|
||||||
|
void Park_spot::clock(Customer& c_customer) {
|
||||||
|
if (!taken) {
|
||||||
|
parked_customer = c_customer.id;
|
||||||
|
taken = true;
|
||||||
|
c_customer.clock_in(id);
|
||||||
|
update_db();
|
||||||
|
} else {
|
||||||
|
taken = false;
|
||||||
|
c_customer.clock_out(id);
|
||||||
|
parked_customer = 0;
|
||||||
|
update_db();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------- db functs
|
||||||
|
|
||||||
|
void Park_spot::update_db() {
|
||||||
|
string statement =
|
||||||
|
"UPDATE Park_spot SET taken = '', customer_id = '' where id = '';";
|
||||||
|
statement.insert(63, to_string(id));
|
||||||
|
if (taken) {
|
||||||
|
statement.insert(49, to_string(parked_customer));
|
||||||
|
statement.insert(30, "1");
|
||||||
|
} else {
|
||||||
|
statement.insert(49, "NULL");
|
||||||
|
statement.insert(30, "0");
|
||||||
|
}
|
||||||
|
data::db.exec(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Park_spot::save_db() {
|
||||||
|
//(int id, bool taken, int customer_id)
|
||||||
|
string statement{"insert into Park_spot values ( , , );"};
|
||||||
|
// after ( = 28)
|
||||||
|
statement.insert(34, "NULL");
|
||||||
|
statement.insert(32, "0");
|
||||||
|
statement.insert(30, to_string(id));
|
||||||
|
SQLite::Transaction transaction(data::db);
|
||||||
|
data::db.exec(statement);
|
||||||
|
transaction.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Park_spot::delete_db() {
|
||||||
|
string statement = "delete from Park_spot where id= ;";
|
||||||
|
statement.insert(statement.length() - 2, to_string(id));
|
||||||
|
SQLite::Transaction transaction(data::db);
|
||||||
|
data::db.exec(statement);
|
||||||
|
transaction.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
int Park_spot::auto_increment_db() {
|
||||||
|
SQLite::Statement max_id(data::db, "select max(id) from Park_spot;");
|
||||||
|
int id = 0;
|
||||||
|
max_id.executeStep();
|
||||||
|
id = max_id.getColumn(0);
|
||||||
|
max_id.reset();
|
||||||
|
return id;
|
||||||
|
}
|
96
Park_time.cpp
Normal file
96
Park_time.cpp
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
#include "headers/Park_time.h"
|
||||||
|
|
||||||
|
Park_time::Park_time(int c_id, int s_id)
|
||||||
|
: customer_id{c_id},
|
||||||
|
spot_id{s_id},
|
||||||
|
duration{0},
|
||||||
|
start{high_resolution_clock::now()},
|
||||||
|
id{auto_increment_db() + 1} {
|
||||||
|
save_db();
|
||||||
|
}
|
||||||
|
|
||||||
|
Park_time::Park_time(int id_, int customer_id_, int spot_id_, int start_,
|
||||||
|
int duration_)
|
||||||
|
: id{id_},
|
||||||
|
customer_id{customer_id_},
|
||||||
|
spot_id{spot_id_},
|
||||||
|
duration{duration_} {
|
||||||
|
start = time_point<system_clock>(seconds(start_));
|
||||||
|
end = time_point<system_clock>(seconds(start_ + duration_));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Park_time::clock_out(int c_id, int s_id) {
|
||||||
|
|
||||||
|
if (c_id != customer_id) {
|
||||||
|
cout << "wrong customer id, you are at the wrong location";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (s_id != spot_id) {
|
||||||
|
cout << "Wrong spot id, you're at the wrong location";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!duration) {
|
||||||
|
end = high_resolution_clock::now();
|
||||||
|
duration =
|
||||||
|
duration_cast<seconds>(end - start).count(); // use mins later
|
||||||
|
update_db();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
cout << "Already clocked out. Something is wrong \n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Park_time& pt) {
|
||||||
|
std::time_t start_ = system_clock::to_time_t(pt.start);
|
||||||
|
std::time_t end_ = system_clock::to_time_t(pt.end);
|
||||||
|
os << "- - - - - - - - - - - - - - - - - - - -\n";
|
||||||
|
os << "Clocked in :" << std::ctime(&start_);
|
||||||
|
os << "clocked out : " << std::ctime(&end_);
|
||||||
|
os << "duration : " << pt.duration << "\n";
|
||||||
|
os << "- - - - - - - - - - - - - - - - - - - -\n";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Park_time::start_to_int() {
|
||||||
|
auto start_to_epoch = start.time_since_epoch();
|
||||||
|
auto start_value = duration_cast<seconds>(start_to_epoch);
|
||||||
|
int start_seconds = start_value.count();
|
||||||
|
return start_seconds;
|
||||||
|
}
|
||||||
|
|
||||||
|
// db funcs
|
||||||
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void Park_time::save_db() {
|
||||||
|
string statement{"insert into Park_time values ( , , , , , );"};
|
||||||
|
statement.insert(41, "NULL");
|
||||||
|
statement.insert(39, "NULL");
|
||||||
|
statement.insert(37, to_string(start_to_int()));
|
||||||
|
statement.insert(35, to_string(spot_id));
|
||||||
|
statement.insert(33, to_string(customer_id));
|
||||||
|
statement.insert(31, to_string(id));
|
||||||
|
SQLite::Transaction transaction(data::db);
|
||||||
|
data::db.exec(statement);
|
||||||
|
transaction.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Park_time::update_db() {
|
||||||
|
string statement =
|
||||||
|
"UPDATE Park_time SET end = , duration = where id = '';";
|
||||||
|
statement.insert(53, to_string(id));
|
||||||
|
statement.insert(40, to_string(duration));
|
||||||
|
statement.insert(27, to_string(start_to_int() + duration));
|
||||||
|
data::db.exec(statement);
|
||||||
|
}
|
||||||
|
|
||||||
|
// to get id on first save to db
|
||||||
|
int Park_time::auto_increment_db() {
|
||||||
|
SQLite::Statement max_id(data::db, "select max(id) from Park_time;");
|
||||||
|
int id = 0;
|
||||||
|
max_id.executeStep();
|
||||||
|
id = max_id.getColumn(0);
|
||||||
|
max_id.reset();
|
||||||
|
return id;
|
||||||
|
}
|
2
data.cpp
2
data.cpp
@ -19,8 +19,6 @@ SQLite::Database start_db() {
|
|||||||
"create table if not exists Park_time (id integer primary key, "
|
"create table if not exists Park_time (id integer primary key, "
|
||||||
"customer_id int, spot_id int, start int, end int, duration int)");
|
"customer_id int, spot_id int, start int, end int, duration int)");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
} // namespace data
|
} // namespace data
|
55
headers/Customer.h
Normal file
55
headers/Customer.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#ifndef CUSTOMER_H
|
||||||
|
#define CUSTOMER_H
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Park_time.h"
|
||||||
|
#include "data.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using std::vector;
|
||||||
|
|
||||||
|
// will make it easy to represent it in the database while making it easy to use
|
||||||
|
// while programming
|
||||||
|
enum class Verhicle_type { bike = 1, small_car = 2, suv = 3, pickup = 4 };
|
||||||
|
|
||||||
|
/*
|
||||||
|
card code is een randomly generated string moeten zijn, die je bv. op een nfc
|
||||||
|
card zou opslaan en zo zou authenticaten bij je parking spot. We kunnen dit ipv
|
||||||
|
of samen met een password gebruiken. clock in en out creeert en compleet een
|
||||||
|
park_time object. Voegt het toe aan een vector.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Customer {
|
||||||
|
public:
|
||||||
|
int id;
|
||||||
|
string name;
|
||||||
|
string password;
|
||||||
|
Customer(string name_, string password_, Verhicle_type verhicle_);
|
||||||
|
Customer(int id_, string name_, // needed to construct from db
|
||||||
|
string password_,
|
||||||
|
Verhicle_type verhicle_, // TODO: how init. p_time instances?
|
||||||
|
vector<Park_time> instances);
|
||||||
|
void clock_in(int s_id);
|
||||||
|
void clock_out(int s_id);
|
||||||
|
|
||||||
|
void update_db();
|
||||||
|
void delete_db();
|
||||||
|
|
||||||
|
void gen_monthly(); // remove, make it a function in data
|
||||||
|
Verhicle_type verhicle;
|
||||||
|
|
||||||
|
private:
|
||||||
|
vector<Park_time> park_instances;
|
||||||
|
void save_db();
|
||||||
|
int auto_increment_db();
|
||||||
|
};
|
||||||
|
|
||||||
|
static vector<Customer> park_customers;
|
||||||
|
// save the customers that are parked in here
|
||||||
|
// parking_spot uses pointers, so it's better to save the parked customers here
|
||||||
|
// where we know they'll be destroyed at the end of this scope, instead of too
|
||||||
|
// early and end up with dangling pointers
|
||||||
|
|
||||||
|
#endif // CUSTOMER_H
|
29
headers/Park_spot.h
Normal file
29
headers/Park_spot.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include "Customer.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
db representation:
|
||||||
|
int id not null
|
||||||
|
bool taken not null
|
||||||
|
int customer_id (null) (many to one, foreign key, whatever)
|
||||||
|
|
||||||
|
Dit representeert een parkeerplaats. Het heeft als internal state alleen dat t
|
||||||
|
bezet is of niet.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Park_spot {
|
||||||
|
public:
|
||||||
|
int id;
|
||||||
|
bool taken;
|
||||||
|
int parked_customer;
|
||||||
|
Park_spot();
|
||||||
|
Park_spot(int id_, bool taken_, Customer& parked);
|
||||||
|
void clock(Customer& c_customer);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void save_db();
|
||||||
|
void update_db();
|
||||||
|
void delete_db();
|
||||||
|
int auto_increment_db();
|
||||||
|
};
|
||||||
|
|
||||||
|
static vector<Park_spot> parking_spots; // to save the parking spots in memory
|
44
headers/Park_time.h
Normal file
44
headers/Park_time.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#ifndef PARK_TIME_H
|
||||||
|
#define PARK_TIME_H
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "data.h"
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <ctime>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std::chrono;
|
||||||
|
using std::cout;
|
||||||
|
using std::string;
|
||||||
|
using std::to_string;
|
||||||
|
/*
|
||||||
|
|
||||||
|
|
||||||
|
Record of who parked at what park_spot and at what time.
|
||||||
|
*/
|
||||||
|
|
||||||
|
class Park_time {
|
||||||
|
public:
|
||||||
|
Park_time(int c_id, int s_id);
|
||||||
|
Park_time(int id_, int customer_id_, int spot_id_, int start_,
|
||||||
|
int duration_);
|
||||||
|
int id;
|
||||||
|
int customer_id;
|
||||||
|
int spot_id;
|
||||||
|
int duration;
|
||||||
|
|
||||||
|
void clock_out(int c_id, int s_id);
|
||||||
|
friend std::ostream& operator<<(std::ostream& os, const Park_time& pt);
|
||||||
|
|
||||||
|
private:
|
||||||
|
high_resolution_clock::time_point start;
|
||||||
|
high_resolution_clock::time_point end;
|
||||||
|
void save_db();
|
||||||
|
void update_db();
|
||||||
|
int auto_increment_db(); // helper
|
||||||
|
int start_to_int(); // helper
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // Park_time
|
@ -5,8 +5,7 @@
|
|||||||
#include "encrypt.h"
|
#include "encrypt.h"
|
||||||
|
|
||||||
namespace data {
|
namespace data {
|
||||||
SQLite::Database
|
SQLite::Database start_db();
|
||||||
start_db();
|
|
||||||
static SQLite::Database db = start_db();
|
static SQLite::Database db = start_db();
|
||||||
|
|
||||||
} // namespace data
|
} // namespace data
|
||||||
|
16
main.cpp
16
main.cpp
@ -1,13 +1,11 @@
|
|||||||
#include "headers/data.h"
|
#include "headers/Park_spot.h"
|
||||||
|
|
||||||
#include <array>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
using namespace std::chrono;
|
using namespace std::chrono;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Code strucure like this:
|
Code strucure like this:
|
||||||
class declarations zijn in /headers/class_naam.h, en definitions van de member
|
class declarations zijn in /headers/class_naam.h, en definitions van de member
|
||||||
@ -33,13 +31,5 @@ a wait function where 1 sec represents 1 hour irl.
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
std::vector<string> test {
|
|
||||||
"werrgwergfwerrfvwerrf", "2rwefeqfeqfqewwfqwdfdwqdfqwdfd",
|
|
||||||
"qwerfqwefwqddweqdqwdwqed","qwefwqedwqdqwedwqdwqdwqd,",
|
|
||||||
"qwefqwedfwqdwqeddwqdqwedwqede "
|
|
||||||
};
|
|
||||||
#include <iostream>
|
|
||||||
for (auto i: test){
|
|
||||||
std::cout << hash_password(i) << "\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user