Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: JUnit test for ER

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default JUnit test for ER

    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!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JUnit test for ER

    Rather than do the sorting yourself, the Collections provides several sort methods which makes sorting much easier. Just 1) have Patient implement Comparable or 2) write an object which implements Comparator (see the static sort methods in Collections (Java Platform SE 6) )

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JUnit test for ER

    Hi copeg, thanks for the suggestion. Unfortunately we have not covered Collections and implementation yet in my class, so I'm looking for a simple search algorithm that I can implement in my code.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JUnit test for ER

    Quote Originally Posted by raphytaffy View Post
    Unfortunately we have not covered Collections and implementation yet in my class, so I'm looking for a simple search algorithm that I can implement in my code.
    I presume then you have covered sorting algorithms? If the answer is no, then you may be further over your head doing a sort manually rather than relying a previous implementation (aka Collections). I hate to dodge addressing your original question, so I'll provide an alterative solution to having a sort method: create a list that is sorted as you create it - when you add a Patient, loop through and find its insertion point based upon the flanking priority values of the Patients. Here you don't have to sort because each insertion places the patient at the right position, unless of course you wish to sort by some other value later on.

Similar Threads

  1. [SOLVED] JUnit initialization error
    By raphytaffy in forum Java IDEs
    Replies: 2
    Last Post: September 20th, 2010, 05:33 PM
  2. JDBC API Test suite 1.3.1
    By prabhuaakannan in forum JDBC & Databases
    Replies: 0
    Last Post: March 26th, 2010, 09:57 AM
  3. Test and Set Explanation
    By bananasplitkids in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 6th, 2010, 01:10 PM
  4. A simple test for you all
    By Maximillian in forum The Cafe
    Replies: 6
    Last Post: December 2nd, 2009, 04:15 PM