|
| 1 | + |
| 2 | +ContosoOnlineStore/ |
| 3 | +├── Program.cs |
| 4 | +├── Product.cs |
| 5 | +├── ProductCatalog.cs |
| 6 | +├── Order.cs |
| 7 | +├── OrderItem.cs |
| 8 | +├── OrderProcessor.cs |
| 9 | +├── InventoryManager.cs |
| 10 | +├── EmailService.cs |
| 11 | +├── ContosoOnlineStore.csproj |
| 12 | +└── README.md |
| 13 | + |
| 14 | + |
| 15 | +# 🛒 ContosoOnlineStore |
| 16 | + |
| 17 | +## Program.cs |
| 18 | +```csharp |
| 19 | +using System; |
| 20 | +using System.Diagnostics; |
| 21 | + |
| 22 | +namespace ContosoOnlineStore |
| 23 | +{ |
| 24 | + class Program |
| 25 | + { |
| 26 | + static async Task Main(string[] args) |
| 27 | + { |
| 28 | + Console.WriteLine("=== Contoso Online Store ==="); |
| 29 | + |
| 30 | + ProductCatalog catalog = new ProductCatalog(); |
| 31 | + InventoryManager inventory = new InventoryManager(catalog); |
| 32 | + EmailService emailService = new EmailService(); |
| 33 | + OrderProcessor processor = new OrderProcessor(catalog, inventory, emailService); |
| 34 | + |
| 35 | + Order order = new Order(); |
| 36 | + order.Items.Add(new OrderItem(2, 5)); |
| 37 | + order.Items.Add(new OrderItem(7, 3)); |
| 38 | + order.Items.Add(new OrderItem(4, 10)); |
| 39 | + order.Items.Add(new OrderItem(9, 2)); |
| 40 | + |
| 41 | + Stopwatch sw = Stopwatch.StartNew(); |
| 42 | + decimal total = await processor.FinalizeOrder(order); |
| 43 | + sw.Stop(); |
| 44 | + |
| 45 | + Console.WriteLine($"\nOrder processed. Total Cost = ${total:F2}"); |
| 46 | + Console.WriteLine($"Processing Time = {sw.ElapsedMilliseconds} ms"); |
| 47 | + |
| 48 | + Console.WriteLine("Remaining stock:"); |
| 49 | + foreach (var item in order.Items) |
| 50 | + { |
| 51 | + Product prod = catalog.GetProductById(item.ProductId); |
| 52 | + int stock = inventory.GetStockLevel(item.ProductId); |
| 53 | + Console.WriteLine($"- {prod.Name}: {stock} units"); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Product.cs |
| 61 | +```csharp |
| 62 | +namespace ContosoOnlineStore |
| 63 | +{ |
| 64 | + public class Product |
| 65 | + { |
| 66 | + public int Id { get; } |
| 67 | + public string Name { get; } |
| 68 | + public decimal Price { get; } |
| 69 | + public int InitialStock { get; } |
| 70 | + |
| 71 | + public Product(int id, string name, decimal price, int initialStock) |
| 72 | + { |
| 73 | + Id = id; |
| 74 | + Name = name; |
| 75 | + Price = price; |
| 76 | + InitialStock = initialStock; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## ProductCatalog.cs |
| 83 | +```csharp |
| 84 | +using System.Collections.Generic; |
| 85 | +using System.Linq; |
| 86 | + |
| 87 | +namespace ContosoOnlineStore |
| 88 | +{ |
| 89 | + public class ProductCatalog |
| 90 | + { |
| 91 | + private List<Product> _products; |
| 92 | + |
| 93 | + public ProductCatalog() |
| 94 | + { |
| 95 | + _products = new List<Product> |
| 96 | + { |
| 97 | + new Product(1, "Phone", 299.99m, 50), |
| 98 | + new Product(2, "Headphones", 99.99m, 200), |
| 99 | + new Product(3, "Laptop", 1499.00m, 20), |
| 100 | + new Product(4, "Monitor", 389.50m, 75), |
| 101 | + new Product(5, "Charger", 19.99m, 500), |
| 102 | + new Product(6, "Speaker", 79.95m, 120), |
| 103 | + new Product(7, "SSD", 159.49m, 60), |
| 104 | + new Product(8, "Keyboard", 49.99m, 150), |
| 105 | + new Product(9, "Webcam", 39.99m, 300), |
| 106 | + new Product(10, "Earbuds", 129.99m, 80) |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + public Product GetProductById(int productId) |
| 111 | + { |
| 112 | + // Performance Issue: Linear search |
| 113 | + return _products.FirstOrDefault(p => p.Id == productId); |
| 114 | + } |
| 115 | + |
| 116 | + public List<Product> GetAllProducts() => _products; |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +## Order.cs |
| 122 | +```csharp |
| 123 | +using System.Collections.Generic; |
| 124 | + |
| 125 | +namespace ContosoOnlineStore |
| 126 | +{ |
| 127 | + public class Order |
| 128 | + { |
| 129 | + public List<OrderItem> Items { get; } |
| 130 | + |
| 131 | + public Order() |
| 132 | + { |
| 133 | + Items = new List<OrderItem>(); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## OrderItem.cs |
| 140 | +```csharp |
| 141 | +namespace ContosoOnlineStore |
| 142 | +{ |
| 143 | + public class OrderItem |
| 144 | + { |
| 145 | + public int ProductId { get; set; } |
| 146 | + public int Quantity { get; set; } |
| 147 | + |
| 148 | + public OrderItem(int productId, int quantity) |
| 149 | + { |
| 150 | + ProductId = productId; |
| 151 | + Quantity = quantity; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +## InventoryManager.cs |
| 158 | +```csharp |
| 159 | +using System.Collections.Generic; |
| 160 | + |
| 161 | +namespace ContosoOnlineStore |
| 162 | +{ |
| 163 | + public class InventoryManager |
| 164 | + { |
| 165 | + private Dictionary<int, int> _stockByProductId; |
| 166 | + |
| 167 | + public InventoryManager(ProductCatalog catalog) |
| 168 | + { |
| 169 | + _stockByProductId = new Dictionary<int, int>(); |
| 170 | + foreach (var product in catalog.GetAllProducts()) |
| 171 | + { |
| 172 | + _stockByProductId[product.Id] = product.InitialStock; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public int GetStockLevel(int productId) |
| 177 | + { |
| 178 | + return _stockByProductId.ContainsKey(productId) ? _stockByProductId[productId] : 0; |
| 179 | + } |
| 180 | + |
| 181 | + public void UpdateStockLevels(Order order) |
| 182 | + { |
| 183 | + foreach (OrderItem item in order.Items) |
| 184 | + { |
| 185 | + if (_stockByProductId.ContainsKey(item.ProductId)) |
| 186 | + { |
| 187 | + _stockByProductId[item.ProductId] -= item.Quantity; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | + |
| 195 | +## EmailService.cs |
| 196 | +```csharp |
| 197 | +using System; |
| 198 | +using System.Threading.Tasks; |
| 199 | + |
| 200 | +namespace ContosoOnlineStore |
| 201 | +{ |
| 202 | + public class EmailService |
| 203 | + { |
| 204 | + public async Task SendConfirmationAsync(Order order) |
| 205 | + { |
| 206 | + Console.WriteLine("Sending confirmation email..."); |
| 207 | + await Task.Delay(2000); // Performance Issue: Simulated blocking call |
| 208 | + Console.WriteLine("Email sent."); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | +``` |
| 213 | + |
| 214 | +## OrderProcessor.cs |
| 215 | +```csharp |
| 216 | +using System.Collections.Generic; |
| 217 | +using System.Threading.Tasks; |
| 218 | + |
| 219 | +namespace ContosoOnlineStore |
| 220 | +{ |
| 221 | + public class OrderProcessor |
| 222 | + { |
| 223 | + private ProductCatalog _catalog; |
| 224 | + private InventoryManager _inventory; |
| 225 | + private EmailService _emailService; |
| 226 | + |
| 227 | + public OrderProcessor(ProductCatalog catalog, InventoryManager inventory, EmailService emailService) |
| 228 | + { |
| 229 | + _catalog = catalog; |
| 230 | + _inventory = inventory; |
| 231 | + _emailService = emailService; |
| 232 | + } |
| 233 | + |
| 234 | + public decimal CalculateOrderTotal(Order order) |
| 235 | + { |
| 236 | + decimal total = 0; |
| 237 | + var productCache = new Dictionary<int, Product>(); |
| 238 | + |
| 239 | + foreach (OrderItem item in order.Items) |
| 240 | + { |
| 241 | + if (!productCache.ContainsKey(item.ProductId)) |
| 242 | + { |
| 243 | + productCache[item.ProductId] = _catalog.GetProductById(item.ProductId); |
| 244 | + } |
| 245 | + |
| 246 | + var prod = productCache[item.ProductId]; |
| 247 | + if (prod != null) |
| 248 | + { |
| 249 | + total += prod.Price * item.Quantity; |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + return total; |
| 254 | + } |
| 255 | + |
| 256 | + public async Task<decimal> FinalizeOrder(Order order) |
| 257 | + { |
| 258 | + decimal total = CalculateOrderTotal(order); |
| 259 | + _inventory.UpdateStockLevels(order); |
| 260 | + await _emailService.SendConfirmationAsync(order); |
| 261 | + return total; |
| 262 | + } |
| 263 | + } |
| 264 | +} |
| 265 | +``` |
0 commit comments