Skip to content

Commit 5fc7415

Browse files
committed
Change program management for execution of patterns
1 parent 3eac46b commit 5fc7415

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

src/DesignPatternsLibrary/Program.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,69 @@ namespace DesignPatternsLibrary
66
{
77
public class Program
88
{
9+
private static readonly string ExitCode = "q";
10+
private static readonly SortedDictionary<int, PatternExecutor> Executors = PatternExecutorsRegistry.Instance.GetAll();
11+
912
private static void Main(string[] args)
1013
{
11-
string choice;
12-
var executors = PatternExecutorsRegistry.Instance.GetAll();
13-
14-
while (!(choice = SelectFromMenu(executors)).Equals("q"))
14+
while (IsChoiceDifferentFromExitCode(out string choice))
1515
{
16-
if (!int.TryParse(choice, out int translatedChoice))
17-
{
18-
Console.WriteLine("Please choose one of the following!");
19-
continue;
20-
}
21-
22-
if (!executors.ContainsKey(translatedChoice))
16+
if (IsChoiceUnsupported(choice, out int choiceKey))
2317
{
24-
Console.WriteLine("Please choose one of the following!");
18+
Console.WriteLine("Please choose one of the following options!");
2519
continue;
2620
}
2721

28-
executors[translatedChoice].Execute();
22+
Executors[choiceKey].Execute();
2923
}
3024

3125
Console.WriteLine("Thank you!");
3226
Console.ReadLine();
3327
}
3428

35-
private static string SelectFromMenu(SortedDictionary<int, PatternExecutor> executors)
29+
private static bool IsChoiceDifferentFromExitCode(out string choice)
3630
{
37-
ListAvailableMenuOptions(executors);
38-
var choice = ChooseOneOption();
39-
40-
Console.ResetColor();
31+
ShowAvailableMenuOptions();
32+
choice = ChooseOneOptionFromMenu();
4133

42-
return choice;
34+
return !choice.Equals(ExitCode);
4335
}
4436

45-
private static void ListAvailableMenuOptions(SortedDictionary<int, PatternExecutor> executors)
37+
private static void ShowAvailableMenuOptions()
4638
{
4739
Console.ForegroundColor = ConsoleColor.DarkGreen;
4840
Console.WriteLine(Environment.NewLine);
4941

50-
foreach (var executor in executors)
42+
foreach (var executor in Executors)
5143
{
5244
Console.WriteLine($"{executor.Key}. {executor.Value.Name}");
5345
}
5446

55-
Console.WriteLine("q. Quit");
47+
Console.WriteLine($"{ExitCode}. Quit");
5648
}
5749

58-
private static string ChooseOneOption()
50+
private static string ChooseOneOptionFromMenu()
5951
{
6052
Console.ForegroundColor = ConsoleColor.DarkGray;
61-
Console.WriteLine($"\n Your choice: ");
53+
Console.WriteLine("\nYour choice: ");
54+
55+
var choice = Console.ReadLine();
56+
57+
Console.ResetColor();
58+
59+
return choice;
60+
}
61+
62+
private static bool IsChoiceUnsupported(string choice, out int choiceKey)
63+
{
64+
if (!int.TryParse(choice, out int key) || !Executors.ContainsKey(key))
65+
{
66+
choiceKey = -1;
67+
return true;
68+
}
6269

63-
return Console.ReadLine().ToLower();
70+
choiceKey = key;
71+
return false;
6472
}
6573
}
6674
}

0 commit comments

Comments
 (0)