Compare commits
3 Commits
5fa84e866f
...
9ae95aef1c
Author | SHA1 | Date | |
---|---|---|---|
9ae95aef1c | |||
601f6c92bc | |||
cf1cfdfd79 |
@ -11,16 +11,18 @@ include_directories(
|
||||
|
||||
add_executable(park
|
||||
main.cpp
|
||||
|
||||
data.cpp
|
||||
headers/data.h
|
||||
encrypt.cpp
|
||||
headers/encrypt.h
|
||||
|
||||
Customer.cpp
|
||||
headers/Customer.h
|
||||
Park_spot.cpp
|
||||
headers/Park_spot.h
|
||||
Park_time.cpp
|
||||
headers/Park_time.h
|
||||
encrypt.cpp
|
||||
headers/encrypt.h
|
||||
Query.cpp
|
||||
headers/Query.h
|
||||
)
|
||||
@ -34,7 +36,7 @@ if (UNIX)
|
||||
sqlite3
|
||||
pthread
|
||||
dl
|
||||
libsodium
|
||||
sodium
|
||||
)
|
||||
elseif (MSYS OR MINGW)
|
||||
target_link_libraries(park
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
// constructors
|
||||
Customer::Customer(string name_, string password_, Verhicle_type verhicle_)
|
||||
: name{name_}, verhicle{verhicle_}, password{hash_password(password)} {
|
||||
: name{name_}, verhicle{verhicle_}, password{hash_password(password_)} {
|
||||
id = auto_increment_db() + 1;
|
||||
save_db();
|
||||
}
|
||||
|
@ -3,12 +3,12 @@
|
||||
// constructors
|
||||
|
||||
Park_spot::Park_spot()
|
||||
: parked{nullptr}, id{auto_increment_db() + 1}, taken{false} {
|
||||
: parked_customer{0}, id{auto_increment_db() + 1}, taken{false} {
|
||||
save_db();
|
||||
}
|
||||
|
||||
Park_spot::Park_spot(Customer* parked_, int id_, bool taken_)
|
||||
: parked{nullptr},
|
||||
Park_spot::Park_spot(int id_, bool taken_, int parked)
|
||||
: parked_customer{parked},
|
||||
id{id_},
|
||||
taken{taken_} // TODO: think about how init parked?
|
||||
{}
|
||||
@ -16,16 +16,16 @@ Park_spot::Park_spot(Customer* parked_, int id_, bool taken_)
|
||||
|
||||
// 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) {
|
||||
void Park_spot::clock(Customer& c_customer) {
|
||||
if (!taken) {
|
||||
parked = c_customer;
|
||||
parked_customer = c_customer.id;
|
||||
taken = true;
|
||||
parked->clock_in(id);
|
||||
c_customer.clock_in(id);
|
||||
update_db();
|
||||
} else {
|
||||
taken = false;
|
||||
parked->clock_out(id);
|
||||
parked = nullptr;
|
||||
c_customer.clock_out(id);
|
||||
parked_customer = 0;
|
||||
update_db();
|
||||
}
|
||||
}
|
||||
@ -37,7 +37,7 @@ void Park_spot::update_db() {
|
||||
"UPDATE Park_spot SET taken = '', customer_id = '' where id = '';";
|
||||
statement.insert(63, to_string(id));
|
||||
if (taken) {
|
||||
statement.insert(49, to_string(parked->id));
|
||||
statement.insert(49, to_string(parked_customer));
|
||||
statement.insert(30, "1");
|
||||
} else {
|
||||
statement.insert(49, "NULL");
|
||||
|
34
Query.cpp
34
Query.cpp
@ -25,6 +25,10 @@ vector<Park_time> query_parktimes_for_customer(int cid) {
|
||||
return park_times;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------- customers
|
||||
|
||||
|
||||
vector<Customer> query_customer_with_name(string name) {
|
||||
/*
|
||||
We use this instead of plain customers because:
|
||||
@ -66,33 +70,25 @@ Customer query_customer_with_id(int id) {
|
||||
Customer result{
|
||||
id, name, password, Verhicle_type(verhicle), park_instances};
|
||||
// DEBUG
|
||||
cout << "{" << result.id << "," <<result.password <<"," << int(verhicle) << "}\n";
|
||||
// cout << "{" << result.id << "," <<result.password <<"," << int(verhicle) << "}\n";
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
void query_all_parking_spots() {
|
||||
SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > ?;");
|
||||
query.bind(1, 0);
|
||||
|
||||
// -------------- paroking spots
|
||||
|
||||
vector<Park_spot> query_all_parking_spots() {
|
||||
vector<Park_spot> spots;
|
||||
SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > 2;");
|
||||
// query.bind(1, 2);
|
||||
while (query.executeStep()) {
|
||||
int id = query.getColumn(0);
|
||||
int taken = query.getColumn(1);
|
||||
int cid = query.getColumn(2);
|
||||
park_customers.push_back(query_customer_with_id(cid));
|
||||
parking_spots.push_back(
|
||||
Park_spot{get_customer_ptr_for_parkspot(cid), id, bool(taken)});
|
||||
// park_customers.push_back(query_customer_with_id(cid));
|
||||
spots.push_back({id, taken, cid});
|
||||
}
|
||||
return spots;
|
||||
}
|
||||
|
||||
Customer* get_customer_ptr_for_parkspot(int id) {
|
||||
if (!id) {
|
||||
return nullptr;
|
||||
}
|
||||
for (int i = 0; i < park_customers.size(); i++) {
|
||||
if (park_customers[i].id == id) {
|
||||
|
||||
return &park_customers[i];
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
1
data.cpp
1
data.cpp
@ -8,6 +8,7 @@ SQLite::Database start_db() {
|
||||
while (sodium_init() < 0) {
|
||||
std::cout << "SODIUM NOT WORKING";
|
||||
}
|
||||
|
||||
db.exec(
|
||||
"create table if not exists Customer (id integer primary key, name "
|
||||
"text, password text, verhicle int)");
|
||||
|
@ -9,8 +9,8 @@ string hash_password(string password) {
|
||||
*/
|
||||
const char* password_ = password.c_str();
|
||||
char hashed_password_[crypto_pwhash_STRBYTES];
|
||||
int memory_limit = 1.28e+8; // 1.28 e+8 = 128 e6 = 128 mb
|
||||
int cpu_limit = 2; // this is n_threads
|
||||
int memory_limit = 3.2e+7; // 3.2e7 = 32e6 = 32 mb
|
||||
int cpu_limit = 1; // this is n_threads
|
||||
|
||||
int result = crypto_pwhash_str(hashed_password_,
|
||||
password_,
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
#include "Park_time.h"
|
||||
#include "data.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
using std::vector;
|
||||
@ -22,7 +23,6 @@ park_time object. Voegt het toe aan een vector.
|
||||
|
||||
class Customer {
|
||||
public:
|
||||
|
||||
int id;
|
||||
string name;
|
||||
string password;
|
||||
@ -38,17 +38,13 @@ class Customer {
|
||||
void delete_db();
|
||||
|
||||
void gen_monthly(); // remove, make it a function in data
|
||||
Verhicle_type verhicle;
|
||||
|
||||
private:
|
||||
Verhicle_type verhicle;
|
||||
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
|
@ -1,5 +1,8 @@
|
||||
#include "Customer.h"
|
||||
#ifndef PARK_SPOT_H
|
||||
#define PARK_SPOT_H
|
||||
#pragma once
|
||||
|
||||
#include "Customer.h"
|
||||
/*
|
||||
db representation:
|
||||
int id not null
|
||||
@ -14,10 +17,10 @@ class Park_spot {
|
||||
public:
|
||||
int id;
|
||||
bool taken;
|
||||
Customer* parked;
|
||||
int parked_customer;
|
||||
Park_spot();
|
||||
Park_spot(Customer* parked_, int id_, bool taken_);
|
||||
void clock(Customer* c_customer);
|
||||
Park_spot(int id_, bool taken_, int parked);
|
||||
void clock(Customer& c_customer);
|
||||
|
||||
private:
|
||||
void save_db();
|
||||
@ -25,5 +28,4 @@ class Park_spot {
|
||||
void delete_db();
|
||||
int auto_increment_db();
|
||||
};
|
||||
|
||||
static vector<Park_spot> parking_spots; // to save the parking spots in memory
|
||||
#endif // CUSTOMER_H
|
@ -10,9 +10,12 @@ vector<Park_time> query_parktimes_for_customer(int cid);
|
||||
|
||||
vector<Customer> query_customer_with_name(string name);
|
||||
Customer query_customer_with_id(int id);
|
||||
Customer* get_customer_ptr_for_parkspot(int id);
|
||||
|
||||
void query_all_parking_spots(); // used for initializing the parking spots at start of the program
|
||||
vector<Park_spot> query_all_parking_spots(); // used for initializing the parking spots at start of the program
|
||||
|
||||
static vector<Park_spot> parking_spots = query_all_parking_spots(); // to save the parking spots in memory
|
||||
static vector<Customer> park_customers;
|
||||
// save the customers that are parked in here
|
||||
|
||||
|
||||
#endif // CUSTOMER_H
|
@ -5,8 +5,8 @@
|
||||
#include "encrypt.h"
|
||||
|
||||
namespace data {
|
||||
SQLite::Database
|
||||
start_db();
|
||||
SQLite::Database start_db();
|
||||
|
||||
static SQLite::Database db = start_db();
|
||||
|
||||
} // namespace data
|
||||
|
37
main.cpp
37
main.cpp
@ -1,8 +1,11 @@
|
||||
#include "headers/Query.h"
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
/*
|
||||
Code strucure like this:
|
||||
class declarations zijn in /headers/class_naam.h, en definitions van de member
|
||||
@ -18,9 +21,6 @@ record die zegt dat een customer voor x tijd geparkeert heeft bij spot x, enz.
|
||||
De client clockt in en uit bij een spot.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
void Wait(int sec)
|
||||
/*
|
||||
a wait function where 1 sec represents 1 hour irl.
|
||||
@ -29,29 +29,14 @@ a wait function where 1 sec represents 1 hour irl.
|
||||
std::this_thread::sleep_for(seconds{sec});
|
||||
}
|
||||
|
||||
Customer* get_customer_ptr_for_parkspot(int id);
|
||||
|
||||
int main() {
|
||||
query_all_parking_spots();
|
||||
|
||||
Customer p0 = query_customer_with_name("Shaquile")[0];
|
||||
Customer p1 = query_customer_with_name("Sagar Ramsaransing")[0];
|
||||
Customer p2 = query_customer_with_name("Joshua karto")[0];
|
||||
Customer p3 = query_customer_with_name("Stefan udit")[0];
|
||||
|
||||
parking_spots[2].clock(&p1);
|
||||
Wait(2);
|
||||
parking_spots[2].clock(&p1);
|
||||
Wait(1);
|
||||
parking_spots[0].clock(&p2);
|
||||
Wait(1);
|
||||
parking_spots[1].clock(&p3);
|
||||
Wait(1);
|
||||
parking_spots[0].clock(&p2);
|
||||
parking_spots[1].clock(&p3);
|
||||
Wait(1);
|
||||
parking_spots[1].clock(&p3);
|
||||
// Customer sagar = query_customer_with_name("stefan udit")[0];
|
||||
Customer sagar = query_customer_with_id(2);
|
||||
cout << sagar.id << "," << sagar.name << "," << sagar.password;
|
||||
// cout << parking_spots.size();
|
||||
|
||||
// for (auto i : parking_spots){
|
||||
// cout << i.id << "," << i.parked_customer;
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user