quick sketch of components

This commit is contained in:
TinyAtoms 2019-05-28 13:46:55 -03:00
commit 951f8f6c15
7 changed files with 247 additions and 0 deletions

30
src/Customer.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "headers/Customer.h"
#include <iostream>
Customer::Customer(int id_, string name_, string card_code_)
: id { id_ }
, name { name_ }
, card_code { card_code_ }
{
}
void Customer::clock_in( int s_id)
{
Park_time pt{id, s_id};
park_instances.push_back(pt);
}
void Customer::clock_out(int s_id){
park_instances[park_instances.size()-1].clock_out(id, s_id);
}
void Customer::gen_monthly(){
std::cout << "NAME: " << name << " card code: " << card_code << "\n";
std::cout << "-------------------------------------------------\n";
for (auto& i : park_instances) {
// need some logic to only include from this month
std::cout << i;
}
std::cout << "-------------------------------------------------\n\n";
}

20
src/Park_spot.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "headers/Park_spot.h"
Park_spot::Park_spot(int id_){
parked = nullptr;
id = id_;
taken = false;
}
void Park_spot::clock(Customer* c_customer){
if (!taken){
parked = c_customer;
taken = true;
parked->clock_in(id);
}
else{
taken = false;
parked->clock_out(id);
parked = nullptr;
}
}

45
src/Park_time.cpp Normal file
View File

@ -0,0 +1,45 @@
#include"headers/Park_time.h"
#include <iostream>
#include <ctime>
Park_time::Park_time(int c_id, int s_id)
: customer_id { c_id }
, spot_id { s_id }
, duration { 0 }
, start { high_resolution_clock::now() }
{
}
void Park_time::clock_out(int c_id, int s_id)
{
if (c_id != customer_id) {
std::cout << "wrong customer id, you are at the wrong location";
return;
}
if (s_id != spot_id) {
std::cout << "Wrong spot id, you're at the wrong location";
return;
}
if (!duration) {
end = high_resolution_clock::now();
duration = duration_cast<seconds>(end - start).count(); // use mins later
} else {
std::cout << "Already clocked out. Something is wrong \n";
}
}
std::ostream& operator<<(std::ostream& os, const Park_time & pt){
std::time_t start_ = system_clock::to_time_t(pt.start);
std::time_t end_ = system_clock::to_time_t(pt.end);
os << "- - - - - - - - - - - - - - - - - - - -\n";
os << "Clocked in :" << std::ctime(&start_);
os << "clocked out : " << std::ctime(&end_);
os << "duration : " << pt.duration << "\n";
os << "- - - - - - - - - - - - - - - - - - - -\n";
return os;
}

37
src/headers/Customer.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef CUSTOMER_H
#define CUSTOMER_H
#pragma once
#include <vector>
#include <string>
#include "Park_time.h"
using std::vector;
using std::string;
/*
db repr of Customer
int id (not null, auto increment)
string name (not nulll)
string card_code (not null)
*/
class Customer {
public:
int id;
string name;
string card_code;
void clock_in(int s_id);
void clock_out(int s_id);
// void gen_weekly();
void gen_monthly();
Customer(int id_, string name_, string card_code_);
private:
vector<Park_time> park_instances;
};
#endif // CUSTOMER_H

21
src/headers/Park_spot.h Normal file
View File

@ -0,0 +1,21 @@
#include "Customer.h"
/*
db representation:
int id not null
bool taken not null
int customer_id (null) (many to one, foreign key, whatever)
*/
class Park_spot {
public:
int id;
bool taken;
Customer* parked;
Park_spot(int id_);
void clock(Customer* c_customer);
private:
};

39
src/headers/Park_time.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef PARK_TIME_H
#define PARK_TIME_H
#pragma once
#include <chrono>
#include <iostream>
using namespace std::chrono;
/*
db repr of Park_time
int id (not null, auto increment)
int customer_id (not null) (many to one or something like that)
int spot_id (not null, many to one or something like that)
int duration
datetime start (not null)
datetime end
*/
class Park_time {
public:
int id;
int customer_id;
int spot_id;
int duration;
Park_time(int c_id, int s_id);
void clock_out(int c_id, int s_id);
friend std::ostream& operator<<(std::ostream& os, const Park_time & pt);
private:
high_resolution_clock::time_point start;
high_resolution_clock::time_point end;
};
#endif // Park_time

55
src/main.cpp Normal file
View File

@ -0,0 +1,55 @@
#include "headers/Park_spot.h"
#include <iostream>
#include <thread> // to make pausing work, not sure if i need chrono, or this, or both
#include <vector>
void Wait(int 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", "eddgh" },
{ 2, "Shaq", "wsdfwefgv" },
{ 3, "Josh", "WDFGWEF" },
{ 4, "Stefano", "EDGGERG" }
};
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
/*
so:
stefan parked for 3 secs
josh parked for 17 secs
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();
}