Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- class Program
- {
- public static void Main()
- {
- Work();
- }
- private static void Work()
- {
- Console.Clear();
- Console.OutputEncoding = Encoding.UTF8;
- Console.Title = GetText("Title");
- Dictionary<string, string> dictionary = CreateDictionary();
- PrintHeader();
- bool isRunning = true;
- while (isRunning == true)
- {
- string input = RequestInput();
- if (IsExitCommand(input) == true)
- {
- ShowInfo(GetText("ProgramEnded"));
- isRunning = false;
- }
- else
- {
- ProcessQuery(dictionary, input);
- }
- }
- }
- private static Dictionary<string, string> CreateDictionary()
- {
- Dictionary<string, string> dictionary = new Dictionary<string, string>();
- dictionary.Add("apple", "яблоко");
- dictionary.Add("book", "книга");
- dictionary.Add("car", "машина");
- dictionary.Add("house", "дом");
- dictionary.Add("computer", "компьютер");
- return dictionary;
- }
- private static string RequestInput()
- {
- string? input;
- bool isValid;
- do
- {
- Console.Write(GetText("InputPrompt"));
- input = Console.ReadLine();
- isValid = !string.IsNullOrWhiteSpace(input);
- if (isValid == false)
- {
- ShowWarning(GetText("EmptyInput"));
- Console.WriteLine();
- }
- } while (isValid == false);
- return input!.Trim();
- }
- private static void ProcessQuery(Dictionary<string, string> dictionary, string input)
- {
- bool found = TryFindTranslation(dictionary, input, out string result);
- if (found == true)
- {
- ShowSuccess(result);
- }
- else
- {
- string message = string.Format(GetText("WordNotFound"), input);
- ShowError(message);
- }
- Console.WriteLine();
- }
- private static bool TryFindTranslation(
- Dictionary<string, string> dictionary,
- string input,
- out string result)
- {
- string normalized = input.ToLower();
- bool foundByKey = dictionary.TryGetValue(normalized, out string russianWord);
- if (foundByKey == true)
- {
- result = input + " → " + russianWord;
- return true;
- }
- foreach (KeyValuePair<string, string> pair in dictionary)
- {
- bool equals = string.Equals(pair.Value, input, StringComparison.OrdinalIgnoreCase);
- if (equals == true)
- {
- result = input + " → " + pair.Key;
- return true;
- }
- }
- result = string.Empty;
- return false;
- }
- private static bool IsExitCommand(string input)
- {
- string normalized = input.ToLower();
- bool isExitEng = normalized == "exit";
- bool isExitRus = normalized == "выход";
- return isExitEng || isExitRus;
- }
- private static void PrintHeader()
- {
- ShowHeader(GetText("Border"));
- ShowHeader(GetText("Header"));
- ShowHeader(GetText("Border"));
- Console.WriteLine();
- }
- private static void Display(string message, ConsoleColor color)
- {
- Console.ForegroundColor = color;
- Console.WriteLine(message);
- Console.ResetColor();
- }
- private static void ShowHeader(string message)
- {
- Display(message, ConsoleColor.Cyan);
- }
- private static void ShowSuccess(string message)
- {
- Display("✔ " + message, ConsoleColor.Green);
- }
- private static void ShowError(string message)
- {
- Display("✘ " + message, ConsoleColor.Red);
- }
- private static void ShowWarning(string message)
- {
- Display("! " + message, ConsoleColor.Yellow);
- }
- private static void ShowInfo(string message)
- {
- Display(message, ConsoleColor.Cyan);
- }
- private static string GetText(string key)
- {
- if (key == "Title")
- return "ENG ⇄ RUS Dictionary";
- if (key == "InputPrompt")
- return "Введите слово (или 'exit'/'выход' для выхода): ";
- if (key == "EmptyInput")
- return "Пустой ввод. Попробуйте снова.";
- if (key == "WordNotFound")
- return "Слово \"{0}\" не найдено в словаре.";
- if (key == "ProgramEnded")
- return "Программа завершена.";
- if (key == "Border")
- return "======================================";
- if (key == "Header")
- return " ENG ⇄ RUS Dictionary ";
- return string.Empty;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment