BikeManagment.h
#pragma once
#include "stdafx.h"
#include <vector>
#define protect public
#define private public
using namespace std;
class BikeManagment
{
public:
typedef struct bike
{
int bike_id;
int rent_time;
int return_time;
}bike;
vector<bike *> available;
vector<bike *> rented;
vector<bike *> repair;
int income;
int cost;
void InitSystem();
void UI();
void ShowRentedBike();
void ShowAvailableBike();
void ShowRepairBike();
void AddBike();
void RentBike();
void ReturnBike();
void RepairBike();
void CalculateCost();
void CalculateIncome();
};
BikeManagment.cpp
#include "stdafx.h"
#include "BikeManagment.h"
#include "iostream"
using namespace std;
void BikeManagment::InitSystem()
{
income = 0;
cost = 0;
}
void BikeManagment::UI()
{
bool run = true;
while (run)
{
cout << "1.AddBike" << endl;
cout << "2.RentBike" << endl;
cout << "3.ReturnBike" << endl;
cout << "4.RepairBike" << endl;
cout << "5.CalculateIncome" << endl;
cout << "6.CalculateCost" << endl;
cout << "7.ShowAvailableBike" << endl;
cout << "8.ShowRentedBike" << endl;
cout << "9.ShowRepairBike" << endl;
cout << "0.Exit" << endl;
int input;
cin >> input;
switch (input)
{
case 1:AddBike();break;
case 2:RentBike();break;
case 3:ReturnBike();break;
case 5:CalculateIncome();break;
case 6:CalculateCost();break;
case 7:ShowAvailableBike();break;
case 8:ShowRentedBike();break;
case 9:ShowRepairBike();break;
}
cout << "Continue(1/0):";
cin >> input;
switch (input)
{
case 1:break;
case 0:run = false;
}
}
}
void BikeManagment::AddBike()
{
bike * this_bike = new bike;
this_bike->rent_time = 0;
this_bike->return_time = 0;
cout << "bike_id:";
cin >> this_bike->bike_id;
available.push_back(this_bike);
}
void BikeManagment::RentBike()
{
bike * this_bike;
int rent_time;
if (!available.empty())
{
/*
取出
*/
this_bike = available.back();
available.pop_back();
/*
借出时间
*/
cout << "rent_time:";
cin >> rent_time;
this_bike->rent_time += rent_time;
this_bike->return_time = rent_time;
/*
插入
*/
vector<bike *>::iterator iter;
for (iter = rented.begin();(iter != rented.end()) && ((*iter)->return_time > this_bike->return_time);iter++);
rented.insert(iter, this_bike);
cout << "rent success" << endl;
}
else
{
cout << "available bike is empty" << endl;
}
}
void BikeManagment::ReturnBike()
{
if (!rented.empty())
{
/*
取出
*/
bike * this_bike;
this_bike = rented.back();
rented.pop_back();
/*
插入
*/
vector<bike *>::iterator iter;
iter = available.begin();
iter = available.end();
for (iter = available.begin();(iter != available.end()) && ((*iter)->rent_time > this_bike->rent_time);iter++);
available.insert(iter, this_bike);
/*
结算
*/
income += this_bike->return_time;
this_bike->return_time = 0;
cout << "return success" << endl;
}
else
{
cout << "rented bike is empty" << endl;
}
}
void BikeManagment::RepairBike()
{
if (!available.empty())
{
bike * this_bike = available.back();
available.pop_back();
cost++;
repair.push_back(this_bike);
}
else
{
cout << "nothing to repair" << endl;
}
}
void BikeManagment::ShowAvailableBike()
{
if (!available.empty())
{
vector<bike *>::iterator iter = available.begin();
while (iter != available.end())
{
cout << " bike_id:" << (*iter)->bike_id << " rent_time" << (*iter)->rent_time << endl;
iter++;
}
}
else
{
cout << "available bike is empty" << endl;
}
}
void BikeManagment::ShowRentedBike()
{
if (!rented.empty())
{
vector<bike *>::iterator iter = rented.begin();
while (iter != rented.end())
{
cout << " bike_id:" << (*iter)->bike_id << " rent_time" << (*iter)->rent_time << endl;
iter++;
}
}
else
{
cout << "rented bike is empty" << endl;
}
}
void BikeManagment::ShowRepairBike()
{
if (!repair.empty())
{
vector<bike *>::iterator iter = repair.begin();
while (iter != repair.end())
{
cout << " bike_id:" << (*iter)->bike_id << " rent_time" << (*iter)->rent_time << endl;
iter++;
}
}
else
{
cout << "repair bike is empty" << endl;
}
}
void BikeManagment::CalculateIncome()
{
cout << " income:" << income << endl;
}
void BikeManagment::CalculateCost()
{
cout << " cost:" << cost << endl;
}