using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace WorkTimeTracker { public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); } } public class HomeController : Controller { private readonly Dictionary _employeeTimeRecords = new Dictionary(); public IActionResult Index() { return View(); } [HttpPost] public IActionResult RegisterArrival(string name) { if (!_employeeTimeRecords.ContainsKey(name)) { _employeeTimeRecords[name] = DateTime.Now.TimeOfDay; ViewBag.Message = $"Приход на работу зарегистрирован для сотрудника {name}"; } else { ViewBag.Message = "Ошибка: Сотрудник уже зарегистрирован на работе"; } return View("Index"); } [HttpPost] public IActionResult RegisterDeparture(string name) { if (_employeeTimeRecords.ContainsKey(name)) { TimeSpan arrivalTime = _employeeTimeRecords[name]; TimeSpan workDuration = DateTime.Now.TimeOfDay - arrivalTime; ViewBag.Message = $"Сотрудник {name} отработал {workDuration.TotalHours:F2} часов"; _employeeTimeRecords.Remove(name); } else { ViewBag.Message = "Ошибка: Сотрудник не зарегистрирован на работе"; } return View("Index"); } [HttpPost] public IActionResult DisplayTimeReport() { if (_employeeTimeRecords.Count == 0) { ViewBag.Message = "Нет зарегистрированных сотрудников"; } else { ViewBag.Message = "Отчет по рабочему времени:
"; foreach (var record in _employeeTimeRecords) { ViewBag.Message += $"Сотрудник: {record.Key}, Время прихода: {record.Value}
"; } } return View("Index"); } } public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); }); } }