public void Save(Order order) { if (order.Total <= 0) throw new Exception("Total must be greater than zero"); discountDb.Save(order); }
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); }
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); }
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); }
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); }
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); }
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; }
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"); } } } }
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); } }
public void SaveOrder(Order order) { OrderSaved = order; }
private bool IsValid(Order order) { return order.Id > 0 && order.ItemTotal > 0 && order.Total > 0; }
public void Save(Order order) { if (!IsValid(order)) throw new Exception("Invalid Order"); dataAccess.SaveOrder(order); }