-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
127 lines (122 loc) · 4.02 KB
/
main.cpp
File metadata and controls
127 lines (122 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "myJsonFilms.h"
#define FILM_COUNT 6
int main()
{
std::map<std::string, Film> mFilms;
int command;
cout << "---JSON Films---" << endl;
while(true)
{
showMenu();
cout << "Enter the command:";
cin >> command;
system("cls");
if(command > 2 && command < 7 && mFilms.empty())
{
cout << "Films is not found!" << endl;
cout << "First run command 1 or 2" << endl;
}
else if (command == FROMTXT)
{
cout << "Loading films from txt..." << endl;
for (int i = 0; i < FILM_COUNT; ++i)
{
string fileName = "txt//"+to_string(i+1)+".txt";
ifstream file(fileName);
if(!file.is_open())
{
cout << "File '" << fileName << "' is not found!" << endl;
continue;
}
mFilms.insert(filmFromTxt(file));
cout << "-Success!" << endl;
file.close();
}
}
else if (command == WRITEJSON)
{
nh::json jsonFilms = mFilms;
ofstream jsonFilmsFile("Films.json");
jsonFilmsFile << jsonFilms;
cout << "-Success!" << endl;
jsonFilmsFile.close();
}
else if (command == FROMJSON)
{
cout << "Loading films from json..." << endl;
ifstream jsonFilmsFile("Films.json");
if(jsonFilmsFile.is_open())
{
try
{
nh::json jsonFilms;
jsonFilmsFile >> jsonFilms;
cout << "-Success!" << endl;
mFilms.clear();
mFilms = jsonFilms.get<std::map<std::string, Film>>();
}
catch (nh::json::parse_error& e)
{
cout << "Message: " << e.what() << '\n'
<< "Exception id: " << e.id << '\n'
<< "Byte position of error: " << e.byte << std::endl;
cout << "--Try running commands 1 and 3--" << endl;
}
}
else
{
cout << "File 'Films.json' is not found!" << endl;
cout << "Try running commands 1 and 3" << endl;
}
jsonFilmsFile.close();
}
else if (command == SHOWALLINFO)
{
nh::json jsonFilm = mFilms;
stringstream ss;
ss << std::setw(2) << jsonFilm;
std::cout << "---All films info---\n" << ss.str() << "\n";
}
else if (command == SHOWFILMINFO)
{
string filmName;
cout << "Enter the film name:";
cin >> filmName;
if(mFilms.find(filmName) == mFilms.end())
cout << "The film '" << filmName << "' is not found!" << endl;
else
{
nh::json jsonFilm = mFilms.at(filmName);
stringstream ss;
ss << std::setw(2) << jsonFilm;
std::cout << "--- " << filmName << " info---\n" << ss.str() << "\n";
}
}
else if (command == SEARCH)
{
string name;
cout << "Enter search name(use '_'):";
cin >> name;
std::vector<string> result;
for(const auto& f:mFilms)
for(const auto& c:f.second.Characters)
if(c.actor == name)
result.push_back(f.first + " (" + c.character + ")");
cout << "Films with " << name << ":" << endl;
for (const auto& f:result)
cout <<" - " << f << endl;
}
else if (command == EXIT)
{
cout << "---Bey, bye!---" << endl;
system("pause");
break;
}
else
cout << "Unknown command!" << endl;
system("pause");
system("cls");
cin.clear();
cin.ignore();
}
}