Skip to content

Commit f88962b

Browse files
committed
Refactor UI logic to a new UserController
1 parent 1a4f989 commit f88962b

File tree

3 files changed

+109
-90
lines changed

3 files changed

+109
-90
lines changed

Controller/UserController.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using SimpleInventoryManagementSystem.Domain;
2+
using SimpleInventoryManagementSystem.Interfaces;
3+
4+
namespace SimpleInventoryManagementSystem.Controller;
5+
6+
public class UserController : IUserController
7+
{
8+
private readonly IProductRepository _repository;
9+
public UserController(IProductRepository repository) => _repository = repository;
10+
11+
public void AddProduct()
12+
{
13+
var product = EnterAndValidateProduct();
14+
_repository.AddProduct(product);
15+
}
16+
17+
public void FindAndDisplayProduct()
18+
{
19+
var product = FindProduct();
20+
Console.WriteLine(product == null ? "Product not found!" : product);
21+
}
22+
23+
public void FindAndEditProduct()
24+
{
25+
var product = FindProduct();
26+
if (product == null)
27+
{
28+
Console.WriteLine("Product not found!");
29+
return;
30+
}
31+
32+
Console.WriteLine("Please enter new product information.");
33+
var newProduct = EnterAndValidateProduct();
34+
_repository.DeleteProductByName(product.Name);
35+
_repository.AddProduct(newProduct);
36+
}
37+
38+
public void FindAndDeleteProduct()
39+
{
40+
var product = FindProduct();
41+
if (product == null)
42+
{
43+
Console.WriteLine("Product not found!");
44+
return;
45+
}
46+
47+
_repository.DeleteProductByName(product.Name);
48+
}
49+
50+
public void DisplayProducts()
51+
{
52+
var number = 1;
53+
foreach (var product in _repository.GetAllProducts())
54+
{
55+
Console.WriteLine($"[{number++}] {product}");
56+
}
57+
}
58+
59+
private Product? EnterProduct()
60+
{
61+
Console.WriteLine("Name:");
62+
var name = Console.In.ReadLine() ?? string.Empty;
63+
var valid = !string.IsNullOrEmpty(name);
64+
65+
Console.WriteLine("Price:");
66+
valid &= int.TryParse(Console.In.ReadLine(), out var price);
67+
68+
Console.WriteLine("Quantity:");
69+
valid &= int.TryParse(Console.In.ReadLine(), out var quantity);
70+
71+
return valid ? new Product(name, price, quantity) : null;
72+
}
73+
74+
private Product EnterAndValidateProduct()
75+
{
76+
var product = EnterProduct();
77+
while (product == null)
78+
{
79+
Console.WriteLine("Invalid information!");
80+
product = EnterProduct();
81+
}
82+
83+
return product;
84+
}
85+
86+
private Product? FindProduct()
87+
{
88+
Console.WriteLine("Name:");
89+
var name = Console.In.ReadLine() ?? string.Empty;
90+
return string.IsNullOrEmpty(name) ? null : _repository.GetProductByName(name);
91+
}
92+
}

Interfaces/IUserController.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace SimpleInventoryManagementSystem.Interfaces;
2+
3+
public interface IUserController
4+
{
5+
void AddProduct();
6+
void FindAndDisplayProduct();
7+
void FindAndEditProduct();
8+
void FindAndDeleteProduct();
9+
void DisplayProducts();
10+
}

Program.cs

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// See https://aka.ms/new-console-template for more information
22

3-
using SimpleInventoryManagementSystem.Domain;
4-
using SimpleInventoryManagementSystem.Interfaces;
3+
using SimpleInventoryManagementSystem.Controller;
54
using SimpleInventoryManagementSystem.Repository;
65

7-
IProductRepository inventory = new ProductRepository();
6+
var controller = new UserController(new ProductRepository());
87

98
while (true)
109
{
@@ -22,103 +21,21 @@ else. Exit
2221
switch (input)
2322
{
2423
case "1":
25-
AddProduct();
24+
controller.AddProduct();
2625
break;
2726
case "2":
28-
DisplayProducts();
27+
controller.DisplayProducts();
2928
break;
3029
case "3":
31-
FindAndDisplayProduct();
30+
controller.FindAndDisplayProduct();
3231
break;
3332
case "4":
34-
FindAndEditProduct();
33+
controller.FindAndEditProduct();
3534
break;
3635
case "5":
37-
FindAndDeleteProduct();
36+
controller.FindAndDeleteProduct();
3837
break;
3938
default:
4039
return 0;
4140
}
42-
}
43-
44-
void AddProduct()
45-
{
46-
var product = EnterAndValidateProduct();
47-
inventory.AddProduct(product);
48-
}
49-
50-
void FindAndDisplayProduct()
51-
{
52-
var product = FindProduct();
53-
Console.WriteLine(product == null ? "Product not found!" : product);
54-
}
55-
56-
void FindAndEditProduct()
57-
{
58-
var product = FindProduct();
59-
if (product == null)
60-
{
61-
Console.WriteLine("Product not found!");
62-
return;
63-
}
64-
65-
Console.WriteLine("Please enter new product information.");
66-
var newProduct = EnterAndValidateProduct();
67-
inventory.DeleteProductByName(product.Name);
68-
inventory.AddProduct(newProduct);
69-
}
70-
71-
void FindAndDeleteProduct()
72-
{
73-
var product = FindProduct();
74-
if (product == null)
75-
{
76-
Console.WriteLine("Product not found!");
77-
return;
78-
}
79-
80-
inventory.DeleteProductByName(product.Name);
81-
}
82-
83-
void DisplayProducts()
84-
{
85-
var number = 1;
86-
foreach (var product in inventory.GetAllProducts())
87-
{
88-
Console.WriteLine($"[{number++}] {product}");
89-
}
90-
}
91-
92-
Product? EnterProduct()
93-
{
94-
Console.WriteLine("Name:");
95-
var name = Console.In.ReadLine() ?? string.Empty;
96-
var valid = !string.IsNullOrEmpty(name);
97-
98-
Console.WriteLine("Price:");
99-
valid &= int.TryParse(Console.In.ReadLine(), out var price);
100-
101-
Console.WriteLine("Quantity:");
102-
valid &= int.TryParse(Console.In.ReadLine(), out var quantity);
103-
104-
return valid ? new Product(name, price, quantity) : null;
105-
}
106-
107-
Product EnterAndValidateProduct()
108-
{
109-
var product = EnterProduct();
110-
while (product == null)
111-
{
112-
Console.WriteLine("Invalid information!");
113-
product = EnterProduct();
114-
}
115-
116-
return product;
117-
}
118-
119-
Product? FindProduct()
120-
{
121-
Console.WriteLine("Name:");
122-
var name = Console.In.ReadLine() ?? string.Empty;
123-
return string.IsNullOrEmpty(name) ? null : inventory.GetProductByName(name);
12441
}

0 commit comments

Comments
 (0)