gpt4 book ai didi

java - 设计问题 : Reservation system

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:13:31 27 4
gpt4 key购买 nike

我必须为酒店设计和实现预订系统。我有

  • 预订对象的数组列表
  • 房间对象的数组列表

我想在指定日期“预订”房间。

预订没有日期的房间很容易,但日期部分却让事情变得复杂。我正在为此设计而苦苦挣扎,并且有信心朝着正确的方向轻推我可以对其进行编码。

怎么说一个房间在这个日期被预订但那个日期没有?

没有数据库或任何东西,它只是一个抽象的预订系统。

(我已经盯着这个看了一段时间,如果解决方案很简单,请原谅我)

谢谢。

最佳答案

创建三个类(HotelRoom & Reservation):

  • Reservation 对象在这里用作发票,并且与预订流程分离

  • 每个 Room 对象(专用于酒店中的每个房间号)都包含一个 map,它存储 reservedDates 作为键和 reservationObject 作为值。

  • 酒店房间组成。对于每个预订请求,Hotel 循环遍历 room 列表,每个 room 遍历自己的 map 查找是否可以预订要求 天。

请注意,预订功能将日期作为列表,而不仅仅是两个日期(如开始日期和结束日期)。这与前者可以从后者推导出来是一样的。

示例代码如下:

class Hotel {
private String name, address;
private List<Room> roomList; //key : roomNumber
public Hotel(){
roomList = new ArrayList<Room>();
}
public Reservation bookRoomForDates(List<Integer> dateList, Guest guest){
for(Room room : roomList){
Reservation reservation = room.bookForGivenDates(dateList, guest);
if(reserved != null) return reservation; //Reservation successFull!
}
return null; //Reservation failed!
}
}
class Reservation {
private String id;
private Date inDate, outDate;
private Guest guest;

public Reservation(Room room, int startDate, int endDate, Guest guest){
//populate the member variables.
}
}

class Room {
private String id;
private int roomNumber, floorNum;
private Map<Integer, Reservation> reservedDates; // key : date as Integer (YYYYMMDD)

public Room(int roomNumber){
reservedDates = new HashMap<Integer, Reservation>();
this.roomNumber = roomNumber;
}

//A guest request for booking on dates(in YYYYMMDD format) in the dateList
public Reservation bookForGivenDates(List<Integer> dateList, Guest guest)
{
if(dateList.isEmpty()) return null;

for(Integer date : dateList){
Reservation res = reservedDates.get(date);
if(res != null) { // We don't store null value for unreserved dates for the room.
return null; // Room is reserved on this date by another guest. So, this room is unavailable.
}
}
//this room is unreserved on all requested dates. So go on and reserve this room for asked dates
int startDate = dateList.get(0);
int endDate = dateList.get(dateList.size() - 1);

Reservation newReservation = new Reservation(this, startDate, endDate, guest);
for(Integer date : dateList){
reservedDates.put(date, newReservation);
}
return newReservation;
}
}

关于java - 设计问题 : Reservation system,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8113070/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com