Exemple #1
0
        public void Process()
        {
            PriceList plist = new PriceList();

            plist.AddItem("Apple", 0.45M);
            plist.AddItem("Orange", 0.65M);

            var sc = new ShopCart();

            try
            {
                if (2.0M != sc.Price(listAAOA, plist))
                {
                    ErrorDetected("Failed pricing step 1.1");
                }
                if (3.3M != sc.Price(listAAOAOO, plist))
                {
                    ErrorDetected("Failed pricing step 1.2");
                }
                //if (3.3M != sc.Price(new string[] { "Apple", "Apple", "Orange", "Apple", "Orange", "Orange", "Grape" }, plist))
                //ErrorDetected("Failed pricing step 3");
                Console.WriteLine("Completed step 1 testing.");

                // for step 2, we will add specials testing
                plist.AddSpecial("bogo", 2, 1);
                plist.AddSpecial("3for2", 3, 2);
                plist.SetSpecial("Apple", "Bogo");
                plist.SetSpecial("Orange", "3for2");

                if (1.55M != sc.Price(listAAOA, plist))
                {
                    ErrorDetected("Failed pricing step 2.1");
                }
                if (2.2M != sc.Price(listAAOAOO, plist))
                {
                    ErrorDetected("Failed pricing step 2.2");
                }
                //if (3.3M != sc.Price(new string[] { "Apple", "Apple", "Orange", "Apple", "Orange", "Orange", "Grape" }, plist))
                //    ErrorDetected("Failed pricing step 2.3");
                if (2.2M != sc.Price(listAAOAOOA, plist))
                {
                    ErrorDetected("Failed pricing step 2.3");
                }
                if (2.85M != sc.Price(listAAOAOOAO, plist))
                {
                    ErrorDetected("Failed pricing step 2.4");
                }
                Console.WriteLine("Completed step 2 testing.");

                // step 3 tests, adding bananas and more special options
                plist.AddItem("banana", 0.60M);
                plist.SetSpecial("Banana", "Bogo");
                if (1.25M != sc.Price(listABO, plist))
                {
                    ErrorDetected("Failed pricing step 3.1");
                }
                if (1.85M != sc.Price(listABOB, plist))
                {
                    ErrorDetected("Failed pricing step 3.2");
                }
                if (1.85M != sc.Price(listABOBB, plist))
                {
                    ErrorDetected("Failed pricing step 3.3");
                }
                if (1.85M != sc.Price(listABOBA, plist))
                {
                    ErrorDetected("Failed pricing step 3.4");
                }
                Console.WriteLine("Completed step 3 testing.");

                // step 4 tests, added melon with separate 3for2 offer
                plist.AddItem("melon", 1.0M);
                plist.AddSpecial("3for2m", 3, 2);
                plist.SetSpecial("melon", "3for2m");
                if (3.5M != sc.Price(listABOBBMO, plist))
                {
                    ErrorDetected("Failed pricing step 4.1");
                }
                if (4.5M != sc.Price(listABOBBMOOMM, plist))
                {
                    ErrorDetected("Failed pricing step 4.2");
                }
                if (3.95M != sc.Price(listOOOOMM, plist))
                {
                    ErrorDetected("Failed pricing step 4.3");
                }
                Console.WriteLine("Completed step 4 testing.");

                // step 5 tests, need to do a real-time, running total of checkout
                ShopCartRT scrt = new ShopCartRT();
                foreach (string item in listABOBBMOOMM)
                {
                    Console.WriteLine("{0:C} for {1}", scrt.Price(item, plist), scrt.DisplayList());
                }
                Console.WriteLine("Completed step 5 testing.");

                // stretch tests, part 1 - show evidence that a cheaper price can be gotten for two trips
                // to the checkout instead of a single one with the same items
                Console.WriteLine();
                Console.WriteLine();

                scrt.Clear();
                Decimal trip1 = scrt.Price(new string[] { "Banana", "Banana" }, plist);
                Console.WriteLine("Trip 1 Total: {0:C} for {1}.", trip1, scrt.DisplayList());
                scrt.Clear();
                Decimal trip2 = scrt.Price(new string[] { "Apple", "Apple" }, plist);
                Console.WriteLine("Trip 2 Total: {0:C} for {1}.", trip2, scrt.DisplayList());
                Console.WriteLine("Total for 4 items: {0:C}", trip1 + trip2);
                if (1.05M != trip1 + trip2)
                {
                    ErrorDetected("Failed pricing step stretch step 1");
                }

                scrt.Clear();
                trip1 = scrt.Price(listBBAA, plist);
                Console.WriteLine("Single Trip Total: {0:C} for {1}.", trip1, scrt.DisplayList());
                if (1.2M != trip1)
                {
                    ErrorDetected("Failed pricing step stretch step 2");
                }

                scrt.Clear();
                trip1 = scrt.Price(listBBAA, plist, false);
                Console.WriteLine("Single Trip (not combined offer) Total: {0:C} for {1}.", trip1, scrt.DisplayList());
                if (1.05M != trip1)
                {
                    ErrorDetected("Failed pricing step stretch step 3");
                }
                Console.WriteLine("Completed stretch step testing.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: {0}", ex.Message);
                errors++;
            }

            Console.WriteLine("Total errors detected: {0}", errors);
        }