Java 设计问题:预订系统
我必须设计并实现一个酒店预订系统。我有Java 设计问题:预订系统,java,Java,我必须设计并实现一个酒店预订系统。我有 保留对象的数组列表 房间对象的数组列表 我想在指定日期“预订”房间 预订一个没有日期的房间是很容易的,但是日期部分使它复杂化了。我正在努力设计它,并且有信心在正确的方向上推动它,我可以编写代码 你怎么说一个房间是在这个日期预订的,而不是在那个日期 没有数据库或任何东西,它只是一个抽象的预订系统 (我已经盯着这个看了一段时间,如果解决方法简单,请原谅我) 谢谢。编辑--仔细想想,为什么不让每个房间都有一个预订实例的列表,这些实例又有开始/结束属性,可以告
- 保留对象的数组列表
- 房间对象的数组列表
房间都有一个预订
实例的列表,这些实例又有开始/结束属性,可以告诉您预订发生的时间
这样,要判断一个房间是否有一个特定时间的预订,您只需在房间的预订中循环,查看所讨论的时间是否在任何预订的开始/结束范围内……尽管代码实现起来不太容易(也不太难),但这是基本思想 想想看,房间是一种资源,你可以在给定的时间范围内保留。您还有几个房间可以单独预订。您将如何实施以下方法:
class Room {
boolean isAvailable(Date date) {/*...*/}
Date nextAvailableDate() {/*...*/}
}
提示:房间必须了解预订情况
你的问题不清楚预订的目的是什么。它是否只包含日期范围,还是指定给特定的房间?如果是前者,您可能会遇到为新预订找到一个免费房间的问题-这可以通过循环所有房间并使用上述方法轻松实现
如果是后者:既然预订知道房间,房间也可能知道预订。因此,通过迭代预订来实现上述方法非常简单。创建三个类(Hotel
,Room
&Reservation
):
Reservation
对象在这里像发票一样使用,并且与预订过程保持解耦
- 每个
房间
对象(专用于酒店中的每个房间号)都包含一个地图
,该地图将预订数据
存储为键,将预订对象
存储为值
酒店
由房间组成
。对于每个预订请求,Hotel
循环查看room
列表,然后每个room
遍历其自己的地图
,以确定在请求的天数内是否可以预订
请注意,预订功能将日期作为列表,而不仅仅是两个日期(startDate和endDate)。这和前者可以从后者派生出来是一样的
示例代码如下所示:
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;
}
}
class酒店{
私有字符串名称、地址;
私有列表roomList;//键:roomNumber
公共酒店(){
roomList=新的ArrayList();
}
公共预订bookRoomForDates(列出日期列表,客人){
用于(房间:房间列表){
预订=房间。预订日期(日期列表,客人);
if(reserved!=null)返回reservation;//reservation successFull!
}
返回null;//预约失败!
}
}
班级预定{
私有字符串id;
私有日期为inDate,过期;
私人客人;
公共预订(客房、国际起始日期、国际结束日期、客人){
//填充成员变量。
}
}
教室{
私有字符串id;
私人内部房间号,楼层;
私有映射reservedDates;//键:日期为整数(YYYYMMDD)
公共房间(国际房间号){
reservedDates=新HashMap();
this.roomNumber=roomNumber;
}
//日期列表中日期(YYYYMMDD格式)的客人预订请求
公共预订预订日期(列出日期列表,客人)
{
if(dateList.isEmpty())返回null;
对于(整数日期:日期列表){
Reservation res=reservedDates.get(日期);
如果(res!=null){//我们不会为房间的无保留日期存储null值。
return null;//另一位客人在此日期预订了房间。因此,此房间不可用。
}
}
//这个房间在所有要求的日期都没有预订。所以继续预订这个房间
int startDate=dateList.get(0);
int endDate=dateList.get(dateList.size()-1);
预订newReservation=新预订(this,startDate,endDate,guest);
对于(整数日期:日期列表){
reservedDates.put(日期,新预订);
}
返回新预订;
}
}
}如果这是一个家庭作业,请给它贴上相应的标签。我想你也会发现这很有用:啊,这是个好主意。我在想一个新的类或什么的,但一个房间,存储它的预订以及其他数据,如它的价格等,是一个伟大的想法。编辑:我喜欢这个网站。我不需要明确要求代码或任何东西,当我编写解决方案时,我仍然可以感觉良好,我被暗示方向正确,而不是被告知。@shane,很酷--请注意,在这种结构下搜索开放预订可能会很棘手--您需要搜索每个房间以找到一个开放的位置。我很感激Saurav,但这个问题(从我的学生时代开始)已经有5年了,所以考虑到我现在已经毕业,我不再需要帮助:)欢迎来到Stackoverflow。在回答问题时,最好对代码进行简要解释,或在代码的相关部分添加描述性注释,而不仅仅是发布一大块代码。
import java.util.*;
public class Final_Project {
public static void main(String[] args) {
Scanner ScanObj = new Scanner(System.in);
String choice="";
int a=1;
int x;
int stay=0;
int totalBill;
String name="";
String num="";
String checkIn="";
String checkOut="";
System.out.println("Kindly Fillup");
Date d = new Date();
System.out.print("Name:");
name = ScanObj.nextLine();
System.out.print("Phone Number:");
num =ScanObj.nextLine();
System.out.print("Check-in: ");
checkIn = ScanObj.nextLine();
System.out.print("Check-Out: ");
checkOut = ScanObj.nextLine();
System.out.println("Hello," + name + ". Welcome to Kimberly Hotel");
do{
try{
for(x=1; x==1;) {
System.out.println("[1] Room Reservations");
for(x=1; x==1;) {
System.out.print("ENTER : ");
choice=ScanObj.nextLine();
if (choice.equals("1")) {
System.out.println(" Rooms");
System.out.println("*********************************");
System.out.println("1.)Room 101 | Php 10,000 |5 Persons");
System.out.println("2.)Room 102 | Php 8,000 |4 Persons");
System.out.println("3.)Room 103 | Php 6,500 |3 Persons");
System.out.println("4.)Room 104 | Php 5,000 |2 Persons");
System.out.println("5.)Room 105 | Php 5,000 |2 Persons");
x=0;
}
else
throw new InputMismatchException("");
a=2;
}
}
}catch(Exception e) {
System.out.println("Invalid Input. Please try again.");
}
}while(a==1);
int choices;
int control=1;
do{
try{
System.out.print("Please select a room: ");
choices = ScanObj.nextInt();
System.out.print("How long will you stay: ");
stay = ScanObj.nextInt();
switch(choices)
{
case 1:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println(name+" |"+num+"|1 Room 101 | Php 10,000 | 5 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
totalBill = stay * 10000;
System.out.println("Your total bill is: " + totalBill);
System.out.println("Additional 2 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
case 2:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println("Here is your reservation: ");
System.out.println(name+" |"+num+"| Room 102 | Php 8,000 | 4 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
totalBill = stay * 8000;
System.out.println("Your total bill is: " + totalBill);
System.out.println("Additional 2 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
case 3:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println("Here is your reservation: ");
System.out.println(name+" |"+num+"| Room 103 | Php 6,500 | 3 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
totalBill = stay * 8000;
System.out.println("Your total bill is: " + totalBill);
System.out.println("Additional 2 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
case 4:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println("Here is your reservation: ");
System.out.println(name+" |"+num+"| Room 104 | Php 5,000 | 2 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
totalBill = stay * 8000;
System.out.println("Your total bill is: " + totalBill);
System.out.println("Additional 1 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
case 5:
System.out.println("Here is your reservation as of: " + d.toString());
System.out.println("Here is your reservation: ");
System.out.println(name+" |"+num+"| Room 105 | Php 5,000 | 2 Persons|");
System.out.println("Check-in Date: " + checkIn);
System.out.println("Check-Out Date: " + checkOut);
System.out.println("Additional 1 beddings");
System.out.println("Free Breakfast");
System.out.println("No pets allowed!");
System.out.println("Thank You!");
break;
default:
System.out.println("Number"+" "+choices + " is not given. Please select within the given rooms.");
throw new InputMismatchException("");
}
control=2;
}catch(InputMismatchException ex) {
}
}while(control==1);
System.out.println("");
int choicess;
boolean b=true;
do{
try{
System.out.println("[1] CANCEL");
System.out.println("[2] RESERVE");
System.out.println("Select from above: ");
choicess=ScanObj.nextInt();
switch(choicess)
{
case 1:
System.out.println("Cancellation Done Thanks!");
break;
case 2:
System.out.print("Your hotel room has been successfully reserved. Thank you!");
break;
default:
System.out.println("Error. Please select within the given numbers.");
System.out.println("Thank you!");
throw new InputMismatchException("");
}
b=false;
}catch(Exception exx) {
System.out.println("");
}
}while(b!=false);
}