Files
ProgLab2/Utils/InputHelper.cs
2025-11-13 07:51:17 +04:00

79 lines
2.6 KiB
C#

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");
}
}
}
}
}