customer can be saved into db

This commit is contained in:
MassiveAtoms 2019-06-26 15:12:23 -03:00
parent 2e22008040
commit f11ebc6a9c
7 changed files with 75 additions and 36 deletions

View File

@ -9,7 +9,7 @@ include_directories(
) )
add_executable(park main.cpp Customer.cpp headers/Customer.h Park_spot.cpp headers/Park_spot.h Park_time.cpp headers/Park_time.h) add_executable(park main.cpp data.cpp headers/data.h Customer.cpp headers/Customer.h Park_spot.cpp headers/Park_spot.h Park_time.cpp headers/Park_time.h)
target_link_libraries(park target_link_libraries(park
SQLiteCpp SQLiteCpp

View File

@ -2,29 +2,21 @@
#include <iostream> #include <iostream>
// moet aangepast worden om een verhicle_type toe te voegen // moet aangepast worden om een verhicle_type toe te voegen
Customer::Customer(int id_, string name_) Customer::Customer(int id_, string name_, Verhicle_type verhicle_) : id{id_}, name{name_}, verhicle{verhicle_}, card_code{gen_cardcode()} {}
: id { id_ }
, name { name_ }
{
}
/* /*
creert een park_time object met start time= nu, en voegt t toe aan een vector. creert een park_time object met start time= nu, en voegt t toe aan een vector.
*/ */
void Customer::clock_in( int s_id) void Customer::clock_in(int s_id) {
{
Park_time pt{id, s_id}; Park_time pt{id, s_id};
park_instances.push_back(pt); park_instances.push_back(pt);
} }
// edit de laatste park_time obj in de vector zodat de end_time = now. // edit de laatste park_time obj in de vector zodat de end_time = now.
void Customer::clock_out(int s_id){ void Customer::clock_out(int s_id) { park_instances[park_instances.size() - 1].clock_out(id, s_id); }
park_instances[park_instances.size()-1].clock_out(id, s_id);
}
// monthly report generation. moet nog een manier vinden om af te bakenen. // monthly report generation. moet nog een manier vinden om af te bakenen.
void Customer::gen_monthly(){ void Customer::gen_monthly() {
std::cout << "NAME: " << name << " card code: " << card_code << "\n"; std::cout << "NAME: " << name << " card code: " << card_code << "\n";
std::cout << "-------------------------------------------------\n"; std::cout << "-------------------------------------------------\n";
for (auto& i : park_instances) { for (auto& i : park_instances) {
@ -32,4 +24,33 @@ void Customer::gen_monthly(){
std::cout << i; std::cout << i;
} }
std::cout << "-------------------------------------------------\n\n"; std::cout << "-------------------------------------------------\n\n";
} }
void Customer::update_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, std::to_string(id));
std::cout << statement;
SQLite::Transaction transaction(database);
database.exec(statement);
transaction.commit();
}
//
// used to generate random card codes that will be used to authenticate users.
// they represent contactless rf cards that users will use to authenticate
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;
}

14
data.cpp Normal file
View File

@ -0,0 +1,14 @@
#include "headers/data.h"
namespace data {
SQLite::Database start_db() {
SQLite::Database db("test.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
db.exec("create table if not exists Customer (id integer primary key, name text, card_code varchar(20), verhicle int)");
db.exec("create table if not exists Park_spot (id integer primary key, taken boolean, customer_id int)");
db.exec("create table if not exists Park_time (id integer primary key, customer_id int, spot_id int, start real, end real, duration real)");
return db;
}
} // namespace data

View File

@ -1,15 +1,17 @@
#ifndef CUSTOMER_H #ifndef CUSTOMER_H
#define CUSTOMER_H #define CUSTOMER_H
#pragma once #pragma once
#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
#include <vector>
#include <string>
#include "Park_time.h" #include "Park_time.h"
#include <ctime>
#include <random>
#include <string>
#include <vector>
using std::vector;
using std::string; 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 // 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. // aan te geven als de actual category.
enum class Verhicle_type { enum class Verhicle_type {
small = 1, small = 1,
@ -22,16 +24,16 @@ db repr of Customer
int id (not null, auto increment) int id (not null, auto increment)
string name (not nulll) string name (not nulll)
string card_code (not null) string card_code (not null)
Dit moet nog verandert worden. Dit moet nog verandert worden.
card code zou eigenlijk een randomly generated string moeten zijn, die je bv. op een ndf card zou opslaan en zo zou card code zou eigenlijk een randomly generated string moeten zijn, die je bv. op een ndf card zou opslaan en zo zou
authenticaten bij je parking spot. We kunnen dit ipv of samen met een password gebruiken. 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. clock in en out creeert en compleet een park_time object. Voegt het toe aan een vector.
*/ */
class Customer { class Customer {
public: public:
int id; int id;
string name; string name;
string card_code; string card_code;
@ -39,13 +41,13 @@ public:
void clock_out(int s_id); void clock_out(int s_id);
// void gen_weekly(); TODO: this // void gen_weekly(); TODO: this
void gen_monthly(); void gen_monthly();
Customer(int id_, string name_); Customer(int id_, string name_, Verhicle_type verhicle_);
void update_db(SQLite::Database& database);
private: private:
Verhicle_type verhicle; Verhicle_type verhicle;
vector<Park_time> park_instances; vector<Park_time> park_instances;
string gen_cardcode();
}; };
#endif // CUSTOMER_H #endif // CUSTOMER_H

6
headers/data.h Normal file
View File

@ -0,0 +1,6 @@
#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
#include "Customer.h"
namespace data {
SQLite::Database start_db();
}

View File

@ -1,5 +1,5 @@
#include "headers/Park_spot.h" #include "headers/Park_spot.h"
#include "thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h" #include "headers/data.h"
#include <iostream> #include <iostream>
#include <thread> // to make pausing work, not sure if i need chrono, or this, or both #include <thread> // to make pausing work, not sure if i need chrono, or this, or both
#include <vector> #include <vector>
@ -30,18 +30,11 @@ a wait function where 1 sec represents 1 hour irl.
using std::cout; using std::cout;
int main() { int main() {
SQLite::Database db("test.db3", SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE);
db.exec("create table if not exists Customer (id integer primary key, name text, card_code varchar(20), verhicle int)"); SQLite::Database db = data::start_db();
db.exec("create table if not exists Park_spot (id integer primary key, taken boolean, customer_id int)");
db.exec("create table if not exists Park_time (id integer primary key, customer_id int, spot_id int, start real, end real, duration real)");
SQLite::Transaction transaction(db);
db.exec("insert into Customer values (NULL, 'sagar ram', 'aqwsderfgtaqwsderfgt', 2)");
transaction.commit();
// search example
SQLite::Statement get_sagar(db, "select * from Customer where name like '%sagar%' "); SQLite::Statement get_sagar(db, "select * from Customer where name like '%sagar%' ");
while (get_sagar.executeStep()) { while (get_sagar.executeStep()) {
int id = get_sagar.getColumn(0); int id = get_sagar.getColumn(0);
string name = get_sagar.getColumn(1); string name = get_sagar.getColumn(1);
@ -49,4 +42,7 @@ int main() {
int car = get_sagar.getColumn(3); int car = get_sagar.getColumn(3);
cout << id << "," << name << "," << code << "," << car << std::endl; cout << id << "," << name << "," << code << "," << car << std::endl;
} }
Customer sagar{15, "Ramsaransing", Verhicle_type::medium};
sagar.update_db(db);
} }

BIN
test.db3

Binary file not shown.