Advertisement
sergezhu

Untitled

Apr 23rd, 2023
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. namespace ConsoleApp1;
  2.  
  3. using System.Text;
  4.  
  5. public class Task13
  6. {
  7.     const string SetNameCommand = "SetName";
  8.     const string ChangeConsoleColorCommand = "ChangeConsoleColor";
  9.     const string SetPasswordCommand = "SetPassword";
  10.     const string WriteNameCommand = "WriteName";
  11.     const string WritePasswordCommand = "WritePassword";
  12.     const string EscapeCommand = "Escape";
  13.    
  14.     public void Run()
  15.     {
  16.         Console.InputEncoding = Encoding.Unicode;
  17.         Console.OutputEncoding = Encoding.Unicode;
  18.  
  19.         string name = string.Empty;
  20.         string password = string.Empty;
  21.         bool canExit = false;
  22.         string concatedCommandsNames = $"\n{SetNameCommand}, \n{ChangeConsoleColorCommand}, \n{SetPasswordCommand}, \n{WriteNameCommand}, " +
  23.                                        $"\n{WritePasswordCommand}, \n{EscapeCommand}";
  24.  
  25.         while ( canExit == false )
  26.         {
  27.             Console.WriteLine($"All commands: {concatedCommandsNames} \n\nEnter command:");
  28.  
  29.             string enteredCommand = Console.ReadLine();
  30.            
  31.             switch ( enteredCommand )
  32.             {
  33.                 case SetNameCommand:
  34.                     Console.Write("Input name : ");
  35.                     name = Console.ReadLine();
  36.                     break;
  37.  
  38.                 case ChangeConsoleColorCommand:
  39.                     Console.Write( "Input color : " );
  40.                     string consoleColor = Console.ReadLine();
  41.                     TrySetConsoleColor( consoleColor );
  42.                     break;
  43.  
  44.                 case SetPasswordCommand:
  45.                     Console.Write( "Input password : " );
  46.                     password = Console.ReadLine();
  47.                     break;
  48.  
  49.                 case WriteNameCommand:
  50.                     Console.WriteLine($"Current name is {name}");
  51.                     break;
  52.                
  53.                 case WritePasswordCommand:
  54.                     Console.WriteLine( $"Current password is {password}" );
  55.                     break;
  56.  
  57.                 case EscapeCommand:
  58.                     canExit = true;
  59.                     break;
  60.                
  61.                 default:
  62.                     Console.WriteLine( $"Such command is not valid" );
  63.                     break;
  64.             }
  65.         }
  66.     }
  67.  
  68.     private void TrySetConsoleColor(string inputColor)
  69.     {
  70.         string[] colorNames = Enum.GetNames<ConsoleColor>();
  71.         ConsoleColor[] colorValues = Enum.GetValues<ConsoleColor>();
  72.  
  73.         if ( colorNames.Contains( inputColor ) )
  74.         {
  75.             int colorIndex = colorNames.ToList().FindIndex( color => string.Equals( color, inputColor ) );
  76.             Console.ForegroundColor = colorValues[colorIndex];
  77.            
  78.             Console.WriteLine("Console color changed successfully");
  79.         }
  80.         else
  81.         {
  82.             Console.WriteLine( $"Entered color {inputColor} is invalid" );
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement