feat(morse converter): add loop for repeated conversions and improve user input handling
- Wrap main conversion logic in a while loop to allow multiple conversions - Extract morse code output and user prompting into new showResultInConsole method - Add input validation to ensure user enters non-empty text - Convert user input to lowercase before processing - Refactor translateUserSentenceToMorse to return the translated string instead of void - Improve console UI with better formatting and title - Move beeping and output logic from translation to display method
This commit is contained in:
1 file changed
+62
-28
+62
-28
@@ -16,14 +16,21 @@ class PI114
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("┌───────────────────────────────────────────────┐");
|
||||
Console.WriteLine("\n cc");
|
||||
Console.WriteLine("└───────────────────────────────────────────────┘");
|
||||
IDictionary<string, string> specialcharacterDictionnary = programeDictionnary();
|
||||
IDictionary<string, string> letterToMorse = programmeLetterToMorseCode();
|
||||
string[] userTextArray = askUserSentence();
|
||||
verifyUserSentence(userTextArray, specialcharacterDictionnary);
|
||||
translateUserSentenceToMorse(userTextArray, letterToMorse);
|
||||
bool convertAgain = true;
|
||||
while (convertAgain)
|
||||
{
|
||||
Console.Title = "Convertisseur T2M";
|
||||
Console.WriteLine("\t\t\t\t┌───────────────────────────────────────────────┐");
|
||||
Console.WriteLine("\t\t\t\t\tConvertisseur de texte en Morse");
|
||||
Console.WriteLine("\t\t\t\t└───────────────────────────────────────────────┘");
|
||||
IDictionary<string, string> specialcharacterDictionnary = programeDictionnary();
|
||||
IDictionary<string, string> letterToMorse = programmeLetterToMorseCode();
|
||||
string[] userTextArray = askUserSentence();
|
||||
verifyUserSentence(userTextArray, specialcharacterDictionnary);
|
||||
string translatedText = translateUserSentenceToMorse(userTextArray, letterToMorse);
|
||||
showResultInConsole(translatedText, ref convertAgain);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static IDictionary<string, string> programeDictionnary(){
|
||||
@@ -95,15 +102,20 @@ class PI114
|
||||
}
|
||||
static string[] askUserSentence()
|
||||
{
|
||||
Console.Write("Entrez votre phrase à déchiffrer : ");
|
||||
Console.Write("Entrez votre phrase à déchiffrer (Sans accents, A-Z 0-9): ");
|
||||
string userText = Console.ReadLine();
|
||||
while(userText.Length == 0)
|
||||
{
|
||||
Console.Write("[Erreur] ─ Veuillez entrer un texte (Sans accents, A-Z 0-9): ");
|
||||
userText = Console.ReadLine();
|
||||
}
|
||||
string[] userTextArray = userTextToAnArray(userText);
|
||||
return userTextArray;
|
||||
}
|
||||
|
||||
static string[] userTextToAnArray(string userText)
|
||||
{
|
||||
string[] userTextArray = userText.Select(x => x.ToString()).ToArray();
|
||||
string[] userTextArray = userText.Select(x => x.ToString().ToLower()).ToArray();
|
||||
Console.WriteLine(string.Join("", userTextArray));
|
||||
return userTextArray;
|
||||
}
|
||||
@@ -140,34 +152,56 @@ class PI114
|
||||
}
|
||||
return userTextArray;
|
||||
}
|
||||
static void translateUserSentenceToMorse(string[] userTextArray, IDictionary<string, string> letterToMorse)
|
||||
static string translateUserSentenceToMorse(string[] userTextArray, IDictionary<string, string> letterToMorse)
|
||||
{
|
||||
Console.WriteLine(string.Join(" ", userTextArray));
|
||||
string translatedText = null;
|
||||
foreach (string k in userTextArray)
|
||||
{
|
||||
foreach(KeyValuePair<string, string> v in letterToMorse)
|
||||
{
|
||||
if (k == v.Key)
|
||||
{
|
||||
if (v.Value.Contains("."))
|
||||
{
|
||||
var resultCount = v.Value.Count(x => x.ToString() == ".");
|
||||
for (int i = 0 ; i < resultCount; i++)
|
||||
{
|
||||
Console.Beep(1000, 100);
|
||||
}
|
||||
}
|
||||
else if (v.Value.Contains("-"))
|
||||
{
|
||||
var resultCount = v.Value.Count(x => x.ToString() == "-");
|
||||
for (int i = 0 ; i < resultCount; i++)
|
||||
{
|
||||
Console.Beep(1000, 400);
|
||||
}
|
||||
}
|
||||
Console.Write(v.Value + " ");
|
||||
translatedText += v.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return translatedText;
|
||||
}
|
||||
static bool showResultInConsole(string translatedText, ref bool convertAgain)
|
||||
{
|
||||
string[] translatedTextArray = translatedText.Select(x => x.ToString()).ToArray();
|
||||
foreach (string v in translatedTextArray)
|
||||
{
|
||||
if (v.Equals("."))
|
||||
{
|
||||
Console.Write(v);
|
||||
Console.Beep(1000, 100);
|
||||
}
|
||||
else if (v.Equals("-"))
|
||||
{
|
||||
Console.Write(v);
|
||||
Console.Beep(1000, 400);
|
||||
}
|
||||
else if (v.Equals("/")){
|
||||
Console.Write(v);
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("");
|
||||
Console.Write("Voulez vous refaire une conversion ? (O/N) : ");
|
||||
string wannaDoItAgain = Console.ReadLine();
|
||||
wannaDoItAgain = wannaDoItAgain.ToLower();
|
||||
if (wannaDoItAgain == "o")
|
||||
{
|
||||
Console.Clear();
|
||||
return convertAgain = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.Clear();
|
||||
Console.WriteLine("Merci aurevoir !!");
|
||||
return convertAgain = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user