Exemplo n.º 1
0
  /*
  Method: addSynonymFromMenu - database addition
  Purpose: adds a synonym to an existing entry
  Parameters:
      String wordToAddTo      the word to update
  Returns:
      none - the results are printed here
  */
  public void addSynonymFromMenu(String wordToAddTo) {
    if (wordToAddTo.equals("")) {
      // if the word is empty go back to caller
      System.out.println("\nPlease type a valid word\n");
      return;
    }

    // checks whether the word is in the database and gets it
    if (!this.containsEntry(wordToAddTo)) {
      // if the word isn't there, go back to caller
      System.out.println("Sorry, I could not find that word in the thesaurus\n");
      return;
    }
    TreeNode entryToModify = TreeNode.getNodeByString(wordToAddTo, entryRoot);

    // controls if the loop is repeated
    boolean repeatCheck = true;
    while (repeatCheck) {
      System.out.println("What synonym would you like " + "to add to the word: " + wordToAddTo);
      System.out.print(">> ");
      // Scanner to take user input
      Scanner inputScanner = new Scanner(System.in);
      // the synonym to be added
      // will accept spaces in words
      String userInput = inputScanner.nextLine();

      if (userInput.equals("")) {
        System.out.println("Sorry, I can't add nothing\n");
        // jump down to loop control
      } else {
        // checks to see if the entry already has that synonym

        if (entryToModify.containsSynonym(userInput))
          System.out.println(wordToAddTo + " already has that synonym\n");
        else {
          // adds the synonym to the entry
          entryToModify.addSynonymInOrder(userInput);
          System.out.println(userInput + " was added to the entry\n");
        }
      }

      // loop control menu
      System.out.println("\nWould you like to add another synonym?");
      System.out.print("type (yes/no) >>");
      // does the user want to add another entry?
      String userResponse = inputScanner.next();
      if (userResponse.equalsIgnoreCase("yes")) {
      } else {
        System.out.println("\nGoing back to the main menu\n");
        repeatCheck = false;
      }
    }
  }
Exemplo n.º 2
0
  /*
  Method: searchForWord - database search
  Purpose: Searches database for matching word
              and returns the results of the search
  Parameters:
      String wordSearched      the word to be found
  Returns:
      String        What we found/not found
  */
  public String searchForWord(String wordSearched) {
    if (this.isEmpty()) {
      // can't search an empty list
      return "There are no entries to search";
    }
    if (wordSearched.equals("")) {
      // can't search for an empty string
      return ("I can't search for nothing");
    }
    if (this.containsEntry(wordSearched)) {
      // its in the list, so get it
      TreeNode found = TreeNode.getNodeByString(wordSearched, entryRoot);

      return found.toString();
    }

    // I couldn't find it
    return ("I can't find: \"" + wordSearched + "\"");
  }
Exemplo n.º 3
0
  /*
  Method: removeEntryFromMenu - database removal
  Purpose: interface between menu and actual remove entry method
  Parameters:
      String removeMe      the word to remove
  Returns:
      boolean - did I do it?
  */
  public boolean removeEntryFromMenu(String removeMe) {
    // removeEntry is farther on down, just before the toString
    if (this.containsEntry(removeMe)) {
      System.out.println("Are you sure you want to remove the entry: " + removeMe + "?");
      System.out.print("(Yes/No) >>");
      // Will accept spaces in words
      // Scanner to take user input
      Scanner inputScanner = new Scanner(System.in);
      // the wood the user wants to add
      String userInput = inputScanner.nextLine();

      if (userInput.equalsIgnoreCase("yes")) {
        TreeNode.removeTreeNode(TreeNode.getNodeByString(removeMe, entryRoot), entryRoot, null);
        return true;
      } else {
        // go back to the menu
        return false;
      }
    } else {
      // removeEntry failed
      System.out.println("I could not find an entry: " + removeMe + "\n");
      return false;
    }
  }
Exemplo n.º 4
0
  /*
  Method: removeSynonymFromMenu - database removal
  Purpose: removes a synonym from an existing entry
  Parameters:
      String wordToRemove      the word to remove a synonym from
  Returns:
      none
  */
  public void removeSynonymFromMenu(String wordToRemoveFrom) {
    if (wordToRemoveFrom.equals("")) {
      // if the word is empty go back to caller
      System.out.println("\nPlease type a valid word\n");
      return;
    }

    // checks whether the word is in the database and gets it
    if (!this.containsEntry(wordToRemoveFrom)) {
      // if the word isn't there, go back to caller
      System.out.println("Sorry, I could not find that word in the thesaurus\n");
      return;
    }
    // if the entry exists, get the entry using its index
    TreeNode removeFromThis = TreeNode.getNodeByString(wordToRemoveFrom, entryRoot);

    // loop control, will loop till user does not say "yes" at end
    boolean repeatCheck = true;
    while (repeatCheck) {
      System.out.println(
          "What synonym would you like " + "to remove from the word: " + wordToRemoveFrom);
      System.out.print(">> ");
      // Scanner to take user input
      Scanner inputScanner = new Scanner(System.in);
      // the synonym to be added
      // will accept spaces in words
      String removeMe = inputScanner.nextLine();

      if (removeMe.equals("")) {
        System.out.println("Sorry, I can't remove nothing\n");
        // jump down to loop control
      } else {

        if (!removeFromThis.containsSynonym(removeMe)) {
          // if the word does not have the synonym skip down to loop control
          System.out.println(wordToRemoveFrom + "does not have that synonym\n");
        } else {
          System.out.println("Are you sure you want to remove the Synonym: " + removeMe + "?");
          System.out.print("(Yes/No) >>");
          // Will accept spaces in words
          String userInput = inputScanner.nextLine();

          if (userInput.equalsIgnoreCase("yes")) {
            TreeNode nodeToRemove =
                TreeNode.getNodeByString(removeMe, removeFromThis.getSynonymTree());

            TreeNode.removeTreeNode(nodeToRemove, removeFromThis.getSynonymTree(), null);
            System.out.println(removeMe + " was successfully removed from the entry.");
            return;
          } else {
            // go back to the menu
            System.out.println("Going back to the main menu");
            return;
          }
        }
      }

      // loop control menu
      System.out.println("\nWould you like to remove another synonym?");
      System.out.print("type (yes/no) >>");
      // does the user want to add another entry?
      String userResponse = inputScanner.next();
      if (userResponse.equalsIgnoreCase("yes")) {
      } else {
        System.out.println("\nGoing back to the main menu\n");
        repeatCheck = false;
      }
    }
  }