Mini Kabibi Habibi
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Xml.Linq;
using System.Web.SessionState;
using System.ComponentModel;
using System.Globalization;
using System.IO;
public partial class MarketWidget {
static string SessionKey = "MarketWidget_Quotes";
static string[] Symbols = new string[] { "MSFT", "CSCO", "GOOG", "AAPL", "ORCL", "YHOO", "EBAY", "INTC" };
static Random random;
public static BindingList<StockQuote> SelectQuotes() {
return new BindingList<StockQuote>(Quotes);
}
static HttpSessionState Session { get { return HttpContext.Current.Session; } }
static Random Random {
get {
if(random == null)
random = new Random();
return random;
}
}
static List<StockQuote> Quotes {
get {
if(Session[SessionKey] == null) {
Session[SessionKey] = LoadActualQuotes();
RandomizeQuotes();
}
return (List<StockQuote>)Session[SessionKey];
}
}
static List<StockQuote> LoadActualQuotes() {
var quotes = new List<StockQuote>();
var soapClient = new Client();
foreach(var symbol in Symbols) {
string data = GetServiceResponse(soapClient, symbol);
var quote = new StockQuote(data);
quotes.Add(quote);
}
return quotes;
}
static string GetServiceResponse(Client client, string symbol) {
string respone = null;
//try {
// respone = client.GetQuote(symbol); // request to the real web service
//}
//catch { }
if(!string.IsNullOrEmpty(respone))
return respone;
return File.ReadAllText(HttpContext.Current.Server.MapPath(string.Format("~/App_Data/off_Market_{0}.xml", symbol)));
}
static void RandomizeQuotes() {
foreach(var quote in Quotes) {
var change = (decimal)Random.NextDouble() * (decimal)Random.Next(-1, 2);
quote.PercentChange = change == 0 ? (decimal)0.05 : change;
quote.Last = quote.Previous + quote.Previous * quote.PercentChange / 100;
for(int i = 0; i < 7; i++) {
var ptChange = (decimal)Random.NextDouble() * (decimal)Random.Next(-1, 2) / 20;
var lastValue = quote.Points.Last().Value;
quote.Points.Add(new StockQuotePoint(quote.Points.Count, lastValue + lastValue * ptChange));
}
}
}
}
public class StockQuote {
public StockQuote(string data) {
var xdoc = XDocument.Parse(data);
var stockElement = xdoc.Root.Element("Stock");
Symbol = stockElement.Element("Symbol").Value;
Previous = decimal.Parse(stockElement.Element("Last").Value, new CultureInfo("en-US"));
Last = Previous;
StockDate = DateTime.Now;
Name = stockElement.Element("Name").Value;
Points = new List<StockQuotePoint>(new StockQuotePoint[] { new StockQuotePoint(0, Last), new StockQuotePoint(1, Previous) });
}
public string Symbol { get; set; }
public decimal Last { get; set; }
public decimal Previous { get; set; }
public decimal PercentChange { get; set; }
public DateTime StockDate { get; set; }
public string Name { get; set; }
public string ChangeState {
get {
return PercentChange > 0 ? "positive" :
PercentChange < 0 ? "negative" :
"const";
}
}
public List<StockQuotePoint> Points { get; protected set; }
public BindingList<StockQuotePoint> PointsList { get { return new BindingList<StockQuotePoint>(Points); } }
public override string ToString() {
return Symbol;
}
}
public class StockQuotePoint {
public StockQuotePoint(int number, decimal value) {
Number = number;
Value = value;
}
public decimal Value { get; set; }
public int Number { get; set; }
}