@@ -6,61 +6,69 @@ namespace DesignPatternsLibrary
6
6
{
7
7
public class Program
8
8
{
9
+ private static readonly string ExitCode = "q" ;
10
+ private static readonly SortedDictionary < int , PatternExecutor > Executors = PatternExecutorsRegistry . Instance . GetAll ( ) ;
11
+
9
12
private static void Main ( string [ ] args )
10
13
{
11
- string choice ;
12
- var executors = PatternExecutorsRegistry . Instance . GetAll ( ) ;
13
-
14
- while ( ! ( choice = SelectFromMenu ( executors ) ) . Equals ( "q" ) )
14
+ while ( IsChoiceDifferentFromExitCode ( out string choice ) )
15
15
{
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 ) )
23
17
{
24
- Console . WriteLine ( "Please choose one of the following!" ) ;
18
+ Console . WriteLine ( "Please choose one of the following options !" ) ;
25
19
continue ;
26
20
}
27
21
28
- executors [ translatedChoice ] . Execute ( ) ;
22
+ Executors [ choiceKey ] . Execute ( ) ;
29
23
}
30
24
31
25
Console . WriteLine ( "Thank you!" ) ;
32
26
Console . ReadLine ( ) ;
33
27
}
34
28
35
- private static string SelectFromMenu ( SortedDictionary < int , PatternExecutor > executors )
29
+ private static bool IsChoiceDifferentFromExitCode ( out string choice )
36
30
{
37
- ListAvailableMenuOptions ( executors ) ;
38
- var choice = ChooseOneOption ( ) ;
39
-
40
- Console . ResetColor ( ) ;
31
+ ShowAvailableMenuOptions ( ) ;
32
+ choice = ChooseOneOptionFromMenu ( ) ;
41
33
42
- return choice ;
34
+ return ! choice . Equals ( ExitCode ) ;
43
35
}
44
36
45
- private static void ListAvailableMenuOptions ( SortedDictionary < int , PatternExecutor > executors )
37
+ private static void ShowAvailableMenuOptions ( )
46
38
{
47
39
Console . ForegroundColor = ConsoleColor . DarkGreen ;
48
40
Console . WriteLine ( Environment . NewLine ) ;
49
41
50
- foreach ( var executor in executors )
42
+ foreach ( var executor in Executors )
51
43
{
52
44
Console . WriteLine ( $ "{ executor . Key } . { executor . Value . Name } ") ;
53
45
}
54
46
55
- Console . WriteLine ( "q . Quit") ;
47
+ Console . WriteLine ( $ " { ExitCode } . Quit") ;
56
48
}
57
49
58
- private static string ChooseOneOption ( )
50
+ private static string ChooseOneOptionFromMenu ( )
59
51
{
60
52
Console . ForegroundColor = ConsoleColor . DarkGray ;
61
- Console . WriteLine ( $ "\n Your choice: ") ;
53
+ Console . WriteLine ( "\n Your 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
+ }
62
69
63
- return Console . ReadLine ( ) . ToLower ( ) ;
70
+ choiceKey = key ;
71
+ return false ;
64
72
}
65
73
}
66
74
}
0 commit comments