4

I want to check whether a patient object already exists in an ArrayList queue using the unique ID nhsNumber. But when I add a patient to the queue who I know already exists in the queue, the following code does not detect it. Any idea why this is?

public boolean checkIfInQueue(Patient p) {
    // set nhsNumber equal to the nhsNumberLabel on the page
    String nhsNumber = nhsNumberLabel.getText();
    System.out.println("Checking if " + nhsNumber + " already in the queue");

    // create boolean to state whether a person is in the queue or not (defaults to false)
    boolean isInQueue = false;

    for (int i = 0; i < Queue.queue.size(); i++) {
      if (Queue.queue.size() == 0) {
        System.out.println("Queue is empty");
        isInQueue = false;
        break;
      } else if (Queue.queue.get(i).getNhsNumber() == p.getNhsNumber()) {
        System.out.println(p.getFirstName() + " is already in the queue (checkIfInQueue() method)");
        isInQueue = true;
        break;
      } else {
        System.out.println(p.getFirstName() + " is not is the queue (checkIfInQueue() method)");
        isInQueue = false;
      }
    }

    return isInQueue;
  }

thanks, K

KvnH
  • 496
  • 1
  • 9
  • 30

3 Answers3

3

You can do this in a more standard and elegant way.

Override the equals() method in the Patient class.

public boolean equals(Object obj){
    if(obj instanceof Patient){
        return ((ThisClass)obj.getNhsNumber().equals(this.getNhsNumber());
    }
    return false;
}

Then call queue.contains() to check if the queue contains the object.

Queue.queue.contains(p) // Call the contains method with the p object.

contains() will loop through the queue and check if the objects are equal to your p object by calling .equals().

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
  • 1
    thanks Sam, I will try this method. My original error was using `==` instead of `.equals()` – KvnH Apr 26 '15 at 16:11
0

For every elements which is not a primitive, you need to use equals() instead of ==.

Moreover you should read the Javadoc of Set. This collection avoid you to check if you already store the Patient and the performances will be really better.

Happy
  • 1,815
  • 2
  • 18
  • 33
0
    if (Queue.queue.size() == 0) {
        System.out.println("Queue is empty");
        isInQueue = false;
        break;
      } 
      >> This code does not serve purpose because the for loop already check the condition of 0.

    Modify like this
    for (int i = 0; i < Queue.queue.size(); i++) {
       String nhsnumber=Queue.queue.get(i).getNhsNumber();
       if (nhsnumber.equalIgnoreCase(p.getNhsNumber())==true) {
        System.out.println(p.getFirstName() + " is already in the queue (checkIfInQueue() method)");
        isInQueue = true;
        break;
      } else {
        System.out.println(p.getFirstName() + " is not is the queue (checkIfInQueue() method)");
        isInQueue = false;
      }
      }

     >> isInQueue variable is also not used so remove the variable if its not used
Mohan Raj
  • 1,104
  • 9
  • 17