Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2122S1#79 from itzblim/add-list-commands
Browse files Browse the repository at this point in the history
Add list commands
  • Loading branch information
luminousleek authored Oct 13, 2021
2 parents dab6e01 + 3a3d354 commit c0e09d3
Show file tree
Hide file tree
Showing 15 changed files with 357 additions and 41 deletions.
24 changes: 0 additions & 24 deletions src/main/java/seedu/address/logic/commands/ListCommand.java

This file was deleted.

16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/commands/list/ListCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package seedu.address.logic.commands.list;

import seedu.address.logic.commands.Command;

/**
* Lists all modules / lessons / exams in the address book to the user.
*/
public abstract class ListCommand extends Command {

public static final String COMMAND_WORD = "list";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Lists all modules / lessons / exams.\n"
+ "Parameters: [mod / lesson / exam]\n"
+ "Example: " + COMMAND_WORD + " mod";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seedu.address.logic.commands.list;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_MODULES;

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.GuiState;
import seedu.address.model.Model;

/**
* Lists all exams in the ModBook to the user.
*/
public class ListExamCommand extends ListCommand {

public static final String MESSAGE_SUCCESS = "Listed all exams.";

@Override
public CommandResult execute(Model model) {
requireNonNull(model);

model.updateFilteredModuleList(PREDICATE_SHOW_ALL_MODULES);
return new CommandResult(MESSAGE_SUCCESS, false, GuiState.EXAMS);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ListExamCommand); // state check
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seedu.address.logic.commands.list;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_MODULES;

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.GuiState;
import seedu.address.model.Model;

/**
* Lists all lessons in the ModBook to the user.
*/
public class ListLessonCommand extends ListCommand {

public static final String MESSAGE_SUCCESS = "Listed all lessons.";

@Override
public CommandResult execute(Model model) {
requireNonNull(model);

model.updateFilteredModuleList(PREDICATE_SHOW_ALL_MODULES);
return new CommandResult(MESSAGE_SUCCESS, false, GuiState.LESSONS);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ListLessonCommand); // state check
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seedu.address.logic.commands.list;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_MODULES;

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.GuiState;
import seedu.address.model.Model;

/**
* Lists all modules in the ModBook to the user.
*/
public class ListModCommand extends ListCommand {

public static final String MESSAGE_SUCCESS = "Listed all modules.";

@Override
public CommandResult execute(Model model) {
requireNonNull(model);

model.updateFilteredModuleList(PREDICATE_SHOW_ALL_MODULES);
return new CommandResult(MESSAGE_SUCCESS, false, GuiState.SUMMARY);
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ListModCommand); // state check
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.GuiState;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.list.ListCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ public Command parseCommand(String userInput, GuiState guiState) throws ParseExc
return new FindCommandParser().parse(arguments, guiState);

case ListCommand.COMMAND_WORD:
return new ListCommand();
return new ListCommandParser().parse(arguments, guiState);

case DetailCommand.COMMAND_WORD:
return new DetailCommandParser().parse(arguments, guiState);
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/seedu/address/logic/parser/ListCommandParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;

import seedu.address.logic.commands.GuiState;
import seedu.address.logic.commands.list.ListCommand;
import seedu.address.logic.commands.list.ListExamCommand;
import seedu.address.logic.commands.list.ListLessonCommand;
import seedu.address.logic.commands.list.ListModCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new ListCommand object
*/
public class ListCommandParser implements Parser<ListCommand> {

/**
* Parses the given {@code String} of arguments in the context of the ListCommand
* and returns a ListCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public ListCommand parse(String args, GuiState guiState) throws ParseException {

if (hasMultipleArguments(args)) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ListCommand.MESSAGE_USAGE));
}

try {
Type type = ParserUtil.parseFirstArg(args, ListCommand.MESSAGE_USAGE);
switch (type) {
case LESSON:
return new ListLessonCommand();
case EXAM:
return new ListExamCommand();
default:
return new ListModCommand();
}
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ListCommand.MESSAGE_USAGE), pe);
}
}

/**
* Checks if an argument string has multiple arguments
*/
private static boolean hasMultipleArguments(String args) {
return args.trim().split(" ", 2).length != 1;
}

}
6 changes: 3 additions & 3 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
import org.junit.jupiter.api.io.TempDir;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.GuiState;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
Expand Down Expand Up @@ -70,8 +70,8 @@ public void execute_commandExecutionError_throwsCommandException() {

@Test
public void execute_validCommand_success() throws Exception {
String listCommand = ListCommand.COMMAND_WORD;
assertCommandSuccess(listCommand, ListCommand.MESSAGE_SUCCESS, model);
String clearCommand = ClearCommand.COMMAND_WORD;
assertCommandSuccess(clearCommand, ClearCommand.MESSAGE_SUCCESS, model);
}

@Test
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/seedu/address/logic/commands/CommandTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.module.Module;
import seedu.address.model.module.predicates.HasModuleCodePredicate;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.person.Person;
import seedu.address.testutil.EditPersonDescriptorBuilder;
Expand Down Expand Up @@ -118,6 +120,7 @@ public static void assertCommandFailure(Command command, Model actualModel, Stri
assertEquals(expectedAddressBook, actualModel.getAddressBook());
assertEquals(expectedFilteredList, actualModel.getFilteredPersonList());
}

/**
* Updates {@code model}'s filtered list to show only the person at the given {@code targetIndex} in the
* {@code model}'s address book.
Expand All @@ -132,4 +135,17 @@ public static void showPersonAtIndex(Model model, Index targetIndex) {
assertEquals(1, model.getFilteredPersonList().size());
}

/**
* Updates {@code model}'s filtered list to show only the module at the given {@code targetIndex} in the
* {@code model}'s ModBook.
*/
public static void showModuleAtIndex(Model model, Index targetIndex) {
assertTrue(targetIndex.getZeroBased() < model.getFilteredModuleList().size());

Module module = model.getFilteredModuleList().get(targetIndex.getZeroBased());
model.updateFilteredModuleList(new HasModuleCodePredicate(module.getCode()));

assertEquals(1, model.getFilteredModuleList().size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seedu.address.logic.commands.list;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showModuleAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_MODULE;
import static seedu.address.testutil.TypicalModules.getTypicalModBook;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.GuiState;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;

/**
* Contains integration tests (interaction with the Model) and unit tests for ListExamCommand.
*/
public class ListExamCommandTest {

private Model model;
private Model expectedModel;

@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalAddressBook(), getTypicalModBook(), new UserPrefs());
expectedModel = new ModelManager(getTypicalAddressBook(), getTypicalModBook(), new UserPrefs());
}

@Test
public void execute_listIsNotFiltered_showsSameList() {
assertCommandSuccess(new ListExamCommand(), model,
new CommandResult(ListExamCommand.MESSAGE_SUCCESS, false, GuiState.EXAMS),
expectedModel);
}

@Test
public void execute_listIsFiltered_showsEverything() {
showModuleAtIndex(model, INDEX_FIRST_MODULE);
assertCommandSuccess(new ListExamCommand(), model,
new CommandResult(ListExamCommand.MESSAGE_SUCCESS, false, GuiState.EXAMS),
expectedModel);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package seedu.address.logic.commands.list;

import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.logic.commands.CommandTestUtil.showModuleAtIndex;
import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_MODULE;
import static seedu.address.testutil.TypicalModules.getTypicalModBook;
import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.GuiState;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;

/**
* Contains integration tests (interaction with the Model) and unit tests for ListLessonCommand.
*/
public class ListLessonCommandTest {

private Model model;
private Model expectedModel;

@BeforeEach
public void setUp() {
model = new ModelManager(getTypicalAddressBook(), getTypicalModBook(), new UserPrefs());
expectedModel = new ModelManager(getTypicalAddressBook(), getTypicalModBook(), new UserPrefs());
}

@Test
public void execute_listIsNotFiltered_showsSameList() {
assertCommandSuccess(new ListLessonCommand(), model,
new CommandResult(ListLessonCommand.MESSAGE_SUCCESS, false, GuiState.LESSONS),
expectedModel);
}

@Test
public void execute_listIsFiltered_showsEverything() {
showModuleAtIndex(model, INDEX_FIRST_MODULE);
assertCommandSuccess(new ListLessonCommand(), model,
new CommandResult(ListLessonCommand.MESSAGE_SUCCESS, false, GuiState.LESSONS),
expectedModel);
}
}
Loading

0 comments on commit c0e09d3

Please sign in to comment.