WIP refractoring

This commit is contained in:
TinyAtoms
2019-06-29 19:59:48 -03:00
parent 8160f84e20
commit a606119626
9 changed files with 27 additions and 17 deletions

100
old/Customer.cpp Normal file
View File

@@ -0,0 +1,100 @@
#include "headers/Customer.h"
#include <iostream>
// constructors
Customer::Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db):
name{name_}, verhicle{verhicle_},
card_code{gen_cardcode()}
{
id = auto_increment_db(db) + 1;
save_db(db);
}
Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, vector<Park_time> instances)
: name{name_},
card_code{card_code_},
verhicle{verhicle_},
park_instances{instances}
{
}
// clock methods ====================================================================================
/*
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 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";
std::cout << "-------------------------------------------------\n";
for (auto& i : park_instances) {
// TODO: need some logic to only include from this month
std::cout << i;
}
std::cout << "-------------------------------------------------\n\n";
}
//================================================================================================
// functions that interact with the database
void Customer::save_db(SQLite::Database& database) {
string statement{"insert into Customer values (, '', '', );"};
// after ( = 28)
statement.insert(38, std::to_string(int(verhicle)));
statement.insert(36, card_code);
statement.insert(32, name);
statement.insert(29, "null");
SQLite::Transaction transaction(database);
database.exec(statement);
transaction.commit();
}
void Customer::update_db(SQLite::Database& database) {
string statement = "UPDATE Customer SET name = \"\", card_code = \"\" where id = '';";
statement.insert(58, std::to_string(id));
statement.insert(44, card_code);
statement.insert(28, name);
// std::cout << statement; TODO: set some logging here
database.exec(statement);
}
void Customer::delete_db(SQLite::Database& database) {
string statement = "delete from Customer where id= ;";
statement.insert(statement.length() - 2, std::to_string(id));
// std::cout << statement;
SQLite::Transaction transaction(database);
database.exec(statement);
transaction.commit();
}
int Customer::auto_increment_db(SQLite::Database& database) {
SQLite::Statement max_id(database, "select max(id) from Customer;");
int id = 0;
max_id.executeStep();
id = max_id.getColumn(0);
max_id.reset();
return id;
}
// random helpers=============================================================
std::mt19937 mt(time(0));
std::uniform_int_distribution<int> dist(65, 127);
string Customer::gen_cardcode() {
string code;
for (int i = 0; i < 20; i++) {
char letter = char(dist(mt));
code += letter;
}
return code;
}

21
old/Park_spot.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include "headers/Park_spot.h"
Park_spot::Park_spot(int id_){
parked = nullptr;
id = id_;
taken = false;
}
// 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);
}
else{
taken = false;
parked->clock_out(id);
parked = nullptr;
}
}

56
old/Park_time.cpp Normal file
View File

@@ -0,0 +1,56 @@
#include"headers/Park_time.h"
#include <iostream>
#include <ctime>
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() }
{
}
void Park_time::clock_out(int c_id, int s_id)
{
if (c_id != customer_id) {
std::cout << "wrong customer id, you are at the wrong location";
return;
}
if (s_id != spot_id) {
std::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
} else {
std::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;
}
void Park_time::debug() {
auto start_to_epoch = start.time_since_epoch();
auto start_value = std::chrono::duration_cast<std::chrono::seconds>(start_to_epoch);
int start_seconds = start_value.count();
auto end_to_epoch = end.time_since_epoch();
auto end_value = std::chrono::duration_cast<std::chrono::seconds>(start_to_epoch);
int end_seconds = end_value.count();
std::cout << "<" << start_seconds << "-" << end_seconds << ">" ;
}

58
old/headers/Customer.h Normal file
View File

@@ -0,0 +1,58 @@
#ifndef CUSTOMER_H
#define CUSTOMER_H
#pragma once
#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
#include "Park_time.h"
#include <ctime>
#include <random>
#include <string>
#include <vector>
using std::string;
using std::vector;
// 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 {
small = 1,
medium = 2,
large = 3,
};
/*
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:
Customer(string name_, Verhicle_type verhicle_, SQLite::Database& db);
Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_,
vector<Park_time> instances); // needed to construct from db
// potentially: add a destructor that calls update_db() before being destroyed
int id;
string name;
string card_code;
void clock_in(int s_id);
void clock_out(int s_id);
void update_db(SQLite::Database& database);
void delete_db(SQLite::Database& database);
// void gen_weekly(); TODO: this
void gen_monthly();
private:
Verhicle_type verhicle;
vector<Park_time> park_instances;
string gen_cardcode();
void save_db(SQLite::Database& database);
int auto_increment_db(SQLite::Database& database);
};
#endif // CUSTOMER_H

21
old/headers/Park_spot.h Normal file
View File

@@ -0,0 +1,21 @@
#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;
Customer* parked; //TODO: think about memory management
Park_spot(int id_);
void clock(Customer* c_customer);
private:
};

44
old/headers/Park_time.h Normal file
View File

@@ -0,0 +1,44 @@
#ifndef PARK_TIME_H
#define PARK_TIME_H
#pragma once
#include <chrono>
#include <iostream>
using namespace std::chrono;
/*
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
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:
int id;
int customer_id;
int spot_id;
int duration;
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);
void debug();
private:
high_resolution_clock::time_point start;
high_resolution_clock::time_point end;
//TODO: discuss pros cons of using chrono, ctime, or 3th party lib
};
#endif // Park_time