code compiles, doesnt run tho

This commit is contained in:
MassiveAtoms 2019-06-24 21:42:08 -03:00
parent 9a10e33279
commit ecbf41ee07
6 changed files with 117 additions and 118 deletions

9
.clang-format Normal file
View File

@ -0,0 +1,9 @@
BasedOnStyle: LLVM
IndentWidth: 4
#-------------
Language: Cpp
PointerAlignment: Left
ColumnLimit: 160

60
db.cpp
View File

@ -1,21 +1,55 @@
#include "headers/db.h"
namespace db
{
namespace data {
char* error_message; // no clue, prolly error message
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++)
{
std::cout<<azColName[i]<<" = " << (argv[i] ? argv[i] : "NULL")<<"\n";
}
std::cout<<"\n";
return 0;
static int callback(void* NotUsed, int argc, char** argv, char** azColName) {
int i;
for (i = 0; i < argc; i++) {
std::cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << "\n";
}
std::cout << "\n";
return 0;
}
void open_db(sqlite3* db) {
int status = sqlite3_open("database.sqlite", &db);
if (!status) {
std::cout << "Can't open database: " << sqlite3_errmsg(db) << "\n";
}
}
// sqlite3_close(db);
void initialize_db(sqlite3* db) {
// ugh, sqlite3_exec doesn't accept const strings, so i have to use const
// char*
const char* customer_table = "create table Customer (id int, name "
"varchar(50), card_code varchar(20), "
"verhicle int)";
const char* parkspot_table = "create table Park_spot (id int, taken boolean, customer_id int)";
const char* parktime_table = "create table Park_time (id int, customer_id int, spot_id int, start "
"real, end real, duration real)";
int success;
}
success = sqlite3_exec(db, customer_table, callback, 0, &error_message);
if (success != SQLITE_OK) {
std::cout << "SQL error: " << sqlite3_errmsg(db) << "\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
success = sqlite3_exec(db, parkspot_table, callback, 0, &error_message);
if (success != SQLITE_OK) {
std::cout << "SQL error: " << sqlite3_errmsg(db) << "\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
success = sqlite3_exec(db, parktime_table, callback, 0, &error_message);
if (success != SQLITE_OK) {
std::cout << "SQL error: " << sqlite3_errmsg(db) << "\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
}
} // namespace data

View File

@ -2,14 +2,14 @@
#define CUSTOMER_H
#pragma once
#include <vector>
#include <string>
#include "Park_time.h"
#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
// 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,
@ -22,16 +22,16 @@ db repr of Customer
int id (not null, auto increment)
string name (not nulll)
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.
clock in en out creeert en compleet een park_time object. Voegt het toe aan een vector.
*/
class Customer {
public:
public:
int id;
string name;
string card_code;
@ -41,13 +41,10 @@ public:
void gen_monthly();
Customer(int id_, string name_, Verhicle_type verhicle_);
private:
private:
Verhicle_type verhicle;
vector<Park_time> park_instances;
string gen_cardcode();
};
#endif // CUSTOMER_H

View File

@ -1,56 +1,16 @@
#include <iostream>
#include <sqlite3.h>
#include <string>
#include <iostream>
namespace db
{
namespace data {
static int callback(void *NotUsed, int argc, char **argv, char **azColName);
static int callback(void* NotUsed, int argc, char** argv, char** azColName);
sqlite3* db;
char* error_message;
void open_db(sqlite3* db_);
/*
this initializes the table in the database.
*/
void initialize_db(sqlite3* db_);
void open_db(sqlite3* db_){
int success = sqlite3_open("sqlite.db", &db);
if (!success) {
std::cout << "Can't open database: "<<sqlite3_errmsg(db)<<"\n";
}
}
// stupid lib
void initialize_db(sqlite3* db_){
// ugh, sqlite3_exec doesn't accept const strings, so i have to use const char*
const char* customer_table = "create table Customer (id int, name varchar(50), card_code varchar(20), verhicle int)";
const char* parkspot_table = "create table Park_spot (id int, taken boolean, customer_id int)";
const char* parktime_table = "create table Park_time (id int, customer_id int, spot_id int, start real, end real, duration real)";
int success;
success = sqlite3_exec(db_, customer_table, callback, 0, &error_message);
if( success!=SQLITE_OK )
{
std::cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
success = sqlite3_exec(db_, parkspot_table, callback, 0, &error_message);
if( success!=SQLITE_OK )
{
std::cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
success = sqlite3_exec(db_, parktime_table, callback, 0, &error_message);
if( success!=SQLITE_OK )
{
std::cout<<"SQL error: "<<sqlite3_errmsg(db)<<"\n";
sqlite3_free(error_message);
return; // TODO: error handling instead of just logging
}
}
}
} // namespace data

View File

@ -1,22 +1,22 @@
#include "headers/Park_spot.h"
#include "headers/db.h"
#include <iostream>
#include <thread> // to make pausing work, not sure if i need chrono, or this, or both
#include <vector>
/*
Code strucure like this:
class declarations zijn in /headers/class_naam.h, en definitions van de member functs in /class_naam.cpp
elke klas in zn eigen file omdat ik incomplete class declarations wilt tegengaan, omdat ik ze niet goed begrijp.
En header/source split om multiple definition errors tegen te gaan.
class declarations zijn in /headers/class_naam.h, en definitions van de member
functs in /class_naam.cpp elke klas in zn eigen file omdat ik incomplete class
declarations wilt tegengaan, omdat ik ze niet goed begrijp. En header/source
split om multiple definition errors tegen te gaan.
Park_spot representeert een parkeermeter bij elke parkeer spot.
Een customer is een customer.
Park time is een object die reffereert naar parkspot en customer, basically een record die zegt dat
een customer voor x tijd geparkeert heeft bij spot x, enz.
Een customer is een customer.
Park time is een object die reffereert naar parkspot en customer, basically een
record die zegt dat een customer voor x tijd geparkeert heeft bij spot x, enz.
De client clockt in en uit bij een spot.
De client clockt in en uit bij een spot.
*/
void Wait(int sec)
@ -24,42 +24,35 @@ void Wait(int sec)
a wait function where 1 sec represents 1 hour irl.
*/
{
std::this_thread::sleep_for(seconds { sec });
std::this_thread::sleep_for(seconds{sec});
}
using std::cout;
int main() {
std::vector<Park_spot> spots{1, 2, 3, 4, 5};
std::vector<Customer> customers{
{1, "Sagar Ram", Verhicle_type::small}, {2, "Shaq", Verhicle_type::medium}, {3, "Josh", Verhicle_type::large}, {4, "Stefano", Verhicle_type::small}};
int main()
{
std::vector<Park_spot> spots {
1, 2, 3, 4, 5
};
std::vector<Customer> customers {
{ 1, "Sagar Ram", Verhicle_type::small },
{ 2, "Shaq", Verhicle_type::medium },
{ 3, "Josh", Verhicle_type::large },
{ 4, "Stefano", Verhicle_type::small }
};
// spots[1].clock(&customers[3]); // stefano parks at spot 2
// Wait(2);
// spots[3].clock(&customers[2]); // josh parks at spot 4
// Wait(1);
// spots[1].clock(&customers[3]); // stefano clocks out of spot 1
// Wait(5);
// spots[1].clock(&customers[1]); // shaq clocks in at spot 1
// Wait(6);
// spots[2].clock(&customers[0]); // sagar clocks in at spot 3. what the fuck
// // is he doing here?
// Wait(2);
// spots[2].clock(&customers[0]); // sagar clocks out from spot 2
// Wait(3);
// spots[3].clock(&customers[2]); // josh clocks out from spot 4
// spots[1].clock(&customers[1]); // shaq clocks out at spot 1
spots[1].clock(&customers[3]); // stefano parks at spot 2
Wait(2);
spots[3].clock(&customers[2]); // josh parks at spot 4
Wait(1);
spots[1].clock(&customers[3]); // stefano clocks out of spot 1
Wait(5);
spots[1].clock(&customers[1]); // shaq clocks in at spot 1
Wait(6);
spots[2].clock(&customers[0]); // sagar clocks in at spot 3. what the fuck is he doing here?
Wait(2);
spots[2].clock(&customers[0]); // sagar clocks out from spot 2
Wait(3);
spots[3].clock(&customers[2]); // josh clocks out from spot 4
spots[1].clock(&customers[1]); // shaq clocks out at spot 1
spots[2].clock(&customers[1]); // shaq clocks out at spot 3
Wait(4);
spots[2].clock(&customers[1]); // shaq clocks out at spot 2
// spots[2].clock(&customers[1]); // shaq clocks out at spot 3
// Wait(4);
// spots[2].clock(&customers[1]); // shaq clocks out at spot 2
/*
so:
@ -68,8 +61,14 @@ int main()
shaq parked 2 times, once for 4 and another for 11 secs
sagar parked for 2 secs
*/
customers[0].gen_monthly();
customers[1].gen_monthly();
customers[2].gen_monthly();
customers[3].gen_monthly();
}
customers[0].gen_monthly();
customers[1].gen_monthly();
customers[2].gen_monthly();
customers[3].gen_monthly();
// test
sqlite3* db;
data::open_db(db);
data::initialize_db(db);
sqlite3_close(db);
}

View File

@ -1,7 +1,7 @@
use
```
g++ main.cpp Park_time.cpp Customer.cpp Park_spot.cpp -o test.exe
g++ Park_time.cpp Customer.cpp Park_spot.cpp db.cpp main.cpp -lsqlite3 -o test.exe
```
to build the project