-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNatobase.cpp
More file actions
170 lines (148 loc) · 5.4 KB
/
Natobase.cpp
File metadata and controls
170 lines (148 loc) · 5.4 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//Stanislaw Dutkiewicz 263509
#include "Natobase.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <istream>
#include <algorithm>
using namespace std;
int Natobase::counter = 0;
Natobase::Natobase(string n, string ci, string co, int ri, int tan, int pla, int lan)
: name(n), city(ci), country(co), rifles(ri), tanks(tan), planes(pla), launcher(lan) {};
Natobase::Natobase(string init) : Natobase("20thDiv", "Kuldiga", "Latvia", 100, 50, 75, 32) {};
std::ostream &tosave(std::ostream &sv, const Natobase &pro) {
sv << pro.counter << pro.ID << " " << pro.name << " " << pro.city << " " << pro.country << " " << pro.rifles
<< " " << pro.tanks << " " << pro.planes << " " << pro.launcher;
return sv;
}
std::istream &toim(std::istream &imm, Natobase &pro) {
imm >> pro.counter >> pro.ID >> pro.name >> pro.city >> pro.country >> pro.rifles >>
pro.tanks >> pro.planes >> pro.launcher;
return imm;
}
std::ostream &printnames(std::ostream &pr, Natobase &pro) {
pr << "\nID:" << pro.ID << "\nName:" << pro.name;
return pr;
}
std::ostream &operator<<(std::ostream &pr, const Natobase &pro) {
pr << "ID: " << pro.get_ID() << "\n\n" "Name: " << pro.get_name()
<< "\n" << "City: " << pro.get_city()
<< "\n" << "Country: " << pro.get_country()
<< "\n" << "Number of rifles: " << pro.get_rifle()
<< "\n" << "Number of tanks: " << pro.get_tanks()
<< "\n" << "Number of planes: " << pro.get_planes()
<< "\n" << "Number of launcher: " << pro.get_launch();
return pr;
}
std::istream &operator>>(std::istream &re, Natobase &product) {
std::cout << "Enter the name of the base: \n";
re >> product.name;
std::cout << "Enter the city where it is located: \n";
re >> product.city;
std::cout << "Enter the country where it is located: \n";
re >> product.country;
std::cout << "Enter the number of guns held: \n";
re >> product.rifles;
std::cout << "Enter the number of tanks stationing: \n";
re >> product.tanks;
std::cout << "Enter the number of planes stationing: \n";
re >> product.planes;
std::cout << "Enter the number of launchers stationing: \n";
re >> product.launcher;
product.get_counter();
product.ID = product.counter;
return re;
}
///////////////////////////////////////////////////
void Database::Read_data() {;
Natobase Nato;
if (cin >> Nato) {
DbMain.push_back(Nato);
} else {
std::cerr << "Data Error" << std::endl;
}
}
void Database::Print_data() {
cout << "The bases are:\n\n";
for (auto it = std::begin(DbMain); it != std::end(DbMain); ++it) {
cout << std::endl;
cout << *it;
}
}
void Database::SaveDB() {
std::ofstream file;
std::string filename = "Database.txt";
file.open(filename, std::ofstream::trunc);
for (auto it = std::begin(DbMain); it != std::end(DbMain); ++it) {
tosave(file, *it) << "\n";
}
}
void Database::OpenDB() {
std::ifstream filein("Database.txt");
Natobase uw;
if (!filein) {
cout << "\nAn error has been occurred with the loading Natobases\n";
} else {
while (toim(filein, uw)) {
DbMain.push_back(uw);
}
filein.close();
}
}
void Database::RemoveDB() {
int det;
cout << "Enter the ID of the base that you want to remove:\n";
std::cout << "The bases are:\n";
Printids();
cin >> det;
auto iter = std::find_if(std::begin(DbMain), std::end(DbMain), [&](Natobase &nb) { return nb.get_ID() == det; });
if (iter != DbMain.end()) {
DbMain.erase(iter);
}
}
void Database::EditDB() {
int editt;
cout << "Enter the number of the base that you want to change:\n";
std::cout << "The bases are:\n";
Printids();
cin >> editt;
auto iter = std::find_if(std::begin(DbMain), std::end(DbMain), [&](Natobase &nb) {
return nb.get_ID() == editt;
}); //declared an iterator that uses function find_if to match the iterator with the ID of the database
if (iter != DbMain.end()) {
cout << "\nPlease enter the input of the base one more time" << endl;
cin >> *iter;
}
}
void Database::Printids() {
for (auto it = std::begin(DbMain); it != std::end(DbMain); ++it) {
std::cout << std::endl;
printnames(std::cout, *it) << std::endl;
}
}
void Database::Compare() {
int comp;
cout << "Enter the number of the base that you want to change:\n";
std::cout << "The bases are:\n";
Printids();
cin >> comp;
auto it = std::find_if(std::begin(DbMain), std::end(DbMain), [&](Natobase &nb) { return nb.get_ID() == comp; });
if (it != DbMain.end()) {
int num;
std::cout << "\nPlease enter the number of Russian divisions attacking the base:\n";
std::cin >> num;
int sum = 1 * it->get_rifle() + 0.9 * it->get_tanks() + 2 * it->get_planes() + 3 * it->get_launch();
int Russian = num * 1300;
if (Russian < sum) {
std::cout << "\n\nCongratulations!!The base will survive the attack\n";
} else if (Russian > sum) {
std::cout << "\n\nThe base will not survive\n";
} else {
std::cout << "\n\nError\n";
}
}
}
void Database::Clear() {
DbMain.clear();
}