2015-05-29 59 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(); 
    } 
相關問題