diff Stocks/Stocks.Common/Factory.cs @ 0:e5d46bb6cdb0

Initial commit
author adminSH stevenhollidge@hotmail.com
date Mon, 20 Feb 2012 13:52:35 +0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Stocks/Stocks.Common/Factory.cs	Mon Feb 20 13:52:35 2012 +0700
@@ -0,0 +1,36 @@
+using NLog;
+using Stocks.Common.Core;
+using Stocks.Common.Models;
+using System;
+using Stocks.Common.Exceptions;
+
+namespace Stocks.Common
+{
+  public class Factory
+  {
+    public static Price CreatePrice(string webPriceData)
+    {
+      if (string.IsNullOrEmpty(webPriceData))
+        throw new InvalidWebPriceDataException(webPriceData);
+
+      try
+      {
+        var symbol = webPriceData.Substring(1, webPriceData.IndexOf('"', 2) - 1);
+
+        decimal price = decimal.Parse(webPriceData.Substring(
+            webPriceData.Length - webPriceData.Reverse().IndexOf(",")));
+
+        return new Price()
+        {
+          Symbol = symbol,
+          PreviousPrice = price,
+          CurrentPrice = price
+        };
+      }
+      catch (Exception e)
+      {
+        throw new InvalidWebPriceDataException(webPriceData, e);
+      }
+    }
+  }
+}