コード例 #1
0
        public void Save(Order order)
        {
            if (order.Total <= 0)
                throw new Exception("Total must be greater than zero");

            discountDb.Save(order);
        }
コード例 #2
0
        public void CalculateTotal_WithoutCoupon_ReturnLineItemTotal()
        {
            Order order = new Order { ItemTotal = 100 };
            OrderServices orderServices = new OrderServices(new DataAccess());

            var total = orderServices.CalculateTotal(order);

            Assert.AreEqual(100, total);
        }
        public void CalculateTotal_WithCoupon_ReturnLineItemWithDiscount()
        {
            Order order = new Order { Coupon = "christmas", ItemTotal = 100 };
            OrderServices orderServices = new OrderServices(new DataAccess());

            var total = orderServices.CalculateTotal(order);

            Assert.AreEqual(90, total);
        }
コード例 #4
0
        public void Save_ValidOrder_TheOrderIsPersisted()
        {
            Order order = new Order { Id = 1, ItemTotal = 100, Total = 110 };
            var dataAccess = new SimpleDataAccess();
            OrderServices orderProcessor = new OrderServices(dataAccess);

            orderProcessor.Save(order);

            Assert.AreEqual(order, dataAccess.OrderSaved);
        }
コード例 #5
0
        public void Save_ValidOrder_TheOrderIsPersisted()
        {
            Order order = new Order { Id = 1, ItemTotal = 100, Total = 110 };
            var dataAccess = new Mock<IDataAccess>();
            OrderServices orderProcessor = new OrderServices(dataAccess.Object);

            orderProcessor.Save(order);

            dataAccess.Verify(x => x.SaveOrder(order));
        }
        public void Save_ValidOrder_TheOrderIsPersisted()
        {
            Order order = new Order { Id = 1, ItemTotal = 100, Total = 110 };
            OrderServices orderProcessor = new OrderServices(new DataAccess());

            orderProcessor.Save(order);

            Order orderFromDb = orderProcessor.GetOrder(order.Id);
            Assert.IsNotNull(orderFromDb);
        }
コード例 #7
0
        public void CalculateTotal_WithCoupon_ReturnLineItemWithDiscount()
        {
            Order order = new Order { Coupon = "christmas", ItemTotal = 100 };
            var dataAccess = new SimpleDataAccess();
            dataAccess.CouponPercentage = 10;
            OrderServices orderServices = new OrderServices(dataAccess);

            var total = orderServices.CalculateTotal(order);

            Assert.AreEqual(90, total);
        }
コード例 #8
0
        public void CalculateTotal_WithCoupon_ReturnLineItemWithDiscount()
        {
            Order order = new Order { Coupon = "christmas", ItemTotal = 100 };
            var dataAccess = new Mock<IDataAccess>();
            dataAccess.Setup(x => x.GetPromotionalDiscount(order.Coupon)).Returns(10);
            OrderServices orderServices = new OrderServices(dataAccess.Object);

            var total = orderServices.CalculateTotal(order);

            Assert.AreEqual(90, total);
        }
コード例 #9
0
        public decimal CalculateTotal(Order order)
        {
            decimal itemTotal = order.ItemTotal;
            decimal discountPercentage = 0;

            if (!string.IsNullOrEmpty(order.Coupon))
            {
                discountPercentage = this.dataAccess.GetPromotionalDiscount(order.Coupon);
            }
            return itemTotal - itemTotal * discountPercentage / 100;
        }
コード例 #10
0
ファイル: DiscountDB.cs プロジェクト: snahider/Presentations
        public void Save(Order order)
        {
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString))
            {
                using (var command = new SqlCommand("Insert into Orders values(@Id,@Total)", conn))
                {

                    command.Parameters.AddWithValue("Id", order.Id);
                    command.Parameters.AddWithValue("Total", order.Total);

                    conn.Open();
                    int rows = command.ExecuteNonQuery();
                    if (rows == 0)
                    {
                        throw new Exception("Operation Failed");
                    }
                }
            }
        }
コード例 #11
0
        public void SaveOrder(Order order)
        {
            var orders = new List<Order>();
            using (TextReader textReader = new StreamReader(dataDirectory + "/orders.txt"))
            {
                var csvReader = new CsvReader(textReader);
                orders = csvReader.GetRecords<Order>().ToList();
            }

            var orderSaved = orders.SingleOrDefault(x => x.Id == order.Id);
            if (orderSaved != null)
                throw new Exception("Primary Constraint Exception: another object already exists with same Id");
            orders.Add(order);

            using (TextWriter textWriter = new StreamWriter(dataDirectory + "/orders.txt"))
            {
                var csvWriter = new CsvWriter(textWriter);
                csvWriter.WriteRecords(orders);
            }
        }
コード例 #12
0
 public void SaveOrder(Order order)
 {
     OrderSaved = order;
 }
コード例 #13
0
 private bool IsValid(Order order)
 {
     return order.Id > 0 && order.ItemTotal > 0 && order.Total > 0;
 }
コード例 #14
0
 public void Save(Order order)
 {
     if (!IsValid(order))
         throw new Exception("Invalid Order");
     dataAccess.SaveOrder(order);
 }