Reputation: 31
So, in the code below, In the last method, I want to re use the second one public static Valute GetValuteByDate(DateTime date, string valuteCharCode)
, but I really don't understand what parameters to give. As you can see, I successfully re-used first method in the second method. Any idea what I can do to re-use the second method in the third one? Or maybe you have some useful information?
public static class Api
{
public static ValCurs GetValCursByDate(DateTime date)
{
var client = new RestClient("http://bnm.md"); //request
var request = new RestRequest("ro/official_exchange_rates/get_xml=1&date="+date.ToString(), Method.GET); //request
var response = client.Execute<ValCurs>(request);//deserialization
if (response.ErrorException != null) { return null; } //throw exception
return response.Data;
}
public static Valute GetValuteByDate(DateTime date, string valuteCharCode)
{
var curs = GetValCursByDate(date);
Valute valuteByDate = curs.FirstOrDefault(valute => valute.CharCode.Equals(valuteCharCode));
return valuteByDate;
}
public static Valute GetMaxValuteByPeriod(DateTime startDate, DateTime endDate, string charCode)
{
var maxVal = GetValuteByDate(**?**);
}
}
Upvotes: 0
Views: 910
Reputation:
The third one seems to be using a range, so you'd need to call the second one for each day in the range.
for(var day = startDate; date<= endDate; day = day.AddDays(1))
{
Valute value = GetValuteByDate(date, valuteCharCode);
//compare value to the max value and set if higher
}
Note: I didn't test this code so you might have to fiddle with it
Upvotes: 1
Reputation: 1916
public static class Api
{
public static ValCurs GetValCursByDate(DateTime date)
{
var client = new RestClient("http://bnm.md"); //request
var request = new RestRequest("ro/official_exchange_rates/get_xml=1&date="+date.ToString(), Method.GET); //request
var response = client.Execute<ValCurs>(request);//deserialization
if (response.ErrorException != null) { return null; } //throw exception
return response.Data;
}
public static Valute GetValuteByDate(DateTime date, string valuteCharCode)
{
var curs = GetValCursByDate(date);
Valute valuteByDate = curs.FirstOrDefault(valute => valute.CharCode.Equals(valuteCharCode));
return valuteByDate;
}
public static Valute GetMaxValuteByPeriod(DateTime startDate, DateTime endDate, string charCode)
{
var totalDays = (endDate-startDate).TotalDays;
List<Valute> result = new List<Valute>(totalDays);
for(int i = 0; i < totalDays; i++)
{
result.Add(GetValuteByDate(startDate.AddDays(i), charCode);
}
var maxVal = result.Max(p => p.<put here property>);
return maxVal;
}
}
Upvotes: 2