-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRiverSystem.cpp
More file actions
278 lines (230 loc) · 8.09 KB
/
RiverSystem.cpp
File metadata and controls
278 lines (230 loc) · 8.09 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "RiverSystem.h"
#include <iostream>
#include <algorithm>
void RiverSystem::listCities() const {
if (cities.empty()) {
std::cout << "No cities available.\n";
return;
}
std::cout << "Cities:\n";
for (const auto& city : cities) {
std::cout << city.first << '\n';
}
}
void RiverSystem::listProducts() const {
if (products.empty()) {
std::cout << "No products available.\n";
return;
}
std::cout << "Products:\n";
for (const auto& product : products) {
std::cout << "ID: " << product.getId() << ", Weight: " << product.getWeight()
<< ", Volume: " << product.getVolume() << '\n';
}
}
void RiverSystem::addCity(const std::string& cityName) {
cities.emplace(cityName, City(cityName));
}
void RiverSystem::removeCity(const std::string& cityName) {
cities.erase(cityName);
}
void RiverSystem::readRiver() {
int numCities;
std::cout << "Enter the number of cities: ";
std::cin >> numCities;
std::string cityName;
for (int i = 0; i < numCities; ++i) {
std::cout << "Enter city name: ";
std::cin >> cityName;
addCity(cityName);
}
}
void RiverSystem::readInventory(const std::string& cityName) {
auto it = cities.find(cityName);
if (it == cities.end()) {
std::cerr << "City not found." << std::endl;
return;
}
Inventory& inv = it->second.getInventory();
int numItems;
std::cout << "Enter the number of items in inventory: ";
std::cin >> numItems;
for (int i = 0; i < numItems; ++i) {
int productId, units, needed;
std::cout << "Enter product ID, units, needed: ";
std::cin >> productId >> units >> needed;
if (needed <= 0) {
std::cerr << "Invalid units needed for product." << std::endl;
return;
}
inv.addProduct(productId, units, needed);
}
}
void RiverSystem::readInventories() {
std::string cityName;
while (std::cin >> cityName) {
readInventory(cityName);
}
}
void RiverSystem::modifyBoat(int buyId, int buyUnits, int sellId, int sellUnits) {
if (buyId == sellId) {
std::cerr << "Cannot buy and sell the same product." << std::endl;
return;
}
boat.modify(buyId, buyUnits, sellId, sellUnits);
}
void RiverSystem::writeBoatData() const {
boat.printBoatData();
}
void RiverSystem::queryNumProducts() const {
std::cout << "Number of products: " << products.size() << std::endl;
}
void RiverSystem::addProducts(int count, float weight, float volume) {
for (int i = 0; i < count; ++i) {
int id = products.size() + 1;
products.emplace_back(id, weight, volume);
}
}
void RiverSystem::writeProduct(int productId) const {
auto it = std::find_if(products.begin(), products.end(),
[productId](const Product& product) { return product.getId() == productId; });
if (it != products.end()) {
std::cout << "Product ID: " << it->getId() << ", Weight: " << it->getWeight()
<< ", Volume: " << it->getVolume() << std::endl;
} else {
std::cerr << "Product not found." << std::endl;
}
}
void RiverSystem::writeCity(const std::string& cityName) const {
auto it = cities.find(cityName);
if (it != cities.end()) {
const Inventory& inv = it->second.getInventory(); // Use const Inventory
// Implement your inventory details printing
} else {
std::cerr << "City not found." << std::endl;
}
}
void RiverSystem::putProd(const std::string& cityName, int productId, int units, int needed) {
auto it = cities.find(cityName);
if (it == cities.end()) {
std::cerr << "City not found." << std::endl;
return;
}
if (std::find_if(products.begin(), products.end(),
[productId](const Product& product) { return product.getId() == productId; }) == products.end()) {
std::cerr << "Product not found." << std::endl;
return;
}
Inventory& inv = it->second.getInventory();
if (inv.getProduct(productId)) {
std::cerr << "Product already exists in the inventory." << std::endl;
return;
}
if (needed <= 0) {
std::cerr << "Invalid units needed." << std::endl;
return;
}
inv.addProduct(productId, units, needed);
}
void RiverSystem::modifyProd(const std::string& cityName, int productId, int units, int needed) {
auto it = cities.find(cityName);
if (it == cities.end()) {
std::cerr << "City not found." << std::endl;
return;
}
Inventory& inv = it->second.getInventory();
if (!inv.getProduct(productId)) {
std::cerr << "Product not found in the inventory." << std::endl;
return;
}
if (needed <= 0) {
std::cerr << "Invalid units needed." << std::endl;
return;
}
inv.modifyProduct(productId, units, needed);
}
void RiverSystem::removeProd(const std::string& cityName, int productId) {
auto it = cities.find(cityName);
if (it == cities.end()) {
std::cerr << "City not found." << std::endl;
return;
}
Inventory& inv = it->second.getInventory();
if (!inv.getProduct(productId)) {
std::cerr << "Product not found in the inventory." << std::endl;
return;
}
inv.removeProduct(productId);
}
void RiverSystem::queryProd(const std::string& cityName, int productId) const {
auto it = cities.find(cityName);
if (it == cities.end()) {
std::cerr << "City not found." << std::endl;
return;
}
const Inventory& inv = it->second.getInventory();
const ProductData* product = inv.getProduct(productId);
if (!product) {
std::cerr << "Product not found in the inventory." << std::endl;
return;
}
std::cout << "Units: " << product->units << ", Needed: " << product->needed << std::endl;
}
void RiverSystem::trade(const std::string& city1, const std::string& city2) {
auto it1 = cities.find(city1);
auto it2 = cities.find(city2);
if (it1 == cities.end() || it2 == cities.end()) {
std::cerr << "One or both cities not found." << std::endl;
return;
}
Inventory& inv1 = it1->second.getInventory();
Inventory& inv2 = it2->second.getInventory();
for (auto it = inv1.inventory.begin(); it != inv1.inventory.end(); ++it) {
int productId = it->first;
ProductData& data1 = it->second;
auto data2 = inv2.getProduct(productId);
if (data2) {
int surplus1 = data1.units - data1.needed;
int surplus2 = data2->units - data2->needed;
if (surplus1 > 0 && surplus2 < 0) {
int tradeAmount = std::min(surplus1, -surplus2);
inv1.modifyProduct(productId, data1.units - tradeAmount, data1.needed);
inv2.modifyProduct(productId, data2->units + tradeAmount, data2->needed);
}
}
}
}
void RiverSystem::redistribute() {
std::string currentCity = "mouth";
while (!currentCity.empty()) {
auto it = cities.find(currentCity);
if (it != cities.end()) {
City& city = it->second;
Inventory& inv = city.getInventory();
// Example logic to redistribute products
// Adjust the product redistribution logic here
// Update currentCity to the next upstream city
// Customize the logic to find the upstream cities correctly
}
}
}
void RiverSystem::makeTrip() {
std::string currentCity = "mouth";
std::vector<std::string> visitedCities;
while (!currentCity.empty()) {
auto it = cities.find(currentCity);
if (it != cities.end()) {
City& city = it->second;
Inventory& inv = city.getInventory();
// Example trading logic for the boat trip
// Customize this logic to suit your needs
visitedCities.push_back(currentCity);
// Update currentCity to the next city in the route
}
}
std::cout << "Boat visited: ";
for (const std::string& city : visitedCities) {
std::cout << city << " ";
}
std::cout << std::endl;
}