aznishboy

BSTreeSet

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