2014-04-26 52 views
-2

我目前正在測試我的ArrayPhoneDirectory文件,這涉及到我測試該文件中列出的所有單個方法的過程。 經過測試我的addChangeEntry方法後,我需要打印出我的數組(newdir)的內容以檢查我添加的3個聯繫人是否存在,但我不確定是否執行此操作的最佳方式。如何打印一個數組的內容

ArrayPhoneDirectoryTester代碼:

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 

任何幫助,將不勝感激。

編輯 - 添加代碼:

ArrayPhoneDirectory代碼:

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代碼:

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(); 

}

+0

你可以添加PhoneDirectory和ArrayPhoneDirectory類嗎? – 1337

+0

@ L33D我現在已經將它們添加到原始文章 –

回答

0

我會建議建議您使用JUnit測試來實現此目的,並斷言檢查成癮,而不是打印到控制檯中。無論如何,最簡單的方法可能是實施ArrayPhoneDirectorytoString方法,然後致電System.out.println(newdir.toString())。在toString內部,您只需執行Arrays.toString(_theArray_)Arrays.deepToString(_theArray)

編輯:在代碼的光,你已經證明,這將是可能更好地做到以下幾點:

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)); 
} 

當然,你可以只是做System.out.println代替assert,如果你想。

+0

當我將'System.out.println(newdir.toString());'插入我的測試儀類並運行它時,所有打印的是:ArrayPhoneDirectory @ 1fe5052b –

+0

這是因爲你沒有在'ArrayPhoneDirectory'中實現'toString'方法。如果你沒有顯示'ArrayPhoneDirectory'的代碼或API,那麼我可以幫到你。 –

+0

對不起,請檢查我製作的顯示ArrayPhoneDirectory代碼的原始帖子的編輯。 –