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
SQLiteCpp

View File

@ -2,29 +2,21 @@
#include <iostream>
// moet aangepast worden om een verhicle_type toe te voegen
Customer::Customer(int id_, string name_)
: id { id_ }
, name { name_ }
{
}
Customer::Customer(int id_, string name_, Verhicle_type verhicle_) : id{id_}, name{name_}, verhicle{verhicle_}, card_code{gen_cardcode()} {}
/*
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_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);
}
void Customer::clock_out(int 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.
void Customer::gen_monthly(){
void Customer::gen_monthly() {
std::cout << "NAME: " << name << " card code: " << card_code << "\n";
std::cout << "-------------------------------------------------\n";
for (auto& i : park_instances) {
@ -33,3 +25,32 @@ void Customer::gen_monthly(){
}
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,13 +1,15 @@
#ifndef CUSTOMER_H
#define CUSTOMER_H
#pragma once
#include <vector>
#include <string>
#include "../thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
#include "Park_time.h"
#include <ctime>
#include <random>
#include <string>
#include <vector>
using std::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.
@ -31,7 +33,7 @@ clock in en out creeert en compleet een park_time object. Voegt het toe aan een
*/
class Customer {
public:
public:
int id;
string name;
string card_code;
@ -39,13 +41,13 @@ public:
void clock_out(int s_id);
// void gen_weekly(); TODO: this
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;
vector<Park_time> park_instances;
string gen_cardcode();
};
#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 "thirdparty/SQLiteCpp/include/SQLiteCpp/SQLiteCpp.h"
#include "headers/data.h"
#include <iostream>
#include <thread> // to make pausing work, not sure if i need chrono, or this, or both
#include <vector>
@ -30,18 +30,11 @@ a wait function where 1 sec represents 1 hour irl.
using std::cout;
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)");
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();
SQLite::Database db = data::start_db();
// search example
SQLite::Statement get_sagar(db, "select * from Customer where name like '%sagar%' ");
while (get_sagar.executeStep()) {
int id = get_sagar.getColumn(0);
string name = get_sagar.getColumn(1);
@ -49,4 +42,7 @@ int main() {
int car = get_sagar.getColumn(3);
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.