Hi all, I am coding a project that is supposed to resemble an ER. There's a Patient class that stores name, id, and priority. In my Triage class, I have an array list of patients are initialized when a Triage object is created.
In the Triage class, there are methods to addNewPatient(), removePatient(), and moveByPriority(). Patients are to be listed in the array list in the order of their priority first, and then the order that they were added to the array list.
I have coded my Triage class as follows:
package csc216project1; import java.util.Scanner; import java.util.ArrayList; import java.io.*; public class Triage { public Triage() { patientList = new ArrayList<Patient>(); } public Triage(String s) { patientList = new ArrayList<Patient>(); try { File myFile = new File(s); Scanner fileScanner = new Scanner(s); String trash = fileScanner.nextLine(); int i = 0; while (fileScanner.hasNext()) { patientList.add(Patient.parseFromLine(fileScanner.nextLine())); i++; } sortPatients(patientList); } catch (Exception e) { System.exit(1); } } public int getSize() { return patientList.size(); } public void addNewPatient(String name, int priority) { Patient temp = new Patient(name, priority); patientList.add(temp); sortPatients(patientList); } public void removePatient(int id) { for (int i = 0; i < patientList.size(); i++) { if (patientList.get(i).getId() == id) { patientList.remove(i); } } } public void moveByPriority(int id, int newPriority) { for (int i = 0; i < patientList.size(); i++) { if (patientList.get(i).getId() == id) { patientList.get(i).setPriority(newPriority); } } sortPatients(patientList); } public Patient getPatientAt(int index) { return patientList.get(index); } public String print() { System.out.println("Priority ID Name"); } public void printToFile() { } public void sortPatients(ArrayList<Patient> p) { for (int k = 0; k < p.size(); k++) { int small = k; for(int j = k + 1; j < p.size(); j++) { if (p.get(small).getPriority() > p.get(j).getPriority()) { small = j; Patient temp = p.get(k); p.set(k, p.get(small)); p.set(small, p.get(k)); } } } } private ArrayList<Patient> patientList; }
My Patient class is:
package csc216project1; import java.util.Scanner; public class Patient { public Patient(String name, int priority) { nextID++; this.name = name; this.priority = priority; this.id = nextID; } public Patient(String name, int priority, int id) { nextID++; this.name = name; this.priority = priority; this.id = nextID; } public String getName() { return name; } public int getId() { return id; } public int getPriority() { return priority; } public void setPriority(int priority) { this.priority = priority; } public static Patient parseFromLine(String s) { Scanner scanner = new Scanner(s); int tempId = scanner.nextInt(); int tempPriority = scanner.nextInt(); String tempName = scanner.nextLine(); Patient temp = new Patient(tempName, tempPriority, tempId); return temp; } private String name; private int id; private int priority; private static int nextID = 999; }
My JUnit test class is:
package csc216project1; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class TriageTest { Triage t; @Before public void setUp() throws Exception { //super.setUp(); t = new Triage(); t.addNewPatient("A", 2); t.addNewPatient("B", 2); t.addNewPatient("Z", 3); } @Test public void testSetUp() { String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("ABZ", s); } @Test public void testAddNewPatientFirst() { t.addNewPatient("C", 1); String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("CABZ", s); } @Test public void testAddNewPatientMiddle() { t.addNewPatient("C", 2); String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("ABCZ", s); } @Test public void testAddNewPatientLast() { t.addNewPatient("C", 5); String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("ABZC", s); } @Test public void testRemovePatientFirst() { t.removePatient(t.getPatientAt(0).getId()); String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("BZ", s); } @Test public void testRemovePatientMiddle() { t.removePatient(t.getPatientAt(1).getId()); String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("AZ", s); } @Test public void testRemovePatientLast() { t.removePatient(t.getPatientAt(2).getId()); String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("AB", s); } @Test public void testMoveByPriorityFirst() { t.moveByPriority(t.getPatientAt(0).getId(), 4); String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("BZA", s); } @Test public void testMoveByPriorityMiddle() { t.moveByPriority(t.getPatientAt(1).getId(), 1); String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("BAZ", s); } @Test public void testMoveByPriorityLast() { t.moveByPriority(t.getPatientAt(2).getId(), 1); String s = ""; int k = t.getSize(); for (int i = 0; i < k; i++) { s = s + t.getPatientAt(i).getName(); } assertEquals("ZAB", s); } }
All of the testRemovePatient() methods work, but the testAddNewPatient() and testMoveByPriority() methods fail. I've narrowed the problem down to my helper method in sorting the array list when a new element is added or when a priority is changed:
public void sortPatients(ArrayList<Patient> p) { for (int k = 0; k < p.size(); k++) { int small = k; for(int j = k + 1; j < p.size(); j++) { if (p.get(small).getPriority() > p.get(j).getPriority()) { small = j; Patient temp = p.get(k); p.set(k, p.get(small)); [B]p.set(small, temp);[/B] } } } }
I realize my mistake, but I just cannot figure out the logic to get the array list sorted properly. Also, in my sortPatients() method, I have initiated Patient temp = p.get(k) and then p.set(small, temp). However, I have a static int that is incremented each time a new Patient object is created, so I would rather skip initiating a new object. To counter this, I tried deleting the line Patient temp = p.get(k) and changing p.set(small, temp) to p.set(small, p.get(k)) instead. Doing this, however, caused my testAddPatientLast() method to not work. It works otherwise.
Many thanks to all who can help me figure out my logic problem!