forked from ViconiaDeVir/ExchangeRateAnalyser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
192 lines (163 loc) · 6.43 KB
/
Program.cs
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
using ExchangeRateAnalyser.Models;
using ExchangeRateAnalyser.Utils;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExchangeRateAnalyser
{
class Program
{
const string startDateArgCode = "-startDate";
const string endDateArgCode = "-endDate";
const string capitalArgCode = "-capital";
static void Main(string[] args)
{
DateTime startDate = DateTime.MinValue;
DateTime endDate = DateTime.MinValue;
decimal capital = 0;
string startDateString = "";
string endDateString = "";
string capitalString = "";
//Read user defined parameters
for (int i = 0; i < args.Length; i++)
{
if (args[i] == startDateArgCode
&& args.Length > i)
{
startDateString = args[i + 1];
}
else if (args[i] == endDateArgCode
&& args.Length > i)
{
endDateString = args[i + 1];
}
else if (args[i] == capitalArgCode
&& args.Length > i)
{
capitalString = args[i + 1];
}
}
//check if start date parameter is valid and ask to re-enter it in case it is not
bool converted = false;
do
{
if (string.IsNullOrEmpty(startDateString))
{
Console.WriteLine(Strings.InfoEnterValidStartDate);
startDateString = Console.ReadLine();
}
if (DateTime.TryParse(startDateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out startDate))
{
if (startDate.Date > DateTime.Today.Date)
{
Console.WriteLine(Strings.ErrorStartDateGreaterThanToday);
startDate = DateTime.MinValue;
startDateString = "";
}
else
{
converted = true;
}
}
if (!converted)
{
endDate = DateTime.MinValue;
startDate = DateTime.MinValue;
startDateString = "";
}
} while (startDate == DateTime.MinValue);
//check if end date parameter is valid and ask to re-enter it in case it is not
converted = false;
do
{
if (string.IsNullOrEmpty(endDateString))
{
Console.WriteLine(Strings.InfoEnterValidEndDate);
endDateString = Console.ReadLine();
}
if (DateTime.TryParse(endDateString, CultureInfo.InvariantCulture, DateTimeStyles.None, out endDate))
{
if (endDate.Date > DateTime.Today.Date)
{
Console.WriteLine(Strings.ErrorEndDateGreaterThanToday);
}
else if (startDate.Date > endDate.Date)
{
Console.WriteLine(Strings.ErrorStatDateGreaterThanEndDate);
}
else
{
converted = true;
}
}
if (!converted)
{
endDate = DateTime.MinValue;
endDateString = "";
}
} while (endDate == DateTime.MinValue);
//check if capital parameter is valid and ask to re-enter it in case it is not
do
{
if (string.IsNullOrEmpty(capitalString))
{
Console.WriteLine(Strings.InfoEnterValidInitialCapital);
capitalString = Console.ReadLine();
}
if (!decimal.TryParse(capitalString, NumberStyles.Number, CultureInfo.InvariantCulture, out capital)
|| capital <= 0)
{
capitalString = "";
}
} while (string.IsNullOrEmpty(capitalString));
Analyze(startDate, endDate, capital);
Console.ReadKey();
}
/// <summary>
/// Runs ExchangeRateAnalyzer
/// </summary>
/// <param name="startDate">Start date for analysis</param>
/// <param name="endDate">End date for analysis</param>
/// <param name="сapital">Initial amount of money</param>
static void Analyze(DateTime startDate, DateTime endDate, decimal сapital)
{
try
{
Console.WriteLine();
Console.WriteLine(Strings.InfoStartedWithParameters, startDate.ToString("yyyy-MM-dd"), endDate.ToString("yyyy-MM-dd"), сapital.ToString("F2"));
Console.WriteLine();
//Initialise ExchangeRateAnalyzer with user-defined parameters
var analyzer = new ExchangeRateAnalyser.Models.ExchangeRateAnalyser(startDate, endDate, сapital);
var analysisResult = analyzer.PerformAnalysis();
if (string.IsNullOrEmpty(analysisResult.Error)) //if there was no errors
{
Console.WriteLine(Strings.InfoAnalysisCompleted);
Console.WriteLine();
if (analysisResult.Profit < 0) //in case there was no possible profitable deals show warning message to the user
{
Console.WriteLine(Strings.WarningNoProfit);
}
else
{
Console.WriteLine(Strings.InfoSeeResultSet);
Console.WriteLine();
}
Console.WriteLine(analysisResult.ToString());
Console.WriteLine();
}
else //if there was any error
{
Console.WriteLine(analysisResult.Error);
}
}
catch (Exception ex)
{
Logger.Log(Strings.ErrorOccured, ex);
Console.WriteLine(Strings.ErrorOccuredTryLater);
}
}
}
}