well hi guys new to the forum and i need some help

im writing a program where a guest’s name is entered and then assigned to a room. The motel has three floors and 30 rooms thus im using a 2D array (arr[3][10]) its supposed reserve a room and the room cannot be reserved again under a different name.

hope u can help
Tej

Dani AI

Generated

For a simple motel model (3 floors, 10 rooms per floor) the key problems are correct indexing, an atomic check-then-reserve operation, and reasonable input validation. 's example used indices that don't exist for a 3x10 array; Java arrays are 0-based, so a 3x10 String[][] has valid floor indices 0..2 and room indices 0..9. A common human-friendly mapping is: for a room number N in 1..30, compute floorIndex = (N-1) / 10 and roomIndex = (N-1) % 10. See the Java arrays primer for basic rules: Java arrays tutorial.

There is a logic issue in 's occupancy helper: the boolean result should reflect whether a room is already taken, not whether its array slot is null in the opposite sense. Beyond fixing that, a more robust design is to encapsulate each room and centralize reservation logic so the check-and-set is atomic. The following pattern shows a small Room object and a Hotel manager with synchronized reservation methods; this prevents race conditions and centralizes bounds checking.

class Room {
    private String guest;
    boolean isOccupied() { return guest != null; }
    boolean reserve(String name) {
        if (guest != null) return false;
        guest = name;
        return true;
    }
    void checkout() { guest = null; }
    String getGuest() { return guest; }
}

class Hotel {
    private final Room[][] rooms = new Room[3][10];

    public Hotel() {
        for (int f = 0; f < rooms.length; f++)
            for (int r = 0; r < rooms[f].length; r++)
                rooms[f][r] = new Room();
    }

    private int[] indexFromNumber(int roomNumber) {
        int n = roomNumber - 1;
        return new int[] { n / 10, n % 10 };
    }

    public synchronized boolean reserveByNumber(int roomNumber, String guest) {
        int[] idx = indexFromNumber(roomNumber);
        int f = idx[0], r = idx[1];
        if (f < 0 || f >= rooms.length || r < 0 || r >= rooms[f].length)
            return false;
        return rooms[f][r].reserve(guest);
    }

    public synchronized boolean checkoutByNumber(int roomNumber) {
        int[] idx = indexFromNumber(roomNumber);
        int f = idx[0], r = idx[1];
        if (f < 0 || f >= rooms.length || r < 0 || r >= rooms[f].length)
            return false;
        if (!rooms[f][r].isOccupied()) return false;
        rooms[f][r].checkout();
        return true;
    }
}

Additional notes: validate all inputs to avoid ArrayIndexOutOfBoundsException; for concurrent access prefer ConcurrentHashMap with putIfAbsent or synchronized methods as above; persistent state can be saved with simple I/O or serialization if reservations must survive restarts (see Java I/O and concurrency tutorials for details: I/O basics, Concurrency basics). This handles 's requirement to prevent double bookings while keeping the design clear and maintainable.

Recommended Answers

All 4 Replies

Fill the correct spot when someone wants a room:

Someone wants room 20 on floor 5:

array[5][20] = "Persons name";

What's the question anyway?

thanks for the new insight but i still have one other problem how do i stop a person from booking the same room again

-tej

To check if someone is already in the room use the following code:

boolean isRoomOccupied(int floor, int room)
{
   if( array[floor][room] == null )
      return true;
   else
      return false;
}

When someone checks out call this function

void checkOut(int floor, int room)
{
    array[floor][room] = null;
}

It is very important that you reset it back to null when it is check out time otherwise the first function won't work (because it checks for null)

For more help,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.