#include <iostream>
#include <map>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std;
map <string, map <int, string> > depot, base;
map <string, map <int, string> > road;
short int Menu();
void LoadBD();
void AddToMap(string number, int route, string full_name);
void DepartureBus();
void EntryBus();
void ShowDepot();
void ShowRoad();
void ShowByRoute();
main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
LoadBD();
short int j = 0;
while (true) {
j = Menu();
switch(j){
case(1):
ShowDepot();
break;
case(2):
DepartureBus();
break;
case(3):
EntryBus();
break;
case(4):
cout << "\t\tПОЛНЫЙ СПИСОК АВТОБУСОВ ПО НОМЕРУ АВТОБУСА" << endl;
ShowDepot();
cout << endl;
ShowRoad();
break;
case(5):
cout << "\t\tПОЛНЫЙ СПИСОК АВТОБУСОВ ПО НОМЕРУ МАРШРУТА" << endl;
ShowByRoute();
break;
case(6):
cout << "\t\tВЫХОД ИЗ ПРОГРАММЫ";
exit(0);
break;
default: cout << "Вы ввели неверный пункт меню" << endl;
}
}
}
short int Menu(){
short int j = 0;
cout <<"---------------------------------------------------"<<'\n';
cout <<"1-Вывести список автобусов в парке"<<'\n';
cout <<"2-Отметить выезд автобуса из парка"<<'\n';
cout <<"3-Отметить возвращение автобуса в парк"<<'\n';
cout <<"4-Вывести весь список автобусов по номеру автобуса"<<'\n';
cout <<"5-Вывести весь список автобусов по номеру маршрута"<<'\n';
cout <<"6-Выход"<<'\n';
cout <<">";
cin>>j;
if (j!=1 || j!=2 || j!=3 || j!=4 || j!=5 || j!=6) return 0;
cout <<"---------------------------------------------------"<<'\n';
return j;
}
void LoadBD(){
string number, full_name;
int route;
ifstream f("bd.txt");
if (!f.is_open()){
cout << "\t\tОТСУТСТВУЕТ БАЗА ДАННЫХ АВТОБУСНОГО ПАРКА! РАБОТА НЕВОЗМОЖНА!" << endl;
return;
}
while (!f.eof()){
f>>number;
f>>route;
f>>full_name;
AddToMap(number, route, full_name);
}
base=depot;
cout << "\t\tУСПЕШНАЯ ЗАГРУЗКА БАЗЫ ДАННЫХ" << endl;
}
void AddToMap(string number, int route, string full_name){
depot[number][route] = full_name;
}
void DepartureBus(){
string number;
cout << "Введите номер автобуса" << endl;
cout <<">";
cin >> number;
if (depot.find(number) != depot.end()){
map<string, map<int, string> > :: iterator it_depot;
it_depot = depot.find(number);
map<int, string>::iterator it2_depot = it_depot->second.begin();
road[number][it2_depot->first]=it2_depot->second;
depot.erase(it_depot);
cout << "*Автобус с номером " << number << " выехал из парка*" << endl;
}else if(base.find(number) != base.end()) {
cout << "Этот автобус на маршруте" << endl;
}else{
cout << "ОШИБКА #300. Автобус не найден! Проверьте набранный номер." << endl;
}
}
void EntryBus(){
string number;
cout << "Введите номер автобуса" << endl;
cout <<">";
cin >> number;
if (road.find(number) != road.end()){
map<string, map<int, string> > :: iterator it_road;
it_road = road.find(number);
map<int, string>::iterator it2_road = it_road->second.begin();
depot[number][it2_road->first]=it2_road->second;
road.erase(it_road);
cout << "*Автобус с номером " << number << " вернулся в парк*" << endl;
}else if(base.find(number) != base.end()) {
cout << "Этот автобус в парке" << endl;
}else{
cout << "ОШИБКА #300. Автобус не найден! Проверьте набранный номер." << endl;
}
}
void ShowDepot(){
int i=0;
cout<<" АВТОБУСЫ В ПАРКЕ:" << endl;
if (depot.begin() == depot.end()) cout << "Автобусов в парке нет" << endl;
for (map<string, map<int, string> >::iterator it_depot = depot.begin(); it_depot != depot.end(); ++it_depot)
{
cout << i << ") "<< it_depot->first << ' ';
for (map<int, string>::iterator it2_depot = it_depot->second.begin(); it2_depot != it_depot->second.end(); ++it2_depot)
cout << it2_depot->first << ' ' << it2_depot->second;
cout <<endl;
i++;
}
}
void ShowRoad(){
int i=0;
cout<<" АВТОБУСЫ НА МАРШРУТЕ:" << endl;
if (road.begin() == road.end()) cout << "Автобусов на маршруте нет" << endl;
for (map<string, map<int, string> >::iterator it_road = road.begin(); it_road != road.end(); ++it_road)
{
cout << i << ") "<< it_road->first << ' ';
for (map<int, string>::iterator it2_road = it_road->second.begin(); it2_road != it_road->second.end(); ++it2_road)
cout << it2_road->first << ' ' << it2_road->second;
cout <<endl;
i++;
}
}
void ShowByRoute(){
map <int, map <string, string> > temp_map;
map<int, map<string, string> > :: iterator it_temp;
cout<<" АВТОБУСЫ В ПАРКЕ:" << endl;
if (depot.begin() == depot.end()){
cout << "Автобусов в парке нет" << endl;
}else{
for (map<string, map<int, string> >::iterator it_depot = depot.begin(); it_depot != depot.end(); ++it_depot)
{
for (map<int, string>::iterator it2_depot = it_depot->second.begin(); it2_depot != it_depot->second.end(); ++it2_depot)
temp_map[it2_depot->first][it_depot->first]=it2_depot->second;
}
int i=0;
for (it_temp = temp_map.begin(); it_temp != temp_map.end(); ++it_temp)
{
cout << i << ") "<< it_temp->first << ' ';
for (map<string, string> :: iterator it2_temp = it_temp->second.begin(); it2_temp != it_temp->second.end(); ++it2_temp)
cout << it2_temp->first << ' ' << it2_temp->second;
cout <<endl;
i++;
}
}
temp_map.clear();
cout << endl;
cout<<" АВТОБУСЫ НА МАРШРУТЕ:" << endl;
if (road.begin() == road.end()){
cout << "Автобусов на маршруте нет" << endl;
}else{
for (map<string, map<int, string> >::iterator it_road = road.begin(); it_road != road.end(); ++it_road)
{
for (map<int, string>::iterator it2_road = it_road->second.begin(); it2_road != it_road->second.end(); ++it2_road)
temp_map[it2_road->first][it_road->first]=it2_road->second;
}
int i=0;
for (it_temp = temp_map.begin(); it_temp != temp_map.end(); ++it_temp)
{
cout << i << ") "<< it_temp->first << ' ';
for (map<string, string> :: iterator it2_temp = it_temp->second.begin(); it2_temp != it_temp->second.end(); ++it2_temp)
cout << it2_temp->first << ' ' << it2_temp->second;
cout <<endl;
i++;
}
}
temp_map.clear();
}