aznishboy

BSTreeMap

Apr 11th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.75 KB | None | 0 0
  1. package me.andrew.unit10;
  2.  
  3. import java.util.Iterator;
  4. import java.util.TreeMap;
  5.  
  6. import me.andrew.unit08.Item;
  7. import chn.util.*;
  8.  
  9. public class BSTreeMap {
  10.     void testFind(TreeMap list)
  11.   {
  12.     int idToFind;
  13.     Item location;
  14.  
  15.     ConsoleIO console = new ConsoleIO();
  16.  
  17.     System.out.println("Testing search algorithm\n");
  18.     System.out.print("Enter Id value to search for (-1 to quit) --> ");
  19.     idToFind = console.readInt();
  20.  
  21.     while (idToFind >= 0)
  22.     {
  23.       location = (Item)list.get(idToFind);
  24.       if (location == null)
  25.         System.out.println("Id = " + idToFind + "  No such part in stock");
  26.       else
  27.         System.out.println(location);
  28.       System.out.println();
  29.       System.out.print("Enter Id value to search for (-1 to quit) --> ");
  30.       idToFind = console.readInt();
  31.     }
  32.   }
  33.   public void printOrder(TreeMap list){
  34.     Iterator iter = list.keySet().iterator();
  35.     while (iter.hasNext()){
  36.         System.out.println(list.get(iter.next()));
  37.     }
  38.   }
  39.   public void readData(TreeMap list)
  40.   {
  41.     FileInput inFile;
  42.  
  43.     String fileName = "file20.txt";
  44.     int id, inv;
  45.  
  46.     inFile = new FileInput(fileName);
  47.  
  48.     int howMany = inFile.readInt();
  49.     for (int k = 1; k <= howMany; k++)
  50.     {
  51.       id = inFile.readInt();
  52.       inv = inFile.readInt();
  53.       list.put(id, new Item (id, inv));
  54.     }
  55.   }
  56.  
  57.   public void mainMenu (TreeMap head)
  58.   {
  59.     String choice;
  60.     ConsoleIO console = new ConsoleIO();
  61.  
  62.     do
  63.     {
  64.       System.out.println("TreeMap List algorithm menu\n");
  65.       System.out.println("(1) Read data from disk");
  66.       System.out.println("(2) Print ordered list");
  67.       System.out.println("(3) find");
  68.       System.out.println("(4) size");
  69.       System.out.println("(Q) Quit\n");
  70.       System.out.print("Choice ---> ");
  71.       choice = console.readLine() + " ";  // kludge to ensure choice.charAt(0) > 0
  72.  
  73.       System.out.println();
  74.  
  75.       if ('1' <= choice.charAt(0) && choice.charAt(0) <= '8')
  76.       {
  77.         switch (choice.charAt(0))
  78.         {
  79.           case '1' :
  80.             readData(head);
  81.             break;
  82.           case '2' :
  83.             System.out.println();
  84.             System.out.println("The tree printed inorder\n");
  85.             printOrder(head);
  86.             System.out.println();
  87.             break;
  88.           case '3' :
  89.             testFind(head);
  90.             break;
  91.           case '4' :
  92.             System.out.println("Number of nodes = " + head.size());
  93.             System.out.println();
  94.             break;
  95.         }
  96.       }
  97.     }
  98.     while (choice.charAt(0) != 'Q' && choice.charAt(0) != 'q');
  99.   }
  100.  
  101.   public static void main(String[] args)
  102.   {
  103.     BSTreeMap test = new BSTreeMap();
  104.     TreeMap list = new TreeMap();
  105.  
  106.     test.mainMenu (list);
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment