first try
This commit is contained in:
78
Utils/InputHelper.cs
Normal file
78
Utils/InputHelper.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using su.divan2000.SalaryApp.Models;
|
||||
|
||||
namespace su.divan2000.SalaryApp.Utils
|
||||
{
|
||||
public static class InputHelper
|
||||
{
|
||||
public static double AskDouble(string prompt)
|
||||
{
|
||||
double val;
|
||||
while (true)
|
||||
{
|
||||
Console.Write(prompt);
|
||||
if (double.TryParse(Console.ReadLine(), out val))
|
||||
return val;
|
||||
|
||||
Console.WriteLine("Некорректный ввод. Введите число в формате 1.0, 2.5 и т.д.");
|
||||
}
|
||||
}
|
||||
|
||||
public static int AskInt(string prompt)
|
||||
{
|
||||
int val;
|
||||
while (true)
|
||||
{
|
||||
Console.Write(prompt);
|
||||
if (int.TryParse(Console.ReadLine(), out val))
|
||||
return val;
|
||||
|
||||
Console.WriteLine("Некорректный ввод. Введите целое число.");
|
||||
}
|
||||
}
|
||||
|
||||
public static bool AskYesNo(string prompt)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Console.Write(prompt);
|
||||
string? s = Console.ReadLine()?.Trim().ToLower();
|
||||
if (s == "да") return true;
|
||||
if (s == "нет") return false;
|
||||
Console.WriteLine("Введите 'да' или 'нет'.");
|
||||
}
|
||||
}
|
||||
|
||||
public static SalaryData CreateSalaryData()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
double salary = AskDouble("Введите оклад: ");
|
||||
int days = AskInt("Введите количество рабочих дней: ");
|
||||
int night = AskInt("Введите количество ночных смен: ");
|
||||
int overtime = AskInt("Введите количество сверхурочных часов: ");
|
||||
int exp = AskInt("Введите стаж (лет): ");
|
||||
bool viol = AskYesNo("Были нарушения? (да/нет): ");
|
||||
|
||||
SalaryData data = new SalaryData(
|
||||
salary,
|
||||
days,
|
||||
night,
|
||||
overtime,
|
||||
exp,
|
||||
viol
|
||||
);
|
||||
|
||||
return data;
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
Console.WriteLine($"Ошибка ввода: {ex.Message}");
|
||||
Console.WriteLine("Попробуйте ещё раз.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Utils/OutputHelper.cs
Normal file
20
Utils/OutputHelper.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using su.divan2000.SalaryApp.Models;
|
||||
|
||||
namespace su.divan2000.SalaryApp.Utils
|
||||
{
|
||||
public static class OutputHelper
|
||||
{
|
||||
public static void PrintSalaryData(SalaryData data)
|
||||
{
|
||||
Console.WriteLine("\n=== Результат ===");
|
||||
Console.WriteLine(data);
|
||||
}
|
||||
|
||||
public static void WaitForKey(string message = "Нажмите любую клавишу, чтобы продолжить...")
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
100
Utils/TUI/Menu.cs
Normal file
100
Utils/TUI/Menu.cs
Normal file
@@ -0,0 +1,100 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace su.divan2000.SalaryApp.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// TUI menu;
|
||||
/// </summary>
|
||||
internal class Menu
|
||||
{
|
||||
private List<Option> options;
|
||||
private int selected;
|
||||
private string title;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor of menu;
|
||||
/// </summary>
|
||||
/// <param name="title">Title of menu</param>
|
||||
public Menu(string title)
|
||||
{
|
||||
this.title = title;
|
||||
this.options = new List<Option> { };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add option to menu;
|
||||
/// </summary>
|
||||
/// <param name="name">Name of option</param>
|
||||
/// <param name="action">Action, runs if option selected</param>
|
||||
public void AddOption(string name, Action action)
|
||||
{
|
||||
this.options.Add(new Option(name, action));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Run menu;
|
||||
/// Console will be clean!;
|
||||
/// </summary>
|
||||
public void RunMenu()
|
||||
{
|
||||
selected = 0;
|
||||
this.PrintMenu();
|
||||
ConsoleKeyInfo keyinfo;
|
||||
do
|
||||
{
|
||||
keyinfo = Console.ReadKey();
|
||||
|
||||
if (keyinfo.Key == ConsoleKey.DownArrow)
|
||||
{
|
||||
if (selected + 1 < options.Count)
|
||||
{
|
||||
selected++;
|
||||
this.PrintMenu();
|
||||
}
|
||||
}
|
||||
if (keyinfo.Key == ConsoleKey.UpArrow)
|
||||
{
|
||||
if (selected > 0)
|
||||
{
|
||||
selected--;
|
||||
this.PrintMenu();
|
||||
}
|
||||
}
|
||||
if (keyinfo.Key == ConsoleKey.Enter)
|
||||
{
|
||||
Console.Clear();
|
||||
options[selected].Action.Invoke();
|
||||
}
|
||||
} while (keyinfo.Key != ConsoleKey.Enter);
|
||||
}
|
||||
private void PrintMenu()
|
||||
{
|
||||
Console.Clear();
|
||||
Console.WriteLine(title);
|
||||
int optionIndex = 0;
|
||||
foreach (Option option in options)
|
||||
{
|
||||
string pointer = optionIndex == selected ? " ->" : " ";
|
||||
Console.WriteLine($"{pointer}{option.Name}");
|
||||
optionIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
private struct Option
|
||||
{
|
||||
public string Name { get; }
|
||||
public Action Action { get; }
|
||||
|
||||
public Option(string name, Action action)
|
||||
{
|
||||
Name = name;
|
||||
Action = action;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user