On my previous post, I started refactoring a function that reads stock and dates, checks the data against Yahoo Finance and returns some data.
In this function, I take the Stock Ticker and the data and parse the URL to be evaluated by our web scrapper object.
private void btnStockSales_Click(object sender, RoutedEventArgs e)
{
TickerDataCollection = new ObservableCollection();
// no longer care where the data is coming, we just get the data
var stockList = ReadStockData();
List results = new List();
foreach (var item in stockList)
{
int month = DateTime.Parse(item.Date.ToString()).Month;
month -= 1;
string url = "http://finance.yahoo.com/q/hp?s"+ item.Ticker+ "a=" +
month.ToString() +"b" +
DateTime.Parse(item.Date.ToString()).Day.ToString() + "c"+
DateTime.Parse(item.Date.ToString()).Year.ToString() + "d=" +
month.ToString() + "e3b=" +
DateTime.Parse(item.Date.ToString()).Day.ToString() + "f= +
DateTime.Parse(item.Date.ToString()).Year.ToString() "g =d"
TickerData x = new TickerData();
x.Ticker = item.Ticker;
Scraper y = new Scraper();
x = y.Scraping(url);
if (x == null)
{
x = new TickerData();
x.Found = false;
}
else
{
x.Found = true;
}
x.Ticker = item.Ticker;
x.TransactionDate = item.Date.Value;
TickerDataCollection.Add(new TickerData() { Ticker = x.Ticker, High = x.High, Low = x.Low, Found = x.Found, TransactionDate = x.TransactionDate });
}
LstStocks.DataContext = TickerDataCollection;
}
Now, this code can be taken out of this function and moved to the StockData Object
]
public class StockData
{
public string Ticker { get; set; }
public DateTime? Date { get; set; }
public string Url
{
get {
int month = DateTime.Parse(Date.ToString()).Month;
month -= 1; //yahoo starts month numbers with base 0
string url = string.Concat("http://finance.yahoo.com/q/hp?s", Ticker,"a=", month.ToString(), "b=",
DateTime.Parse(Date.ToString()).Day.ToString(), "c=",
DateTime.Parse(Date.ToString()).Year.ToString(), "d=",
month.ToString(), "eb=",
DateTime.Parse(Date.ToString()).Day.ToString(), "f=",
DateTime.Parse(Date.ToString()).Year.ToString(), "g = d");
return url;
}
}
This function itself could be refactored a little more, but that would later on, back to our original function
private void btnStockSales_Click(object sender, RoutedEventArgs e)
{
TickerDataCollection = new ObservableCollection();
// no longer care where the data is coming, we just get the data
var stockList = ReadStockData();
List results = new List();
foreach (var item in stockList)
{
TickerData x = new TickerData();
x.Ticker = item.Ticker;
Scraper y = new Scraper();
x = y.Scraping(item.Url);
if (x == null)
{
x = new TickerData();
x.Found = false;
}
else
{
x.Found = true;
}
x.Ticker = item.Ticker;
x.TransactionDate = item.Date.Value;
TickerDataCollection.Add(new TickerData() { Ticker = x.Ticker, High = x.High, Low = x.Low, Found = x.Found, TransactionDate = x.TransactionDate });
}
LstStocks.DataContext = TickerDataCollection;
}
You can now start to see a cleaner code that does not deal with details, if the URL was to change, you can extend the StockData class.
2 thoughts on “Refactoring II- Encapsulating”