Exemple #1
0
        private void strategyPatternBtn_Click(object sender, RoutedEventArgs e)
        {
            // Prepare strategies
            IBillingStrategy normalStrategy    = new NormalStrategy();
            IBillingStrategy happyHourStrategy = new HappyHourStrategy();
            Customer         firstCustomer     = new Customer(normalStrategy);

            // Normal billing
            firstCustomer.Add(1.0, 1);

            // Start Happy Hour
            firstCustomer.Strategy = happyHourStrategy;
            firstCustomer.Add(1.0, 2);

            // New Customer
            Customer secondCustomer = new Customer(happyHourStrategy);

            secondCustomer.Add(0.8, 1);

            // The Customer pays
            firstCustomer.PrintBill();

            // End Happy Hour
            secondCustomer.Strategy = normalStrategy;
            secondCustomer.Add(1.3, 2);
            secondCustomer.Add(2.5, 1);
            secondCustomer.PrintBill();
        }
        static void Main(string[] args)
        {
            NormalStrategy    NormalStrategy    = new NormalStrategy();
            HappyHourStrategy HappyHourStrategy = new HappyHourStrategy();

            Customer FirstCustomer = new Customer(NormalStrategy);

            //Normal
            FirstCustomer.Add(1.0, 2);

            //Happy Hour starts
            FirstCustomer.BillingStartegy = HappyHourStrategy;
            FirstCustomer.Add(2, 2);

            FirstCustomer.PrintBill();

            Customer SecondCustomer = new Customer(HappyHourStrategy);

            SecondCustomer.Add(4, 2);
            SecondCustomer.Add(6, 2);

            SecondCustomer.PrintBill();

            //Happy hour finishes
            Customer ThirdCustomer = new Customer(NormalStrategy);

            ThirdCustomer.Add(2, 3);
            ThirdCustomer.PrintBill();
        }
Exemple #3
0
        public void Test_Execute()
        {
            // Prepare strategies
            IBillingStrategy normalStrategy    = new NormalStrategy();
            IBillingStrategy happyHourStrategy = new HappyHourStrategy();

            Customer firstCustomer = new Customer(normalStrategy);

            // Normal billing
            firstCustomer.Add(1.0, 1);

            // Start Happy Hour
            firstCustomer.Strategy = happyHourStrategy;
            firstCustomer.Add(1.0, 2);

            // New Customer
            Customer secondCustomer = new Customer(happyHourStrategy);

            secondCustomer.Add(0.8, 1);

            // End Happy Hour
            secondCustomer.Strategy = normalStrategy;
            secondCustomer.Add(1.3, 2);
            secondCustomer.Add(2.5, 1);

            // Check calculated bill
            Assert.Equal(2.0, firstCustomer.GetCurrentBill());
            Assert.Equal(5.5, secondCustomer.GetCurrentBill());

            // The Customers pay, clear bill
            firstCustomer.PrintBill();
            secondCustomer.PrintBill();

            // Check bill cleared
            Assert.Equal(0, firstCustomer.GetCurrentBill());
            Assert.Equal(0, secondCustomer.GetCurrentBill());
        }