IoTcat 5 years ago
parent 6adefcf346
commit ea7ed683cb
  1. 106
      lib/ovo.h
  2. 45
      src/car.h
  3. 0
      src/feeTable.cpp
  4. 189
      src/feeTable.h
  5. 56
      src/main.cpp
  6. 444
      src/park.h
  7. 29
      src/plot.h

@ -452,6 +452,7 @@ namespace ovo{
/* display all elements */
string showAll(){
this->classify();
string s = "{";
map<string, string>::iterator t_iter = this->_data.begin();
while(t_iter != this->_data.end()){
@ -488,6 +489,105 @@ namespace ovo{
};
/**
* Index Storage
*
* @author yimian
* @category ovo
* @package ovo
*/
class index{
public:
index(){};
friend class db;
index(const index& i){
this->_val = i._val;
this->_index = i._index;
};
~index(){
this->_val.erase(this->_val.begin(),this->_val.end());
this->_index.erase(this->_index.begin(),this->_index.end());
};
void push_back(const string& val, const string& index){
this->_val.push_back(val);
this->_index.push_back(index);
};
std::vector<string> getIndex(const string& val){
std::vector<string> v;
std::vector<unsigned int> pos = this->_getPosition(this->_val, val);
for(unsigned int i : pos){
v.push_back(this->_index[i]);
}
return v;
};
string getVal(const string& index){
std::vector<unsigned int> pos = this->_getPosition(this->_index, index);
if(pos.size()) return this->_val[pos[0]];
return string();
};
void delByIndex(const string& index){
std::vector<unsigned int> pos = this->_getPosition(this->_index, index);
sort(pos.begin(), pos.end(), [&](const unsigned int& a, const unsigned int& b)->bool{
return (a > b);
});
for(unsigned int i : pos){
this->_index.erase(this->_index.begin() + i);
this->_val.erase(this->_val.begin() + i);
}
};
void delByVal(const string& val){
std::vector<unsigned int> pos = this->_getPosition(this->_val, val);
sort(pos.begin(), pos.end(), [&](const unsigned int& a, const unsigned int& b)->bool{
return (a > b);
});
for(unsigned int i : pos){
this->_index.erase(this->_index.begin() + i);
this->_val.erase(this->_val.begin() + i);
}
};
int size(){
return this->_index.size();
}
private:
std::vector<string> _val, _index;
std::vector<unsigned int> _getPosition(std::vector<string>& v, const string& s){
std::vector<unsigned int> o;
vector <string>::iterator it = find(v.begin(), v.end(), s);
while(it != v.end()){
o.push_back(distance(v.begin(), it));
it = find(++it, v.end(), s);
}
return o;
};
};
/**
* Database operation
*
@ -513,8 +613,14 @@ namespace ovo{
/* push data to database */
void pushData(const string& key, data& data);
inline void pushData(data& data, const string& key){
pushData(key, data);
};
/* add data to database */
void addData(const string& key, data& data);
inline void addData(data& data, const string& key){
pushData(key, data);
};
/* get data from database */
data getData(const string& key);
/* get data from database */

@ -17,25 +17,37 @@ using namespace std;
class Car{
public:
Car(const string& licenseNum, const string& type){
Car(const string& licenseNum, const string& type, const string& plot){
this->_d["id"] = licenseNum;
this->_d["type"] = type;
this->_d["plot"] = "null";
this->_d["LastInTime"] = "null";
this->_d["plot"] = plot;
this->_d["LastInTime"] = to_string(time(NULL));
this->_d["LastOutTime"] = "null";
this->_isExist = true;
};
Car(const string& licenseNum){
Car(const string& s){
this->_isExist = false;
if(s == "undefined"){
return;
}
this->_d = this->_d.strToData(s);
if(this->_d["id"] == "undefined") return;
this->_d = db.getData(licenseNum);
this->_isExist = true;
};
Car(){
this->_isExist = false;
};
~Car(){
this->_d.classify();
db.pushData(this->_d["id"], this->_d);
};
inline bool isExist(){
@ -50,14 +62,31 @@ public:
return this->_d["type"];
};
inline string getLastInTime(){
return this->_d["LastInTime"];
};
inline string getLastOutTime(){
return this->_d["LastOutTime"];
};
inline string getPlot(){
return this->_d["plot"];
};
inline string showAll(){
return this->_d.showAll();
};
inline string getDataStr(){
this->_d.classify();
return this->_d.dataToStr(this->_d);
};
private:
ovo::data _d;
ovo::db db;
bool _isExist;

@ -0,0 +1,189 @@
#ifndef __FEETABLE_H__
#define __FEETABLE_H__
#include <iostream>
#include <vector>
#include <string>
#include "../lib/ovo.h"
const string g_feeTableID = "__Fee_Table__AS4";
using namespace std;
class FeeTable{
public:
FeeTable(){
this->_d.clear();
this->_d = db.getData(g_feeTableID);
this->_tableData = db.getData(g_feeTableID + "_Table");
this->_tableData.forEach([&](string first, string second){
this->_table[first] = this->_vecFromStr(second);
});
};
~FeeTable(){
this->_d.classify();
db.pushData(g_feeTableID, this->_d);
this->_pushTable();
db.pushData(g_feeTableID + "_Table", this->_tableData);
};
void setTypes(std::vector<string> v){
std::vector<int> vv;
vv = this->_classify(vv);
this->_table.erase(this->_table.begin(), this->_table.end());
for(auto i : v){
this->_table[i] = vv;
}
}
void set(std::map<string, std::vector<int>>& m){
for(auto& i : m){
if(this->_table.count(i.first)){
this->_table[i.first].erase(this->_table[i.first].begin(), this->_table[i.first].end());
this->_table[i.first] = this->_classify(i.second);
}
}
}
inline std::map<string, std::vector<int>> getTable(){
return this->_table;
}
inline std::vector<int> getTable(const string& type){
if(this->_table.count(type)){
return this->_table[type];
}
return std::vector<int>();
}
int getFee(const string& type, const string& LastInTime, const string& LastOutTime){
if(!this->_table.count(type)) return -1;
int t = atoi(LastOutTime.c_str()) - atoi(LastInTime.c_str());
t /= 3600;
int days = t / 24;
int hours = t % 24;
int fee = 0;
for(int i = 0; i < hours; i ++){
fee += this->_table[type][i];
}
int DailyFee = 0;
for(int i : this->_table[type]){
DailyFee += i;
}
fee += days * DailyFee;
return fee;
}
string showAll(){
string s = "{\n";
for(auto i : this->_table){
s += " \"" + i.first + "\": [";
for(auto ii : i.second){
s += to_string(ii) + ", ";
}
s += "],\n";
}
s += "}";
return s;
}
//private:
ovo::data _d, _tableData;
ovo::db db;
ovo::String S;
std::map<string, std::vector<int>> _table;
std::vector<int> _classify(std::vector<int>& v){
std::vector<int> o;
if(v.size() < 24){
for(int i : v){
o.push_back(i);
}
for(int i = 0; i < 24 - v.size(); i ++){
o.push_back(0);
}
}else{
for(int i = 0; i < 24; i ++){
o.push_back(v[i]);
}
}
return o;
}
string _vecToStr(std::vector<int>& v){
string s = "";
for(int i : v){
s += to_string(i) + "|||$$|||";
}
return s;
}
std::vector<int> _vecFromStr(const string& s){
std::vector<string> v;
S.split(s, v, "|||$$|||");
std::vector<int> o;
for(string i : v){
o.push_back(atoi(i.c_str()));
}
return o;
}
void _pushTable(){
this->_tableData.clear();
for(auto i : this->_table){
this->_tableData[i.first] = this->_vecToStr(i.second);
}
}
};
#endif //__FEETABLE_H__

@ -9,15 +9,46 @@
using namespace std;
int main(int argc, char const *argv[])
{/*
{
FeeTable t;
std::vector<string> v;
v.push_back("car");
v.push_back("rac");
//t.setTypes(v);
std::map<string, std::vector<int>> m;
std::vector<int> vv;
vv.push_back(2);
vv.push_back(3);
m["car"] = vv;
//t.set(m);
cout << t.getFee("car", "0", "10000");
cout << t.showAll();
/*
Plot p("eeeee", 3, "222");
ovo::data d;
d["eeeee"] = p.getStrContent();
cout << d.showAll();*/
cout << d.showAll();
*/
/*
Park p;
@ -35,13 +66,30 @@ int main(int argc, char const *argv[])
m["Carsss"] = 99;
v.push_back(m);
p.ini(v);
//p.ini(v);
//cout << p.checkType("Cadrs");
p.join();
cout << p._d.showAll();
string kk = "";
if(!p.newCar("110", "Cars", p.getPlotsID("Cars", false)[0], kk))cout << kk;
int t = time(NULL);
cout << p.getPlotsID().size();
string s = "Cars";
cout << endl<<p.getPlotsID("Cars", false).size();
for(auto i : p.getPlotsID("Cars", false)){
cout << p.getPlot(i).showAll();
}
for(auto i : p.getCarsID(0)){
cout << i;
}
cout << " " << time(NULL) - t;
//p.delCar("110");
cout << p.getPlotByCar("110").showAll();
*/
return 0;
}

@ -13,6 +13,7 @@
#include "plot.h"
#include "car.h"
#include "feeTable.h"
const string g_ParkID = "EEE102AS4";
@ -34,7 +35,9 @@ public:
this->_plotsList = db.getData(this->_d["plotsList"]);
this->_carsList = db.getData(this->_d["carsList"]);
this->_recoverTypes();
this->_feeTable.setTypes(this->_types);
this->_levels = atoi(this->_d["levels"].c_str());
this->_threadFinished = true;
});
@ -84,7 +87,7 @@ public:
return false;
}
return true;
};*/
};
std::vector<Plot> getPlots(){
if(!this->_threadFinished) this->_t->join();
@ -94,15 +97,15 @@ public:
});
return v;
};
std::vector<string> getPlotsID(){
*/
/* std::vector<string> getPlotsID(){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
v.push_back(this->_plotsList.strToData(second)["id"]);
});
return v;
};
};*/
/*
std::vector<Plot> getPlots(bool isOccupied){
if(!this->_threadFinished) this->_t->join();
@ -251,24 +254,402 @@ public:
if(!this->_threadFinished) this->_t->join();
this->_d.clear();
this->_plotsList.clear();
this->_carsList.clear();
this->_getTypes(v);
this->_levels = v.size();
this->_d["id"] = g_ParkID;
this->_d["carsList"] = m.randStr();
this->_d["plotsList"] = m.randStr();
this->_d["levels"] = this->_levels;
this->_d["feeTable"] = m.randStr();
this->_d["levels"] = to_string(this->_levels);
this->_d["types"] = "";
for(string i : this->_types){
this->_d["types"] += i + "|||$$|||";
}
this->_setupPlots(v);
this->_feeTable.setTypes(this->_types);
};
void updateFeeTable(std::map<string, std::vector<int>>& m){
this->_feeTable.set(m);
}
vector<string> getPlotsID(){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
v.push_back(first);
});
return v;
};
std::vector<string> getPlotsID(const bool& isOccupied){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
if(isOccupied && this->_simpleGet(second, "car") != "null") v.push_back(first);
if(!isOccupied && this->_simpleGet(second, "car") == "null") v.push_back(first);
});
return v;
};
std::vector<string> getPlotsID(const int& level){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
if(this->_simpleGet(second, "level") == to_string(level)) v.push_back(first);
});
return v;
};
std::vector<string> getPlotsID(const string& type){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
if(this->_simpleGet(second, "type") == type) v.push_back(first);
});
return v;
};
std::vector<string> getPlotsID(const int& level, const bool& isOccupied){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
if(isOccupied && this->_simpleGet(second, "car") != "null" && this->_simpleGet(second, "level") == to_string(level)) v.push_back(first);
if(!isOccupied && this->_simpleGet(second, "car") == "null" && this->_simpleGet(second, "level") == to_string(level)) v.push_back(first);
});
return v;
};
std::vector<string> getPlotsID(const string& type, const bool& isOccupied){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
if(isOccupied && this->_simpleGet(second, "car") != "null" && this->_simpleGet(second, "type") == type) v.push_back(first);
if(!isOccupied && this->_simpleGet(second, "car") == "null" && this->_simpleGet(second, "type") == type) v.push_back(first);
});
return v;
};
std::vector<string> getPlotsID(const int& level, const string& type){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
if(this->_simpleGet(second, "level") == to_string(level) && this->_simpleGet(second, "type") == type) v.push_back(first);
});
return v;
};
std::vector<string> getPlotsID(const string& type, const int& level){
return this->getPlotsID(level, type);
}
std::vector<string> getPlotsID(const int& level, const string& type, const bool& isOccupied){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_plotsList.forEach([&](string first, string second){
if(isOccupied && this->_simpleGet(second, "car") != "null" && this->_simpleGet(second, "level") == to_string(level) && this->_simpleGet(second, "type") == type) v.push_back(first);
if(!isOccupied && this->_simpleGet(second, "car") == "null" && this->_simpleGet(second, "level") == to_string(level) && this->_simpleGet(second, "type") == type) v.push_back(first);
});
return v;
};
inline std::vector<string> getPlotsID(const string& type, const int& level, const bool& isOccupied){
return this->getPlotsID(level, type, isOccupied);
};
Plot getPlot(const string& id){
if(this->_plotsList[id] == "undefined"){
return Plot();
}
return Plot(this->_plotsList[id]);
};
Plot getPlotByCar(const string& licenseNum){
if(this->_carsList[licenseNum] == "undefined"){
return Plot();
}
return Plot(this->_plotsList[this->_simpleGet(this->_carsList[licenseNum], "plot")]);
};
Plot getPlotByCar(Car& car){
if(this->_carsList[car.getID()] == "undefined"){
return Plot();
}
return Plot(this->_plotsList[this->_simpleGet(this->_carsList[car.getID()], "plot")]);
};
bool newCar(const string& licenseNum, const string& type, const string& plotID){
string s;
return this->newCar(licenseNum, type, plotID, s);
}
bool newCar(const string& licenseNum, const string& type, const string& plotID, string& msg){
if(licenseNum.length() < 1){
msg += "Car " + licenseNum + " LicenseNum Illegal!!!";
return false;
}
if(this->_carsList.isExist(licenseNum)){
msg += "Car " + licenseNum + " Already in the Park!!";
return false;
}
if(this->isGoodType(type)){
msg += "Car " + licenseNum + " Wrong Type!!!";
return false;
}
if(this->_plotsList[plotID] == "undefined"){
msg += "Car " + licenseNum + " Assigned Plot Not Exist!!!";
return false;
}
Car c(licenseNum, type, plotID);
this->_carsList[licenseNum] = c.getDataStr();
this->_plotsList[plotID] = this->_simpleUpdate(this->_plotsList[plotID], "car", licenseNum);
return true;
};
bool delCar(const string& licenseNum){
string s;
return this->delCar(licenseNum, s);
}
bool delCar(const string& licenseNum, string& msg){
if(!this->_carsList.isExist(licenseNum)){
msg += "Car " + licenseNum + " Not In Plot!!!";
return false;
}
Car c(this->_carsList[licenseNum]);
this->_plotsList[c.getPlot()] = this->_simpleUpdate(this->_plotsList[c.getPlot()], "car", "null");
this->_carsList.clear(licenseNum);
return true;
}
std::vector<string> getCarsID(){
if(!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_carsList.forEach([&](string first, string second){
v.push_back(first);
});
return v;
};
std::vector<string> getCarsID(const int& level){
std::vector<string> v;
v = this->getPlotsID(level, true);
for(string& i : v){
i = this->_simpleGet(this->_plotsList[i], "car");
}
return v;
};
std::vector<string> getCarsID(const string& type){
std::vector<string> v;
v = this->getPlotsID(type, true);
for(string& i : v){
i = this->_simpleGet(this->_plotsList[i], "car");
}
return v;
};
std::vector<string> getCarsID(const int& level, const string& type){
std::vector<string> v;
v = this->getPlotsID(level, type, true);
for(string& i : v){
i = this->_simpleGet(this->_plotsList[i], "car");
}
return v;
};
Car getCar(const string& licenseNum){
if(this->_carsList[licenseNum] == "undefined"){
return Car();
}
return Car(this->_carsList[licenseNum]);
}
Car getCarByPlot(string& id){
if(this->_carsList[id] == "undefined"){
return Car();
}
Plot p = this->getPlot(id);
return Car(this->_carsList[p.getCar()]);
}
inline Car getCarByPlot(Plot& p){
if(this->_carsList[p.getCar()] == "undefined"){
return Car();
}
return Car(this->_carsList[p.getCar()]);
}
bool updatePlot(Plot& plot, const int& level){
if(level < 0 || level > this->_levels){
return false;
}
this->_plotsList[plot.getID()] = this->_simpleUpdate(this->_plotsList[plot.getID()],
"level", to_string(level));
plot.updateLevel(level);
return true;
}
bool updatePlot(Plot& plot, const string& type){
if(!this->isGoodType(type)){
return false;
}
this->_plotsList[plot.getID()] = this->_simpleUpdate(this->_plotsList[plot.getID()],
"type", type);
plot.updateType(type);
return true;
}
bool checkIn(const string& licenseNum, const string& type){
string msg;
return checkIn(licenseNum, type, msg);
}
bool checkIn(const string& licenseNum, const string& type, string& msg){
std::vector<string> v = this->getPlotsID(type, false);
if(!v.size()){
msg += type + " has no more Plots!!";
return false;
}
if(!newCar(licenseNum, type, v[0], msg)) return false;
return true;
}
bool checkIn(const string& licenseNum, const string& type, const int& level){
string msg;
return checkIn(licenseNum, type, level, msg);
}
bool checkIn(const string& licenseNum, const string& type, const int& level, string& msg){
std::vector<string> v = this->getPlotsID(level, type, false);
if(!v.size()){
msg += type + " on Level " + to_string(level) + " has no more Plots!!";
return false;
}
if(!newCar(licenseNum, type, v[0], msg)) return false;
return true;
}
bool checkInByPlotID(const string& licenseNum, const string& type, const string& plotID){
string msg;
return checkInByPlotID(licenseNum, type, plotID, msg);
}
bool checkInByPlotID(const string& licenseNum, const string& type, const string& plotID, string& msg){
if(!newCar(licenseNum, type, plotID, msg)) return false;
return true;
}
int checkOut(const string& licenseNum){
if(!this->_carsList.isExist(licenseNum)) return -1;
this->_carsList[licenseNum] = this->_simpleUpdate(this->_carsList[licenseNum], "LastOutTime", to_string(time(NULL)));
Car c(this->_carsList[licenseNum]);
this->delCar(licenseNum);
return this->_feeTable.getFee(c.getType(), c.getLastInTime(), c.getLastOutTime());
}
//private:
ovo::data _d, _carsList, _plotsList;
ovo::index _isOccupiedIndex, _typeIndex, _levelIndex;
std::vector<string> _types;
unsigned int _levels;
ovo::math m;
@ -276,11 +657,11 @@ public:
ovo::db db;
std::thread *_t;
bool _threadFinished;
FeeTable _feeTable;
void _getTypes(std::vector<std::map<string, int>>& v){
this->_types.clear(); //清空vec
for(auto i : v){
for(auto ii : i){
if(find(this->_types.begin(), this->_types.end(), ii.first) == this->_types.end()){
@ -293,7 +674,6 @@ public:
void _setupPlots(std::vector<std::map<string, int>>& v){
int t = time(NULL);
for(unsigned int i = 0; i < v.size(); i ++){
for(auto ii : v[i]){
@ -301,17 +681,61 @@ public:
for(int iii = 0; iii < ii.second; iii ++){
string s = m.randStr();
this->_plotsList[s] = this->_simplePlot(s, i, ii.first);
//cout << _simpleGet(_plotsList[s], "type") << endl;
}
}
}
cout << "Used" << time(NULL) - t;
};
string _simplePlot(const string& id, const int& level, const string& type){
return "__OVO_DATA__id$$||$$" + id + "$$||$$level$$||$$" + to_string(level) + "$$||$$type$$||$$"
+ type + "$$||$$car$$||$$null$$||$$LastOperateTime$$||$$null$$||$$CreatedTime$$||$$null$$||$$";
+ type + "$$||$$car$$||$$null$$||$$LastOperateTime$$||$$" + to_string(time(NULL)) + "$$||$$CreatedTime$$||$$" + to_string(time(NULL)) + "$$||$$";
}
string _simpleGet(const string& s, const string& what){
std::vector<string> v;
S.split(s, v, "$$||$$");
vector <string>::iterator it = find(v.begin(), v.end(), what);
if(it != v.end()) return v[distance(v.begin(), it) + 1];
return string();
}
string _simpleUpdate(const string& s, const string& what, const string& to){
std::vector<string> v;
S.split(s, v, "$$||$$");
vector <string>::iterator it = find(v.begin(), v.end(), what);
if(it == v.end()) return s;
v[distance(v.begin(), it) + 1] = to;
it = find(v.begin(), v.end(), "LastOperateTime");
if(it != v.end()) v[distance(v.begin(), it) + 1] = to_string(time(NULL));
string ss = "";
for(string i : v){
ss += i + "$$||$$";
}
return ss;
}
void _updateIndex(){
};
void _recoverTypes(){
S.split(this->_d["types"], this->_types, "|||$$|||");
}
bool isGoodType(const string& type){
return (bool)(find(this->_types.begin(), this->_types.end(), type) == this->_types.end());
}
};

@ -27,20 +27,24 @@ public:
};
Plot(const string& s){
this->_isExist = false;
if(s == "undefined"){
return;
}
this->_d = this->_d.strToData(s);
if(this->_d["id"] == "undefined"){
return;
}
this->_isExist = true;
};
Plot(const string& id, const string& isExist){
Plot(){
this->_isExist = false;
};
~Plot(){
this->_d.classify();
db.pushData(this->_d["id"], this->_d);
};
inline isExist(){
@ -67,25 +71,30 @@ public:
return (this->_d["car"] == "null") ? false : true;
};
inline void updateLevel(const unsigned int& level){
this->_d["level"] = to_string(level);
};
inline void updateType(const string& type){
this->_d["type"] = type;
inline string showAll(){
return this->_d.showAll();
};
friend class Park;
//private:
ovo::data _d;
ovo::db db;
bool _isExist;
string getStrContent(){
return this->_d.dataToStr(this->_d);
};
inline void updateLevel(const unsigned int& level){
this->_d["level"] = to_string(level);
};
inline void updateType(const string& type){
this->_d["type"] = type;
};
};

Loading…
Cancel
Save