Example #1
0
        private static void Factory_Method_Pattern()
        {
            // TODO when you are about to instantiate lets encapsulate that instantiation so that we can make uniform in all places ->
            // TODO -> you can use the factory when you want to instantiate and the factory is responsible to instantiate appropriately

            CardFactory factory = null;

            Console.Write("Enter the card type you would like to visit: ");
            string card = Console.ReadLine();

            switch (card.ToLower())
            {
            case "moneyback":
                factory = new MoneyBackFactory(50000, 0);
                break;

            case "titanium":
                factory = new TitaniumFactory(100000, 500);
                break;

            case "platinum":
                factory = new PlatinumFactory(500000, 1000);
                break;

            default:
                break;
            }

            CreditCard creditCard = factory.GetCreditCard();

            Console.WriteLine("\nYour card details are below : \n");
            Console.WriteLine("Card Type: {0}\nCredit Limit: {1}\nAnnual Charge: {2}",
                              creditCard.CardType, creditCard.CreditLimit, creditCard.AnnualCharge);
            Console.ReadKey();
        }
Example #2
0
        //Concrete meaning real or actual
        //Product and Creator
        //Then things as RealCreator and RealProduct
        //Factory method pattern falls under Creational Pattern of Gang of Four (GoF) Design Patterns.
        static void Main(string[] args)
        {
            CardFactory factory = null;

            Console.Write("Enter the card type you would like to visit: ");
            string car = Console.ReadLine();

            switch (car.ToLower())
            {
            case "moneyback":
                factory = new MoneyBackFactory(50000, 0);
                break;

            case "titanium":
                factory = new TitaniumFactory(100000, 500);
                break;

            case "platinum":
                factory = new PlatinumFactory(500000, 1000);
                break;

            default:
                break;
            }

            CreditCard creditCard = factory.GetCreditCard();

            Console.WriteLine("\nYour card details are below : \n");
            Console.WriteLine("Card Type: {0}\nCredit Limit: {1}\nAnnual Charge: {2}",
                              creditCard.CardType, creditCard.CreditLimit, creditCard.AnnualCharge);
            Console.ReadKey();
        }
Example #3
0
    static void Main(string[] args)
    {
        var creditCard = new PlatinumFactory().CreateProduct();

        if (creditCard != null)
        {
            Console.WriteLine("Card Type : " + creditCard.GetCardType());
            Console.WriteLine("Credit Limit : " + creditCard.GetCreditLimit());
            Console.WriteLine("Annual Charge :" + creditCard.GetAnnualCharge());
        }
        else
        {
            Console.Write("Invalid Card Type");
        }

        Console.WriteLine("--------------");

        creditCard = new MoneyBackFactory().CreateProduct();

        if (creditCard != null)
        {
            Console.WriteLine("Card Type : " + creditCard.GetCardType());
            Console.WriteLine("Credit Limit : " + creditCard.GetCreditLimit());
            Console.WriteLine("Annual Charge :" + creditCard.GetAnnualCharge());
        }
        else
        {
            Console.Write("Invalid Card Type");
        }

        Console.ReadLine();
    }
Example #4
0
        // GET: api/DesignPattern

        /*public IEnumerable<string> Get()
         * {
         *  return new string[] { "value1", "value2" };
         * }*/

        // GET: api/DesignPattern/5
        public string Get(string card)
        {
            CardFactory factory = null;

            switch (card.ToLower())
            {
            case "moneyback":
                factory = new MoneyBackFactory(50000, 0);
                break;

            case "titanium":
                factory = new TitaniumFactory(100000, 500);
                break;

            case "platinum":
                factory = new PlatinumFactory(500000, 1000);
                break;

            default:
                break;
            }
            var creditCard = factory.GetCreditCard();

            var cardDetails = $" Card Type : {creditCard.CardType}  Credit Limit : {creditCard.CreditLimit}  Annual Charges : {creditCard.AnnualCharge}";

            return(cardDetails);
        }
Example #5
0
        public void Factory_pattern_test()
        {
            CardFactory factory  = null;
            string      cardType = "moneyback";

            switch (cardType.ToLower())
            {
            case "moneyback":
                factory = new MoneyBackFactory(50000, 0);
                break;

            case "titanium":
                factory = new TitaniumFactory(100000, 500);
                break;

            case "platinum":
                factory = new PlatinumFactory(500000, 1000);
                break;

            default:
                break;
            }

            CreditCard card = factory.GetCreditCard();

            Assert.AreEqual("MoneyBack", card.CardType);
            Assert.AreEqual(50000, card.CreditLimit);
            Assert.AreEqual(0, card.AnnualCharge);
        }
        public void test_credit_card()
        {
            // MoneyBack card
            {
                ICreditCard creditCard = new MoneyBackFactory().CreateProduct();

                Assert.That(creditCard.GetCardType() == CreditCardType.MoneyBack);
                Assert.That(creditCard.GetAnnualCharge() == 500);
                Assert.That(creditCard.GetCreditLimit() == 15000);
            }

            // Platium card
            {
                ICreditCard creditCard = new PlatiumFactory().CreateProduct();

                Assert.That(creditCard.GetCardType() == CreditCardType.Platium);
                Assert.That(creditCard.GetAnnualCharge() == 2000);
                Assert.That(creditCard.GetCreditLimit() == 35000);
            }

            // Titanium card
            {
                ICreditCard creditCard = new TitaniumFactory().CreateProduct();

                Assert.That(creditCard.GetCardType() == CreditCardType.Titanium);
                Assert.That(creditCard.GetAnnualCharge() == 1500);
                Assert.That(creditCard.GetCreditLimit() == 25000);
            }
        }
Example #7
0
        public void CreditCardFactoryMethod()
        {
            ICreditCard creditCard = new PlatinumFactory().CreateProduct();

            if (creditCard != null)
            {
                Console.WriteLine("Card Type : " + creditCard.GetCardType());
                Console.WriteLine("Credit Limit : " + creditCard.GetCreditLimit());
                Console.WriteLine("Annual Charge :" + creditCard.GetAnnualCharge());
            }
            else
            {
                Console.Write("Invalid Card Type");
            }
            Console.WriteLine("--------------");
            creditCard = new MoneyBackFactory().CreateProduct();
            if (creditCard != null)
            {
                Console.WriteLine("Card Type : " + creditCard.GetCardType());
                Console.WriteLine("Credit Limit : " + creditCard.GetCreditLimit());
                Console.WriteLine("Annual Charge :" + creditCard.GetAnnualCharge());
            }
            else
            {
                Console.Write("Invalid Card Type");
            }
            Console.ReadLine();
        }
Example #8
0
        public void TestMethod1()
        {
            MoneyBackFactory _factory;

            _factory = new MoneyBackFactory(5000, 0);

            var value = _factory.GetCreditCard();

            Assert.AreEqual("MoneyBack", value.CardType);
            Assert.AreEqual(5000, value.CreditLimit);
            Assert.AreEqual(0, value.AnnualCharge);
        }
Example #9
0
        public void Test1()
        {
            var        fact      = new MoneyBackFactory(50000, 0);
            CreditCard card      = fact.GetCreditCard();
            string     expected1 = card.CardType;
            int        expected2 = card.CreditLimit;
            int        expected3 = card.AnnualCharge;
            string     actual1   = "MoneyBack";
            int        actual2   = 50000;
            int        actual3   = 0;

            //Assert
            Assert.Equal(expected1, actual1);
            Assert.Equal(expected2, actual2);
            Assert.Equal(expected3, actual3);
        }
        static void Main(string[] args)
        {
            ICardFactory factory  = null;
            string       cardType = "MoneyBack";

            switch (cardType)
            {
            case "MoneyBack":
                factory = new MoneyBackFactory(12000, 0);
                break;

            case "RewardBack":
                factory = new RewardBackFactory(8000, 100);
                break;

            default:
                break;
            }

            var creditCard = factory.GetCreditCard();

            Console.WriteLine(
                $"Card details: \nCard Type: {creditCard.CardType}\nCredit Limit: {creditCard.CardLimit}\nAnnual Fee: {creditCard.AnnualFee}");
        }