我有一个带有主要实体的出租车管理系统
public class Cab {
private int cabID;
private String registrationNumber;
private String model;
private int driverID;
// Constructors, getters, and setters
}
public class Booking {
private int bookingID;
private int userID;
private int cabID;
private Date date;
private String pickupLocation;
private String dropOffLocation;
private String status;
// Constructors, getters, and setters
}
cab 和 booking 具有一对多的单向关系
我有一个方法来检查可用的出租车
方法 1:加载所有预订,按日期过滤并列出所有可用出租车
private boolean isCabAvailable(Cab cab, Date date) {
// Get all bookings for the given cab
List<Booking> bookings = getBookingsForCab(cab);
// Check if any of the bookings overlap with the given date
for (Booking booking : bookings) {
if (booking.getDate().equals(date)) {
return false; // Cab is not available on the given date
}
}
return true; // Cab is available on the given date
}
缺点 从巨大的预订表中加载所有预订 它不会考虑尚未预订过的出租车
方法 2:加载所有出租车,加载所有出租车预订,按日期过滤
List<Booking> bookings = getBookingsForCab(cab);
// Check if any of the bookings overlap with the given date range
for (Booking booking : bookings) {
// Check if the booking overlaps with the given date range
if (bookingOverlap(booking, startDate, endDate)) {
return false; // Cab is not available for the entire date range
}
}
缺点
我正在寻找建议来更好地设计我的实体关系,以便从表中加载较小的数据
您可以将
date
传递给 getBookingsForCab
方法吗? getBookingsForCab(cab, date)
。您的 postgresql 查询可以过滤该日期的预订,以返回最小结果集。在 date
字段上建立索引,它应该是瞬时的