public static decimal ComputeDiscount(Order o, decimal total) { // Count the number of items ordered int count = 0; foreach (OrderItem i in o.Items) { count += i.Quantity; } decimal pct = 0; if (total > 500) pct = (decimal)0.20; if (total > 200) pct = (decimal)0.15; if (total > 100) pct = (decimal)0.10; // Calculate the discount amount decimal discount = total * pct; // Subtract a dollar for every item ordered discount -= (decimal)count; // Make sure it’s not less than zero if (discount < 0) discount = 0; Console.WriteLine("Discount computed: ${0}", discount.ToString()); return discount; }
public static decimal ComputeDiscount(Order o, decimal total) { // count number of items ordered var count = o.Items.Sum(i => i.Quantity); // determine the discount percentage var pct = 0m; if (total > 500) pct = 0.2m; else if (total > 200) pct = .15m; else if (total > 100) pct = .1m; // calculate the discount amount var discount = total*pct; // Subtract a dollar for every item ordered discount -= (decimal) count; // Make sure it's not less than zero if (discount < 0) discount = 0; Console.WriteLine($"Discount computed: {discount}"); return discount; }
static void Main(string[] args) { Order myOrder = new Order { OrderID = 1, Description = "Need some stuff", ShippingMethod = "2ndDay", TotalWeight = 100 }; myOrder.Items.Add(new OrderItem { OrderItemID = 1, Quantity = 1, ItemCode = "12345", Description = "Widget" }); myOrder.Items.Add(new OrderItem { OrderItemID = 2, Quantity = 3, ItemCode = "12346", Description = "Gadget" }); myOrder.Items.Add(new OrderItem { OrderItemID = 1, Quantity = 1, ItemCode = "12345", Description = "Widget" }); myOrder.Items.Add(new OrderItem { OrderItemID = 2, Quantity = 3, ItemCode = "12346", Description = "Gadget" }); IDictionary<string, object> input = new Dictionary<string, object> { {"argOrderInfo", myOrder} }; Activity workflow1 = new OrderWF(); IDictionary<string, object> output = WorkflowInvoker.Invoke(workflow1, input); // Get the TotalAmount returned by the workflow decimal total = (decimal)output["argTotalAmount"]; Console.WriteLine("Workflow returned ${0} for my order total", total); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); }
static void Main() { var myOrder = new Order { OrderId = 1, Description = "Need some stuff", ShippingMethod = "2ndDay", TotalWeight = 100 }; myOrder.Items.Add(new OrderItem { OrderItemId = 1, Quantity = 1, ItemCode = "12345", Description = "Widget" }); myOrder.Items.Add(new OrderItem { OrderItemId = 2, Quantity = 3, ItemCode = "12346", Description = "Gadget" }); myOrder.Items.Add(new OrderItem { OrderItemId = 3, Quantity = 2, ItemCode = "12347", Description = "Super Widget" }); var input = new Dictionary<string, object> { {"OrderInfo", myOrder} }; var output = WorkflowInvoker.Invoke(new OrderWF(), input); var total = (decimal) output["TotalAmount"]; Console.WriteLine($"Workflow returned ${total} for my order total"); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); }