1

The queue generates an error if more than 3 names are entered into it:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at hotelobjects.Queue.addqueue(Queue.java:17)
    at hotelobjects.HotelObjects.addCustomers(HotelObjects.java:82)
    at hotelobjects.HotelObjects.main(HotelObjects.java:44)
Java Result: 1

How do I ensure that any names entered after the original 3 will be placed at the front of the queue - in a circular way.

package hotelobjects;
import java.util.*;
import java.io.*;

public class HotelObjects {
static Queue myQueue = new Queue();
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        String command;

        Scanner input = new Scanner(System.in);

        Room[] myHotel = new Room[10];
        for (int x = 0; x < myHotel.length; x++) {
        myHotel[x] = new Room();
        }

        System.out.println("10 Rooms Created");

        while (true) {
            System.out.print("\nEnter command or 'X' to exit: ");
            command = input.next();
            command = command.toLowerCase();

            if (command.charAt(0) == 'x') {
                System.exit(0);
            }

            if (command.charAt(0) == 'a') {
                addCustomers(myHotel);
            }

            if (command.charAt(0) == '3') {
                displayNames(myHotel);
            }
        }
    }

    private static void addCustomers(Room myHotel[]) {
        String roomName;
        int roomNum;
        System.out.println("Enter room number (0-10) or 11 to exit:");
        Scanner input = new Scanner(System.in);
        roomNum = input.nextInt();
        if (roomNum<11) {
            System.out.println("Enter name for room " + roomNum + " :");
            roomName = input.next();
            roomName = roomName.toLowerCase();
            myHotel[roomNum].setName(roomName);
            myQueue.addqueue(roomName);
        }
        else {
            System.exit(0);
        }
    }

    private static void displayNames(Room[] myHotel) {
        System.out.println("The last 3 names entered are: ");
        for (int x = 0; x < 3; x++) {
            myQueue.takequeue();
        }
    } 
}

Here is the 'queue' class:

package hotelobjects;

public class Queue {
    String qitems[] = new String[3];
    int front = 0, end = 0;

    void addqueue(String roomName) {
        qitems[end] = roomName;
        end++;
    }

    void takequeue() {
        if (end > front) {
            System.out.println("Name Entered : " + qitems[front]);
            front++;
        } else {
            System.out.println("No Name");
        }
    }
}

1 Answer 1

1

Increment your index module the maximum number of elements in the queue:

void addqueue(String roomName) {
    qitems[end] = roomName;
    end = (end + 1) % 3;
}

So you get:

end = (0 + 1) % 3 = 1
end = (1 + 1) % 3 = 2
end = (2 + 1) % 3 = 0
end = (0 + 1) % 3 = 1
...

You should also change the takequeue() method to consider the fact that the queue is now circular. Something like this:

void takequeue() {
    System.out.println("Name Entered : " + qitems[front]);
    front = (front + 1) % 3;
}

It is also a good idea to create a constant to store the queue capacity instead of repeating the 3 value all over the code:

private static final int CAPACITY = 3;
Sign up to request clarification or add additional context in comments.

3 Comments

I tried adding your changes, but the queue now erases the original names
That's how a circular queue works. If you need to store more elements in the queue, you need to increase its capacity.
The problem was in takequeue(). I updated the answer, hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.