almost done
This commit is contained in:
		@@ -23,8 +23,8 @@ add_executable(park
 | 
				
			|||||||
        headers/Park_spot.h
 | 
					        headers/Park_spot.h
 | 
				
			||||||
        Park_time.cpp
 | 
					        Park_time.cpp
 | 
				
			||||||
        headers/Park_time.h
 | 
					        headers/Park_time.h
 | 
				
			||||||
        # Query.cpp
 | 
					        Query.cpp
 | 
				
			||||||
        # headers/Query.h
 | 
					        headers/Query.h
 | 
				
			||||||
 )
 | 
					 )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,8 +7,8 @@ Park_spot::Park_spot()
 | 
				
			|||||||
    save_db();
 | 
					    save_db();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Park_spot::Park_spot(int id_, bool taken_, Customer& parked)
 | 
					Park_spot::Park_spot(int id_, bool taken_, int parked)
 | 
				
			||||||
    : parked_customer{parked.id},
 | 
					    : parked_customer{parked},
 | 
				
			||||||
      id{id_},
 | 
					      id{id_},
 | 
				
			||||||
      taken{taken_} // TODO: think about how init parked?
 | 
					      taken{taken_} // TODO: think about how init parked?
 | 
				
			||||||
{}
 | 
					{}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										94
									
								
								Query.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								Query.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
				
			|||||||
 | 
					#include "headers/Query.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vector<Park_time> query_parktimes_for_customer(int cid) {
 | 
				
			||||||
 | 
					    /*
 | 
				
			||||||
 | 
					    This is needed to initialize the park_instances for the customer constructor
 | 
				
			||||||
 | 
					    that is supposed to create a customer from data in the db.
 | 
				
			||||||
 | 
					    This should not be called on on it's own outside query_customer();
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    vector<Park_time> park_times;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    SQLite::Statement query(data::db,
 | 
				
			||||||
 | 
					                            "SELECT * FROM Park_time WHERE customer_id = ?;");
 | 
				
			||||||
 | 
					    query.bind(1, cid);
 | 
				
			||||||
 | 
					    while (query.executeStep()) {
 | 
				
			||||||
 | 
					        int id = query.getColumn(0);
 | 
				
			||||||
 | 
					        int spot_id = query.getColumn(2);
 | 
				
			||||||
 | 
					        int start = query.getColumn(3);
 | 
				
			||||||
 | 
					        int duration = query.getColumn(5);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Park_time result{id, cid, spot_id, start, duration};
 | 
				
			||||||
 | 
					        park_times.push_back(result);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    query.reset();
 | 
				
			||||||
 | 
					    return park_times;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//--------------------------------------------- customers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vector<Customer> query_customer_with_name(string name) {
 | 
				
			||||||
 | 
					    /*
 | 
				
			||||||
 | 
					    We use this instead of plain customers because:
 | 
				
			||||||
 | 
					    1. no error handling needed here if there are no customers
 | 
				
			||||||
 | 
					    2. multiple customers could be returned with the same name.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    vector<Customer> result;
 | 
				
			||||||
 | 
					    SQLite::Statement query(
 | 
				
			||||||
 | 
					        data::db,
 | 
				
			||||||
 | 
					        "SELECT id, name, password, verhicle FROM Customer WHERE name = ?;");
 | 
				
			||||||
 | 
					    query.bind(1, name);
 | 
				
			||||||
 | 
					    while (query.executeStep()) {
 | 
				
			||||||
 | 
					        int id = query.getColumn(0);
 | 
				
			||||||
 | 
					        string name_ = query.getColumn(1);
 | 
				
			||||||
 | 
					        string password = query.getColumn(2);
 | 
				
			||||||
 | 
					        int verhicle = query.getColumn(3); // cast to verhicle
 | 
				
			||||||
 | 
					        vector<Park_time> park_instances = query_parktimes_for_customer(id);
 | 
				
			||||||
 | 
					        result.push_back(Customer{
 | 
				
			||||||
 | 
					            id, name_, password, Verhicle_type(verhicle), park_instances});
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return result;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Customer query_customer_with_id(int id) {
 | 
				
			||||||
 | 
					    /*  do not call this function if you are not certain a customer with this id
 | 
				
			||||||
 | 
					    exists.
 | 
				
			||||||
 | 
					    // the only legitimate caller of this function is query_parkspot_x
 | 
				
			||||||
 | 
					    // there is no error handling in this function
 | 
				
			||||||
 | 
					    // for when this function doesn't find the customer with this id !!!!
 | 
				
			||||||
 | 
					    */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    SQLite::Statement query(data::db, "SELECT * FROM Customer WHERE id = ?;");
 | 
				
			||||||
 | 
					    query.bind(1, id);
 | 
				
			||||||
 | 
					    while (query.executeStep()) {
 | 
				
			||||||
 | 
					        string name = query.getColumn(1);
 | 
				
			||||||
 | 
					        string password = query.getColumn(2);
 | 
				
			||||||
 | 
					        int verhicle = query.getColumn(3); // cast to verhicle
 | 
				
			||||||
 | 
					        vector<Park_time> park_instances = query_parktimes_for_customer(id);
 | 
				
			||||||
 | 
					        Customer result{
 | 
				
			||||||
 | 
					            id, name, password, Verhicle_type(verhicle), park_instances};
 | 
				
			||||||
 | 
					        // DEBUG
 | 
				
			||||||
 | 
					        // cout << "{" << result.id << "," <<result.password <<"," << int(verhicle) << "}\n";
 | 
				
			||||||
 | 
					        return result;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// -------------- paroking spots
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vector<Park_spot>  query_all_parking_spots() {
 | 
				
			||||||
 | 
					    vector<Park_spot> spots;
 | 
				
			||||||
 | 
					    SQLite::Statement query(data::db, "SELECT * FROM Park_spot WHERE id > 2;");
 | 
				
			||||||
 | 
					    // query.bind(1, 2);
 | 
				
			||||||
 | 
					    while (query.executeStep()) {
 | 
				
			||||||
 | 
					        int id = query.getColumn(0);
 | 
				
			||||||
 | 
					        int taken = query.getColumn(1);
 | 
				
			||||||
 | 
					        int cid = query.getColumn(2);
 | 
				
			||||||
 | 
					        // park_customers.push_back(query_customer_with_id(cid));
 | 
				
			||||||
 | 
					        spots.push_back({id, taken, cid});
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return spots;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -46,10 +46,5 @@ class Customer {
 | 
				
			|||||||
    int auto_increment_db();
 | 
					    int auto_increment_db();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static vector<Customer> park_customers; 
 | 
					 | 
				
			||||||
// save the customers that are parked in here
 | 
					 | 
				
			||||||
// parking_spot uses pointers, so it's better to save the parked customers here
 | 
					 | 
				
			||||||
// where we know they'll be destroyed at the end of this scope, instead of too
 | 
					 | 
				
			||||||
// early and end up with dangling pointers
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // CUSTOMER_H
 | 
					#endif // CUSTOMER_H
 | 
				
			||||||
@@ -1,5 +1,8 @@
 | 
				
			|||||||
#include "Customer.h"
 | 
					#ifndef PARK_SPOT_H
 | 
				
			||||||
 | 
					#define PARK_SPOT_H
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "Customer.h"
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
db representation:
 | 
					db representation:
 | 
				
			||||||
int id not null
 | 
					int id not null
 | 
				
			||||||
@@ -16,7 +19,7 @@ class Park_spot {
 | 
				
			|||||||
    bool taken;
 | 
					    bool taken;
 | 
				
			||||||
    int parked_customer;
 | 
					    int parked_customer;
 | 
				
			||||||
    Park_spot();
 | 
					    Park_spot();
 | 
				
			||||||
    Park_spot(int id_, bool taken_, Customer& parked);
 | 
					    Park_spot(int id_, bool taken_, int parked);
 | 
				
			||||||
    void clock(Customer& c_customer);
 | 
					    void clock(Customer& c_customer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private:
 | 
					  private:
 | 
				
			||||||
@@ -25,5 +28,4 @@ class Park_spot {
 | 
				
			|||||||
    void delete_db();
 | 
					    void delete_db();
 | 
				
			||||||
    int auto_increment_db();
 | 
					    int auto_increment_db();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					#endif // CUSTOMER_H
 | 
				
			||||||
static vector<Park_spot> parking_spots; // to save the parking spots in memory
 | 
					 | 
				
			||||||
							
								
								
									
										21
									
								
								headers/Query.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								headers/Query.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					#ifndef QUERY_H
 | 
				
			||||||
 | 
					#define QUERY_H
 | 
				
			||||||
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "Park_spot.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <array>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vector<Park_time> query_parktimes_for_customer(int cid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vector<Customer> query_customer_with_name(string name);
 | 
				
			||||||
 | 
					Customer query_customer_with_id(int id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					vector<Park_spot> query_all_parking_spots(); // used for initializing the parking spots at start of the program
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static vector<Park_spot> parking_spots = query_all_parking_spots(); // to save the parking spots in memory
 | 
				
			||||||
 | 
					static vector<Customer> park_customers; 
 | 
				
			||||||
 | 
					// save the customers that are parked in here
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#endif // CUSTOMER_H
 | 
				
			||||||
@@ -6,6 +6,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace data {
 | 
					namespace data {
 | 
				
			||||||
SQLite::Database start_db();
 | 
					SQLite::Database start_db();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static SQLite::Database db = start_db();
 | 
					static SQLite::Database db = start_db();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
} // namespace data
 | 
					} // namespace data
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										9
									
								
								main.cpp
									
									
									
									
									
								
							
							
						
						
									
										9
									
								
								main.cpp
									
									
									
									
									
								
							@@ -1,4 +1,4 @@
 | 
				
			|||||||
#include "headers/Park_spot.h"
 | 
					#include "headers/Query.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <chrono>
 | 
					#include <chrono>
 | 
				
			||||||
#include <thread>
 | 
					#include <thread>
 | 
				
			||||||
@@ -30,6 +30,13 @@ a wait function where 1 sec represents 1 hour irl.
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main() {
 | 
					int main() {
 | 
				
			||||||
 | 
					    // Customer sagar = query_customer_with_name("stefan udit")[0];
 | 
				
			||||||
 | 
					    Customer sagar = query_customer_with_id(2);
 | 
				
			||||||
 | 
					    cout << sagar.id << "," << sagar.name << "," << sagar.password;
 | 
				
			||||||
 | 
					    // cout << parking_spots.size();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // for (auto i : parking_spots){
 | 
				
			||||||
 | 
					    //     cout << i.id << "," << i.parked_customer;
 | 
				
			||||||
 | 
					    // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user