-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
371 lines (324 loc) · 13.6 KB
/
Copy pathProgram.cs
File metadata and controls
371 lines (324 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using InventorySystem.Models;
using InventorySystem.Services;
using InventorySystem.Utils;
namespace InventorySystem
{
/// <summary>
/// Main program class for the Inventory Management System.
/// </summary>
class Program
{
private static InventoryService _inventoryService = new InventoryService();
/// <summary>
/// Entry point of the application.
/// </summary>
/// <param name="args">Command line arguments.</param>
static void Main(string[] args)
{
Console.WriteLine("Welcome to the Inventory Management System!");
Console.WriteLine("==========================================");
// Main application loop
bool isRunning = true;
while (isRunning)
{
try
{
DisplayMenu();
var choice = GetMenuChoice();
isRunning = ProcessUserChoice(choice);
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
Console.WriteLine("Press any key to continue...");
if (Console.IsInputRedirected == false)
{
Console.ReadKey();
}
}
}
Console.WriteLine("Thank you for using the Inventory Management System!");
// Only wait for key press if console input is available
if (Console.IsInputRedirected == false)
{
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
/// <summary>
/// Displays the main menu options to the user.
/// </summary>
private static void DisplayMenu()
{
Console.Clear();
Console.WriteLine("=== Inventory Management System ===");
Console.WriteLine("1. Add Product");
Console.WriteLine("2. Update Inventory");
Console.WriteLine("3. View All Products");
Console.WriteLine("4. Delete Product");
Console.WriteLine("5. Exit");
Console.WriteLine("====================================");
Console.WriteLine(_inventoryService.GetInventorySummary());
Console.WriteLine("====================================");
}
/// <summary>
/// Gets and validates the user's menu choice.
/// </summary>
/// <returns>The selected menu option.</returns>
private static MenuOption GetMenuChoice()
{
while (true)
{
Console.Write("Enter your choice (1-5): ");
string? input = Console.ReadLine();
if (InputValidator.ValidateMenuChoice(input ?? string.Empty, out MenuOption choice))
{
return choice;
}
Console.WriteLine("Invalid choice. Please enter a number between 1 and 5.");
}
}
/// <summary>
/// Processes the user's menu choice and executes the corresponding action.
/// </summary>
/// <param name="choice">The menu option selected by the user.</param>
/// <returns>True to continue running the application; false to exit.</returns>
private static bool ProcessUserChoice(MenuOption choice)
{
switch (choice)
{
case MenuOption.AddProduct:
AddProductFlow();
break;
case MenuOption.UpdateInventory:
UpdateInventoryFlow();
break;
case MenuOption.ViewProducts:
ViewProductsFlow();
break;
case MenuOption.DeleteProduct:
DeleteProductFlow();
break;
case MenuOption.Exit:
return false;
default:
Console.WriteLine("Invalid option selected.");
break;
}
if (choice != MenuOption.Exit)
{
Console.WriteLine("\nPress any key to continue...");
if (Console.IsInputRedirected == false)
{
Console.ReadKey();
}
}
return true;
}
/// <summary>
/// Handles the flow for adding a new product.
/// </summary>
private static void AddProductFlow()
{
Console.WriteLine("\n=== Add New Product ===");
try
{
// Get product name
string name = InputValidator.GetValidatedInput<string>(
"Enter product name: ",
input => (InputValidator.ValidateProductName(input), input),
"Product name must be at least 2 characters long and not empty."
);
// Get product price
decimal price = InputValidator.GetValidatedInput<decimal>(
"Enter product price: $",
input => {
bool isValid = InputValidator.ValidatePrice(input, out decimal parsedPrice);
return (isValid, parsedPrice);
},
"Price must be a positive number."
);
// Get product quantity
int quantity = InputValidator.GetValidatedInput<int>(
"Enter product quantity: ",
input => {
bool isValid = InputValidator.ValidateQuantity(input, out int parsedQuantity);
return (isValid, parsedQuantity);
},
"Quantity must be a non-negative integer."
);
// Add the product
var product = _inventoryService.AddProduct(name, price, quantity);
Console.WriteLine($"\n✓ Product '{product.Name}' added successfully!");
Console.WriteLine($"Product Details: {product}");
}
catch (ArgumentException ex)
{
Console.WriteLine($"\n✗ Error: {ex.Message}");
}
catch (InvalidOperationException)
{
Console.WriteLine("\n✗ Operation cancelled due to invalid input.");
}
}
/// <summary>
/// Handles the flow for updating product inventory.
/// </summary>
private static void UpdateInventoryFlow()
{
Console.WriteLine("\n=== Update Product Inventory ===");
if (_inventoryService.IsInventoryEmpty())
{
Console.WriteLine("No products available to update.");
return;
}
try
{
// Display current products
DisplayCurrentProducts();
// Get product ID
int productId = InputValidator.GetValidatedInput<int>(
"Enter product ID to update: ",
input => {
bool isValid = InputValidator.ValidateProductId(input, out int parsedId);
return (isValid, parsedId);
},
"Product ID must be a positive integer."
);
// Check if product exists
var existingProduct = _inventoryService.FindProductById(productId);
if (existingProduct == null)
{
Console.WriteLine($"✗ Product with ID {productId} not found.");
return;
}
Console.WriteLine($"Current product: {existingProduct}");
// Get new quantity
int newQuantity = InputValidator.GetValidatedInput<int>(
"Enter new quantity: ",
input => {
bool isValid = InputValidator.ValidateQuantity(input, out int parsedQuantity);
return (isValid, parsedQuantity);
},
"Quantity must be a non-negative integer."
);
// Update the product
var updatedProduct = _inventoryService.UpdateProductQuantity(productId, newQuantity);
Console.WriteLine($"\n✓ Product '{updatedProduct.Name}' quantity updated to {newQuantity} successfully!");
Console.WriteLine($"Updated Details: {updatedProduct}");
}
catch (ArgumentException ex)
{
Console.WriteLine($"\n✗ Error: {ex.Message}");
}
catch (InvalidOperationException)
{
Console.WriteLine("\n✗ Operation cancelled due to invalid input.");
}
}
/// <summary>
/// Handles the flow for viewing all products.
/// </summary>
private static void ViewProductsFlow()
{
Console.WriteLine("\n=== Current Inventory ===");
var products = _inventoryService.GetAllProducts();
if (products.Count == 0)
{
Console.WriteLine("No products in inventory.");
return;
}
// Display products in a formatted table
Console.WriteLine($"{"ID",-4} | {"Name",-20} | {"Price",-10} | {"Quantity",-8}");
Console.WriteLine(new string('-', 50));
for (int i = 0; i < products.Count; i++)
{
var product = products[i];
Console.WriteLine($"{product.Id,-4} | {product.Name,-20} | ${product.Price,-9:F2} | {product.Quantity,-8}");
}
Console.WriteLine(new string('-', 50));
Console.WriteLine($"Total Products: {products.Count}");
Console.WriteLine($"Total Inventory Value: ${_inventoryService.GetTotalInventoryValue():F2}");
// Show low stock warning
var lowStockProducts = _inventoryService.GetLowStockProducts();
if (lowStockProducts.Count > 0)
{
Console.WriteLine("\n⚠️ Low Stock Alert:");
foreach (var product in lowStockProducts)
{
Console.WriteLine($" - {product.Name} (Quantity: {product.Quantity})");
}
}
}
/// <summary>
/// Handles the flow for deleting a product.
/// </summary>
private static void DeleteProductFlow()
{
Console.WriteLine("\n=== Delete Product ===");
if (_inventoryService.IsInventoryEmpty())
{
Console.WriteLine("No products available to delete.");
return;
}
try
{
// Display current products
DisplayCurrentProducts();
// Get product ID
int productId = InputValidator.GetValidatedInput<int>(
"Enter product ID to delete: ",
input => {
bool isValid = InputValidator.ValidateProductId(input, out int parsedId);
return (isValid, parsedId);
},
"Product ID must be a positive integer."
);
// Check if product exists
var productToDelete = _inventoryService.FindProductById(productId);
if (productToDelete == null)
{
Console.WriteLine($"✗ Product with ID {productId} not found.");
return;
}
Console.WriteLine($"Product to delete: {productToDelete}");
// Confirm deletion
bool confirmed = InputValidator.GetConfirmation("Are you sure you want to delete this product?");
if (confirmed)
{
bool deleted = _inventoryService.DeleteProduct(productId);
if (deleted)
{
Console.WriteLine($"\n✓ Product '{productToDelete.Name}' deleted successfully!");
}
else
{
Console.WriteLine($"\n✗ Failed to delete product '{productToDelete.Name}'.");
}
}
else
{
Console.WriteLine("\n❌ Deletion cancelled.");
}
}
catch (InvalidOperationException)
{
Console.WriteLine("\n✗ Operation cancelled due to invalid input.");
}
}
/// <summary>
/// Displays a compact list of current products for selection purposes.
/// </summary>
private static void DisplayCurrentProducts()
{
var products = _inventoryService.GetAllProducts();
Console.WriteLine("\nCurrent Products:");
for (int i = 0; i < products.Count; i++)
{
Console.WriteLine($" {products[i].Id}. {products[i].Name} (Qty: {products[i].Quantity})");
}
Console.WriteLine();
}
}
}