我想写一个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.
itterrate槽房,最终找到匹配,或者把所有的房间进入地图,哪个房间ID将成为关键。 – user902383
http://stackoverflow.com/questions/7776306/java-calling-a-method-from-another-class我认为这是你寻找... –