forked from AY2425S1-CS2103T-F15-2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into id-prefix-delete-command
- Loading branch information
Showing
3 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package seedu.address.ui.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Manages the history of commands entered by the user. | ||
*/ | ||
public class CommandHistory { | ||
private final List<String> commandHistory; | ||
private int historyIndex; | ||
|
||
/** | ||
* Constructs a {@code CommandHistory} object. | ||
*/ | ||
public CommandHistory() { | ||
commandHistory = new ArrayList<>(); | ||
historyIndex = -1; // Start with no commands | ||
} | ||
|
||
/** | ||
* Adds a command to the history. | ||
* Resets the index to point to the latest command. | ||
* | ||
* @param command The command to be added to the history. | ||
*/ | ||
public void addCommand(String command) { | ||
commandHistory.add(command); | ||
historyIndex = commandHistory.size(); // Reset to end of list | ||
} | ||
|
||
/** | ||
* Returns the previous command in history. | ||
* If at the start, it returns null. | ||
* | ||
* @return The previous command, or null if at the start of the list. | ||
*/ | ||
public String getPreviousCommand() { | ||
if (historyIndex > 0) { | ||
historyIndex--; | ||
return commandHistory.get(historyIndex); | ||
} | ||
return null; // No previous command | ||
} | ||
|
||
/** | ||
* Returns the next command in history. | ||
* If at the end, it returns null. | ||
* | ||
* @return The next command, or null if at the end of the list. | ||
*/ | ||
public String getNextCommand() { | ||
if (historyIndex < commandHistory.size() - 1) { | ||
historyIndex++; | ||
return commandHistory.get(historyIndex); | ||
} | ||
historyIndex = commandHistory.size(); // Reset to end | ||
return null; // No next command | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package seedu.address.ui; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.ui.util.CommandHistory; | ||
|
||
/** | ||
* Unit tests for {@code CommandHistory}. | ||
*/ | ||
public class CommandHistoryTest { | ||
|
||
private CommandHistory commandHistory; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
commandHistory = new CommandHistory(); | ||
} | ||
|
||
@Test | ||
public void addCommand_singleCommand_success() { | ||
commandHistory.addCommand("first command"); | ||
assertEquals("first command", commandHistory.getPreviousCommand()); | ||
} | ||
|
||
@Test | ||
public void addCommand_multipleCommands_success() { | ||
commandHistory.addCommand("first command"); | ||
commandHistory.addCommand("second command"); | ||
commandHistory.addCommand("third command"); | ||
|
||
assertEquals("third command", commandHistory.getPreviousCommand()); | ||
assertEquals("second command", commandHistory.getPreviousCommand()); | ||
assertEquals("first command", commandHistory.getPreviousCommand()); | ||
} | ||
|
||
@Test | ||
public void getPreviousCommand_noHistory_returnsNull() { | ||
assertNull(commandHistory.getPreviousCommand()); | ||
} | ||
|
||
@Test | ||
public void getPreviousCommand_atStartOfHistory_returnsNull() { | ||
commandHistory.addCommand("first command"); | ||
|
||
// Move to the start of the history | ||
commandHistory.getPreviousCommand(); | ||
assertNull(commandHistory.getPreviousCommand()); | ||
} | ||
|
||
@Test | ||
public void getNextCommand_noHistory_returnsNull() { | ||
assertNull(commandHistory.getNextCommand()); | ||
} | ||
|
||
@Test | ||
public void getNextCommand_atEndOfHistory_returnsNull() { | ||
commandHistory.addCommand("first command"); | ||
commandHistory.addCommand("second command"); | ||
|
||
// Move to the start of history | ||
commandHistory.getPreviousCommand(); | ||
commandHistory.getPreviousCommand(); | ||
|
||
// Move forward to the end of history | ||
commandHistory.getNextCommand(); | ||
commandHistory.getNextCommand(); | ||
assertNull(commandHistory.getNextCommand()); | ||
} | ||
|
||
@Test | ||
public void getNextCommand_fromMiddleOfHistory_success() { | ||
commandHistory.addCommand("first command"); | ||
commandHistory.addCommand("second command"); | ||
commandHistory.addCommand("third command"); | ||
|
||
// Move to the start of history | ||
commandHistory.getPreviousCommand(); | ||
commandHistory.getPreviousCommand(); | ||
commandHistory.getPreviousCommand(); | ||
|
||
// Move forward one step in history | ||
assertEquals("second command", commandHistory.getNextCommand()); | ||
} | ||
|
||
@Test | ||
public void addCommand_afterNavigatingHistory_resetsHistoryIndex() { | ||
commandHistory.addCommand("first command"); | ||
commandHistory.addCommand("second command"); | ||
|
||
// Move to the start of history | ||
commandHistory.getPreviousCommand(); | ||
commandHistory.getPreviousCommand(); | ||
|
||
// Add a new command | ||
commandHistory.addCommand("new command"); | ||
|
||
// New command should be at the end of history | ||
assertEquals("new command", commandHistory.getPreviousCommand()); | ||
assertNull(commandHistory.getNextCommand()); // At the end of history | ||
} | ||
} |