|
| 1 | +using CSharp.Tutorials.CodeRefactoring.Samples; |
| 2 | + |
| 3 | +namespace CSharp.Tutorials.CodeRefactoring |
| 4 | +{ |
| 5 | + class ExtractMethod |
| 6 | + { |
| 7 | + // After refactoring |
| 8 | + public static void ProcessOrder(Order order) |
| 9 | + { |
| 10 | + Console.WriteLine("Processing order by using seperated methods..."); |
| 11 | + ValidateOrder(order); |
| 12 | + UpdateInventory(order); |
| 13 | + CalculateTotalPrice(order); |
| 14 | + NotifyCustomer(order); |
| 15 | + } |
| 16 | + |
| 17 | + static void ValidateOrder(Order order) |
| 18 | + { |
| 19 | + if (!order.IsValid) |
| 20 | + { |
| 21 | + throw new ArgumentException("Invalid order."); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + static void UpdateInventory(Order order) |
| 26 | + { |
| 27 | + foreach (var product in order.Products) |
| 28 | + { |
| 29 | + // Reduce inventory by 1 for each ordered product |
| 30 | + int updatedQuantity = product.AvailableQuantity - 1; |
| 31 | + |
| 32 | + // Ensure the updated quantity is non-negative |
| 33 | + updatedQuantity = Math.Max(updatedQuantity, 0); |
| 34 | + |
| 35 | + // Update the product's available quantity |
| 36 | + product.AvailableQuantity = updatedQuantity; |
| 37 | + |
| 38 | + // Print a message to simulate inventory update |
| 39 | + Console.WriteLine($"Inventory updated for product '{product.Name}': Available Quantity = {product.AvailableQuantity}"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static void CalculateTotalPrice(Order order) |
| 44 | + { |
| 45 | + double totalPrice = order.Products.Sum(p => p.Price); |
| 46 | + totalPrice -= CalculateDiscount(order); |
| 47 | + Console.WriteLine($"Order Total: {totalPrice}"); |
| 48 | + } |
| 49 | + |
| 50 | + static double CalculateDiscount(Order order) |
| 51 | + { |
| 52 | + double discount = 0; |
| 53 | + double totalPrice = order.Products.Sum(p => p.Price); |
| 54 | + |
| 55 | + // Check if the customer is a preferred customer and apply a discount if eligible |
| 56 | + if (order.Customer.IsPreferredCustomer) |
| 57 | + { |
| 58 | + // Apply a 10% discount for preferred customers |
| 59 | + discount = totalPrice * 0.1; |
| 60 | + } |
| 61 | + |
| 62 | + // Check if the order total exceeds a certain threshold and apply additional discount |
| 63 | + if (totalPrice >= 1000) |
| 64 | + { |
| 65 | + // Apply a 50ドル discount for orders totaling 1000ドル or more |
| 66 | + discount += 50; |
| 67 | + } |
| 68 | + else if (totalPrice >= 500) |
| 69 | + { |
| 70 | + // Apply a 20ドル discount for orders totaling 500ドル or more |
| 71 | + discount += 20; |
| 72 | + } |
| 73 | + |
| 74 | + // Ensure the discount does not exceed the order total |
| 75 | + discount = Math.Min(discount, totalPrice); |
| 76 | + |
| 77 | + return discount; |
| 78 | + } |
| 79 | + |
| 80 | + static void NotifyCustomer(Order order) |
| 81 | + { |
| 82 | + string shippingLabel = GenerateShippingLabel(order.Customer, order.Address); |
| 83 | + Console.WriteLine($"Sending notification to customer {order.Customer.Name}: Your order has been shipped. Shipping label: {shippingLabel}"); |
| 84 | + } |
| 85 | + |
| 86 | + static string GenerateShippingLabel(Customer customer, Address address) |
| 87 | + { |
| 88 | + return $"Shipping label for {customer.Name} at {address.Street}, {address.City}, {address.ZipCode}"; |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments