Beispiel #1
0
        public void Calculate_Order_TotalSum()
        {
            // Arrange.
            Order order = new Order();

            order.Add(new OrderItem {
                Name = "Milk", Price = 10.0M, Quantity = 10
            });
            order.Add(new OrderItem {
                Name = "Chocolate", Price = 4.5M, Quantity = 20
            });
            order.Add(new OrderItem {
                Name = "Coffee", Price = 1M, Quantity = 345
            });

            decimal excepted = 0;

            foreach (var orderItem in order.GetOrderItems())
            {
                excepted += orderItem.Price * orderItem.Quantity;
            }

            // Act.
            decimal result = TotalSumCalculator.Calculate(order);

            // Assert.
            Assert.Equal(excepted, result);
        }
Beispiel #2
0
        /// <summary>
        /// Entry point.
        /// </summary>
        static void Main(string[] args)
        {
            // Create order.
            Order order = new Order();

            // Order item.
            OrderItem orderItem;

            // User input.
            string input;

            // Mathes pattern user input.
            bool isMathesPattern;

            Console.WriteLine("Enter line by line the list of products that the customer ordered: (Input template: \"Milk,10.0,4\")\n" +
                              "If you need to complete, make an empty entry.\n");

            while (true)
            {
                input = Console.ReadLine();

                if (string.IsNullOrEmpty(input))
                {
                    break;
                }

                // Check user input.
                isMathesPattern = Checker.CheckLine(input);

                if (isMathesPattern == false)
                {
                    Console.WriteLine("Input does not match the pattern.");
                    continue;
                }

                // Create order item.
                orderItem = OrderItemParser.Parse(input);

                try
                {
                    // Add order item to order.
                    order.Add(orderItem);
                }
                catch (OrderAddException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            // Total sum of order.
            decimal totalSum = TotalSumCalculator.Calculate(order);

            // Total sum with tax of order.
            decimal totalSumWithTax = TaxCalculator.Calculate(totalSum, 3);

            // Use a certain culture
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

            Console.WriteLine($"Total: {totalSum:C}");
            Console.WriteLine($"Total with tax: {totalSumWithTax:C}");

            // Delay.
            Console.ReadKey();
        }