二分探索木を使って 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);
}
私が抱えている問題は次のとおりです。映画を追加するための add メソッドと、ツリーに既に存在する映画を追加するためのメソッドは正常に機能しますが、確認メッセージが次のように 2 回出力されることを除きます。テスト用に 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.
これは、表示される 2 番目の確認メッセージです。
両方のメッセージが出力されるのはなぜですか? また、どうすれば修正できますか?
編集:
これは、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;
}
私が今抱えている問題は次のとおりです。
映画を追加します: カリブ海の海賊のパイレーツ オブ ザ カリビアンの 1 つのコピーが追加されます 次に、別の映画を追加します: ファインディング ニモ ファインディング ニモの 1 つのコピーが追加されます 次に、ファインディング ニモの別のコピーを追加しようとします: ファインディング ニモ 別のcopy が追加されますが、両方の確認メッセージが表示されます。次に、パイレーツ オブ カリビアンの別のコピーを追加しようとしました: パイレーツ オブ ザ カリビアン 別のコピーが追加され、確認メッセージが 1 つだけ表示されます。
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.
編集で何が間違っていましたか?