package zhangshe.json;

import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Hashtable;

public class JSONObject
    implements JSONValue
{
  /**
   * A PrintWriter for print()
   */
  static PrintWriter pen = new PrintWriter(System.out, true);
  // +--------+----------------------------------------------------------
  // | Fields |
  // +--------+
  /**
   * A Hashtable to store objects
   */
  Hashtable<JSONValue, JSONValue> hash;

  /**
   * The size of this JSONObject
   */
  int length = 0;

  // +-------------+----------------------------------------------------------
  // | Constructor |
  // +-------------+
  /**
   * Construct a JSONObject
   */
  public JSONObject()
  {
    this.hash = new Hashtable<JSONValue, JSONValue>();
    length++; // '{';
  } // JSONObject()

  // +---------+----------------------------------------------------------
  // | Methods |
  // +---------+
  /**
   * Add paris into JSONObject
   * 
   * @param key
   *          A JSONValue
   * @param value
   *          A JSONValue
   * @pre Both key and value should be valid JSONValue
   * @post this JSONObject contains one more pair, and the size is incremented
   *       by 1
   */
  public void add(JSONValue key, JSONValue value)
  {
    this.hash.put(key, value);
    length += (key.size() + value.size() + 1); // ':'
  } // add(String, JSONValue)

  /**
   * Convert a JSONObject into a string
   * 
   * @pre none
   * @post a String is given
   */
  @Override
  public String toJSONString()
  {

    String str = "{"; // initialize the string
    Enumeration<JSONValue> keys = hash.keys(); // get all keys in the HashTable

    while (keys.hasMoreElements())
      {
        JSONValue temp = keys.nextElement();
        str += temp.toJSONString() + ":" + hash.get(temp).toJSONString();
        if (keys.hasMoreElements())
          str += ",";
      } // for(i)
    str += "}";
    return str;
  } // toJSONString()

  /**
   * Get the size of the JSONObject
   * 
   * @post An Integer is given
   */
  @Override
  public int size()
  {
    return length;
  } // size()

  @Override
  public void print()
  {
    this.print(1);
  } // print()

  @Override
  public void print(int format)
  {
    pen.format("%" + format + "s", "");
    pen.print("{");

    Enumeration<JSONValue> keys = hash.keys(); // get all keys in the HashTable
    if (!keys.hasMoreElements())
      {
        pen.print("}");
        pen.flush();
        return;
      } // if keys have no more elements
    while (keys.hasMoreElements())
      {
        pen.println();
        JSONValue temp = keys.nextElement();
        temp.print(format + 2);
        pen.print(":");
        pen.flush();
        hash.get(temp).print(format+1);
        if (keys.hasMoreElements())
          pen.print(",");
      } // for(i)
    pen.println();
    pen.format("%" + format + "s", "");
    pen.print("}");
    pen.flush();
  } // print(String)
} // class JSONObject

@@ -260,16 +260,17 @@
  true);%0A
+%0A
   // +--
@@ -469,35 +469,97 @@
   * 
-The size of this JSONObject
+Store the number of characters in the JSONObject, including the open and closed brackets.
 %0A   
@@ -966,32 +966,33 @@
  // +---------+%0A
+%0A
   /**%0A   * Add p
@@ -2162,78 +2162,485 @@
 %0A%0A  
-@Override%0A  public void print()%0A  %7B%0A    this.print(1);%0A  %7D // print()%0A
+/**%0A   * Get the value in the Object by key%0A   * %0A   * @param key%0A   * @return %0A   * The value paired with the given key%0A   */%0A  public JSONValue get(JSONValue key)%0A  %7B%0A    return this.hash.get(key);%0A  %7D // get(JSONValue)%0A%0A%0A  /**%0A   * print the content in the JSONObject%0A   */%0A  @Override%0A  public void print()%0A  %7B%0A    // print the object with format = 1%0A    this.print(1);%0A  %7D // print()%0A%0A  /**%0A   * print the content in the JSONObject, format the result by given spaces%0A   */
 %0A  @
@@ -2680,24 +2680,65 @@
 format)%0A  %7B%0A
+    // format and print the open bracket%0A
     pen.form
@@ -2786,16 +2786,53 @@
 (%22%7B%22);%0A%0A
+    // get all keys in the HashTable%0A
     Enum
@@ -2873,41 +2873,75 @@
 s();
- // get all keys 
+%0A    // if the Object is empty, pr
 in
+t
  the 
-HashTable
+closed bracket and return
 %0A   
@@ -3079,16 +3079,79 @@
 elements
+%0A%0A    // otherwise, print all keys and values in the JSONObject
 %0A    whi
@@ -3366,17 +3366,19 @@
 t(format
-+
+ + 
 1);%0A    
@@ -3448,24 +3448,76 @@
  %7D // for(i)
+%0A%0A    // start a new line and put the closed bracket
 %0A    pen.pri

add comments and get method in JSONObject