我正在尝试编写一个Java程序来预订酒店房间。我在一个班级写过方法,我试图从我的班级打电话给我,但我不知道怎么做。
该程序要求用户输入方法的必要信息,但我不知道如何执行它。
我也想知道如何将房间的状态改为" B" - 用于预订,当用户进入RoomId并确认他们的预订,然后使用Room.java中的print()方法打印信息时。
任何帮助都会很棒。谢谢
的 Room.java
package utilities;
public class Room
{
private String roomId;
private String description;
private String status;
private double dailyRate;
private DateTime bookingStartDate;
private DateTime bookingEndDate;
public Room(String roomId, String description, double dailyRate)
{
this.setRoomId(roomId);
this.setDescription(description);
this.setDailyRate(dailyRate);
this.status = "A";
}
public boolean bookRoom (String customerID, int nightsRequired)
{
if (status.equals('A'))
{
System.out.println("You have booked the room");
status = "B";
return true;
}
else
{
System.out.println("This room cannot be booked");
return false;
}
}
public void print()
{
System.out.println("Room ID: " + getRoomId());
System.out.println("Description: "+ getDescription());
System.out.println("Status: " + status);
System.out.println("Daily Rate: " + getDailyRate());
if (status.equals('B'))
{
System.out.println(bookingStartDate);
System.out.println(bookingEndDate);
}
else{
}
}
/**
* @return the dailyRate
*/
public double getDailyRate() {
return dailyRate;
}
/**
* @param dailyRate the dailyRate to set
*/
public void setDailyRate(double dailyRate) {
this.dailyRate = dailyRate;
}
/**
* @return the description
*/
public String getDescription() {
return description;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the roomId
*/
public String getRoomId() {
return roomId;
}
/**
* @param roomId the roomId to set
*/
}
TestRoom.java
package stuff;
import java.util.Scanner;
import utilities.Room;
public class TestRoom {
public static void main(String[] args)
{
Room [] rooms =
{
new Room("GARDEN0001", "NorthWest Garden View", 45.00),
new Room("GARDEN0002", "SouthEast Garden View", 65.0),
new Room("GARDEN0003", "North Garden View", 35.00),
new Room("GARDEN0004", "South Garden View", 52.0),
new Room("GARDEN0005", "West Garden View", 35.00),
new Room("GARDEN0006", "East Garden View", 35.00)
};
Scanner userInput = new Scanner(System.in);
System.out.println("Input a lower price range. ");
int lower = userInput.nextInt();
System.out.println("Input an upper price range. ");
int upper = userInput.nextInt();
if (lower < 65)
{
for ( int i = 0; i < rooms.length; i++ )
{
if (rooms[i].getDailyRate() > lower && rooms[i].getDailyRate() < upper )
{
System.out.println("ID: \t" + rooms[i].getRoomId() );
System.out.println("DESC: \t" + rooms[i].getDescription() );
System.out.println("RATE: \t" + rooms[i].getDailyRate() + "\n");
}
}
}
else
{
System.out.println("There are no rooms that fit your criteria.");
}
System.out.println("Input the ID of the room you wish to book.");
String roomId = userInput.next();
System.out.println("Please enter your customer ID");
String customerID = userInput.next();
System.out.println("Please enter the amount of nights you wish to stay.");
int nightsRequired = userInput.nextInt();
// Code to actually make the booking. I don't know what goes here.
答案 0 :(得分:1)
只需获取Room
的实例并调用方法。
硬编码方法如下:
Room toBook = null;
for (Room r : rooms) {
if (r.getRoomId().equals(roomId))
toBook = r;
}
if (toBook != null) {
if (toBook.bookRoom(customerID, nightsRequired)) {
// room booked succesfully
} else {
// can't book the room
}
} else {
// room does not exists
}
但是,只要这是一个更大的应用程序,我建议您对own equals method进行编码,然后Arrays.contains()
会找到Room
。
Room toBook = new Room(roomid);
if (rooms.contains(toBook) && toBook.bookRoom(customerID, nightsRequired)){
// room booked succesfully
} else if (rooms.contains(toBook)) {
// can't book the room
} else {
// room does not exists
}
答案 1 :(得分:0)
在Test class
中的main()方法的末尾添加代码 Room r=null;
boolean val=false;
for(int i=0;i<rooms.length;i++){
if(rooms[i].getRoomId().equals(roomId)){
r=new Room(rooms[i].getRoomId(), rooms[i].getDescription(), rooms[i].getDailyRate());
r.bookRoom(customerID, nightsRequired);
val=true;
break;
}
}
if(val){
r.print();
}