Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string CommandDrawBar = "1";
- const string CommandExit = "2";
- bool isSelecting = true;
- string userInput;
- while (isSelecting)
- {
- Console.WriteLine($"Здравствуйте. В меню два пункта:\n" +
- $"Нарисовать полосу - {CommandDrawBar}. " +
- $"\nВыход - {CommandExit}");
- userInput = Console.ReadLine();
- switch (userInput)
- {
- case CommandDrawBar:
- WorkWithBar();
- break;
- case CommandExit:
- Console.WriteLine("Досвидания");
- isSelecting = false;
- break;
- }
- }
- }
- static void WorkWithBar()
- {
- int value = 0;
- int maxValue = 0;
- int cursorPositionToDisplayMenu = 2;
- bool isStripDraw = true;
- while (isStripDraw)
- {
- ReturnData(out value, out maxValue);
- DrawBar(value, maxValue, '_');
- Console.SetCursorPosition(0, cursorPositionToDisplayMenu);
- isStripDraw = false;
- }
- }
- static void DrawBar(float value, int maxValue, char symbol = ' ')
- {
- float occupancyPercentage = DetermLengthAndPercentage(value, maxValue); ;
- Console.Write('[');
- Console.BackgroundColor = ConsoleColor.Green;
- if (occupancyPercentage > value)
- {
- occupancyPercentage = value;
- }
- DrawPartBar(0, (int)occupancyPercentage, symbol);
- Console.ResetColor();
- DrawPartBar(occupancyPercentage, (int)value, symbol);
- Console.Write(']');
- }
- static float DetermLengthAndPercentage(float value, int maxValue)
- {
- int percent = 100;
- float occupancyPercentage = 0;
- if (value <= 0 || maxValue <= 0)
- {
- value = 0;
- maxValue = 0;
- }
- occupancyPercentage = value * maxValue / percent;
- return occupancyPercentage;
- }
- static void DrawPartBar(float occupancyPercentage, int value, char symbol)
- {
- string bar = "";
- for (float i = occupancyPercentage; i < value; i++)
- {
- bar += symbol;
- }
- Console.Write(bar);
- }
- static void ReturnData(out int value, out int maxValue)
- {
- string dataRequest = "Сколько всего единиц:";
- string percentageRequest = "Введите процент заполненности:";
- value = RequestData(dataRequest);
- maxValue = RequestData(percentageRequest);
- Console.Clear();
- }
- static int RequestData(string info)
- {
- Console.WriteLine(info);
- int data = 0;
- string userInput = Console.ReadLine();
- while (!int.TryParse(userInput, out data))
- {
- userInput = Console.ReadLine();
- }
- return data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement