Exemple #1
0
        static void Main(string[] args)
        {
            BankCard card = new BankCard(DateTime.Now.AddSeconds(2), 100);

            DateTime queryTime  = DateTime.Now;
            decimal  available1 =
                card.GetAvailableAmount(20, queryTime); // returns 20

            decimal available2 =
                card.GetAvailableAmount(20, queryTime.AddSeconds(3)); // returns 0

            decimal available3 =
                card.GetAvailableAmount(20, queryTime); // returns 20

            // Previous implementation - does not compile
            //BankCard card = new BankCard()
            //{
            //    ValidBefore = DateTime.Now.AddSeconds(2),
            //    Balance = 100
            //};

            //decimal available1 = card.GetAvailableAmount(20); // returns 20

            //card.Balance = 15;
            //decimal available2 = card.GetAvailableAmount(20); // returns 15

            //Thread.Sleep(3000); // three seconds later...
            //decimal available3 = card.GetAvailableAmount(20); // returns 0
        }
Exemple #2
0
        static void Main(string[] args)
        {
            var card = new BankCard()
            {
                ValidBefore = DateTime.Now.AddSeconds(2),
                Balance     = 100
            };

            Console.WriteLine(card.GetAvailableAmount(20)); // returns 20

            card.Balance = 15;

            Console.WriteLine(card.GetAvailableAmount(20)); // returns 15

            Thread.Sleep(3000);                             // three seconds later...

            Console.WriteLine(card.GetAvailableAmount(20)); // returns 0

            var immutableCard = new ImmutableBankCard(DateTime.Now.AddSeconds(2), 100);

            var queryTime = DateTime.Now;

            Console.WriteLine(immutableCard.GetAvailableAmount(20, queryTime));               // returns 20

            Console.WriteLine(immutableCard.GetAvailableAmount(20, queryTime.AddSeconds(3))); // returns 0

            Console.WriteLine(immutableCard.GetAvailableAmount(20, queryTime));               // returns 20
        }
 public SpecificCard(Currency currency, BankCard card) : base(currency)
 {
     this.Card = card ?? throw new ArgumentNullException(nameof(card));
 }