master
IoTcat 5 years ago
parent 4cbb51065e
commit 66cc7e5be5
  1. 4
      lib/ovo.h
  2. 815
      src/main.cpp
  3. 20
      src/park.cpp
  4. 10
      src/park.h

@ -367,14 +367,14 @@ namespace ovo{
}
/* clear all elements */
inline void clear(){
//this->_data.erase(this->_data.begin(),this->_data.end());
this->_data.erase(this->_data.begin(),this->_data.end());
}
/* clear elements */
template <typename T>
inline void clear(const T& key){
if(this->_data.count(this->toStr(key))){
map<string, string>::iterator t_iter = this->find(this->toStr(key));
//this->_data.erase(t_iter);
this->_data.erase(t_iter);
}
}
/* clear elements */

@ -1,3 +1,10 @@
/*
* @Author: IoTcat (https://iotcat.me)
* @Date: 2019-05-26 18:59:20
* @Last Modified by: IoTcat
* @Last Modified time: 2019-05-26 18:59:20
*/
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
@ -8,193 +15,763 @@
using namespace std;
int main(int argc, char const *argv[]) {
Park p;
int t = time(NULL);
if (!p.isExist()) {cout << "exist";
ovo::math m;
std::vector<std::map<string, int>> ParkIniInfo;
std::map<string, int> t_map;
for (int i = 0; i < 100; i++) {
t_map["Car"] = m.rand(0, 600);
t_map["Bicycle"] = m.rand(0, 600);
t_map["Airplane"] = m.rand(0, 600);
t_map["Ship"] = m.rand(0, 600);
ParkIniInfo.push_back(t_map);
int g_time = 0;
void recordCostTime(auto f);
void autoIni(Park& p);
void manualIni(Park& p);
void displayInfo(auto f, auto get);
void displayLog(auto f);
int main(int argc, char const* argv[]) {
Park* p = new Park();
cout << "This program aims to test the API of Back-End System.\nOnly for "
"TEST purpose!!!!"
<< endl;
system("pause");
if (!p->isExist()) {
while (1) {
system("cls");
cout << "There is NO Former Data FOUND!! You can choose to:" << endl
<< endl;
cout << "1. Randomly generate Plot and Price Info." << endl;
cout << "2. Set Plots and Price info Manually." << endl;
cout << endl << "Your Selection is = ";
string s;
cin >> s;
if (s == "1") {
autoIni(*p);
break;
}
if (s == "2") {
manualIni(*p);
break;
}
}
}
p.ini(ParkIniInfo);
while (1) {
system("cls");
cout << "1. Show Park Info." << endl;
cout << "2. Show FeeTable Info." << endl;
cout << "3. Test for Get Plots Info." << endl;
cout << "4. Test for Get Cars Info." << endl;
cout << "5. Test for Check In and Check Out." << endl;
cout << "6. Test for Change Plots Info." << endl;
cout << "7. Test for Get History LOG." << endl;
cout << "8. Delete All Data and Exit." << endl;
cout << "9. Exit" << endl;
cout << endl << "Please Select = ";
string slct;
cin >> slct;
std::map<string, std::vector<int>> feeTable;
std::vector<int> fee;
if (slct == "1") {
system("cls");
for (int i = 0; i < 24; i++) {
fee.push_back(m.rand(0, 60));
cout << p->showAll() << endl;
system("pause");
}
feeTable["Car"] = fee;
if (slct == "2") {
system("cls");
for (int i = 0; i < 24; i++) {
fee[i] = m.rand(0, 60);
cout << p->showFeeTableInfo() << endl;
system("pause");
}
feeTable["Bicycle"] = fee;
if (slct == "3") {
system("cls");
cout << "1. Get All Plots." << endl;
cout << "2. Get Plots By if Occupied." << endl;
cout << "3. Get Plots By Floor." << endl;
cout << "4. Get Plots By Type." << endl;
cout << "5. Get Plots By Floor and if Occupied." << endl;
cout << "6. Get Plots By Type and if Occupied." << endl;
cout << "7. Get Plots By Floor and Type." << endl;
cout << "8. Get Plots By Floor, Type and if Occupied." << endl;
cout << "9. Get Plots By Car LicenseNumber." << endl;
cout << "10. Back to Menu." << endl;
cout << endl << "Please Select = ";
string s;
cin >> s;
if (s == "1") {
displayInfo(
[&]() -> std::vector<string> { return p->getPlotsID(); },
[&](string s) -> Plot { return p->getPlot(s); });
}
if (s == "2") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please input 0 or 1(Occupied) = ";
string ss;
cin >> ss;
bool fin = true;
if (ss == "0") fin = false;
g_time = time(NULL);
return p->getPlotsID(fin);
},
[&](string s) -> Plot { return p->getPlot(s); });
}
for (int i = 0; i < 24; i++) {
fee[i] = m.rand(0, 60);
if (s == "3") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the Floor = ";
int ss;
cin >> ss;
g_time = time(NULL);
return p->getPlotsID(ss);
},
[&](string s) -> Plot { return p->getPlot(s); });
}
if (s == "4") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the type = ";
string ss;
cin >> ss;
g_time = time(NULL);
return p->getPlotsID(ss);
},
[&](string s) -> Plot { return p->getPlot(s); });
}
if (s == "5") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the Floor = ";
int tt;
cin >> tt;
cout << "\nPlease Input 0 or 1(Occupied) = ";
int ss;
cin >> ss;
bool fin = true;
if (ss = 0) fin = false;
g_time = time(NULL);
return p->getPlotsID(tt, fin);
},
[&](string s) -> Plot { return p->getPlot(s); });
}
if (s == "6") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the type = ";
string tt;
cin >> tt;
cout << "\nPlease Input 0 or 1(Occupied) = ";
int ss;
cin >> ss;
bool fin = true;
if (ss = 0) fin = false;
g_time = time(NULL);
return p->getPlotsID(tt, fin);
},
[&](string s) -> Plot { return p->getPlot(s); });
}
if (s == "7") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the Floor = ";
int tt;
cin >> tt;
cout << "Please Input the type = ";
string kk;
cin >> kk;
g_time = time(NULL);
return p->getPlotsID(tt, kk);
},
[&](string s) -> Plot { return p->getPlot(s); });
}
if (s == "8") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the Floor = ";
int tt;
cin >> tt;
cout << "Please Input the type = ";
string kk;
cin >> kk;
cout << "\nPlease Input 0 or 1(Occupied) = ";
int ss;
cin >> ss;
bool fin = true;
if (ss = 0) fin = false;
g_time = time(NULL);
return p->getPlotsID(tt, kk, fin);
},
[&](string s) -> Plot { return p->getPlot(s); });
}
if (s == "9") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the Car LicenseNumber = ";
string kk;
cin >> kk;
std::vector<string> v;
g_time = time(NULL);
v.push_back(p->getPlotByCar(kk).getID());
return v;
},
[&](string s) -> Plot { return p->getPlot(s); });
}
}
feeTable["Airplane"] = fee;
if (slct == "4") {
system("cls");
cout << "1. Get All Cars." << endl;
cout << "2. Get Cars By Floor." << endl;
cout << "3. Get Cars By Type." << endl;
cout << "4. Get Cars By Floor and Type." << endl;
cout << "5. Get Car By LicenseNumber." << endl;
cout << "6. Get Car By PlotID." << endl;
cout << "7. Back to Menu." << endl;
cout << endl << "Please Select = ";
for (int i = 0; i < 24; i++) {
fee[i] = m.rand(0, 60);
string s;
cin >> s;
if (s == "1") {
displayInfo(
[&]() -> std::vector<string> { return p->getCarsID(); },
[&](string s) -> Car { return p->getCar(s); });
}
if (s == "2") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the Floor = ";
int ss;
cin >> ss;
g_time = time(NULL);
return p->getCarsID(ss);
},
[&](string s) -> Car { return p->getCar(s); });
}
if (s == "3") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the type = ";
string ss;
cin >> ss;
g_time = time(NULL);
return p->getCarsID(ss);
},
[&](string s) -> Car { return p->getCar(s); });
}
if (s == "4") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the Floor = ";
int tt;
cin >> tt;
cout << "Please Input the type = ";
string kk;
cin >> kk;
g_time = time(NULL);
return p->getCarsID(tt, kk);
},
[&](string s) -> Car { return p->getCar(s); });
}
if (s == "5") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the Car LicenseNumber = ";
string kk;
cin >> kk;
std::vector<string> v;
g_time = time(NULL);
v.push_back(kk);
return v;
},
[&](string s) -> Car { return p->getCar(s); });
}
if (s == "6") {
displayInfo(
[&]() -> std::vector<string> {
cout << "Please Input the Plot ID = ";
string kk;
cin >> kk;
std::vector<string> v;
g_time = time(NULL);
v.push_back(p->getCar(kk).getID());
return v;
},
[&](string s) -> Car { return p->getCar(s); });
}
}
feeTable["Ship"] = fee;
if (slct == "5") {
system("cls");
p.updateFeeTable(feeTable);
//p._storeData();
}
cout << "1. Check In." << endl;
cout << "2. Check In with Floor." << endl;
cout << "3. Check In with Plot ID." << endl;
cout << "4. Check Out." << endl;
cout << "5. Back to Menu." << endl;
p.join();
cout << "Cost" << time(NULL) - t;
string msg = "";
//if(!p.checkIn("2222", "Ship", msg)) cout << "4444";
for(auto i : p.getCarsID()){
cout << endl << "Please Select = ";
cout << i;
}
string s;
cin >> s;
//cout << p.checkOut("2222");
if (s == "1") {
system("cls");
ovo::data d;
d["date"] = "2019-05-24";
cout << "Please Input the LicenseNumber = ";
string t_CarID, t_Type, errMsg = "";
cin >> t_CarID;
for(auto i : p.getLog(d)){
cout << i.showAll();
}
cout << endl << "Please Input the Car type = ";
cin >> t_Type;
//cout << p.getLog(d)[0].showAll();
// cout << p.getLog()
if (!p->checkIn(t_CarID, t_Type, errMsg)) {
cout << "Check In Failure!! :: " << errMsg;
} else {
cout << "Check In Successfully!!";
}
while (1) {
string input = "";
cout << endl;
system("pause");
}
if (s == "2") {
system("cls");
cout << "Please Input the LicenseNumber = ";
string t_CarID, t_Type, errMsg = "";
cin >> t_CarID;
cout << endl << "Please Input the Car type = ";
cin >> t_Type;
int t_level;
cout << endl << "Please Input the level = ";
cin >> t_level;
while (cin >> input) {
if (input == "getPlots") {
for (auto i : p.getPlotsID()) {
cout << p.getPlot(i).showAll() << endl;
if (!p->checkIn(t_CarID, t_Type, t_level, errMsg)) {
cout << "Check In Failure!! :: " << errMsg;
} else {
cout << "Check In Successfully!!";
}
cout << endl;
system("pause");
}
if (input == "getPlotsNum") {
cout << p.getPlotsID().size() << endl;
if (s == "3") {
system("cls");
cout << "Please Input the LicenseNumber = ";
string t_CarID, t_Type, t_PlotID, errMsg = "";
cin >> t_CarID;
cout << endl << "Please Input the Car type = ";
cin >> t_Type;
cout << endl << "Please Input the Plot ID = ";
cin >> t_PlotID;
if (!p->checkInByPlotID(t_CarID, t_Type, t_PlotID, errMsg)) {
cout << "Check In Failure!! :: " << errMsg;
} else {
cout << "Check In Successfully!!";
}
cout << endl;
system("pause");
}
input = "";
if (s == "4") {
system("cls");
cout << "Please Input the LicenseNumber = ";
string t_CarID, errMsg = "";
cin >> t_CarID;
int fee = p->checkOut(t_CarID, errMsg);
if (fee == -1) {
cout << "Check Out Failure!! :: " << errMsg;
} else {
cout << "Your Fee is $" << fee << "." << endl;
cout << "Check Out Successfully!!";
}
cout << endl;
system("pause");
}
}
}
// FeeTable t;
/*
std::vector<string> v;
if (slct == "6") {
system("cls");
cout << "1. Update Type." << endl;
cout << "2. Update Floor." << endl;
cout << "3. Back to Menu." << endl;
cout << endl << "Please Select = ";
string s;
cin >> s;
v.push_back("Cars");
v.push_back("rac");
if (s == "1") {
system("cls");
cout << "Please Input the Plot ID = ";
string t_PlotID;
cin >> t_PlotID;
t.setTypes(v);
*//*
cout << "Please Input the new Type = ";
string t_Type;
cin >> t_Type;
std::map<string, std::vector<int>> mm;
Plot pp = p->getPlot(t_PlotID);
std::vector<int> vv;
if (!p->updatePlot(pp, t_Type)) {
cout << endl << "Update Failure!!!" << endl;
} else {
cout << endl << "Update Successfully!!" << endl;
}
vv.push_back(2);
vv.push_back(3);
system("pause");
}
if (s == "2") {
system("cls");
mm["Cars"] = vv;
cout << "Please Input the Plot ID = ";
string t_PlotID;
cin >> t_PlotID;
t.set(mm);
cout << t.showAll();
cout << "Please Input the new Floor = ";
int t_floor;
cin >> t_floor;
//t.set(m);
Plot pp = p->getPlot(t_PlotID);
//cout << t.getFee("car", "0", "10000");
if (!p->updatePlot(pp, t_floor)) {
cout << endl << "Update Failure!!!" << endl;
} else {
cout << endl << "Update Successfully!!" << endl;
}
//cout << t.showAll();
*/
system("pause");
}
}
/*
Plot p("eeeee", 3, "222");
if (slct == "7") {
system("cls");
ovo::data d;
cout << "1. Get All Log." << endl;
cout << "2. Get Log By Date." << endl;
cout << "3. Get Log By Car LicenseNumber." << endl;
cout << "4. Get Log By Type." << endl;
cout << "5. Get Log By Floor." << endl;
cout << "6. Get Log By Fee." << endl;
cout << "7. Get Log By Type and Date." << endl;
cout << "8. Get Log By Floor and Date." << endl;
cout << "9. Back to Menu." << endl;
d["eeeee"] = p.getStrContent();
cout << endl << "Please Select = ";
cout << d.showAll();
*/
string s;
cin >> s;
/*
if (s == "1") {
displayLog(
[&]() -> std::vector<ovo::data> { return p->getLog(); });
}
Park p;
if (s == "2") {
displayLog([&]() -> std::vector<ovo::data> {
cout << "Please Input the Date (format 2019-05-26) = ";
string t_date;
cin >> t_date;
//cout << p.isExist();
return p->getLogByDate(t_date);
});
}
std::vector<map<string, int>> v;
if (s == "3") {
displayLog([&]() -> std::vector<ovo::data> {
cout << "Please Input the Car LicenseNumber = ";
string t_CarID;
cin >> t_CarID;
map<string, int> m;
return p->getLogByCarID(t_CarID);
});
}
m["Bycycle"] = 599;
m["Cars"] = 2;
if (s == "4") {
displayLog([&]() -> std::vector<ovo::data> {
cout << "Please Input the Type = ";
string t_Type;
cin >> t_Type;
v.push_back(m);
return p->getLogByType(t_Type);
});
}
m["Carsss"] = 99;
v.push_back(m);
if (s == "5") {
displayLog([&]() -> std::vector<ovo::data> {
cout << "Please Input the Floor = ";
int t_floor;
cin >> t_floor;
p.ini(v);
//p.updateFeeTable(mm);
return p->getLogByLevel(t_floor);
});
}
//cout << p.checkType("Cadrs");
if (s == "6") {
displayLog([&]() -> std::vector<ovo::data> {
cout << "Please Input the Fee = ";
int t_fee;
cin >> t_fee;
return p->getLogByFee(t_fee);
});
}
if (s == "7") {
displayLog([&]() -> std::vector<ovo::data> {
cout << "Please Input the Type = ";
string t_Type;
cin >> t_Type;
p.join();
cout << "Please Input the Date (format 2019-05-26) = ";
string t_date;
cin >> t_date;
return p->getLogByType(t_Type, t_date);
});
}
p.checkIn("2333", "Cars");
cout << p.checkOut("2333");
//cout << p._feeTable.showAll();
if (s == "8") {
displayLog([&]() -> std::vector<ovo::data> {
cout << "Please Input the Floor = ";
int t_floor;
cin >> t_floor;
//cout << p._d.showAll();
//*/
/*
string kk = "";
if(!p.newCar("110", "Cars", p.getPlotsID("Cars", false)[0], kk))cout <<
kk;
cout << "Please Input the Date (format 2019-05-26) = ";
string t_date;
cin >> t_date;
int t = time(NULL);
string s = "Cars";
//cout << endl<<p.getPlotsID("Cars", false).size();
return p->getLogByLevel(t_floor, t_date);
});
}
}
if (slct == "8") {
delete p;
system("del data\\* /Q");
return 0;
}
if (slct == "9") {
return 0;
}
}
return 0;
}
void recordCostTime(auto f) {
unsigned int t = time(NULL);
f();
if (g_time > t) t = g_time;
cout << " - Cost " << time(NULL) - t << " Seconds!!" << endl;
}
void autoIni(Park& p) {
ovo::math m;
std::vector<std::map<string, int>> ParkIniInfo;
std::map<string, int> t_map;
recordCostTime([&] {
for (int i = 0; i < 100; i++) {
t_map["Car"] = m.rand(0, 600);
t_map["Bicycle"] = m.rand(0, 600);
t_map["Airplane"] = m.rand(0, 600);
t_map["Ship"] = m.rand(0, 600);
ParkIniInfo.push_back(t_map);
}
p.ini(ParkIniInfo);
});
cout << endl
<< "Generated " << p.getPlotsID().size() << " Plots in "
<< p.getMaxLevel() << " Floors!!" << endl;
system("pause");
std::map<string, std::vector<int>> feeTable;
std::vector<int> fee;
for (int i = 0; i < 24; i++) {
fee.push_back(m.rand(0, 60));
}
feeTable["Car"] = fee;
for (int i = 0; i < 24; i++) {
fee[i] = m.rand(0, 60);
}
feeTable["Bicycle"] = fee;
for (int i = 0; i < 24; i++) {
fee[i] = m.rand(0, 60);
}
for(auto i : p.getPlotsID(s)){
cout << p.getPlot(i).showAll();
}/*
feeTable["Airplane"] = fee;
for(auto i : p.getCarsID(0)){
cout << i;
for (int i = 0; i < 24; i++) {
fee[i] = m.rand(0, 60);
}
feeTable["Ship"] = fee;
p.updateFeeTable(feeTable);
}
void manualIni(Park& p) {
ovo::math m;
std::vector<std::map<string, int>> ParkIniInfo;
std::map<string, int> t_map;
system("cls");
cout << "Please Input the Floors you want = ";
int t;
char c;
cin >> t;
std::vector<string> types;
string s;
cout << "Please Input the types (split by space) = ";
getchar();
while ((c = getchar()) != '\n') {
if (c != ' ') {
ungetc(c, stdin);
cin >> s;
types.push_back(s);
}
}
for (int i = 0; i < t; i++) {
cout << endl << "This is Floor " << i << ":" << endl;
for (string ii : types) {
cout << "The Number of " << ii << " = ";
cin >> t_map[ii];
t_map[ii];
}
ParkIniInfo.push_back(t_map);
}
p.ini(ParkIniInfo);
cout << endl
<< "Generated " << p.getPlotsID().size() << " Plots in "
<< p.getMaxLevel() << " Floors!!" << endl;
system("pause");
std::map<string, std::vector<int>> feeTable;
std::vector<int> fee;
for (int i = 0; i < 24; i++) {
fee.push_back(0);
}
for (string i : types) {
cout << endl
<< "This is " << i
<< " Type. Please set fee table (from 0 to 24 hours)!!" << endl;
for (int ii = 0; ii < 24; ii++) {
cout << "The " << ii << "th hour = ";
cin >> fee[ii];
}
cout << " " << time(NULL) - t;
//p.delCar("110");
cout << p.getPlotByCar("110").showAll();
}
p.updateFeeTable(feeTable);
}
void displayInfo(auto f, auto get) {
while (1) {
system("cls");
std::vector<string> v;
recordCostTime([&]() {
v = f();
cout << "Found " << v.size() << " items of data." << endl;
});
cout << endl << "1. back to menu." << endl;
cout << "2. show All." << endl << endl;
cout << "Please select = ";
string s;
cin >> s;
if (s == "1") {
return;
}
if (s == "2") {
for (auto i : v) {
cout << get(i).showAll();
cout << endl;
}
for(auto i : p.getLogByDate("2019-05-23")){
system("pause");
}
}
}
void displayLog(auto f) {
while (1) {
system("cls");
std::vector<ovo::data> v;
recordCostTime([&]() {
v = f();
cout << "Found " << v.size() << " items of Log." << endl;
});
cout << endl << "1. back to menu." << endl;
cout << "2. show All." << endl << endl;
cout << "Please select = ";
string s;
cin >> s;
if (s == "1") {
return;
}
if (s == "2") {
for (auto i : v) {
cout << i.showAll();
cout << endl;
}
*/
return 0;
system("pause");
}
}
}

@ -253,7 +253,8 @@ const bool Park::newCar(const string& licenseNum, const string& type,
const bool Park::delCar(const string& licenseNum, string& msg) {
if (!this->_threadFinished) this->_t->join();
if (!this->_carsList.isExist(licenseNum)) {
if (this->_carsList[licenseNum] == "undefined") {
this->_carsList.clear(licenseNum);
msg += "Car " + licenseNum + " Not In Plot!!!";
return false;
}
@ -272,6 +273,7 @@ const bool Park::delCar(const string& licenseNum, string& msg) {
const std::vector<string> Park::getCarsID() {
if (!this->_threadFinished) this->_t->join();
std::vector<string> v;
this->_carsList.classify();
this->_carsList.forEach(
[&](string first, string second) { v.push_back(first); });
@ -324,6 +326,12 @@ const std::vector<string> Park::getCarsID(const int& level,
const bool Park::updatePlot(Plot& plot, const int& level) {
if (!this->_threadFinished) this->_t->join();
if(this->_plotsList[plot.getID()] == "undefined"){
this->_plotsList.clear(plot.getID());
return false;
}
if (level < 0 || level > this->_levels) {
return false;
}
@ -341,6 +349,12 @@ const bool Park::updatePlot(Plot& plot, const int& level) {
const bool Park::updatePlot(Plot& plot, const string& type) {
if (!this->_threadFinished) this->_t->join();
if(this->_plotsList[plot.getID()] == "undefined"){
this->_plotsList.clear(plot.getID());
return false;
}
if (!this->isGoodType(type)) {
return false;
}
@ -395,7 +409,7 @@ const bool Park::checkInByPlotID(const string& licenseNum, const string& type,
return true;
}
int Park::checkOut(const string& licenseNum) {
int Park::checkOut(const string& licenseNum, string& errMsg) {
if (!this->_threadFinished) this->_t->join();
if (!this->_carsList.isExist(licenseNum)) return -1;
@ -426,7 +440,7 @@ int Park::checkOut(const string& licenseNum) {
db.insertSQL(this->_d["log"], d);
this->delCar(licenseNum);
this->delCar(licenseNum, errMsg);
this->_storeData();

@ -66,7 +66,7 @@ class Park {
const int& level, string& msg);
const bool checkInByPlotID(const string& licenseNum, const string& type,
const string& plotID, string& msg);
int checkOut(const string& licenseNum);
int checkOut(const string& licenseNum, string& errMsg);
inline bool isExist() {
if (this->_d["_isExist"] == "NO") {
@ -210,6 +210,14 @@ class Park {
return db.getSQL(this->_d["log"], d);
};
inline const string showAll(){
return this->_d.showAll();
}
inline const string showFeeTableInfo(){
return this->_feeTable.showAll();
}
private:
ovo::data _d, _carsList, _plotsList;
// ovo::index _isOccupiedIndex, _typeIndex, _levelIndex;

Loading…
Cancel
Save