Compare commits
No commits in common. "97ed3150ecfdac94e5c42b20914c448e751c27a4" and "0eee67d3d94dd4bdc77a9d90268b62b403bd3312" have entirely different histories.
97ed3150ec
...
0eee67d3d9
13
Customer.cpp
13
Customer.cpp
@ -10,7 +10,6 @@ Customer::Customer(string name_, Verhicle_type verhicle_):
|
||||
save_db();
|
||||
}
|
||||
|
||||
|
||||
Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, vector<Park_time> instances)
|
||||
: name{name_},
|
||||
card_code{card_code_},
|
||||
@ -21,20 +20,19 @@ Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhi
|
||||
}
|
||||
|
||||
|
||||
// clock in/out methods ====================================================================================
|
||||
|
||||
// clock methods ====================================================================================
|
||||
/*
|
||||
Create a p_time object with start=now and adds to vector
|
||||
creert een park_time object met start time= nu, en voegt t toe aan een 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
|
||||
// edit de laatste park_time obj in de vector zodat de end_time = 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() {
|
||||
std::cout << "NAME: " << name << " card code: " << card_code << "\n";
|
||||
@ -46,7 +44,6 @@ void Customer::gen_monthly() {
|
||||
std::cout << "-------------------------------------------------\n\n";
|
||||
}
|
||||
|
||||
|
||||
//================================================================================================
|
||||
// functions that interact with the database
|
||||
|
||||
@ -56,7 +53,7 @@ void Customer::save_db() {
|
||||
statement.insert(38, std::to_string(int(verhicle)));
|
||||
statement.insert(36, card_code);
|
||||
statement.insert(32, name);
|
||||
statement.insert(29, to_string(id));
|
||||
statement.insert(29, "null");
|
||||
SQLite::Transaction transaction(data::db);
|
||||
data::db.exec(statement);
|
||||
transaction.commit();
|
||||
|
@ -1,79 +1,21 @@
|
||||
#include "headers/Park_spot.h"
|
||||
|
||||
|
||||
|
||||
// constructors
|
||||
|
||||
Park_spot::Park_spot(){
|
||||
Park_spot::Park_spot(int id_){
|
||||
parked = nullptr;
|
||||
id = auto_increment_db() + 1;
|
||||
id = id_;
|
||||
taken = false;
|
||||
save_db();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 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 = c_customer;
|
||||
taken = true;
|
||||
parked->clock_in(id);
|
||||
update_db();
|
||||
}
|
||||
else{
|
||||
taken = false;
|
||||
parked->clock_out(id);
|
||||
parked = nullptr;
|
||||
update_db();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// --------------------- db functs
|
||||
|
||||
void Park_spot::update_db() {
|
||||
string statement = "UPDATE Park_spot SET taken = '', customer_id = '' where id = '';";
|
||||
// 29, 48, 62
|
||||
statement.insert(63, to_string(id));
|
||||
if (taken){
|
||||
statement.insert(49, to_string(parked->id));
|
||||
statement.insert(30, "true");
|
||||
}
|
||||
else {
|
||||
statement.insert(49, "NULL");
|
||||
statement.insert(30, "false");
|
||||
}
|
||||
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, "false");
|
||||
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, std::to_string(id));
|
||||
// std::cout << statement;
|
||||
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;
|
||||
}
|
@ -77,6 +77,7 @@ void Park_time::update_db() {
|
||||
statement.insert(53, std::to_string(id));
|
||||
statement.insert(40, to_string(duration));
|
||||
statement.insert(27, to_string(start_to_int() + duration));
|
||||
std::cout << statement; // TODO: set some logging here
|
||||
data::db.exec(statement);
|
||||
}
|
||||
|
||||
|
@ -12,12 +12,12 @@
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
// will make it easy to represent it in the database while making it easy to use while programming
|
||||
// enum type is basically een manier om categories te representen als een integer in the background, maar om t in code
|
||||
// aan te geven als de actual category.
|
||||
enum class Verhicle_type {
|
||||
bike = 1,
|
||||
small_car = 2,
|
||||
suv = 3,
|
||||
pickup = 4
|
||||
small = 1,
|
||||
medium = 2,
|
||||
large = 3,
|
||||
};
|
||||
|
||||
/*
|
||||
@ -43,13 +43,15 @@ class Customer {
|
||||
void update_db();
|
||||
void delete_db();
|
||||
|
||||
void gen_monthly(); // remove, make it a function in data
|
||||
// void gen_weekly(); TODO: this
|
||||
void gen_monthly();
|
||||
|
||||
private:
|
||||
Verhicle_type verhicle;
|
||||
vector<Park_time> park_instances;
|
||||
|
||||
string gen_cardcode();
|
||||
|
||||
void save_db();
|
||||
int auto_increment_db();
|
||||
};
|
||||
|
@ -15,11 +15,7 @@ class Park_spot {
|
||||
int id;
|
||||
bool taken;
|
||||
Customer* parked; //TODO: think about memory management
|
||||
Park_spot();
|
||||
Park_spot(int id_);
|
||||
void clock(Customer* c_customer);
|
||||
private:
|
||||
void save_db();
|
||||
void update_db();
|
||||
void delete_db();
|
||||
int auto_increment_db();
|
||||
};
|
@ -12,20 +12,26 @@ using namespace std::chrono;
|
||||
using std::string;
|
||||
using std::to_string;
|
||||
/*
|
||||
db repr of Park_time
|
||||
int id (not null, auto increment)
|
||||
int customer_id (not null) (many to one or something like that)
|
||||
int spot_id (not null, many to one or something like that)
|
||||
int duration
|
||||
datetime start (not null)
|
||||
datetime end
|
||||
|
||||
|
||||
Record of who parked at what park_spot and at what time.
|
||||
Dit is gewoon een record van hoe lang, wie en waar iemand parkeert. Basically, een component van
|
||||
de internal state van customer.
|
||||
*/
|
||||
|
||||
class Park_time {
|
||||
public:
|
||||
Park_time(int c_id, int s_id);
|
||||
// Park_time(int c_id, int s_id );
|
||||
int id;
|
||||
int customer_id;
|
||||
int spot_id;
|
||||
int duration;
|
||||
|
||||
Park_time(int c_id, int s_id);
|
||||
// Park_time(int c_id, int s_id );
|
||||
void clock_out(int c_id, int s_id);
|
||||
friend std::ostream& operator<<(std::ostream& os, const Park_time & pt);
|
||||
|
||||
|
21
main.cpp
21
main.cpp
@ -5,8 +5,6 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <thread>
|
||||
|
||||
/*
|
||||
Code strucure like this:
|
||||
class declarations zijn in /headers/class_naam.h, en definitions van de member
|
||||
@ -22,25 +20,16 @@ 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.
|
||||
*/
|
||||
{
|
||||
std::this_thread::sleep_for(seconds{sec});
|
||||
}
|
||||
|
||||
|
||||
|
||||
using std::cout;
|
||||
|
||||
int main() {
|
||||
class Customer sagar{"Sagar Ramsaransing", Verhicle_type::bike};
|
||||
class Customer sagar{"nonsense", Verhicle_type::medium};
|
||||
sagar.update_db();
|
||||
Park_spot p1;
|
||||
p1.clock(&sagar);
|
||||
Wait(2);
|
||||
p1.clock(&sagar);
|
||||
|
||||
Park_spot p1{1};
|
||||
p1.clock(&sagar);
|
||||
p1.clock(&sagar);
|
||||
sagar.gen_monthly();
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user