Exemplo n.º 1
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();

            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlert, CalculatedLeveledDiscount, AlertUser):C2}");
            Console.WriteLine();


            decimal total = cart.GenerateTotal((subTotal) => Console.WriteLine($"The subtotal for cart 2 is {subTotal:c2}"),
                                               (products, subTotal) =>
            {
                if (products.Count > 3)
                {
                    return(subTotal * 0.5m);
                }
                else
                {
                    return(subTotal);
                }
            },
                                               (message) => Console.WriteLine($"Cart 2 alert {message}"));

            Console.WriteLine($"The total for the 2 cart is {total}");

            Console.WriteLine();
            Console.Write("Please press any key to exit the application...");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        // Display way 1:
        private void messageBoxDemoButton_Click(object sender, EventArgs e)
        {
            // this is benefits of deleagetes. we can use ShoppingCartModel with ConsoleUI or WinFormUI.
            decimal total = cart.GenerateTotal(SubTotalAlert, CalculateLevelDiscount, PrintOutDiscountAlert);

            MessageBox.Show($"Total is : {Math.Round(total)}");
        }
Exemplo n.º 3
0
        static void Main()
        {
            PopulateCartWithDemoData();

            //passing in just the name of the method without any parentheses <- delegate passing
            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlert, CalculateLeveledDiscount, AlertUser)}");
            Console.WriteLine();
            //passing in an anonymous method - shorthand
            //the delegate cares only about output and input types.
            //passing in subTotal to a method without a name the arrow describes the logic behind the method.
            //the same goes for (products, subTotal) and (message).
            decimal total = cart.GenerateTotal((subTotal) => Console.WriteLine($"The subtotal for cart 2 is {subTotal}"),
                                               (products, subTotal) => {
                if (products.Count > 3)
                {
                    return(subTotal * 0.5M);
                }
                else
                {
                    return(subTotal);
                }
            },
                                               (message) => Console.WriteLine($"Cart 2 Alert: { message }"));

            Console.WriteLine($"The total for cart 2 is {total}");
            Console.WriteLine();

            Console.WriteLine();
            Console.Write("Please press any key to exit the application...");
            Console.ReadKey();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();

            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlter, CalculatedDiscount, AlertUser)}");

            Console.WriteLine();

            // Passing new functions
            decimal total = cart.GenerateTotal(
                (subTotal => Console.WriteLine($"Subtotal for cart 2: {subTotal}")),
                ((products, subTotal) => {
                if (products.Count > 2)
                {
                    return(subTotal * 0.5M);
                }
                else
                {
                    return(subTotal);
                }
            }
                ),
                (message => Console.WriteLine($"Cart 2: {message}"))
                );

            Console.WriteLine($"Total for cart 2: {total}");

            Console.ReadLine();
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            PopulateCartWithItems();

            Console.WriteLine($"Total cost for the items (after discount) {cart.GenerateTotal(SubtotalAlert, calculateDiscount, Alert):C2}");

            //representing the call above with anoymous methods

            decimal finaltotal = cart.GenerateTotal(
                (subtotal) => Console.WriteLine($"Cart2: The subtotal is {subtotal:C2}"),
                (products, subtotal) =>
            {
                if (products.Count > 3)
                {
                    return(subtotal * 0.5M);
                }
                else
                {
                    return(subtotal);
                }
            },
                (alertmsg) => Console.WriteLine($"Cart2: alert: {alertmsg}"));

            Console.WriteLine($"Cart2: Total cost for the items {finaltotal:C2}");

            Console.ReadLine();
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();

            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlert,LeveledDiscountCalculation,AlertUserWithExclamation):C2}");
            Console.WriteLine();
            decimal total = cart.GenerateTotal(
                (subTotal => Console.WriteLine($"The subtotal for cart 2 is {subTotal:C2}")),
                (products, subTotal) => {
                if (products.Count > 3)
                {
                    return(subTotal * 0.5M);
                }
                else
                {
                    return(subTotal);
                }
            },
                (message) => Console.WriteLine($"Cart 2 alert : {message} "));

            Console.WriteLine($"The total for Cart 2 = {total:C2}");

            //subTotal is parameter name passed to the annonymous function,
            //lambda expression- unnamed function we can pass, since delegate doesn't matter about the name just in ant out types.
            //2nd argument is passed Func, the "{}" allow us to have multiple lines of code
            Console.WriteLine();
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();

            //C2 Where C is currency (US Dollar) and 2 is the decimal place
            //SubTotalAlert method is being passed to GenerateTotal.
            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlert, CalculateLeveledDiscount, AlertUser):C2}");
            Console.WriteLine();


            //Another way to use delegates, creating an inline method (Anonymous method) or shorthand.
            //This is the same way as - cart.GenerateTotal(SubTotalAlert, CalculateLeveledDiscount, AlertUser):C2} but just shorthand and calculates discount differently.
            //We don't need to name all delegates, a delegate just cares about the in types (params) and the return type.
            decimal total = cart.GenerateTotal((subTotal) => Console.WriteLine($"The subTotal for cart 2 is {subTotal:C2}"),
                                               (products, subTotal) => {
                if (products.Count > 3)
                {
                    return(subTotal * 0.5M);
                }
                else
                {
                    return(subTotal);
                }
            },
                                               (message) => Console.WriteLine($"Cart 2 Alert: {message}"));

            Console.WriteLine($"The total for cart 2 is {total:C2}");
            Console.WriteLine();

            Console.WriteLine();
            Console.Write("Please press any key to exit the application...");
            Console.ReadKey();
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();

            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlert, CalculateLeveledDiscount,AlertUser):C2}");
            Console.WriteLine();

            decimal total = cart.GenerateTotal((subTotal) => Console.WriteLine($"The subtotal for cart 2 is {subTotal:C2}"),
                                               (products, subTotal) =>
            {
                if (products.Count > 3)
                {
                    return(subTotal * 0.5M);
                }
                else
                {
                    return(subTotal);
                }
            },
                                               (message) => Console.WriteLine($"Cart 2 Alert: { message}"));

            Console.WriteLine($"The total for cart 2 is {total:C2}");

            Console.WriteLine();

            IEnumerable <ProductModel> cartItems = cart.Items;

            Console.WriteLine($"The Highest product price is {cartItems.Max(x => x.Price):C2}");

            Console.WriteLine("Please press any key");

            Console.ReadLine();
        }
Exemplo n.º 9
0
        static void Main()
        {
            PopulateCartWithDemoData();

            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlert, CalculateLeveledDiscount, AlertUser):C2}");
            Console.WriteLine();
            Console.WriteLine($"The total for the cart 2 is {cart.GenerateTotal((subTotal) => Console.WriteLine($"The Subtotal for cart 2 is {subTotal:C2}"),(items, subTotal) => subTotal * 0.50M,(message) => Console.WriteLine(message))}");

            Console.WriteLine();
            Console.Write("Please press any key to exit the application...");
            Console.ReadKey();
        }
        // we have 3 diffrent deleagete.
        // One is simple way of doing with delegate keyword
        // other one is Func<> and
        // the other one is Action<>
        // (SubTotalAlert, CalculateLevelDiscount, AlertUser)
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();
            // Display Way 1: passing 3 created methods to generatetotal SubTotalAlert, CalculateLevelDiscount, AlertUser
            Console.WriteLine($"Total for the cart is (included discount) : {Math.Round(cart.GenerateTotal(SubTotalAlert, CalculateLevelDiscount, AlertUser),2)}");

            Console.WriteLine();

            // Display way 2: instead of passing 3 method (SubTotalAlert, CalculateLevelDiscount, AlertUser)
            // we create those methods inline (inside generatetotal as paramater)
            decimal total = cart.GenerateTotal((subTotal) => Console.WriteLine($"The subtotal for cart 2 is {Math.Round(subTotal,2)}"),
                                               (products, subTotal) =>
            {
                if (products.Count > 3)
                {
                    return(subTotal * 0.5M);
                }
                else
                {
                    return(subTotal);
                }
            },
                                               (message) => Console.WriteLine($"Cart 2 Alert: {message}"));

            Console.WriteLine($"The total for cart 2 is : {total} ");

            Console.WriteLine();
            Console.Write("Please press any key to exit the application...");
            Console.ReadKey();
        }
Exemplo n.º 11
0
        private void TotalTextBoxBtn_Click(object sender, EventArgs e)
        {
            //I don't know how to do this without the total variable;  :(

            //TotalTextBox.Text = cart.GenerateTotal(
            //    (preTotal) => PreTotalTextBox.Text = $"{preTotal:C2}",
            //    (items, preTotal) => preTotal - (items.Count * 2),
            //    (x) => { }).ToString();


            decimal total = cart.GenerateTotal(
                (preTotal) => PreTotalTextBox.Text = $"{preTotal:C2}",
                (items, preTotal) => preTotal - (items.Count * 2),
                (x) => { });

            TotalTextBox.Text = $"{total:C2}";
        }
Exemplo n.º 12
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();
            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(Alert, CalculateDiscountedSubTotal, Alert):C2}");
            Console.WriteLine("******************");
            var total = cart.GenerateTotal(subTotal => Console.WriteLine($"The subtotal for cart 2 is {subTotal:C2}"),
                                           (items, subTotal) => items.Count > 3 ? subTotal * 0.50M : subTotal,
                                           message => Console.WriteLine(message)
                                           );

            Console.WriteLine($"The total for the cart2 is {total:C2}");

            Console.WriteLine();
            Console.Write("Please press any key to exit the application...");
            Console.WriteLine();
            Console.ReadKey();
        }
Exemplo n.º 13
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();

            cart.GenerateTotal(getSubtotal, GetTotalBeforeDiscount, computeDiscountedTotal, printTotalAfterDiscount);

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }
Exemplo n.º 14
0
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            decimal total = cart.GenerateTotal((subtotal) => txtSubtotal.Text += $"{subtotal:C2}. ",
                                               (productlist, subtotal) => subtotal - productlist.Count(),
                                               (msg) => { }
                                               );

            txtTotalDisc.Text = $"after discount: ${total:C2}";
        }
Exemplo n.º 15
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();

            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlert, CalculateLeveledDiscount, AlertUser):C2}");
            Console.WriteLine();
            Console.Write("Please press any key to exit the application");
            Console.ReadKey();
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();

            Console.WriteLine($"The total for the cart is {cart.GenerateTotal():C2}");

            Console.WriteLine();
            Console.Write("Please press any key to exit the application...");
            Console.ReadKey();
        }
Exemplo n.º 17
0
        static void Main(string[] args)
        {
            PopulateCartWithDemoData();

            /*
             *  c2 indicates that I want this to be a money which in since I'm in the US is going to be a dollar sign and it's going to have two places after the
             *  decimal that's the two and so they'll show it formatted as money.
             */

            Console.WriteLine($"The total for the cart is {cart.GenerateTotal(SubTotalAlert, CalculateLeveledDiscount, AlertUser):C2}");
            Console.WriteLine();

            /*
             *  How to create GenerateTotal method inline?
             *  We can do so with the use of Anonymous method. Since it is anonymous we don't need to give it a name for our method because the delegate doesn't care
             *  all the delegate cares about is output type and input types.
             */
            decimal total = cart.GenerateTotal((subTotal) => Console.WriteLine($"The subtotal for cart 2 is {subTotal:C2}"),
                                               (products, subTotal) =>
            {
                if (products.Count > 3)
                {
                    return(subTotal * 0.5M);
                }
                else
                {
                    return(subTotal);
                }
            },
                                               (message) => Console.WriteLine($"Cart 2 Alert: { message }"));

            Console.WriteLine($"The total for Cart 2 is {total:C2}");
            Console.WriteLine();

            Console.WriteLine();
            Console.WriteLine("Please press any key to exit the application");
            Console.ReadKey();
        }
Exemplo n.º 18
0
        private void MessageBoxDemo_Click(object sender, EventArgs e)
        {
            decimal total = cart.GenerateTotal(SubTotalAlert, CalculateLeveledDiscount, PrintOutDiscountAlert);

            MessageBox.Show($"The total is {total:C2}");
        }
Exemplo n.º 19
0
        private void messageBoxDemoButton_Click(object sender, EventArgs e)
        {
            decimal total = cart.GenerateTotal(SubTotalAlert, CalculetedLevelDiscount, PrintOutDiscountAlert);

            MessageBox.Show($"The total is {total}");
        }