Java println问题中库存管理器的二进制搜索树

时间:2013-12-05 21:41:17

标签: java boolean binary-search-tree inventory-management

我使用二叉搜索树创建了一个用Java管理Dvd的程序。除了add命令之外,我按照我想要的方式工作。使用如下命令“a”添加电影时:

a finding nemo

程序应检查几个条件然后添加电影。条件如下: - 如果树为空,请将影片添加到树中,默认副本为1。 - 如果树不为空,则搜索树并将树中每部电影的标题与要添加到树中的电影的标题进行比较。如果在树中找到标题,则将该影片的副本更新为1。 - 如果在树中找不到标题,只需将影片添加到树中,其默认副本为1.

这是我必须使其运作的代码:

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);
    }

我遇到的问题是: 添加电影和添加已存在于树中的电影的添加方法工作正常,除了它打印出两次确认消息,如下所示: (加勒比海盗已经存在于清单中,并且有1份用于测试目的)

a pirates of the caribbean

将另一份盗版副本添加到广告资源中。

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

这是显示的第一条确认消息。

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

这是显示的第二条确认消息。

为什么两条消息都被打印出来以及如何解决?


编辑:

所以这就是我根据对rgettman响应的解释改变了我的代码:

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;
  }

我现在遇到的问题是:

我加了一部电影:加勒比海盗 加勒比海盗加入了一份副本 然后我又添加了一部电影:一个发现nemo 找到nemo得到一个副本 然后我尝试添加另一个查找nemo的副本:一个发现nemo 添加了另一个副本,但它仍然显示两条确认消息。 然后我尝试添加另一个加勒比海盗的副本:加勒比海盗 添加了另一个副本,只显示了一条确认消息。

    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.

在编辑中我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的addIfNotEmpty方法会为boolean found参数设置一个found参数,您希望在调用它的方法中包含您的设定值,在您的调用方法中。

但是,在Java中,参数按值传递。您正在修改addIfNotEmptyfound副本,而不是案例的public static boolean addIfNotEmpty(DvdTreeNode root, int level, String movieToCommand) { 变量。

而不是修改参数,而是返回值。

found

然后在addIfNotEmpty中声明一个本地found变量,将其设置为您已经在做的,然后返回它。将方法调用的结果设置为调用方法的{{1}}。