-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.cpp
More file actions
170 lines (132 loc) · 3.84 KB
/
main.cpp
File metadata and controls
170 lines (132 loc) · 3.84 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
#include <memory>
#include <iostream>
#include <string>
#include <deque>
//converter factory
#include "ConverterFactory.hpp"
//basic class
#include "UnitConverter.hpp"
//derived classes
#include "DollarToEuroConverter.hpp"
#include "CelsiusToFahrenheitConverter.hpp"
#include "FahrenheitToCelsiusConverter.hpp"
#include "YenToEuroConverter.hpp"
#include "GoldToEuroConverter.hpp"
#include "EuroToBritishPoundConverter.hpp"
//inversion decorator
#include "Inversion.hpp"
//Command pattern for Part 2
#include "Command.hpp"
//testing
#include "tinytest.h"
//exception
#include "InvalidInput.hpp"
/*
Remaining of exercise 4
*/
//secure it is a singleton
ConverterFactory* ConverterFactory::s_instance = nullptr;
int main(int argc, char* argv[])
{
/*
Adding objects to registry for Part 2
Mask:
factory->add_object_to_registry("",std::make_shared<>(Converter));
*/
//initializing the factory
auto factory = ConverterFactory::instance();
//CelsiusToFahrenheitConverter
factory->add_object_to_registry("CelsiusToFahrenheit", std::make_shared<CelsiusToFahrenheitConverter>());
//FahrenheitToCelsius
factory->add_object_to_registry("FahrenheitToCelsius", std::make_shared<FahrenheitToCelsiusConverter>());
//DollarToEuro
factory->add_object_to_registry("DollarToEuro", std::make_shared<DollarToEuroConverter>());
//EuroToBritishPound
factory->add_object_to_registry("EuroToBritishPound", std::make_shared<EuroToBritishPoundConverter>());
//GoldToEuro
factory->add_object_to_registry("GoldToEuro", std::make_shared<GoldToEuroConverter>());
//YenToEuro
factory->add_object_to_registry("YenToEuro", std::make_shared<YenToEuroConverter>());
try
{
/*
Remaining of exercise 5
used for exception testing
*/
std::cout << std::endl << std::endl
<< "Part 2 Behavioural Patterns"
<< std::endl << std::endl
<< "Choose from these converters and type a value.\nPress ENTER after each Converter+Value.\nPress strg+d when finished."
<< std::endl << std::endl
<< factory->print()
<< std::endl << std::endl
<< "<Command> <Value>"
<< std::endl;
//initializing the used deque
std::deque<Command> command_list;
std::string input;
//loop provided by the worksheet
//adapted with some conversion
for(std::string line; std::getline(std::cin, input, ' ');)
{
try
{
std::string inputString;
std::getline(std::cin,inputString);
//if(inputString) //test of valid input
//{
// throw InvalidInput();
//}
//wanted converter
auto convert = factory->create(input);
double(UnitConverter::*convertMethod)(double) = NULL;
//reference to the method
convertMethod = &UnitConverter::convert;
if(convert != nullptr) //added due to exception handling
{
//add the reference to the deque
command_list.push_back(Command{convert, convertMethod, std::stod(inputString)});
}
}
catch(std::exception e)
{
std::cout << "TEST: " << e.what() << std::endl;
}
}
//loop though the deque
for(auto&& command : command_list)
{
command.execute();
}
}
catch(int exception)
{
/*
generate the usage string
*/
std::string case_string = "Exception: ";
std::string usage_string = "Usage: " + std::string(argv[0]) + " <Numeric Value> \n";
std::string break_string = "\n------------------------------------------------------\n";
std::string explanation_string = "This is a simple program. Just type in a numeric value and you will get some conversions!\n";
switch(exception)
{
case 1:
{
case_string += "You need help? Take this!\n";
break;
}
case 2:
{
case_string += "Wrong amount of arguments!\n";
break;
}
default:
{
case_string += "Unknown Exception. So sorry!\n";
break;
}
}
std::cout << break_string << case_string << usage_string << break_string << explanation_string << std::endl;
}
return 0;
}