forked from AY2425S1-CS2103T-T12-4/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 pull request AY2425S1-CS2103T-T12-4#122 from flyingsalsa/branch…
…-stateful-command-history Add Command history navigation
- Loading branch information
Showing
4 changed files
with
141 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,40 @@ | ||
package seedu.address.ui; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Container for previous commands | ||
*/ | ||
public class CommandHistory { | ||
private ArrayList<String> commandHistory = new ArrayList<>(); | ||
private int currentHistoryIndex = -1; | ||
|
||
/** | ||
* Adds a command to the ArrayList | ||
*/ | ||
public void addCommand(String command) { | ||
commandHistory.add(command); | ||
currentHistoryIndex = commandHistory.size(); // Reset index to point after the latest command | ||
} | ||
|
||
/** | ||
* Retrieves the previous command from the ArrayList, outputs null if already at the front of the arraylist | ||
*/ | ||
public String getPreviousCommand() { | ||
if (currentHistoryIndex > 0) { | ||
currentHistoryIndex--; | ||
return commandHistory.get(currentHistoryIndex); | ||
} | ||
return null; // No previous command | ||
} | ||
|
||
/** | ||
* Retrieves the next command from the ArrayList, outputs null if already at the end of the arraylist | ||
*/ | ||
public String getNextCommand() { | ||
if (currentHistoryIndex < commandHistory.size() - 1) { | ||
currentHistoryIndex++; | ||
return commandHistory.get(currentHistoryIndex); | ||
} | ||
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
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,70 @@ | ||
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; | ||
|
||
|
||
|
||
public class CommandHistoryTest { | ||
|
||
private CommandHistory commandHistory; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
commandHistory = new CommandHistory(); | ||
} | ||
|
||
@Test | ||
public void testAddCommand() { | ||
commandHistory.addCommand("first command"); | ||
commandHistory.addCommand("second command"); | ||
|
||
// Moving back in history | ||
assertEquals("second command", commandHistory.getPreviousCommand()); | ||
assertEquals("first command", commandHistory.getPreviousCommand()); | ||
|
||
// Should return null as there's no more previous command | ||
assertNull(commandHistory.getPreviousCommand()); | ||
} | ||
|
||
@Test | ||
public void testNextCommand() { | ||
commandHistory.addCommand("first command"); | ||
commandHistory.addCommand("second command"); | ||
|
||
// Go back twice to previous commands | ||
commandHistory.getPreviousCommand(); | ||
commandHistory.getPreviousCommand(); | ||
|
||
// Moving forward in history | ||
assertEquals("second command", commandHistory.getNextCommand()); | ||
assertNull(commandHistory.getNextCommand()); // No further next command after latest | ||
} | ||
|
||
@Test | ||
public void testEmptyHistory() { | ||
// Initially, history should return null when getting previous/next command | ||
assertNull(commandHistory.getPreviousCommand()); | ||
assertNull(commandHistory.getNextCommand()); | ||
} | ||
|
||
@Test | ||
public void testMixedNavigation() { | ||
commandHistory.addCommand("first command"); | ||
commandHistory.addCommand("second command"); | ||
commandHistory.addCommand("third command"); | ||
|
||
// Navigate through commands | ||
assertEquals("third command", commandHistory.getPreviousCommand()); | ||
assertEquals("second command", commandHistory.getPreviousCommand()); | ||
|
||
// Moving forward should bring back "third command" | ||
assertEquals("third command", commandHistory.getNextCommand()); | ||
|
||
// After the latest command, there should be no more next command | ||
assertNull(commandHistory.getNextCommand()); | ||
} | ||
} |