-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
237 lines (198 loc) · 7.92 KB
/
main.cpp
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
enum class ServiceType { Mail, File, Network };
enum class ConnectionType { Input, Output };
std::string formatDateTime(std::time_t dateTime) {
std::tm *ptm = std::localtime(&dateTime);
char buffer[32];
std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M", ptm);
return std::string(buffer);
}
class NetworkService {
public:
virtual ServiceType getType() const = 0;
virtual ConnectionType getConnectionType() const = 0;
virtual std::string getAddress() const = 0;
virtual void setAddress(const std::string& addr) = 0;
virtual double calculateCost() const = 0;
virtual std::string getDateTime() const = 0;
virtual double getVolume() const = 0;
virtual void setVolume(double newVolume) = 0;
virtual ~NetworkService() {}
};
class MailDescriptor : public NetworkService {
protected:
ConnectionType connectionType;
std::string address;
std::time_t dateTime;
double volume;
public:
MailDescriptor(ConnectionType type, std::string addr, std::time_t dt, double vol)
: connectionType(type), address(addr), dateTime(dt), volume(vol) {}
ServiceType getType() const override { return ServiceType::Mail; }
ConnectionType getConnectionType() const override { return connectionType; }
std::string getAddress() const override { return address; }
void setAddress(const std::string& addr) override { address = addr; }
double calculateCost() const override { return volume * 0.05; }
std::string getDateTime() const override { return formatDateTime(dateTime); }
double getVolume() const override { return volume; }
void setVolume(double newVolume) override { volume = newVolume; }
};
class FileDescriptor : public NetworkService {
protected:
ConnectionType connectionType;
std::string address;
std::time_t dateTime;
double volume;
public:
FileDescriptor(ConnectionType type, std::string addr, std::time_t dt, double vol)
: connectionType(type), address(addr), dateTime(dt), volume(vol) {}
ServiceType getType() const override { return ServiceType::File; }
ConnectionType getConnectionType() const override { return connectionType; }
std::string getAddress() const override { return address; }
void setAddress(const std::string& addr) override { address = addr; }
double calculateCost() const override { return volume * 0.05; }
std::string getDateTime() const override { return formatDateTime(dateTime); }
double getVolume() const override { return volume; }
void setVolume(double newVolume) override { volume = newVolume; }
};
class NetworkDescriptor : public NetworkService {
protected:
std::string senderAddress;
std::time_t dateTime;
int duration;
double volume;
public:
NetworkDescriptor(std::string senderAddr, std::time_t dt, int dur, double vol)
: senderAddress(senderAddr), dateTime(dt), duration(dur), volume(vol) {}
ServiceType getType() const override { return ServiceType::Network; }
ConnectionType getConnectionType() const override { return ConnectionType::Output; }
std::string getAddress() const override { return senderAddress; }
void setAddress(const std::string& addr) override { senderAddress = addr; }
double calculateCost() const override { return volume * 0.05 + duration * 0.1; }
std::string getDateTime() const override { return formatDateTime(dateTime); }
double getVolume() const override { return volume; }
void setVolume(double newVolume) override { volume = newVolume; }
};
class Subscriber {
private:
std::string networkAddress;
public:
Subscriber(std::string addr) : networkAddress(addr) {}
std::string getAddress() const { return networkAddress; }
};
class Table {
private:
std::vector<NetworkService*> services;
public:
void addService(NetworkService* service) {
services.push_back(service);
}
std::vector<NetworkService*> getAllServices() const {
return services;
}
void display() const {
for (const auto* service : services) {
std::cout << "Àäðåñ: " << service->getAddress() << "\n"
<< "Òèï ñåðâèñà: " << static_cast<int>(service->getType()) << "\n"
<< "Âðåìÿ: " << service->getDateTime() << "\n"
<< "Ñòîèìîñòü: " << service->calculateCost() << std::endl;
}
}
};
class ServerDescriptor {
private:
std::string networkAddress;
std::string networkName;
Table connectionTable;
public:
ServerDescriptor(std::string addr, std::string name)
: networkAddress(addr), networkName(name) {}
std::string getNetworkAddress() const { return networkAddress; }
void addSubscriber(NetworkService* service) {
connectionTable.addService(service);
}
NetworkService* findServiceByAddress(const std::string& address) const {
auto services = connectionTable.getAllServices();
for (auto* service : services) {
if (service->getAddress() == address) {
return service;
}
}
return nullptr;
}
void displayConnectionTable() const {
connectionTable.display();
}
};
class Application {
private:
Table connectionTable;
public:
void addService(NetworkService* service) {
connectionTable.addService(service);
}
void displayServicesForSubscriber(const std::string& subscriberAddress) const {
auto services = connectionTable.getAllServices();
bool serviceFound = false;
for (const auto* service : services) {
if (service->getAddress() == subscriberAddress) {
std::cout << "Àäðåñ: " << service->getAddress() << "\n"
<< "Òèï ñåðâèñà: " << static_cast<int>(service->getType()) << "\n"
<< "Âðåìÿ: " << service->getDateTime() << "\n"
<< "Ñòîèìîñòü: " << service->calculateCost() << std::endl;
serviceFound = true;
}
}
if (!serviceFound) {
std::cout << "Óñëóãè äëÿ àáîíåíòà ñ àäðåñîì " << subscriberAddress << " íå íàéäåíû." << std::endl;
}
}
};
int main() {
setlocale(LC_ALL, "Russian");
ServerDescriptor server("192.168.1.1", "MainServer");
Application app;
// Äåìîíñòðàöèÿ ñîçäàíèÿ ðàçëè÷íûõ ñåðâèñîâ
MailDescriptor mailService(ConnectionType::Output, "192.168.1.2", std::time(nullptr), 10);
FileDescriptor fileService(ConnectionType::Input, "192.168.1.3", std::time(nullptr), 15);
NetworkDescriptor networkService("192.168.1.4", std::time(nullptr), 30, 20);
// Äîáàâëåíèå ñåðâèñîâ â òàáëèöó ñåðâåðà
server.addSubscriber(&mailService);
server.addSubscriber(&fileService);
server.addSubscriber(&networkService);
// Äîáàâëåíèå ñåðâèñîâ â ïðèëîæåíèå
app.addService(&mailService);
app.addService(&fileService);
app.addService(&networkService);
bool running = true;
while (running) {
int choice;
std::cout << "1. Ïîêàçàòü òàáëèöó ñîåäèíåíèé ñåðâåðà\n";
std::cout << "2. Ïîêàçàòü óñëóãè äëÿ êîíêðåòíîãî àáîíåíòà\n";
std::cout << "3. Âûéòè\n";
std::cout << "Âûáåðèòå îïåðàöèþ: ";
std::cin >> choice;
switch (choice) {
case 1: {
server.displayConnectionTable();
break;
}
case 2: {
std::string address;
std::cout << "Ââåäèòå ñåòåâîé àäðåñ àáîíåíòà: ";
std::cin >> address;
app.displayServicesForSubscriber(address);
break;
}
case 3:
running = false;
break;
default:
std::cout << "Íåâåðíûé âûáîð, ïîïðîáóéòå ñíîâà.\n";
}
}
return 0;
}