2015-05-29 43 views
-2


我想写一个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

itterrate槽房,最终找到匹配,或者把所有的房间进入地图,哪个房间ID将成为关键。 – user902383

+0

http://stackoverflow.com/questions/7776306/java-calling-a-method-from-another-class我认为这是你寻找... –

回答

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 methodArrays.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 
} 
+0

我试过这个,我得到的房间doesn不存在。 – iReaperX

+0

我的不好,我虽然'roomId'是一个int ... **在Java中,我们必须将'String'总是与'equals'进行比较,而不是'=='** ...检查我的编辑 –

0

添加的代码在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(); 
    } 
相关问题