Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.andrew.unit10;
- import java.util.Iterator;
- import java.util.TreeMap;
- import me.andrew.unit08.Item;
- import chn.util.*;
- public class BSTreeMap {
- void testFind(TreeMap list)
- {
- int idToFind;
- Item location;
- ConsoleIO console = new ConsoleIO();
- System.out.println("Testing search algorithm\n");
- System.out.print("Enter Id value to search for (-1 to quit) --> ");
- idToFind = console.readInt();
- while (idToFind >= 0)
- {
- location = (Item)list.get(idToFind);
- if (location == null)
- System.out.println("Id = " + idToFind + " No such part in stock");
- else
- System.out.println(location);
- System.out.println();
- System.out.print("Enter Id value to search for (-1 to quit) --> ");
- idToFind = console.readInt();
- }
- }
- public void printOrder(TreeMap list){
- Iterator iter = list.keySet().iterator();
- while (iter.hasNext()){
- System.out.println(list.get(iter.next()));
- }
- }
- public void readData(TreeMap list)
- {
- FileInput inFile;
- String fileName = "file20.txt";
- int id, inv;
- inFile = new FileInput(fileName);
- int howMany = inFile.readInt();
- for (int k = 1; k <= howMany; k++)
- {
- id = inFile.readInt();
- inv = inFile.readInt();
- list.put(id, new Item (id, inv));
- }
- }
- public void mainMenu (TreeMap head)
- {
- String choice;
- ConsoleIO console = new ConsoleIO();
- do
- {
- System.out.println("TreeMap List algorithm menu\n");
- System.out.println("(1) Read data from disk");
- System.out.println("(2) Print ordered list");
- System.out.println("(3) find");
- System.out.println("(4) size");
- System.out.println("(Q) Quit\n");
- System.out.print("Choice ---> ");
- choice = console.readLine() + " "; // kludge to ensure choice.charAt(0) > 0
- System.out.println();
- if ('1' <= choice.charAt(0) && choice.charAt(0) <= '8')
- {
- switch (choice.charAt(0))
- {
- case '1' :
- readData(head);
- break;
- case '2' :
- System.out.println();
- System.out.println("The tree printed inorder\n");
- printOrder(head);
- System.out.println();
- break;
- case '3' :
- testFind(head);
- break;
- case '4' :
- System.out.println("Number of nodes = " + head.size());
- System.out.println();
- break;
- }
- }
- }
- while (choice.charAt(0) != 'Q' && choice.charAt(0) != 'q');
- }
- public static void main(String[] args)
- {
- BSTreeMap test = new BSTreeMap();
- TreeMap list = new TreeMap();
- test.mainMenu (list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment