Binary search tree for an inventory manager in Java println issue

424 views Asked by At

I created a program to manage Dvd's in Java using a binary search tree. I got everything to work the way I want except the add command. When a movie is added using the command "a" like this:

a finding nemo

The program is supposed to check a few conditions then add the movie. The conditions are as follows: -If the tree is empty, add the movie to the tree with a default copy of 1. -If the tree is not empty, search the tree and compare the title of each movie in the tree to the title of the movie to be added to the tree. If the title is found in the tree, update the copy of that movie by 1. -If the title is not found in the tree, just add the movie to the tree with its default copy of 1.

This is the code I have to make it work:

case 'a':
  movieToCommand = input.substring(2, input.length()).toLowerCase();

  if (movies.isEmpty()) {
    movies.add(new Dvd(movieToCommand));
    System.out.println("\"" + movieToCommand + "\"" + " has been added "
      + "to the inventory.");
  } else /*(!movies.isEmpty())*/ {
    boolean found = false;
    addIfNotEmpty(movies.returnRoot(), 1, movieToCommand, found);

    if (!found) {
      movies.add(new Dvd(movieToCommand));
      System.out.println("\"" + movieToCommand + "\"" + " has been " 
        + "added to the inventory.");
    }
  }
break;

public static void addIfNotEmpty(DvdTreeNode root, int level, 
  String movieToCommand, boolean found) {
    if (root == null) {
      return;
    }
    addIfNotEmpty(root.getRight(), level+1, movieToCommand, found);
    if (root.getItem().getTitle().equalsIgnoreCase(movieToCommand)) {
      root.getItem().addCopy();
      System.out.println("You have added another copy of \""
        + movieToCommand
        + "\" to the inventory.");
        found = true;
      }
    addIfNotEmpty(root.getLeft(), level+1, movieToCommand, found);
    }

The problem I am having is this: my add methods for adding a movie and adding a movie that already exists in the tree work fine, except that it prints out a confirmation message twice like this: (pirates of the caribbean exists in the inventory already and has 1 copy for testing purposes)

a pirates of the caribbean

Adds another copy of pirates to the inventory.

You have added another copy of “pirates of the caribbean” to the inventory.

This is the first confirmation message that is displayed.

“pirates of the caribbean" has been added to the inventory.

This is the second confirmation message that is displayed.

Why are both messages being printed out and how do I fix it?


EDIT:

So this is what I changed my code to based on my interpretation of rgettman's response:

case 'a':
  movieToCommand = input.substring(2, input.length()).toLowerCase();

  if (movies.isEmpty()) {
    movies.add(new Dvd(movieToCommand));
    System.out.println("\"" + movieToCommand + "\"" + " has been added "
    + "to the inventory.");
  } else /*(!movies.isEmpty())*/ {
    boolean found;
    found = addIfNotEmpty(movies.returnRoot(), 1, movieToCommand);

    if (!found) {
      movies.add(new Dvd(movieToCommand));
      System.out.println("\"" + movieToCommand + "\"" + " has been " 
      + "added to the inventory.");
    }
  }
break;

public static boolean addIfNotEmpty(DvdTreeNode root, int level, 
  String movieToCommand) {
    boolean found = false;
    if (root == null) {
      return false;
    }
    addIfNotEmpty(root.getRight(), level+1, movieToCommand);
    if (root.getItem().getTitle().equalsIgnoreCase(movieToCommand)) {
      root.getItem().addCopy();
      System.out.println("You have added another copy of \""
      + movieToCommand
      + "\" to the inventory.");
      found = true;
    }
    addIfNotEmpty(root.getLeft(), level+1, movieToCommand);
    return found;
  }

The problem I am now having is:

I add a movie: a pirates of the caribbean pirates of the caribbean gets added with one copy I then add another movie: a finding nemo finding nemo gets added with one copy I then try to add another copy of finding nemo: a finding nemo Another copy gets added but it still displays both confirmation messages. I then try to add another copy of pirates of the caribbean: a pirates of the caribbean Another copy gets added and only 1 confirmation message gets displayed the way it should be.

    Enter a command (H for help): 
    a pirates of the caribbean
    "pirates of the caribbean" has been added to the inventory.

    Enter a command (H for help): 
    a finding nemo
    "finding nemo" has been added to the inventory.

    Enter a command (H for help): 
    a finding nemo
    You have added another copy of "finding nemo" to the inventory.
    "finding nemo" has been added to the inventory.

    Enter a command (H for help): 
    a pirates of the caribbean
    You have added another copy of "pirates of the caribbean" to the inventory.

What did I do wrong in my edits?

1

There are 1 answers

1
rgettman On

Your addIfNotEmpty method takes a boolean parameter for found that you expect to contain your set value in the method in which you call it, in your calling method's case.

However, in Java, parameters are passed by value. You are modifying a copy of found in addIfNotEmpty, not the case's found variable.

Instead of modifying a parameter, return the value instead.

public static boolean addIfNotEmpty(DvdTreeNode root, int level, 
  String movieToCommand) {

Then declare a local found variable in addIfNotEmpty, set it as you already are doing, and then return it. Set the result of the method call to your calling method's found.