import java.io.*;
import java.util.*;

/**  CSC B63 Assignment #1 Programming component
 * Provides command line access to the THeap class
 *
 * This file has been provided to make it easier to test your THeap code.
 *
 * DO NOT SUBMIT THIS FILE
 **/

public class Heap{
  
  public static void main(String[] args) throws IOException{
    
    int key = 0;
    THeap theap = new THeap();
    System.out.println("Commands: (d)ecrease-key; (e)xtract-min; (i)nsert; (m)inimum; (p)rint; (q)uit"); 
    
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Command: ");
    
    String command = in.readLine();
    char c = command.charAt(0);  
    
    /**
     * The heap commands are executed based on the user's choice.
     * If a key or value is needed, the user is prompted to enter
     * the additional information.
     * We use try and catch to handle exceptions. 
     **/
    
    while (! command.equals("q")){
      switch(c){
        
        //Prints the binomial heap 
        case 'p':
        case 'P':
          System.out.println(theap.print());
          break;

        //Prints the minimum of the binomial heap
        case 'm':
        case 'M':
          try {
            System.out.println(theap.minimum());
          }
          catch(NoSuchElementException e) {
            System.out.println("empty heap");
          }
          break; 
          
        //Inserts a new node into the binomial heap        
        case 'i': 
        case 'I':
          System.out.print("Key to insert: ");
          try {
            key = Integer.parseInt(in.readLine());
            theap.insert(key);
          }
          catch(NumberFormatException f) {
            System.out.println("Invalid number.");
          }
          break;
          
          //Extracts the minimum key from the binomial heap
        case 'e':
        case 'E':
          try {
            theap.extractMin();
          }
          catch(NoSuchElementException e) {
            System.out.println("empty heap");
          }
          catch(NumberFormatException f) {
            System.out.println("Invalid number.");
          }
          break;
          
          //Decreases the key of a node in the binomial heap 
        case 'd':
        case 'D':
          Object handle = new Object();
          System.out.print("Key to be decreased: ");
          try {
            key = Integer.parseInt(in.readLine());
          }
          catch(NumberFormatException f) {
            System.out.println("Invalid number.");
          }
          try{
            handle = theap.search(key);
          }
          catch(NoSuchElementException e) {
            System.out.println("empty heap");
          }
          if (handle == null)
            System.out.println("Key not found.");
          else{
            System.out.print("The new decreased key: ");
            try {
              key = Integer.parseInt(in.readLine());
            }
            catch(NumberFormatException f) {
              System.out.println("Invalid number.");
            }
            try{
              theap.decreaseKey(handle, key);
            }
            catch(Exception e) {
              System.out.println("New key cannot be greater than the old key.");
            }
          }
          break;
      } //switch
      
      System.out.print("Command: ");
      command = in.readLine();
      c=command.charAt(0);
    } //while
  } // main(String[])
  
} // Heap 


