-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
105 lines (89 loc) · 3.71 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using unscramble.Data;
using unscramble.Workers;
namespace unscramble
{
class Program
{
private static readonly FileReader _fileReader = new FileReader();
private static readonly WordMatcher _wordMatcher = new WordMatcher();
static void Main(string[] args)
{
try
{
bool continueWordScramble = true;
do
{
Console.WriteLine(Constants.OptionsOnHowToEnterScrambledWords);
var option = Console.ReadLine() ?? string.Empty; // If user input was null, default to an empty string
switch (option.ToUpper())
{
case Constants.File:
Console.Write(Constants.EnterScrambledWordsViaFile);
FileScenario();
break;
case Constants.Manual:
Console.Write(Constants.EnterScrambledWordsManually);
ManualEntryScenario();
break;
default:
Console.Write(Constants.EnterScrambledWordsOptionNotRecognised);
break;
}
var continueDecision = string.Empty; // Either "Y" or end
do
{
Console.WriteLine(Constants.OptionsOnContinuingTheProgram);
continueDecision = (Console.ReadLine() ?? string.Empty);
} while (
!continueDecision.Equals(Constants.Yes, StringComparison.OrdinalIgnoreCase) &&
!continueDecision.Equals(Constants.No, StringComparison.OrdinalIgnoreCase));
continueWordScramble = continueDecision.Equals(Constants.Yes, StringComparison.OrdinalIgnoreCase);
} while (continueWordScramble);
}
catch (Exception ex)
{
Console.WriteLine(Constants.ErrorProgramWillBeTerminated + ex.Message);
}
}
private static void ManualEntryScenario()
{
var manualInput = Console.ReadLine() ?? string.Empty;
string[] scrambledWords = manualInput.Split(",");
DisplayMatched(scrambledWords);
}
private static void FileScenario()
{
try
{
var filename = Console.ReadLine() ?? string.Empty;
string[] scrambledWords = _fileReader.Read(filename);
DisplayMatched(scrambledWords);
}
catch (Exception ex)
{
// Load the error message that we saved as a constant:
Console.WriteLine(Constants.ErrorScrambledWordsCannotBeLoaded + ex.Message);
}
}
private static void DisplayMatched(string[] scrambledWords)
{
string[] wordList = _fileReader.Read(Constants.WordListFileName);
List<MatchedWord> matchedWords = _wordMatcher.Match(scrambledWords, wordList);
if (matchedWords.Any()) // If this list has anything in it
{
foreach ( var matchedWord in matchedWords)
{
Console.WriteLine(Constants.MatchFound, matchedWord.ScrambledWord, matchedWord.Word);
//$"Match found for {matchedWord.ScrambledWord}, {matchedWord.Word}");
}
}
else
{
Console.WriteLine(Constants.MatchNotFound);
}
}
}
}