forked from der-freddy/software-engineering-ws15
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
253 lines (226 loc) · 6.03 KB
/
main.cpp
File metadata and controls
253 lines (226 loc) · 6.03 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
#include <memory>
#include <iostream>
#include <stack>
#include <sstream>
#include <deque>
#include <vector>
#include <exception>
#include "converterfactory.hpp"
#include "inversion.hpp"
#include "command.hpp"
std::shared_ptr<ConverterFactory> ConverterFactory::s_instance = 0;
void help()
{
std::cout << std::endl;
std::cout << "Software Engineering Exercise Class WS15/16" << std::endl;
std::cout << "===========================================" << std::endl;
std::cout << "Exercise 5: Unit Converters (topic: Design Patterns (Part 2))"
<< std::endl;
std::cout << "by theelstner, AIpeter and elmeyer" << std::endl << std::endl;
std::cout << "Usage: ./main" << std::endl;
std::cout << "Possible conversions are the following:" << std::endl;
std::cout << " dollarToEuro\n dollarToPeso\n";
std::cout << " euroToPound\n mileToKilometer\n";
std::cout << " meterToYard\n celsiusToFahrenheit\n";
std::cout << " celsiusToKelvin\n";
std::cout << "Conversions can be capsuled and inverted (by prefixing the ";
std::cout << "desired converter with invert)." << std::endl;
std::cout << std::endl;
}
int main(int argc, char* argv[])
{
if(argc >= 2)
{
std::string input1 = argv[1];
if(input1 == "-h")
{
help();
return 0;
}
else
{
std::cout << "Invalid argument(s)!" << std::endl;
std::cout << "For help on how to use this program, use ./main -h."
<< std::endl;
return 1;
}
}
std::deque<std::shared_ptr<Command>> commands;
std::cout << "Please enter the desired conversions line by line, separated ";
std::cout << "by pressing Enter.\n";
for (std::string line; std::getline(std::cin, line);)
{
std::deque<std::string> converters;
std::shared_ptr<ConverterFactory> factory = ConverterFactory::instance();
std::shared_ptr<UnitConverter> converter;
std::istringstream stream{line};
std::string word;
int c = 0;
while(stream.good())
{
stream >> word;
converters.push_back(word);
++c;
}
if (c < 2)
{
std::cout << "\033[5;31mERROR:\033[0m Insufficient number of arguments!\n";
continue;
}
std::string value = converters.back();
converters.pop_back();
if(converters.front() == "invert")
{
converters.pop_front();
try
{
converter = std::make_shared<Inversion>(factory->create(converters.front()));
}
catch (std::exception const& e)
{
std::cout << e.what() << std::endl;
converter = NULL;
}
}
else
{
try
{
converter = factory->create(converters.front());
}
catch (std::exception const& e)
{
std::cout << e.what() << std::endl;
converter = NULL;
}
}
converters.pop_front();
if(converters.size() > 0)
{
for(int j = 0; j < converters.size(); ++j)
{
std::shared_ptr<UnitConverter> temp;
if(converters.front() == "invert")
{
converters.pop_front();
try
{
temp = std::make_shared<Inversion>(factory->create(converters.front()));
}
catch (std::exception const& e)
{
std::cout << e.what() << std::endl;
continue;
}
}
else
{
try
{
temp = factory->create(converters.front());
}
catch (std::exception const& e)
{
std::cout << e.what() << std::endl;
continue;
}
}
try
{
temp->link(converter);
}
catch (std::exception const& e)
{}
converter = temp;
converters.pop_front();
}
}
try
{
std::shared_ptr<Command> command = std::make_shared<Command>(converter,
&UnitConverter::convert, std::stod(value));
commands.push_back(command);
}
catch (std::exception const& e)
{}
}
int size = commands.size();
for(int i = 0; i < size; ++i)
{
try
{
double result = commands.front()->execute();
std::cout<<"Converted "<<commands.front()->getValue()
<<" to "<<result<<"!"
<<std::endl;
}
catch (std::exception const& e)
{
std::cout << e.what() << std::endl;
}
commands.pop_front();
}
/*
if(argc >= 3)
{
std::stack<std::string> converters;
std::shared_ptr<ConverterFactory> factory = ConverterFactory::instance();
std::shared_ptr<UnitConverter> converter;
std::string value = argv[argc-1];
for(int i = argc-2; i > 0; --i)
{
converters.push(argv[i]);
}
if(converters.top() == "invert")
{
converters.pop();
converter = std::make_shared<Inversion>(factory->create(converters.top()));
}
else
{
converter = factory->create(converters.top());
}
converters.pop();
if(converters.size() > 0)
{
for(int j = 0; j < converters.size(); ++j)
{
std::shared_ptr<UnitConverter> temp;
if(converters.top() == "invert")
{
converters.pop();
temp = std::make_shared<Inversion>(factory->create(converters.top()));
}
else
{
temp = factory->create(converters.top());
}
temp->link(converter);
converter = temp;
converters.pop();
}
}
if (converter != NULL)
{
std::cout<<converter->toString()<<" converted "<<value
<<" to "<<converter->convert(std::stod(value))<<"!"
<<std::endl;
}
else
{
std::cout << "Invalid converter: " << argv[1] << "!" << std::endl;
std::cout << "For a list of valid converters, use ./main -h."
<< std::endl;
return 1;
}
}
*/
/*
auto myConverter = std::make_shared<dollarToEuroConverter>();
double aLotOfDollars = 10000;
double aLotOfEuros = myConverter->convert(aLotOfDollars);
std::cout << myConverter->toString() << " has converted "<< aLotOfDollars
<< " Dollar to " << aLotOfEuros <<" Euros!"<<std::endl;
*/
return 0;
}