Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.andrew.unit10;
- import java.util.*;
- import me.andrew.unit08.Item;
- import chn.util.*;
- public class BSTreeSet {
- void testFind(TreeSet list)
- {
- int idToFind;
- boolean 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 = list.contains(new Item(idToFind, 0));
- if (location == false)
- System.out.println("Id = " + idToFind + " No such part in stock");
- else if (location == true){
- Iterator iter = list.iterator();
- while (iter.hasNext()){
- Comparable a = (Item)iter.next();
- if (a.compareTo(new Item (idToFind, 0)) == 0)
- System.out.println(a);
- }
- }
- System.out.println();
- System.out.print("Enter Id value to search for (-1 to quit) --> ");
- idToFind = console.readInt();
- }
- }
- public void printOrder(TreeSet list){
- Iterator iter = list.iterator();
- while (iter.hasNext()){
- System.out.println((Item)iter.next());
- }
- }
- public void readData(TreeSet 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.add(new Item (id, inv));
- }
- }
- public void mainMenu (TreeSet head)
- {
- String choice;
- ConsoleIO console = new ConsoleIO();
- do
- {
- System.out.println("TreeSet 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)
- {
- BSTreeSet test = new BSTreeSet();
- TreeSet list = new TreeSet();
- test.mainMenu (list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment