Driver.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * Driver for Dictionary implemented via an AVL tree.
  3. *
  4. * @author Jonathan Landrum
  5. */
  6. import java.util.Scanner;
  7. public class Driver {
  8. /**
  9. * Main:
  10. * Initiates a Scanner object and a Dictionary object,
  11. * and puts the user in a loop for input.
  12. */
  13. public static void main (String[] args) {
  14. String input = "", term, definition;
  15. Scanner scan = new Scanner(System.in);
  16. Dictionary dictionary = new Dictionary();
  17. /*
  18. * Introduce the program
  19. */
  20. System.out.println ("=================================================");
  21. System.out.println ();
  22. System.out.println ("Java Ordered Dictionary Utilized With An AVL Tree");
  23. System.out.println ();
  24. System.out.println ("=================================================");
  25. System.out.println ();
  26. /*
  27. * Start a loop for user input
  28. */
  29. while (!input.equalsIgnoreCase("X")) {
  30. System.out.println ("Please select from the following choices:");
  31. System.out.println ();
  32. System.out.println (" --------------------");
  33. System.out.println (" A.......Add an entry");
  34. System.out.println (" D....Delete an entry");
  35. System.out.println (" L....Lookup an entry");
  36. System.out.println (" S...Show all entries");
  37. System.out.println (" X...............Exit");
  38. System.out.println (" --------------------");
  39. System.out.println ();
  40. System.out.print ("Enter a selection: ");
  41. input = scan.nextLine ();
  42. // Add
  43. if (input.equalsIgnoreCase("A")) {
  44. System.out.println ();
  45. System.out.print ("Enter the term to be defined: ");
  46. term = scan.nextLine ();
  47. System.out.print ("Enter the definition for \"" + term + "\": ");
  48. definition = scan.nextLine ();
  49. dictionary.insertEntry (term, definition);
  50. System.out.println ();
  51. System.out.println ("Added:");
  52. System.out.println (" " + term + " " + definition);
  53. System.out.println ();
  54. input = "";
  55. term = "";
  56. definition = "";
  57. }
  58. // Delete
  59. if (input.equalsIgnoreCase("D")) {
  60. System.out.println ();
  61. System.out.print ("Enter the term to remove: ");
  62. term = scan.nextLine ();
  63. System.out.println ("Removed:");
  64. System.out.print (" ");
  65. System.out.print (dictionary.removeEntry (term));
  66. System.out.println ("\n");
  67. input = "";
  68. term = "";
  69. }
  70. // Lookup
  71. if (input.equalsIgnoreCase("L")) {
  72. System.out.println ();
  73. System.out.print ("Enter the term to find: ");
  74. term = scan.nextLine ();
  75. System.out.println ("Found:");
  76. System.out.print (" ");
  77. System.out.print (dictionary.findEntry (term));
  78. System.out.println ();
  79. input = "";
  80. term = "";
  81. }
  82. // Show
  83. if (input.equalsIgnoreCase("S")) {
  84. if (dictionary.isEmpty())
  85. System.out.println ("\nThe dictionary is empty.\n");
  86. else {
  87. System.out.println ();
  88. System.out.println ("Current Root:");
  89. System.out.println (" " + dictionary.root.toString());
  90. System.out.println ();
  91. System.out.println ("All entries:");
  92. System.out.println (dictionary.toString());
  93. System.out.println ();
  94. }
  95. }
  96. }
  97. /*
  98. * Exit the program
  99. */
  100. System.out.println ();
  101. System.out.println ("-------------------------------------------------");
  102. System.out.println ("\\\\//_ Live long and prosper.");
  103. }
  104. }