1

I have a method to add a patient to the correct index depending on their priority. However it seems that when I add more than one person, it doesn't get added. I'm not too sure what I'm missing in my add method. My guess is there is something wrong with the if-else statement but I'm not able to pinpoint where the problem is.

public void addPatient(Patient sickPerson)
{
    int lim = patients.size();

    if (patients.isEmpty()) //if empty
    {
        patients.add(sickPerson);
    }
    else //if not empty
    {
        for (int i = 0; i < lim; i++) 
        {
        if (patients.get(i).compareTo(sickPerson) > 0) 
            // if the sickperson is more important...
            {
                patients.add(i, sickPerson);
            } 

        else if (patients.get(i).compareTo(sickPerson) == 0)
            // if the sickperson's priority is the same, 
            {
                patients.add(i + 1, sickPerson);
            }
        else 
            {
                continue;
            }
        }
    }

}

@Test
public void getNumWaitingTest() {

    WaitingRoom a = new WaitingRoom();
    Patient b = new Patient("name", "bad", 1, 1);
    Patient c = new Patient("name2", "bad2", 2, 2);

    a.addPatient(b);
    a.addPatient(c);

    assertEquals(2, a.getNumWaiting());
}

and if I run this test case, I would get 1 for the numWaiting for some reason... where could I have gone wrong in this code??

1 Answer 1

2

After the for cycle you are not adding any patient if they have lower priority than the existing ones.

As soon as you add a patient in the list you should return from the method; if you reach the end of the method it means that the new patient has the lowest priority and you should add him at the end of the list.

Sign up to request clarification or add additional context in comments.

Comments

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.