-2

I am currently in the process of testing my ArrayPhoneDirectory file, which involves me testing all of the individual methods set out in that file. After testing my addChangeEntry method, I need to print out the contents of my array (newdir) in order to check that the 3 contacts i have added are there, but am not sure of the best way to do this.

ArrayPhoneDirectoryTester code:

public class ArrayPhoneDirectoryTester {
public static void main (String[] args)
{
    //creates a new PhoneDirectory
    PhoneDirectory newdir = new ArrayPhoneDirectory();

    System.out.println("ArrayPhoneDirectoryTester Running");
    System.out.println("********************************* \n");

    System.out.println("TEST 1: addChangeEntry");
    System.out.println("********************** \n");

    newdir.addChangeEntry("Joe Perkins", "07762573872");
    newdir.addChangeEntry("Annie Howell", "0876273862");
    newdir.addChangeEntry("Buzz Killington", "99999999999");


    // PRINT ARRAY TO PROVE THEY HAVE BEEN ADDED

any help with this would be much appreciated.

EDIT- ADDING CODE:

ArrayPhoneDirectory code:

import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ArrayPhoneDirectory implements PhoneDirectory {

private static final int INIT_CAPACITY = 100;
private int capacity = INIT_CAPACITY;

//holds telno of directory entries
private int size = 0;

//Array to contain directory entries
private DirectoryEntry[] theDirectory = new DirectoryEntry[capacity];


//Holds name of data file to be read
private String sourceName = null;

/**
 * Flag to indicate whether directory was modified since it was last loaded
 * or saved.
 */
private boolean modified = false;

// PUBLIC INTERFACE METHODS

public void loadData(String sourceName) {
    Scanner scan = new Scanner(sourceName).useDelimiter("\\Z");

    while (scan.hasNextLine()) {
        String name = scan.nextLine();
        String telno = scan.nextLine();

        add(name, telno);
    }
}

/**
 * find method is called, returning the position in the array of the given
 * name.
 */
public String lookUpEntry(String name) {
    find(name);
    return null;

}

/**
 * for loop that checks every DirectoryEntry inside theDirectory and then
 * checks it against the name and telno given in the parameter, if both are
 * equal, the telno is updated, else it is added to theDirectory
 *
 */

public String addChangeEntry(String name, String telno) {
    try {
        for (DirectoryEntry x : theDirectory) {
            if (x.getName().equals(name)) {
                x.setNumber(telno);
                return x.getNumber();
            }
            add(name, telno);  
        }

    }
    catch (NullPointerException e) {
        System.err.println("NullPointerExcepttion : " + e.getMessage());

    }

    finally {


    }
    return null;
}



//TO COMPLETE
public String removeEntry(String name) {
    return null;
}

/**
 * A new PrintWriter object is created, and a for loop is used to print the
 * name and number of each DirectoryEntry to the console.
 */
public void save() {
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(new FileWriter("directory.txt", true)); 

        for (DirectoryEntry x : theDirectory) {
            pw.write(x.getName());
            pw.write(x.getNumber());               
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    finally {
        pw.close();    
    }
}




//Private helper methods

private void reallocate() {
    capacity = capacity * 2;
    DirectoryEntry[] newDirectory = new DirectoryEntry[capacity];
    System.arraycopy(theDirectory, 0, newDirectory,
            0, theDirectory.length);

    theDirectory = newDirectory;
}

private void add(String name, String telno) {
    if (size >= capacity) {
        reallocate();
    }
    theDirectory[size] = new DirectoryEntry(name, telno);
    size = size + 1;
}

private int find(String name) {
    int i = 0;
    for (DirectoryEntry x : theDirectory) {
        if (x.getName().equals(name)) {
            return i;
        }
        i++;
    }
    return -1;
}

@Override
public String format() {
    return null;
}

}

PhoneDirectory Code:

public interface PhoneDirectory {

/**
 * Load file containing directory entries
 *
 * @param sourceName is name of the file containing the directory entries
 */
void loadData(String sourceName);

/**
 * Look up an entry.
 *
 * @param name The name of person to look up
 * @return The telno or null if name is not in the directory
 */
String lookUpEntry(String name);

/**
 * Add new entry or change an existing entry.
 *
 * @param name The name of the person being added or whose telno is going to
 * change
 * @param telno The telno being changed or added
 * @return The old telno or if a new entry null
 */
String addChangeEntry(String name, String telno);

/**
 * Remove an entry from the directory.
 *
 * @param name The name of the person to be removed
 * @return The current telno. If not in the directory, null is returned.
 */
String removeEntry(String name);

/**
 * If the directory has been modified the contents of the directory are
 * written back to the file
 */
void save();

/**
 * Builds a single string representing directory contents Each entry is
 * terminated by a new line
 *
 * @return The formatted list of directory contents
 */
String format();

}

Joe Perkins
  • 261
  • 3
  • 9
  • 17

1 Answers1

0

I would recommend you to use JUnit tests for this and assertions to check the addiction, rather than printing into console. Anyway, the easiest way would probably be to implement the toString method of the ArrayPhoneDirectory and then call System.out.println(newdir.toString()). Inside the toString, you would just do Arrays.toString(_theArray_) or Arrays.deepToString(_theArray).

EDIT: In the light of the code you've shown, it would be probably better to do the following:

Map<String, String> testValues = new HashMap<String, String();
testValues.put("Joe Perkins", "07762573872");
testValues.put("Annie Howell", "0876273862");
testValues.put("Buzz Killington", "99999999999");

for(String key : testValues.keySet()) {
    newdir.addChangeEntry(key, testValues.get(key));
}

for(String key : testValues.keySet()) {
    assert testValues.get(key).equals(newdir.lookUpEntry(key));
}

Of course you could just do System.out.println instead of the assert if you wish.

Tadeas Kriz
  • 524
  • 4
  • 14
  • When I insert `System.out.println(newdir.toString());` into my tester class and run it, all that is printed is: ArrayPhoneDirectory@1fe5052b – Joe Perkins Apr 26 '14 at 14:39
  • That's because you didn't implement the `toString` method inside the `ArrayPhoneDirectory`. If you don't show the code or API of `ArrayPhoneDirectory` there is not much more I can help you with. – Tadeas Kriz Apr 26 '14 at 14:41
  • Sorry, please check the edit of the original post i have made which shows the ArrayPhoneDirectory code. – Joe Perkins Apr 26 '14 at 14:44