Загрузка данных
using System;
using System.Collections.Generic;
using System.Linq;
namespace HotelBooking
{
public enum RoomType { Single, Double, Suite, Presidential }
public enum BookingStatus { Pending, Confirmed, Cancelled, Completed }
public class Room
{
public int Number { get; set; }
public RoomType Type { get; set; }
public decimal Price { get; set; }
public List<DateTime> BookedDates { get; set; } = new();
public bool IsAvailable(DateTime start, DateTime end)
{
for (var date = start; date < end; date = date.AddDays(1))
if (BookedDates.Contains(date.Date)) return false;
return true;
}
public override string ToString() => $"№{Number} ({Type}) - {Price:C}/ночь";
}
public class Guest
{
public string Passport { get; set; }
public string Name { get; set; }
public string Contact { get; set; }
public override string ToString() => $"{Name} (Паспорт: {Passport})";
}
public class Booking
{
public int Id { get; set; }
public Guest Guest { get; set; }
public Room Room { get; set; }
public DateTime CheckIn { get; set; }
public DateTime CheckOut { get; set; }
public BookingStatus Status { get; set; } = BookingStatus.Pending;
public decimal CalculateCost()
{
int nights = (int)(CheckOut - CheckIn).TotalDays;
return nights * Room.Price;
}
public bool Cancel()
{
if (DateTime.Now > CheckIn.AddHours(-24)) return false;
Status = BookingStatus.Cancelled;
return true;
}
}
public class Hotel
{
public List<Room> Rooms { get; set; } = new();
public List<Booking> Bookings { get; set; } = new();
public List<Room> FindAvailableRooms(DateTime start, DateTime end)
{
return Rooms.Where(r => r.IsAvailable(start, end)).ToList();
}
public Booking CreateBooking(Guest guest, Room room, DateTime checkIn, DateTime checkOut)
{
if (!room.IsAvailable(checkIn, checkOut)) return null;
var booking = new Booking
{
Id = Bookings.Count + 1,
Guest = guest,
Room = room,
CheckIn = checkIn,
CheckOut = checkOut,
Status = BookingStatus.Confirmed
};
for (var date = checkIn; date < checkOut; date = date.AddDays(1))
room.BookedDates.Add(date.Date);
Bookings.Add(booking);
return booking;
}
public void ShowReport()
{
Console.WriteLine($"Номера: {Rooms.Count}, Брони: {Bookings.Count}");
var today = DateTime.Today;
var occupied = Rooms.Count(r => r.BookedDates.Contains(today));
Console.WriteLine($"Занято сегодня: {occupied}, Свободно: {Rooms.Count - occupied}");
}
}
class Program
{
static void Main()
{
var hotel = new Hotel();
// Добавляем номера
hotel.Rooms.Add(new Room { Number = 101, Type = RoomType.Single, Price = 2500 });
hotel.Rooms.Add(new Room { Number = 102, Type = RoomType.Double, Price = 4000 });
hotel.Rooms.Add(new Room { Number = 103, Type = RoomType.Suite, Price = 8000 });
bool running = true;
while (running)
{
Console.WriteLine("\n1. Номера 2. Поиск 3. Бронировать 4. Отчет 0. Выход");
var choice = Console.ReadLine();
switch (choice)
{
case "1":
hotel.Rooms.ForEach(r => Console.WriteLine(r));
break;
case "2":
Console.Write("Начало (дд.мм.гггг): ");
if (DateTime.TryParse(Console.ReadLine(), out var start))
{
Console.Write("Конец (дд.мм.гггг): ");
if (DateTime.TryParse(Console.ReadLine(), out var end))
{
hotel.FindAvailableRooms(start, end).ForEach(r => Console.WriteLine(r));
}
}
break;
case "3":
var guest = new Guest
{
Passport = "4500112233",
Name = "Иванов Иван",
Contact = "+79111234567"
};
Console.Write("Начало (дд.мм.гггг): ");
if (DateTime.TryParse(Console.ReadLine(), out start))
{
Console.Write("Конец (дд.мм.гггг): ");
if (DateTime.TryParse(Console.ReadLine(), out var end))
{
var rooms = hotel.FindAvailableRooms(start, end);
if (rooms.Any())
{
Console.WriteLine("Доступные номера:");
for (int i = 0; i < rooms.Count; i++)
Console.WriteLine($"{i + 1}. {rooms[i]}");
if (int.TryParse(Console.ReadLine(), out int roomIdx) && roomIdx > 0 && roomIdx <= rooms.Count)
{
var booking = hotel.CreateBooking(guest, rooms[roomIdx - 1], start, end);
Console.WriteLine(booking != null ? $"Бронь #{booking.Id} создана!" : "Ошибка!");
}
}
}
}
break;
case "4":
hotel.ShowReport();
break;
case "0":
running = false;
break;
}
}
}
}
}