001package edu.pdx.cs410J.phonebillweb;
002
003import com.google.common.annotations.VisibleForTesting;
004
005import java.io.PrintWriter;
006import java.io.Writer;
007import java.util.Map;
008
009public class PrettyPrinter {
010  private final Writer writer;
011
012  @VisibleForTesting
013  static String formatWordCount(int count )
014  {
015    return String.format( "Dictionary on server contains %d words", count );
016  }
017
018  @VisibleForTesting
019  static String formatDictionaryEntry(String word, String definition )
020  {
021    return String.format("  %s -> %s", word, definition);
022  }
023
024
025  public PrettyPrinter(Writer writer) {
026    this.writer = writer;
027  }
028
029  public void dump(Map<String, String> dictionary) {
030    try (
031      PrintWriter pw = new PrintWriter(this.writer)
032    ) {
033
034      pw.println(formatWordCount(dictionary.size()));
035
036      for (Map.Entry<String, String> entry : dictionary.entrySet()) {
037        String word = entry.getKey();
038        String definition = entry.getValue();
039        pw.println(formatDictionaryEntry(word, definition));
040      }
041
042      pw.flush();
043    }
044
045  }
046}