Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从其他类调用方法?_Java_Methods - Fatal编程技术网

Java 如何从其他类调用方法?

Java 如何从其他类调用方法?,java,methods,Java,Methods,我正在尝试编写一个Java程序来预订酒店房间。我在一个类中编写了方法,并试图从我的主类中调用它,但我不知道如何做 程序要求用户输入方法的必要信息,但我不知道如何执行它 我还想知道当用户输入RoomId并确认他们的预订,然后使用room.java中的print()方法打印信息时,如何将房间的状态更改为“B”-以进行预订。任何帮助都将非常有用。谢谢 Room.java package utilities; public class Room { private String roomId;


我正在尝试编写一个Java程序来预订酒店房间。我在一个类中编写了方法,并试图从我的主类中调用它,但我不知道如何做
程序要求用户输入方法的必要信息,但我不知道如何执行它
我还想知道当用户输入RoomId并确认他们的预订,然后使用room.java中的print()方法打印信息时,如何将房间的状态更改为“B”-以进行预订。

任何帮助都将非常有用。谢谢

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
 */
}
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.
TestRoom.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
 */
}
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.
包装材料;
导入java.util.Scanner;
进口公用设施。客房;
公共课测试室{
公共静态void main(字符串[]args)
{
房间[]房间=
{
新房间(“花园0001”,“西北花园景观”,45.00),
新房间(“GARDEN0002”,“东南花园景观”,65.0),
新房间(“GARDEN0003”,“北园景”,35.00),
新房间(“GARDEN0004”,“南花园视图”,52.0),
新房间(“GARDEN0005”,“西园景”,35.00),
新房间(“GARDEN0006”,“东花园视图”,35.00)
};
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“输入较低的价格范围”);
int lower=userInput.nextInt();
System.out.println(“输入价格上限范围”);
int upper=userInput.nextInt();
如果(低于65)
{
对于(int i=0;i下部和&rooms[i].getDailyRate()<上部)
{    
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”);
}
}
}
其他的
{
System.out.println(“没有符合您标准的房间。”);
}
System.out.println(“输入您想要预订的房间的ID”);
字符串roomId=userInput.next();
System.out.println(“请输入您的客户ID”);
字符串customerID=userInput.next();
System.out.println(“请输入您希望入住的夜数。”);
int nightsRequired=userInput.nextInt();
//我不知道这里有什么。

只需获取
房间的实例
并调用该方法即可

硬编码方法如下所示:

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
}
但是,只要这将是一个更大的应用程序,我建议您编码您的
数组。contains()
将找到
房间

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
}

在测试类的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();
    }
房间r=null;
布尔值=假;

对于(int i=0;i通过整理房间,找到匹配的房间,或将所有房间放入地图,哪个房间id将是一把钥匙。我想这就是你要找的…我试过了,我发现房间不存在。我的错,我认为
roomId
是一个int…在Java中,我们必须将
字符串
始终与
相等进行比较,而不是与
=/code>进行比较。…检查我的编辑