Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Net
Imports System.Xml.Linq
Imports System.Web.SessionState
Imports System.ComponentModel
Imports System.Globalization
Imports System.IO
Partial Public Class MarketWidget
Private Shared SessionKey As String = "MarketWidget_Quotes"
Private Shared Symbols() As String = { "MSFT", "CSCO", "GOOG", "AAPL", "ORCL", "YHOO", "EBAY", "INTC" }
Private Shared random_Renamed As Random
Public Shared Function SelectQuotes() As BindingList(Of StockQuote)
Return New BindingList(Of StockQuote)(Quotes)
End Function
Private Shared ReadOnly Property Session() As HttpSessionState
Get
Return HttpContext.Current.Session
End Get
End Property
Private Shared ReadOnly Property Random() As Random
Get
If random_Renamed Is Nothing Then
random_Renamed = New Random()
End If
Return random_Renamed
End Get
End Property
Private Shared ReadOnly Property Quotes() As List(Of StockQuote)
Get
If Session(SessionKey) Is Nothing Then
Session(SessionKey) = LoadActualQuotes()
RandomizeQuotes()
End If
Return CType(Session(SessionKey), List(Of StockQuote))
End Get
End Property
Private Shared Function LoadActualQuotes() As List(Of StockQuote)
Dim quotes = New List(Of StockQuote)()
Dim soapClient = New Client()
For Each symbol In Symbols
Dim data As String = GetServiceResponse(soapClient, symbol)
Dim quote = New StockQuote(data)
quotes.Add(quote)
Next symbol
Return quotes
End Function
Private Shared Function GetServiceResponse(ByVal client As Client, ByVal symbol As String) As String
Dim respone As String = Nothing
'try {
' respone = client.GetQuote(symbol); // request to the real web service
'}
'catch { }
If (Not String.IsNullOrEmpty(respone)) Then
Return respone
End If
Return File.ReadAllText(HttpContext.Current.Server.MapPath(String.Format("~/App_Data/off_Market_{0}.xml", symbol)))
End Function
Private Shared Sub RandomizeQuotes()
For Each quote In Quotes
Dim change = CDec(Random.NextDouble()) * CDec(Random.Next(-1, 2))
If change = 0 Then
quote.PercentChange = CDec(0.05)
Else
quote.PercentChange = change
End If
quote.Last = quote.Previous + quote.Previous * quote.PercentChange / 100
For i As Integer = 0 To 6
Dim ptChange = CDec(Random.NextDouble()) * CDec(Random.Next(-1, 2)) / 20
Dim lastValue = quote.Points.Last().Value
quote.Points.Add(New StockQuotePoint(quote.Points.Count, lastValue + lastValue * ptChange))
Next i
Next quote
End Sub
End Class
Public Class StockQuote
Public Sub New(ByVal data As String)
Dim xdoc = XDocument.Parse(data)
Dim 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(Of StockQuotePoint)(New StockQuotePoint() { New StockQuotePoint(0, Last), New StockQuotePoint(1, Previous) })
End Sub
Private privateSymbol As String
Public Property Symbol() As String
Get
Return privateSymbol
End Get
Set(ByVal value As String)
privateSymbol = value
End Set
End Property
Private privateLast As Decimal
Public Property Last() As Decimal
Get
Return privateLast
End Get
Set(ByVal value As Decimal)
privateLast = value
End Set
End Property
Private privatePrevious As Decimal
Public Property Previous() As Decimal
Get
Return privatePrevious
End Get
Set(ByVal value As Decimal)
privatePrevious = value
End Set
End Property
Private privatePercentChange As Decimal
Public Property PercentChange() As Decimal
Get
Return privatePercentChange
End Get
Set(ByVal value As Decimal)
privatePercentChange = value
End Set
End Property
Private privateStockDate As DateTime
Public Property StockDate() As DateTime
Get
Return privateStockDate
End Get
Set(ByVal value As DateTime)
privateStockDate = value
End Set
End Property
Private privateName As String
Public Property Name() As String
Get
Return privateName
End Get
Set(ByVal value As String)
privateName = value
End Set
End Property
Public ReadOnly Property ChangeState() As String
Get
If PercentChange > 0 Then
Return "positive"
Else
If PercentChange < 0 Then
Return "negative"
Else
Return "const"
End If
End If
End Get
End Property
Private privatePoints As List(Of StockQuotePoint)
Public Property Points() As List(Of StockQuotePoint)
Get
Return privatePoints
End Get
Protected Set(ByVal value As List(Of StockQuotePoint))
privatePoints = value
End Set
End Property
Public ReadOnly Property PointsList() As BindingList(Of StockQuotePoint)
Get
Return New BindingList(Of StockQuotePoint)(Points)
End Get
End Property
Public Overrides Function ToString() As String
Return Symbol
End Function
End Class
Public Class StockQuotePoint
Public Sub New(ByVal number As Integer, ByVal value As Decimal)
Number = number
Value = value
End Sub
Private privateValue As Decimal
Public Property Value() As Decimal
Get
Return privateValue
End Get
Set(ByVal value As Decimal)
privateValue = value
End Set
End Property
Private privateNumber As Integer
Public Property Number() As Integer
Get
Return privateNumber
End Get
Set(ByVal value As Integer)
privateNumber = value
End Set
End Property
End Class