refactor(morse): restructure program flow and improve separation of concerns
- Split main method logic into separate concerns: dictionary initialization, user input, text conversion, and translation - Change programeDictionnary() to return dictionary instead of void - Extract askUserSentence() to return user input string - Extract userTextToAnArray() to return array instead of calling translation directly - Remove debug output and dictionary printing - Adjust Console.Beep durations: 100ms for dots, 400ms for dashes - Improve method signatures to better reflect their responsibilities
This commit is contained in:
1 file changed
+13
-18
+13
-18
@@ -18,10 +18,14 @@ class PI114
|
||||
Console.WriteLine("┌───────────────────────────────────────────────┐");
|
||||
Console.WriteLine("\n cc");
|
||||
Console.WriteLine("└───────────────────────────────────────────────┘");
|
||||
programeDictionnary();
|
||||
IDictionary<string, string> specialcharacterDictionnary = programeDictionnary();
|
||||
IDictionary<string, string> letterToMorse = programmeLetterToMorseCode();
|
||||
string userText = askUserSentence();
|
||||
string[] userTextArray = userTextToAnArray(userText);
|
||||
translateUserSentenceToMorse(userTextArray, letterToMorse);
|
||||
}
|
||||
|
||||
static void programeDictionnary(){
|
||||
static IDictionary<string, string> programeDictionnary(){
|
||||
|
||||
Dictionary<string,string> specialCharacterDictionnary = new Dictionary<string, string>();
|
||||
specialCharacterDictionnary.Add("é","e");
|
||||
@@ -45,13 +49,7 @@ class PI114
|
||||
specialCharacterDictionnary.Add("â","a");
|
||||
specialCharacterDictionnary.Add("ä","a");
|
||||
specialCharacterDictionnary.Add("ç","c");
|
||||
|
||||
foreach (var ele in specialCharacterDictionnary)
|
||||
{
|
||||
Console.WriteLine($"Key: {ele.Key}, Value: {ele.Value}");
|
||||
}
|
||||
IDictionary<string, string> letterToMorse = programmeLetterToMorseCode();
|
||||
askUserSentence(letterToMorse);
|
||||
return specialCharacterDictionnary;
|
||||
}
|
||||
public static IDictionary<string,string> programmeLetterToMorseCode()
|
||||
{
|
||||
@@ -94,19 +92,17 @@ class PI114
|
||||
letterToMorse.Add(" ", "/");
|
||||
return letterToMorse;
|
||||
}
|
||||
static void askUserSentence(IDictionary<string, string> letterToMorse)
|
||||
static string askUserSentence()
|
||||
{
|
||||
Console.Write("Entrez votre phrase à déchiffrer : ");
|
||||
string userText = Console.ReadLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(userText);
|
||||
userTextToAnArray(userText, letterToMorse);
|
||||
return userText;
|
||||
}
|
||||
|
||||
static void userTextToAnArray(string userText, IDictionary<string, string> letterToMorse)
|
||||
static string[] userTextToAnArray(string userText)
|
||||
{
|
||||
string[] userTextArray = userText.Select(x => x.ToString()).ToArray();
|
||||
translateUserSentenceToMorse(userTextArray, letterToMorse);
|
||||
return userTextArray;
|
||||
}
|
||||
static void translateUserSentenceToMorse(string[] userTextArray, IDictionary<string, string> letterToMorse)
|
||||
{
|
||||
@@ -122,7 +118,7 @@ class PI114
|
||||
var resultCount = v.Value.Count(x => x.ToString() == ".");
|
||||
for (int i = 0 ; i < resultCount; i++)
|
||||
{
|
||||
Console.Beep(1000, 200);
|
||||
Console.Beep(1000, 100);
|
||||
}
|
||||
}
|
||||
else if (v.Value.Contains("-"))
|
||||
@@ -130,7 +126,7 @@ class PI114
|
||||
var resultCount = v.Value.Count(x => x.ToString() == "-");
|
||||
for (int i = 0 ; i < resultCount; i++)
|
||||
{
|
||||
Console.Beep(1000, 200);
|
||||
Console.Beep(1000, 400);
|
||||
}
|
||||
}
|
||||
Console.Write(v.Value + " ");
|
||||
@@ -138,5 +134,4 @@ class PI114
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user