Advertisement
yakovmonarh

Решение с использованием ООП

Aug 13th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace source
  7. {
  8.     class Program
  9.     {
  10.         public static void Main(string[] args)
  11.         {  
  12.             var objCommand = new List<SetName>
  13.             {
  14.                 new SetName("SetName"),
  15.                 new DisplayName("DisplayName"),
  16.                 new SetColor("SetColor"),
  17.                 new DicplayBoxWithName("DicplayBoxWithName")
  18.             };
  19.            
  20.             while(true)
  21.             {
  22.                 Console.Clear();
  23.                 Console.WriteLine("Введите команду:");
  24.                 GetCommand(objCommand);
  25.                 CommandExecution(Console.ReadLine(), objCommand);
  26.                 Console.ReadLine();
  27.             }
  28.         }
  29.        
  30.         static void GetCommand(List<SetName> objComm)
  31.         {
  32.             for(int i = 0; i < objComm.Count; i++)
  33.             {
  34.                 Console.WriteLine(objComm[i].NameCommand);
  35.             }
  36.             Console.WriteLine(Environment.NewLine);
  37.         }
  38.        
  39.         static void CommandExecution(string command, List<SetName> objcomm)
  40.         {
  41.             foreach(var obj in objcomm)
  42.             {
  43.                 if(obj.NameCommand == command)
  44.                 {
  45.                     obj.Action();
  46.                     return;
  47.                 }
  48.             }
  49.             Console.WriteLine("Такой команды нет.");
  50.         }
  51.     }
  52.    
  53.     class SetName
  54.     {
  55.         public SetName(string namecommand)
  56.         {
  57.             NameCommand = namecommand;
  58.             Name = "Иван";
  59.         }
  60.         public string NameCommand{get;set;}
  61.         public string Name{get;set;}
  62.         public virtual void Action()
  63.         {
  64.             Console.WriteLine("Введите имя:");
  65.             this.Name = Console.ReadLine();
  66.         }
  67.     }
  68.    
  69.     class DisplayName: SetName
  70.     {
  71.         public DisplayName(string namecommand): base(namecommand)
  72.         {
  73.             base.NameCommand = namecommand;
  74.         }
  75.         public int CountName{get;set;}
  76.         public override void Action()
  77.         {
  78.             Console.WriteLine("Введите целое число:");
  79.             this.CountName = InputNumber();
  80.             for(int i = 0; i < this.CountName; i++)
  81.             {
  82.                 Console.WriteLine(Name);
  83.             }
  84.         }
  85.        
  86.         static public int InputNumber()
  87.         {
  88.             int userNumber;
  89.             while(int.TryParse(Console.ReadLine(), out userNumber) == false)
  90.             {
  91.                 Console.WriteLine("Ошибка ввода! Попробуйте еще раз.");
  92.             }
  93.             return userNumber;
  94.         }
  95.     }
  96.    
  97.     class SetColor: SetName
  98.     {
  99.         public SetColor(string namecommand): base(namecommand)
  100.         {
  101.             base.NameCommand = namecommand;
  102.         }
  103.         public override void Action()
  104.         {
  105.             Console.WriteLine("Выберите цифру цвета:");
  106.             var colors = Enum.GetValues(typeof(ConsoleColor));
  107.             for(int i = 0; i < colors.Length; i++)
  108.             {
  109.                 Console.WriteLine("Цвет - {0}; Команда - {1}", colors.GetValue(i), i);
  110.             }
  111.             Console.WriteLine("Цвет фона:");
  112.             Console.BackgroundColor = (ConsoleColor)colors.GetValue(DisplayName.InputNumber());
  113.            
  114.             Console.WriteLine("Цвет текста:");
  115.             Console.ForegroundColor = (ConsoleColor)colors.GetValue(DisplayName.InputNumber());
  116.         }
  117.     }
  118.    
  119.     class DicplayBoxWithName: SetName
  120.     {
  121.         public DicplayBoxWithName(string namecommand): base(namecommand)
  122.         {
  123.             base.NameCommand = namecommand;
  124.         }
  125.         public override void Action()
  126.         {
  127.             Console.WriteLine("Установите размер стороны квадрата: ");
  128.             int sizeSquare = DisplayName.InputNumber();
  129.             for(int i = 0; i < sizeSquare; i++)
  130.             {
  131.                 if(i == 0 || i == sizeSquare-1)
  132.                 {
  133.                     Console.WriteLine(new String('#', sizeSquare));
  134.                 }
  135.                 else
  136.                 {
  137.                     Char[] mass = Name.ToCharArray();
  138.                     if(sizeSquare/2 == i)
  139.                     {
  140.                         Console.WriteLine("#" + new String(mass, 0, mass.Length) + new String(' ', sizeSquare-2-mass.Length) + "#");
  141.                     }
  142.                     else
  143.                     {
  144.                         Console.WriteLine("#" + new String(' ', sizeSquare - 2) + "#");
  145.                     }
  146.                    
  147.                 }
  148.             }
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement