修复bug
main.cpp#include "CarParking.h"
#include <iostream>
CarParking car_parking;
int main()
{
car_parking.EnterParking(new Car("晋H·A1363"), "2018/04/18 15:24:31");
car_parking.EnterParking(new Car("晋H·YQ911"), "2018/04/18 15:43:52");
Car *car;
car = car_parking.LeaveParking("晋H·YQ911", "2018/04/18 16:46:11");
//std::cout << car->license_plate_number << std::endl;
delete car;
car_parking.ParkingStatus();
car = car_parking.LeaveParking("晋H·A1363", "2018/04/18 16:47:37");
//std::cout << car->license_plate_number << std::endl;
delete car;
car_parking.ParkingStatus();
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
delete car;
car_parking.ParkingStatus();
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
delete car;
car_parking.ParkingStatus();
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
delete car;
car_parking.ParkingStatus();
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
delete car;
car_parking.ParkingStatus();
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car = car_parking.LeaveParking("?????????", "2018/04/18 17:57:01");
delete car;
car_parking.ParkingStatus();
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.EnterParking(new Car("?????????"), "2018/04/18 17:56:39");
car_parking.ParkingStatus();
car = car_parking.LeaveParking("?????????", "2018/04/18 16:47:37");
//std::cout << car->license_plate_number << std::endl;
delete car;
car_parking.ParkingStatus();
car = car_parking.LeaveParking("?????????", "2018/04/18 16:47:37");
//std::cout << car->license_plate_number << std::endl;
delete car;
car_parking.ParkingStatus();
return 0;
}
CarParking.cpp#include "CarParking.h"
#include <iostream>
CarParking::CarParking(): history(parking_space_count)
{
total_income = 0;
}
void CarParking::EnterParking(Car *car, std::string enter_time)
{
waiting_queue.push(InfoNode(car, enter_time));
if(parking_space.size() < 5)
{
parking_space.push_back(waiting_queue.front());
waiting_queue.pop();
}
}
static int IteratorToIndex(std::vector<InfoNode> &v, std::vector<InfoNode>::iterator iter)
{
return iter - v.begin();
}
// 在等待队列的车不能离开停车场^_^,这里也许需要改进,目前就这样了
Car *CarParking::LeaveParking(const std::string &license_plate_number, const std::string leave_time)
{
for(auto iter = parking_space.begin(); iter != parking_space.end(); ++iter)
{
if(iter->car->license_plate_number == license_plate_number)
{
InfoNode in(iter->car, iter->enter_time);
HistoryNode hn(*in.car, in.enter_time, leave_time);
CollectionOfCharges(hn);
history.at(IteratorToIndex(parking_space, iter)).push_back(hn);
parking_space.erase(iter);
// 如果等待队列有车,现在有空位了
// 这里处理进入停车场和离开停车场的车位有点问题,目前就这样了
// 也许停车位该用数组?
if(waiting_queue.size() != 0)
{
parking_space.push_back(waiting_queue.front());
waiting_queue.pop();
}
return in.car;
}
}
return NULL;
}
void CarParking::ParkingStatus() const
{
std::cout << "总收入:" << total_income << std::endl;
ViewHistory();
}
void CarParking::ViewHistory() const
{
std::cout << "**************************************************" << std::endl;
std::cout << "历史记录:" << std::endl;
for(int i = 0; i < parking_space_count; ++i)
{
if(history.at(i).size() == 0)
continue;
std::cout << "第 " << i << "号停车位:" << std::endl;
for(auto iter = history.at(i).begin(); iter != history.at(i).end(); ++iter)
{
std::cout << std::endl;
std::cout << "车牌号:" << iter->car.license_plate_number << std::endl;
std::cout << "进入时间:" << iter->enter_time << std::endl;
std::cout << "离开时间:" << iter->leave_time << std::endl;
std::cout << std::endl;
}
}
std::cout << "**************************************************" << std::endl;
}
void CarParking::CollectionOfCharges(HistoryNode & hn)
{
}
CarParking.h#ifndef _CAR_PARKING_HPP_
#define _CAR_PARKING_HPP_
#include <queue>
#include <string>
#include <vector>
class Car
{
public:
const std::string license_plate_number;
Car(std::string license_plate_number): license_plate_number(license_plate_number){}
};
class InfoNode
{
public:
Car *car;
const std::string enter_time;
InfoNode(Car *car, std::string enter_time): enter_time(enter_time){ this->car = car; }
InfoNode &operator=(const InfoNode &dest)
{
this->car = dest.car;
return *this;
}
};
class HistoryNode
{
public:
const Car car;
const std::string enter_time;
const std::string leave_time;
HistoryNode(const Car car, const std::string enter_time, const std::string leave_time): car(car), enter_time(enter_time), leave_time(leave_time){}
};
class CarParking
{
public:
const int parking_space_count = 5; // 设小点,方便调试
CarParking();
void EnterParking(Car *car, std::string enter_time);
Car *LeaveParking(const std::string &license_plate_number, const std::string leave_time);
void ParkingStatus() const;
void ViewHistory() const;
private:
std::vector<std::vector<HistoryNode>> history;
std::queue<InfoNode> waiting_queue;
std::vector<InfoNode> parking_space;
long total_income;
void CollectionOfCharges(HistoryNode &hn);
};
#endif
|