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

56
db.cpp
View File

@ -1,21 +1,55 @@
#include "headers/db.h" #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){ static int callback(void* NotUsed, int argc, char** argv, char** azColName) {
int i; int i;
for(i=0; i<argc; i++) for (i = 0; i < argc; i++) {
{ std::cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << "\n";
std::cout<<azColName[i]<<" = " << (argv[i] ? argv[i] : "NULL")<<"\n";
} }
std::cout<<"\n"; std::cout << "\n";
return 0; return 0;
} }
void open_db(sqlite3* db) {
int status = sqlite3_open("database.sqlite", &db);
// sqlite3_close(db); if (!status) {
std::cout << "Can't open database: " << sqlite3_errmsg(db) << "\n";
}
} }
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,12 +2,12 @@
#define CUSTOMER_H #define CUSTOMER_H
#pragma once #pragma once
#include <vector>
#include <string>
#include "Park_time.h" #include "Park_time.h"
#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.
@ -31,7 +31,7 @@ clock in en out creeert en compleet een park_time object. Voegt het toe aan een
*/ */
class Customer { class Customer {
public: public:
int id; int id;
string name; string name;
string card_code; string card_code;
@ -41,13 +41,10 @@ public:
void gen_monthly(); void gen_monthly();
Customer(int id_, string name_, Verhicle_type verhicle_); Customer(int id_, string name_, Verhicle_type verhicle_);
private: private:
Verhicle_type verhicle; Verhicle_type verhicle;
vector<Park_time> park_instances; vector<Park_time> park_instances;
string gen_cardcode(); string gen_cardcode();
}; };
#endif // CUSTOMER_H #endif // CUSTOMER_H

View File

@ -1,56 +1,16 @@
#include <iostream>
#include <sqlite3.h> #include <sqlite3.h>
#include <string> #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; void open_db(sqlite3* db_);
char* error_message;
/*
this initializes the table in the database.
*/
void initialize_db(sqlite3* db_);
} // namespace data
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
}
}
}

View File

@ -1,20 +1,20 @@
#include "headers/Park_spot.h" #include "headers/Park_spot.h"
#include "headers/db.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>
/* /*
Code strucure like this: Code strucure like this:
class declarations zijn in /headers/class_naam.h, en definitions van de member functs in /class_naam.cpp class declarations zijn in /headers/class_naam.h, en definitions van de member
elke klas in zn eigen file omdat ik incomplete class declarations wilt tegengaan, omdat ik ze niet goed begrijp. functs in /class_naam.cpp elke klas in zn eigen file omdat ik incomplete class
En header/source split om multiple definition errors tegen te gaan. 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. Park_spot representeert een parkeermeter bij elke parkeer spot.
Een customer is een customer. Een customer is een customer.
Park time is een object die reffereert naar parkspot en customer, basically een record die zegt dat Park time is een object die reffereert naar parkspot en customer, basically een
een customer voor x tijd geparkeert heeft bij spot x, enz. 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.
*/ */
@ -24,42 +24,35 @@ void Wait(int sec)
a wait function where 1 sec represents 1 hour irl. 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; 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() // spots[1].clock(&customers[3]); // stefano parks at spot 2
{ // Wait(2);
std::vector<Park_spot> spots { // spots[3].clock(&customers[2]); // josh parks at spot 4
1, 2, 3, 4, 5 // Wait(1);
}; // spots[1].clock(&customers[3]); // stefano clocks out of spot 1
std::vector<Customer> customers { // Wait(5);
{ 1, "Sagar Ram", Verhicle_type::small }, // spots[1].clock(&customers[1]); // shaq clocks in at spot 1
{ 2, "Shaq", Verhicle_type::medium }, // Wait(6);
{ 3, "Josh", Verhicle_type::large }, // spots[2].clock(&customers[0]); // sagar clocks in at spot 3. what the fuck
{ 4, "Stefano", Verhicle_type::small } // // 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 // spots[2].clock(&customers[1]); // shaq clocks out at spot 3
Wait(2); // Wait(4);
spots[3].clock(&customers[2]); // josh parks at spot 4 // spots[2].clock(&customers[1]); // shaq clocks out at spot 2
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
/* /*
so: so:
@ -72,4 +65,10 @@ int main()
customers[1].gen_monthly(); customers[1].gen_monthly();
customers[2].gen_monthly(); customers[2].gen_monthly();
customers[3].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 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 to build the project