// 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) using System; using DAL; namespace BLL { public class TransferData { private string fromaccount; private string toaccount; private decimal amount; public TransferData() : this("", "", 0.00m) { } public TransferData(string fromaccount, string toaccount, decimal amount) { this.fromaccount = fromaccount; this.toaccount = toaccount; this.amount = amount; } public string Fromaccount { get { return fromaccount; } set { fromaccount = value; } } public string Toaccount { get { return toaccount; } set { toaccount = value; } } public decimal Amount { get { return amount; } set { amount = value; } } } public interface TransferManager { bool ExecuteTransfer(TransferData xfer); } public class TransferManagerFactory { public static TransferManager Create(string tmtyp) { if(tmtyp == "standard") { return new StandardTransferManager(); } if(tmtyp == "strict") { return new StrictTransferManager(); } else { return null; } } } internal class StandardTransferManager : TransferManager { private AccountManager am; public StandardTransferManager() { am = AccountManagerFactory.Create("XML"); } public bool ExecuteTransfer(TransferData xfer) { AccountData fromaccount = am.Load(xfer.Fromaccount); AccountData toaccount = am.Load(xfer.Toaccount); if(fromaccount != null && toaccount != null && fromaccount.Amount >= xfer.Amount) { fromaccount.Amount -= xfer.Amount; toaccount.Amount += xfer.Amount; am.Save(fromaccount); am.Save(toaccount); return true; } else { return false; } } } internal class StrictTransferManager : TransferManager { private const int TRANSFER_LIMIT = 1000; private const int ACCOUNT_LIMIT = 10000; private AccountManager am; public StrictTransferManager() { am = AccountManagerFactory.Create("DB"); } public bool ExecuteTransfer(TransferData xfer) { AccountData fromaccount = am.Load(xfer.Fromaccount); AccountData toaccount = am.Load(xfer.Toaccount); if(fromaccount != null && toaccount != null && fromaccount.Amount >= xfer.Amount && xfer.Amount <= TRANSFER_LIMIT && (toaccount.Amount + xfer.Amount) <= ACCOUNT_LIMIT) { fromaccount.Amount -= xfer.Amount; toaccount.Amount += xfer.Amount; am.Save(fromaccount); am.Save(toaccount); return true; } else { return false; } } } }