added validation for tickers

This commit is contained in:
mgroves 2011-02-26 11:27:52 -05:00
parent 0eeae1cfba
commit 9f49dbc187
4 changed files with 39 additions and 2 deletions

View file

@ -6,5 +6,6 @@ namespace MonoStockPortfolio.Core.StockData
public interface IStockDataProvider
{
IEnumerable<StockQuote> GetStockQuotes(IEnumerable<string> enumerable);
bool IsValidTicker(string ticker);
}
}

View file

@ -47,6 +47,14 @@ namespace MonoStockPortfolio.Core.StockData
return yahooQuoteData.Select(MapYahooData);
}
// Yahoo API will return a stock with price of 0.00
// if it can't find the ticker
public bool IsValidTicker(string ticker)
{
var quote = GetStockQuotes(new[] {ticker}).Single();
return quote.LastTradePrice > 0.0M;
}
private static StockQuote MapYahooData(YahooFinanceStockData data)
{
if (data == null)
@ -78,10 +86,12 @@ namespace MonoStockPortfolio.Core.StockData
{
var d = new YahooFinanceStockData();
d.Ticker = csvReader[0];
d.LastTradePrice = decimal.Parse(csvReader[1]);
decimal.TryParse(csvReader[1], out d.LastTradePrice);
//d.LastTradePrice = decimal.Parse(csvReader[1]);
d.Name = csvReader[2];
d.Volume = csvReader[3];
d.Change = decimal.Parse(csvReader[4]);
decimal.TryParse(csvReader[4], out d.Change);
//d.Change = decimal.Parse(csvReader[4]);
d.LastTradeTime = csvReader[5];
d.RealTimeLastTradeWithTime = csvReader[6];
d.ChangeRealTime = csvReader[7];

View file

@ -35,6 +35,20 @@ namespace MonoStockPortfolio.Tests
Assert.True(items.Any());
}
[Fact]
public void Can_tell_if_a_ticker_is_valid()
{
var svc = new YahooStockDataProvider();
Assert.True(svc.IsValidTicker("GOOG"));
}
[Fact]
public void Can_tell_if_a_ticker_is_invalid()
{
var svc = new YahooStockDataProvider();
Assert.False(svc.IsValidTicker("HARBLHARBLHARBL"));
}
private IPortfolioRepository BuildStubPortfolioRepo()
{
return new StubPortfolioRepo();

View file

@ -4,6 +4,7 @@ using Android.Content;
using Android.OS;
using Android.Widget;
using MonoStockPortfolio.Core.PortfolioRepositories;
using MonoStockPortfolio.Core.StockData;
using MonoStockPortfolio.Entities;
using MonoStockPortfolio.Framework;
@ -13,6 +14,7 @@ namespace MonoStockPortfolio.Activites
public class EditPositionActivity : Activity
{
[IoC] private IPortfolioRepository _repo;
[IoC] private IStockDataProvider _svc;
[LazyView(Resource.Id.addPositionTicker)] protected EditText TickerTextBox;
[LazyView(Resource.Id.addPositionPrice)] protected EditText PriceTextBox;
@ -101,10 +103,20 @@ namespace MonoStockPortfolio.Activites
validator.AddRequired(TickerTextBox, "Please enter a ticker");
validator.AddValidPositiveDecimal(SharesTextBox, "Please enter a valid, positive number of shares");
validator.AddValidPositiveDecimal(PriceTextBox, "Please enter a valid, positive price per share");
validator.AddValidation(TickerTextBox, () => ValidateTicker(TickerTextBox.Text));
return validator;
}
}
private string ValidateTicker(string ticker)
{
if(_svc.IsValidTicker(ticker))
{
return string.Empty;
}
return "Invalid Ticker Name";
}
private Position GetPositionToSave()
{
Position positionToSave;