' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System

Imports DAL

Namespace BLL
    Public Class TransferData
        Private _fromaccount As String
        Private _toaccount As String
        Private _amount As Decimal
        Public Sub New()
            MyClass.New("", "", 0D)
        End Sub
        Public Sub New(ByVal fromaccount As String, ByVal toaccount As String, ByVal amount As Decimal)
            _fromaccount = fromaccount
            _toaccount = toaccount
            _amount = amount
        End Sub
        Public Property Fromaccount() As String
            Get
                Return _fromaccount
            End Get
            Set
                _fromaccount = value
            End Set
        End Property
        Public Property Toaccount() As String
            Get
                Return _toaccount
            End Get
            Set
                _toaccount = value
            End Set
        End Property
        Public Property Amount() As Decimal
            Get
                Return _amount
            End Get
            Set
                _amount = value
            End Set
        End Property
    End Class 

    Public Interface TransferManager
        Function ExecuteTransfer(ByVal xfer As TransferData) As Boolean
    End Interface 

    Public Class TransferManagerFactory
        Public Shared Function Create(ByVal tmtyp As String) As TransferManager
            If tmtyp = "standard" Then
                Return New StandardTransferManager
            End If
            If tmtyp = "strict" Then
                Return New StrictTransferManager
            Else
                Return Nothing
            End If
        End Function
    End Class 

    Friend Class StandardTransferManager
    Implements TransferManager
        Private am As AccountManager
        Public Sub New()
            am = AccountManagerFactory.Create("XML")
        End Sub
        Public Function ExecuteTransfer(ByVal xfer As TransferData) As Boolean Implements TransferManager.ExecuteTransfer
            Dim fromaccount As AccountData = am.Load(xfer.Fromaccount)
            Dim toaccount As AccountData = am.Load(xfer.Toaccount)
            If Not (fromaccount Is Nothing) AndAlso Not (toaccount Is Nothing) AndAlso fromaccount.Amount >= xfer.Amount Then
                fromaccount.Amount -= xfer.Amount
                toaccount.Amount += xfer.Amount
                am.Save(fromaccount)
                am.Save(toaccount)
                Return True
            Else
                Return False
            End If
        End Function
    End Class 

    Friend Class StrictTransferManager
    Implements TransferManager
        Private Const TRANSFER_LIMIT As Integer = 1000
        Private Const ACCOUNT_LIMIT As Integer = 10000
        Private am As AccountManager
        Public Sub New()
            am = AccountManagerFactory.Create("DB")
        End Sub
        Public Function ExecuteTransfer(ByVal xfer As TransferData) As Boolean Implements TransferManager.ExecuteTransfer
            Dim fromaccount As AccountData = am.Load(xfer.Fromaccount)
            Dim toaccount As AccountData = am.Load(xfer.Toaccount)
            If Not (fromaccount Is Nothing) AndAlso Not (toaccount Is Nothing) AndAlso fromaccount.Amount >= xfer.Amount AndAlso xfer.Amount <= TRANSFER_LIMIT AndAlso (toaccount.Amount + xfer.Amount) <= ACCOUNT_LIMIT Then
                fromaccount.Amount -= xfer.Amount
                toaccount.Amount += xfer.Amount
                am.Save(fromaccount)
                am.Save(toaccount)
                Return True
            Else
                Return False
            End If
        End Function
    End Class 
End Namespace
