Succesful refactor #3

Merged
MassiveAtoms merged 7 commits from refactor into sqlitecpp 2019-06-30 13:12:37 +00:00
8 changed files with 44 additions and 43 deletions
Showing only changes of commit 085cd5af08 - Show all commits

View File

@ -1,5 +1,4 @@
#include "headers/Customer.h" #include "headers/Customer.h"
#include <iostream>
// constructors // constructors
Customer::Customer(string name_, Verhicle_type verhicle_): Customer::Customer(string name_, Verhicle_type verhicle_):
@ -20,6 +19,10 @@ Customer::Customer(int id_, string name_, string card_code_, Verhicle_type verhi
} }
Customer::~Customer(){
update_db();
}
// clock in/out methods ==================================================================================== // clock in/out methods ====================================================================================
/* /*
@ -37,13 +40,13 @@ void Customer::clock_out(int s_id) { park_instances[park_instances.size() - 1].c
// report gen // report gen
void Customer::gen_monthly() { void Customer::gen_monthly() {
std::cout << "NAME: " << name << " card code: " << card_code << "\n"; cout << "NAME: " << name << " card code: " << card_code << "\n";
std::cout << "-------------------------------------------------\n"; cout << "-------------------------------------------------\n";
for (auto& i : park_instances) { for (auto& i : park_instances) {
// TODO: need some logic to only include from this month // TODO: need some logic to only include from this month. scratch that, need to remove gen monthly
std::cout << i; cout << i;
} }
std::cout << "-------------------------------------------------\n\n"; cout << "-------------------------------------------------\n\n";
} }
@ -53,7 +56,7 @@ void Customer::gen_monthly() {
void Customer::save_db() { void Customer::save_db() {
string statement{"insert into Customer values (, '', '', );"}; string statement{"insert into Customer values (, '', '', );"};
// after ( = 28) // after ( = 28)
statement.insert(38, std::to_string(int(verhicle))); statement.insert(38, to_string(int(verhicle)));
statement.insert(36, card_code); statement.insert(36, card_code);
statement.insert(32, name); statement.insert(32, name);
statement.insert(29, to_string(id)); statement.insert(29, to_string(id));
@ -64,17 +67,15 @@ void Customer::save_db() {
void Customer::update_db() { void Customer::update_db() {
string statement = "UPDATE Customer SET name = '', card_code = '' where id = '';"; string statement = "UPDATE Customer SET name = '', card_code = '' where id = '';";
statement.insert(58, std::to_string(id)); statement.insert(58, to_string(id));
statement.insert(44, card_code); statement.insert(44, card_code);
statement.insert(28, name); statement.insert(28, name);
// std::cout << statement; TODO: set some logging here
data::db.exec(statement); data::db.exec(statement);
} }
void Customer::delete_db() { void Customer::delete_db() {
string statement = "delete from Customer where id= ;"; string statement = "delete from Customer where id= ;";
statement.insert(statement.length() - 2, std::to_string(id)); statement.insert(statement.length() - 2, to_string(id));
// std::cout << statement;
SQLite::Transaction transaction(data::db); SQLite::Transaction transaction(data::db);
data::db.exec(statement); data::db.exec(statement);
transaction.commit(); transaction.commit();

View File

@ -4,13 +4,17 @@
// constructors // constructors
Park_spot::Park_spot(){ Park_spot::Park_spot()
parked = nullptr; : parked{nullptr},
id = auto_increment_db() + 1; id{auto_increment_db()+1},
taken = false; taken{false} {
save_db(); save_db();
} }
Park_spot::~Park_spot(){
update_db();
}
// clock in en out, calls de juist(in/out) van de customer aan de hand van internal state van taken // clock in en out, calls de juist(in/out) van de customer aan de hand van internal state van taken
@ -62,8 +66,7 @@ void Park_spot::save_db() {
void Park_spot::delete_db() { void Park_spot::delete_db() {
string statement = "delete from Park_spot where id= ;"; string statement = "delete from Park_spot where id= ;";
statement.insert(statement.length() - 2, std::to_string(id)); statement.insert(statement.length() - 2, to_string(id));
// std::cout << statement;
SQLite::Transaction transaction(data::db); SQLite::Transaction transaction(data::db);
data::db.exec(statement); data::db.exec(statement);
transaction.commit(); transaction.commit();

View File

@ -1,6 +1,4 @@
#include"headers/Park_time.h" #include"headers/Park_time.h"
#include <iostream>
#include <ctime>
Park_time::Park_time(int c_id, int s_id) Park_time::Park_time(int c_id, int s_id)
@ -13,15 +11,19 @@ Park_time::Park_time(int c_id, int s_id)
save_db(); save_db();
} }
Park_time::~Park_time(){
update_db();
}
void Park_time::clock_out(int c_id, int s_id) void Park_time::clock_out(int c_id, int s_id)
{ {
if (c_id != customer_id) { if (c_id != customer_id) {
std::cout << "wrong customer id, you are at the wrong location"; cout << "wrong customer id, you are at the wrong location";
return; return;
} }
if (s_id != spot_id) { if (s_id != spot_id) {
std::cout << "Wrong spot id, you're at the wrong location"; cout << "Wrong spot id, you're at the wrong location";
return; return;
} }
@ -31,7 +33,7 @@ void Park_time::clock_out(int c_id, int s_id)
update_db(); update_db();
} else { } else {
std::cout << "Already clocked out. Something is wrong \n"; cout << "Already clocked out. Something is wrong \n";
} }
} }
@ -50,7 +52,7 @@ std::ostream& operator<<(std::ostream& os, const Park_time & pt){
int Park_time::start_to_int(){ int Park_time::start_to_int(){
auto start_to_epoch = start.time_since_epoch(); auto start_to_epoch = start.time_since_epoch();
auto start_value = std::chrono::duration_cast<std::chrono::seconds>(start_to_epoch); auto start_value = duration_cast<seconds>(start_to_epoch);
int start_seconds = start_value.count(); int start_seconds = start_value.count();
return start_seconds; return start_seconds;
} }
@ -74,7 +76,7 @@ void Park_time::save_db() {
void Park_time::update_db() { void Park_time::update_db() {
string statement = "UPDATE Park_time SET end = , duration = where id = '';"; string statement = "UPDATE Park_time SET end = , duration = where id = '';";
statement.insert(53, std::to_string(id)); statement.insert(53, to_string(id));
statement.insert(40, to_string(duration)); statement.insert(40, to_string(duration));
statement.insert(27, to_string(start_to_int() + duration)); statement.insert(27, to_string(start_to_int() + duration));
data::db.exec(statement); data::db.exec(statement);

View File

@ -1,15 +1,14 @@
#ifndef CUSTOMER_H #ifndef CUSTOMER_H
#define CUSTOMER_H #define CUSTOMER_H
#pragma once #pragma once
#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
#include "Park_time.h"
#include <ctime>
#include <random>
#include <string>
#include <vector>
#include "data.h"
using std::string;
#include "Park_time.h"
#include "data.h"
#include <random>
#include <vector>
using std::vector; using std::vector;
// will make it easy to represent it in the database while making it easy to use while programming // will make it easy to represent it in the database while making it easy to use while programming
@ -32,7 +31,7 @@ class Customer {
Customer(string name_, Verhicle_type verhicle_); Customer(string name_, Verhicle_type verhicle_);
Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_, Customer(int id_, string name_, string card_code_, Verhicle_type verhicle_,
vector<Park_time> instances); // needed to construct from db vector<Park_time> instances); // needed to construct from db
// potentially: add a destructor that calls update_db() before being destroyed ~Customer();
int id; int id;
string name; string name;
string card_code; string card_code;

View File

@ -14,8 +14,9 @@ class Park_spot {
public: public:
int id; int id;
bool taken; bool taken;
Customer* parked; //TODO: think about memory management Customer* parked;
Park_spot(); Park_spot();
~Park_spot();
void clock(Customer* c_customer); void clock(Customer* c_customer);
private: private:
void save_db(); void save_db();

View File

@ -5,12 +5,14 @@
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <ctime>
#include "data.h" #include "data.h"
using namespace std::chrono; using namespace std::chrono;
using std::string; using std::string;
using std::to_string; using std::to_string;
using std::cout;
/* /*
@ -21,6 +23,7 @@ class Park_time {
public: public:
Park_time(int c_id, int s_id); Park_time(int c_id, int s_id);
// Park_time(int c_id, int s_id ); // Park_time(int c_id, int s_id );
~Park_time();
int id; int id;
int customer_id; int customer_id;
int spot_id; int spot_id;

View File

@ -1,9 +1,4 @@
#include "thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
#include "headers/data.h"
#include "headers/Park_spot.h" #include "headers/Park_spot.h"
#include <iostream>
#include <vector>
#include <string>
#include <thread> #include <thread>
@ -31,16 +26,13 @@ a wait function where 1 sec represents 1 hour irl.
} }
using std::cout;
int main() { int main() {
class Customer sagar{"Sagar Ramsaransing", Verhicle_type::bike}; class Customer sagar{"Sagar Ramsaransing", Verhicle_type::bike};
sagar.update_db(); sagar.update_db();
Park_spot p1; Park_spot p1;
p1.clock(&sagar); p1.clock(&sagar);
Wait(2); Wait(2);
p1.clock(&sagar); // p1.clock(&sagar);
} }

BIN
test.db3

Binary file not shown.