Advertisement
AziLif

HealthBar

Dec 11th, 2024 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             const string CommandDrawBar = "1";
  10.             const string CommandExit = "2";
  11.  
  12.             bool isSelecting = true;
  13.             string userInput;
  14.  
  15.             while (isSelecting)
  16.             {
  17.                 Console.WriteLine($"Здравствуйте. В меню два пункта:\n" +
  18.                     $"Нарисовать полосу - {CommandDrawBar}. " +
  19.                     $"\nВыход - {CommandExit}");
  20.  
  21.                 userInput = Console.ReadLine();
  22.  
  23.                 switch (userInput)
  24.                 {
  25.                     case CommandDrawBar:
  26.                         WorkWithBar();
  27.                         break;
  28.  
  29.                     case CommandExit:
  30.                         Console.WriteLine("Досвидания");
  31.                         isSelecting = false;
  32.                         break;
  33.                 }
  34.             }
  35.         }
  36.  
  37.         static void WorkWithBar()
  38.         {
  39.             int value = 0;
  40.             int maxValue = 0;
  41.             int cursorPositionToDisplayMenu = 2;
  42.  
  43.             bool isStripDraw = true;
  44.  
  45.             while (isStripDraw)
  46.             {
  47.                 ReturnData(out value, out maxValue);
  48.  
  49.                 DrawBar(value, maxValue, '_');
  50.  
  51.                 Console.SetCursorPosition(0, cursorPositionToDisplayMenu);
  52.  
  53.                 isStripDraw = false;
  54.             }
  55.         }
  56.  
  57.         static void DrawBar(float value, int maxValue, char symbol = ' ')
  58.         {
  59.             float occupancyPercentage = DetermLengthAndPercentage(value, maxValue); ;
  60.  
  61.             Console.Write('[');
  62.  
  63.             Console.BackgroundColor = ConsoleColor.Green;
  64.  
  65.             if (occupancyPercentage > value)
  66.             {
  67.                 occupancyPercentage = value;
  68.             }
  69.  
  70.             DrawPartBar(0, (int)occupancyPercentage, symbol);
  71.  
  72.             Console.ResetColor();
  73.  
  74.             DrawPartBar(occupancyPercentage, (int)value, symbol);
  75.  
  76.             Console.Write(']');
  77.         }
  78.  
  79.         static float DetermLengthAndPercentage(float value, int maxValue)
  80.         {
  81.             int percent = 100;
  82.  
  83.             float occupancyPercentage = 0;
  84.  
  85.             if (value <= 0 || maxValue <= 0)
  86.             {
  87.                 value = 0;
  88.                 maxValue = 0;
  89.             }
  90.  
  91.             occupancyPercentage = value * maxValue / percent;
  92.  
  93.             return occupancyPercentage;
  94.         }
  95.  
  96.         static void DrawPartBar(float occupancyPercentage, int value, char symbol)
  97.         {
  98.             string bar = "";
  99.  
  100.             for (float i = occupancyPercentage; i < value; i++)
  101.             {
  102.                 bar += symbol;
  103.             }
  104.  
  105.             Console.Write(bar);
  106.         }
  107.  
  108.         static void ReturnData(out int value, out int maxValue)
  109.         {
  110.             string dataRequest = "Сколько всего единиц:";
  111.             string percentageRequest = "Введите процент заполненности:";
  112.  
  113.             value = RequestData(dataRequest);
  114.             maxValue = RequestData(percentageRequest);
  115.  
  116.             Console.Clear();
  117.  
  118.         }
  119.  
  120.         static int RequestData(string info)
  121.         {
  122.             Console.WriteLine(info);
  123.  
  124.             int data = 0;
  125.  
  126.             string userInput = Console.ReadLine();
  127.  
  128.             while (!int.TryParse(userInput, out data))
  129.             {
  130.                 userInput = Console.ReadLine();
  131.             }
  132.  
  133.             return data;
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement