分析:
我们需要开发一个饭店的订餐系统,满足以下要求:
-
餐厅桌子配置:
-
预订规则:
- 可以预订未来一个月内的堂食。
- 如果一个桌子有一个位置已被预订,则该桌子不可再被他人预订。
- 预订最少2人,时长最少1小时。
-
营业时间:
- 上午10:00 - 14:00
- 晚上18:00 - 22:00
-
取消预订:
为满足这些需求,需要:
-
建模桌子:
- 使用一个
Table类表示餐桌,包含桌子ID和容量。
-
管理预订:
- 使用一个
Reservation类表示预订信息,包含预订ID、客户姓名、预订时间、持续时间、人数和对应的桌子。
-
管理餐厅:
- 使用一个
Restaurant类,包含所有的桌子和预订列表。
- 提供方法来创建和取消预订。
- 检查桌子在特定时间段内的可用性。
-
验证逻辑:
- 预订时间必须在营业时间内。
- 预订的人数和持续时间必须满足最小要求。
- 预订日期必须在未来一个月内。
- 检查新预订是否与已有预订时间重叠。
示例代码(C#):
using System;
using System.Collections.Generic;
using System.Linq;
public class Table
{
public int TableId { get; set; }
public int Capacity { get; set; }
}
public class Reservation
{
public int ReservationId { get; set; }
public string CustomerName { get; set; }
public DateTime ReservationDateTime { get; set; } // 开始时间
public TimeSpan Duration { get; set; } // 持续时间
public int NumberOfPeople { get; set; }
public Table ReservedTable { get; set; }
}
public class Restaurant
{
private List<Table> tables;
private List<Reservation> reservations;
private int nextReservationId = 1;
public Restaurant()
{
tables = new List<Table>();
reservations = new List<Reservation>();
InitializeTables();
}
private void InitializeTables()
{
// 添加10张6人桌
for (int i = 1; i <= 10; i++)
{
tables.Add(new Table { TableId = i, Capacity = 6 });
}
// 添加15张8人桌
for (int i = 11; i <= 25; i++)
{
tables.Add(new Table { TableId = i, Capacity = 8 });
}
// 添加20张4人桌
for (int i = 26; i <= 45; i++)
{
tables.Add(new Table { TableId = i, Capacity = 4 });
}
}
public Reservation MakeReservation(string customerName, DateTime reservationDateTime, TimeSpan duration, int numberOfPeople)
{
// 验证输入
if (numberOfPeople < 2)
{
throw new Exception("预订人数至少为2人。");
}
if (duration.TotalHours < 1)
{
throw new Exception("预订时长至少为1小时。");
}
if (!IsWithinOperatingHours(reservationDateTime, duration))
{
throw new Exception("预订时间不在营业时间内。");
}
if (reservationDateTime < DateTime.Now)
{
throw new Exception("预订日期和时间必须在未来。");
}
if (reservationDateTime > DateTime.Now.AddMonths(1))
{
throw new Exception("只能预订未来一个月内的日期。");
}
// 查找可用桌子
Table availableTable = FindAvailableTable(reservationDateTime, duration, numberOfPeople);
if (availableTable != null)
{
// 创建预订
var reservation = new Reservation
{
ReservationId = nextReservationId++,
CustomerName = customerName,
ReservationDateTime = reservationDateTime,
Duration = duration,
NumberOfPeople = numberOfPeople,
ReservedTable = availableTable
};
reservations.Add(reservation);
return reservation;
}
else
{
throw new Exception("在所请求的时间没有可用的桌子。");
}
}
public void CancelReservation(int reservationId)
{
var reservation = reservations.FirstOrDefault(r => r.ReservationId == reservationId);
if (reservation != null)
{
reservations.Remove(reservation);
}
else
{
throw new Exception("未找到该预订。");
}
}
private bool IsWithinOperatingHours(DateTime reservationDateTime, TimeSpan duration)
{
var reservationEndTime = reservationDateTime.Add(duration);
var date = reservationDateTime.Date;
// 营业时间为10:00 - 14:00 和 18:00 - 22:00
var morningStart = date.AddHours(10);
var morningEnd = date.AddHours(14);
var eveningStart = date.AddHours(18);
var eveningEnd = date.AddHours(22);
// 检查预订时间是否在任一营业时段内
bool valid = false;
if (reservationDateTime >= morningStart && reservationEndTime <= morningEnd)
{
valid = true;
}
else if (reservationDateTime >= eveningStart && reservationEndTime <= eveningEnd)
{
valid = true;
}
return valid;
}
private Table FindAvailableTable(DateTime reservationDateTime, TimeSpan duration, int numberOfPeople)
{
// 查找可容纳该人数的桌子
var suitableTables = tables.Where(t => t.Capacity >= numberOfPeople).OrderBy(t => t.Capacity).ToList();
foreach (var table in suitableTables)
{
// 检查桌子在所请求时间是否可用
if (IsTableAvailable(table, reservationDateTime, duration))
{
return table;
}
}
return null; // 没有可用桌子
}
private bool IsTableAvailable(Table table, DateTime reservationDateTime, TimeSpan duration)
{
// 获取该桌子的已有预订
var existingReservations = reservations.Where(r => r.ReservedTable.TableId == table.TableId).ToList();
var requestedStart = reservationDateTime;
var requestedEnd = reservationDateTime.Add(duration);
foreach (var reservation in existingReservations)
{
var existingStart = reservation.ReservationDateTime;
var existingEnd = reservation.ReservationDateTime.Add(reservation.Duration);
// 检查时间是否重叠
if (requestedStart < existingEnd && requestedEnd > existingStart)
{
// 时间重叠,桌子不可用
return false;
}
}
// 没有重叠,桌子可用
return true;
}
public void PrintReservations()
{
foreach (var reservation in reservations)
{
Console.WriteLine($"预订ID: {reservation.ReservationId}, 客户: {reservation.CustomerName}, 桌号: {reservation.ReservedTable.TableId}, 人数: {reservation.NumberOfPeople}, 时间: {reservation.ReservationDateTime}, 时长: {reservation.Duration.TotalHours}小时");
}
}
}
// 示例使用
public class Program
{
public static void Main()
{
Restaurant restaurant = new Restaurant();
try
{
// 创建预订
var reservation1 = restaurant.MakeReservation("张三", new DateTime(2023, 11, 5, 12, 0, 0), TimeSpan.FromHours(2), 4);
Console.WriteLine("预订成功!");
restaurant.PrintReservations();
// 试图在同一时间预订同一桌子
var reservation2 = restaurant.MakeReservation("李四", new DateTime(2023, 11, 5, 12, 30, 0), TimeSpan.FromHours(1.5), 4);
Console.WriteLine("预订成功!");
restaurant.PrintReservations();
}
catch (Exception ex)
{
Console.WriteLine("预订失败:" + ex.Message);
}
// 取消预订
try
{
restaurant.CancelReservation(1);
Console.WriteLine("预订已取消。");
restaurant.PrintReservations();
}
catch (Exception ex)
{
Console.WriteLine("取消失败:" + ex.Message);
}
}
}
说明:
- Table类:表示餐桌,包含桌号和容量。
- Reservation类:表示预订信息,包含预订ID、客户姓名、预订时间、持续时间、人数和预订的桌子。
- Restaurant类:
InitializeTables方法初始化餐厅的桌子列表。
MakeReservation方法用于创建预订,首先验证输入,然后查找可用的桌子,最后创建并添加预订。
CancelReservation方法用于取消预订。
IsWithinOperatingHours方法检查预订时间是否在营业时间内。
FindAvailableTable方法根据人数查找可用的桌子。
IsTableAvailable方法检查特定桌子在指定时间是否可用。
PrintReservations方法用于打印当前的预订列表。
注意事项:
- 在实际应用中,还需要考虑并发访问和数据持久化(例如使用数据库存储)。在此示例中,为了简化,我们使用了内存中的列表来存储桌子和预订信息。
- 错误处理通过抛出异常的方式进行,在实际应用中,可以根据需求进行更友好的错误提示或日志记录。
- 可以根据需要扩展系统的功能,例如增加用户接口、支持更多的预订查询条件等。
希望这个示例能帮助您理解如何开发一个满足要求的订餐系统。