Java 房间预订计划
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33035213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Room Booking Program
提问by beginner
I am new to Java and I try to write a simple room booking program.
我是 Java 新手,我尝试编写一个简单的房间预订程序。
Here is my plan:
这是我的计划:
There will be a class called Booking. It will ask the user to enter room ID to book a room and the user can press x to terminate the program.
将有一个名为 Booking 的课程。它将要求用户输入房间 ID 来预订房间,用户可以按 x 终止程序。
I would like to have another class called RoomDB. I will use arraylist to store the room ID.
我想要另一个名为 RoomDB 的类。我将使用 arraylist 来存储房间 ID。
I guess I also need constructor, so I have another class called Room.
我想我还需要构造函数,所以我有另一个名为 Room 的类。
My idea is when the program run, it will display the available room ID for user to choose. If the user enters the incorrect room ID, the program will show the error message and ask the user to enter again.
我的想法是当程序运行时,它会显示可用的房间 ID 供用户选择。如果用户输入错误的房间 ID,程序将显示错误消息并要求用户再次输入。
If the user enters a correct room ID and the room ID is available, it will display a success message to the user.
如果用户输入正确的房间 ID 且房间 ID 可用,则会向用户显示成功消息。
If the user enters a correct room ID and the room ID is already booked, it will ask the user to enter again.
如果用户输入了正确的房间号并且房间号已经被预订,它会要求用户再次输入。
That's all for the idea and I begin to write the code.
这就是所有的想法,我开始编写代码。
Here is my code for the three classes
这是我的三个类的代码
Booking.java
预订.java
package test;
import java.util.Scanner;
public class Booking
{
static RoomDB roomDB = new RoomDB();
public static void main(String[] args) {
String roomID;
Room room;
Scanner inputID = new Scanner(System.in);
while(true){
System.out.println("Please choose the room you want to book \n"+getRoomList(roomDB));
while(true){
roomID = inputID.nextLine();
if(roomID.equals ("x")){
break;
}
if(getRoom(roomID) == null){
System.out.println("The room ID is incorrect, please enter again or enter x to quit");
}
else{
room = getRoom(roomID);
if(!room.isBooked()){
System.out.println("Book successfully");
break;
}
else{
System.out.println("please enter the room ID again or enter x to quit");
}
}
}
}
}
public static String getRoomList(RoomDB roomDB)
{
String roomList = "";
for(Room r:roomDB.getRoom())
{
if(!r.isBooked())
{
roomList = roomList+r.getRoomID()+" is free"+" ";
}
else
{
roomList = roomList+r.getRoomID()+" has been booked ";
}
}
return roomList;
}
public static Room getRoom(String roomID){
for(Room r:roomDB.getRoom()){
if(r.getRoomID()==roomID){
return r;
}
}
return null;
}
}
RoomDB.java
房间数据库
package test;
import java.util.ArrayList;
public class RoomDB
{
private ArrayList<Room> room;
private String[] roomID = {"Room1","Room2"};
RoomDB(){
room = new ArrayList<Room>();
for(int i=0;i<roomID.length;i++)
{
addRoom(new Room(roomID[i]));
}
}
public void addRoom(Room addRoom){
room.add(addRoom);
}
public ArrayList<Room> getRoom(){
return room;
}
}
Room.java
房间.java
package test;
public class Room
{
private String roomID = null;
private boolean booked = false;
Room(String roomID)
{
this.roomID = roomID;
}
String getRoomID()
{
return roomID;
}
void cancel()
{
booked = false;
}
boolean isBooked()
{
return booked;
}
}
I checked there is no syntax error, but the program cannot run appropriately.
我检查没有语法错误,但程序无法正常运行。
The problem is the program keep asking the user to enter the room ID even the room ID is correct. For example, the user enter Room1, the program still keep asking for the input.
问题是程序不断要求用户输入房间 ID,即使房间 ID 是正确的。例如,用户输入 Room1,程序仍然不断要求输入。
I wonder to know which class cause the problem.
我想知道是哪个类导致了问题。
I should be grateful if someone can give advice on this issue please.
如果有人可以就这个问题提供建议,我将不胜感激。
采纳答案by Vishal Gajera
This is Perfect Answer, See this just copy it and run below code, please remove modify to your package/class name accordingly as you wish,
这是完美的答案,看到这个只需复制它并运行下面的代码,请根据需要相应地删除对您的包/类名称的修改,
Forget to set true after room Allocated, if Room seems to be free then allocate and marked it as a BOOKED->true.
忘记在房间分配后设置真,如果房间似乎是空闲的,则分配并将其标记为 BOOKED->true。
package roombooking;
import java.util.ArrayList;
import java.util.Scanner;
class Room
{
private String roomID = null;
private boolean booked = false;
Room(String roomID)
{
this.roomID = roomID;
}
String getRoomID()
{
return roomID;
}
void cancel()
{
booked = false;
}
boolean isBooked()
{
return booked;
}
public void setBooked(boolean booked) {
this.booked = booked;
}
}
class RoomDB
{
private ArrayList<Room> room;
private String[] roomID = {"Room1","Room2"};
RoomDB(){
room = new ArrayList<Room>();
for(int i=0;i<roomID.length;i++)
{
addRoom(new Room(roomID[i]));
}
}
public void addRoom(Room addRoom){
room.add(addRoom);
}
public ArrayList<Room> getRoom(){
return room;
}
}
public class Booking {
static RoomDB roomDB = new RoomDB();
public static void main(String[] args) {
String roomID;
Room room;
Scanner inputID = new Scanner(System.in);
while(true){
System.out.println("Please choose the room you want to book \n"+getRoomList(roomDB));
while(true){
System.out.println("Enter your Room no. : (Enter x for quite ) : ");
roomID = inputID.nextLine();
System.out.println("X : " + roomID.equals("x"));
if(roomID.equals("x")){
System.out.println("Break");
break;
}
if(getRoom(roomID) == null){
System.out.println("The room ID is incorrect, please enter again or enter x to quit");
}
else{
room = getRoom(roomID);
if(!room.isBooked()){
System.out.println("Book successfully");
room.setBooked(true);
break;
}
else{
System.out.println("please enter the room ID again or enter x to quit");
}
}
}
}
}
public static String getRoomList(RoomDB roomDB)
{
String roomList = "";
for(Room r:roomDB.getRoom())
{
if(!r.isBooked())
{
roomList = roomList+r.getRoomID()+" is free"+" ";
}
else
{
roomList = roomList+r.getRoomID()+" has been booked ";
}
}
return roomList;
}
public static Room getRoom(String roomID){
for(Room r:roomDB.getRoom()){
if(r.getRoomID().equals(roomID)){
System.out.println("r.getRoomID : " + r.getRoomID() + " AND user.roomID :" + roomID);
return r;
}
}
return null;
}
}
EDITED :
编辑:
Here,
这里,
if(!room.isBooked()){
System.out.println("Book successfully");
room.setBooked(true); // this line does, booked room, after booked once it won't assign to any one else.
break;
}
}
回答by Shiladittya Chakraborty
public static Room getRoom(String roomID){
for(Room r:roomDB.getRoom()){
if(r.getRoomID().equals(roomID)){ //Old : r.getRoomID()==roomID
return r;
}
}
return null;
}
For comparison string always use equals.
对于比较字符串,始终使用等于。
回答by Phantomazi
By the looks of it your issue is arising at the moment you are comparing Strings with the "==" operator. This is not possible, as the "==" operator tries to identify if the two objects are equal, not their respective values. You can get a better and full explanation of how String comparison works here: How do I compare strings in Java?
从外观上看,当您将字符串与“==”运算符进行比较时,您的问题就出现了。这是不可能的,因为“==”运算符试图确定两个对象是否相等,而不是它们各自的值。您可以在此处获得有关字符串比较如何工作的更好和完整的解释:如何在 Java 中比较字符串?
As a quick and proper fix of your issue just use:
作为快速正确地解决您的问题,只需使用:
if(r.getRoomID().equals(roomID))
{
return r;
}
回答by Husman
I would also suggest you compare strings in the same case (in 2 places in Booking.java):
我还建议您在相同的情况下比较字符串(在 Booking.java 中的 2 个地方):
if (roomID.equalsIgnoreCase("x"))
and also
并且
if (r.getRoomID().equalsIgnoreCase(roomID))
hope that helps.
希望有帮助。