Skip to content

Commit

Permalink
Merge pull request AY2324S2-CS2103T-T14-1#102 from howen02/branch-fin…
Browse files Browse the repository at this point in the history
…d-free

Branch find free
  • Loading branch information
howen02 authored Apr 4, 2024
2 parents ba4f91e + d918511 commit 7ea9711
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.Messages;
import seedu.address.model.Model;
import seedu.address.model.person.PersonHasNoTaskPredicate;

/**
* Finds and lists all persons in address book who does not have an active task.
*/
public class FindFreePersonCommand extends Command {

public static final String COMMAND_WORD = "findfree";

public static final String MESSAGE_SUCCESS = "Listed all persons without an active task";
private final PersonHasNoTaskPredicate predicate = new PersonHasNoTaskPredicate();

public FindFreePersonCommand() {
}

@Override
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
model.updateFilteredPersonList(new PersonHasNoTaskPredicate());
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
return other instanceof FindFreePersonCommand;
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("predicate", predicate)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import seedu.address.logic.commands.FilterCommand;
import seedu.address.logic.commands.FilterEfficiencyCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.FindFreePersonCommand;
import seedu.address.logic.commands.FindTaskCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.HistoryCommand;
Expand Down Expand Up @@ -118,6 +119,9 @@ public Command parseCommand(String userInput) throws ParseException {
case FindTaskCommand.COMMAND_WORD:
return new FindTaskCommandParser().parse(arguments);

case FindFreePersonCommand.COMMAND_WORD:
return new FindFreePersonCommand();

case CommentCommand.COMMAND_WORD:
return new CommentCommandParser().parse(arguments);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Efficiency.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class Efficiency {
public static final String MESSAGE_CONSTRAINTS =
"Efficiency should be in the range 0 to 100";
"Efficiency should be an integer in the range 0 to 100";
public static final String VALIDATION_REGEX = "\\b(\\d{1,2}|100)\\b";
public final String value;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package seedu.address.model.person;

import java.util.function.Predicate;

import seedu.address.commons.util.ToStringBuilder;

/**
* Tests that a {@code Person}'s {@code Name} has no active task.
*/
public class PersonHasNoTaskPredicate implements Predicate<Person> {
public PersonHasNoTaskPredicate() {
}

@Override
public boolean test(Person person) {
return person.isBusy();
}

@Override
public boolean equals(Object other) {
// Check for identity
if (other == this) {
return true;
}

// Check for correct type
return other instanceof PersonHasNoTaskPredicate;
}

@Override
public String toString() {
return new ToStringBuilder(this).add("a", "a").toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package seedu.address.logic.commands;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static seedu.address.logic.Messages.MESSAGE_PERSONS_LISTED_OVERVIEW;
import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess;
import static seedu.address.testutil.TypicalAddressBook.getTypicalAddressBook;

import org.junit.jupiter.api.Test;

import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.UserPrefs;
import seedu.address.model.person.PersonHasNoTaskPredicate;

/**
* Contains integration tests (interaction with the Model) and unit tests for FindFreePersonCommand.
*/
public class FindFreePersonCommandTest {
private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs());
private CommandHistory commandHistory = new CommandHistory();

@Test
public void execute_multiplePersonsFoundWithNoTasks_multiplePersonFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 4);
FindFreePersonCommand command = new FindFreePersonCommand();
expectedModel.updateFilteredPersonList(new PersonHasNoTaskPredicate());
assertCommandSuccess(command, model, commandHistory, expectedMessage, expectedModel);
assertEquals(expectedModel.getFilteredPersonList(), model.getFilteredPersonList());
}

@Test
public void equals() {
FindFreePersonCommand findFirstCommand = new FindFreePersonCommand();
FindFreePersonCommand findSecondCommand = new FindFreePersonCommand();

// same object -> returns true
assertTrue(findFirstCommand.equals(findFirstCommand));

// different types -> returns false
assertFalse(findFirstCommand.equals(1));

// null -> returns false
assertFalse(findFirstCommand.equals(null));

// same type -> returns true
assertTrue(findFirstCommand.equals(findSecondCommand));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import seedu.address.logic.commands.FilterCommand;
import seedu.address.logic.commands.FilterEfficiencyCommand;
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.FindFreePersonCommand;
import seedu.address.logic.commands.FindTaskCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.HistoryCommand;
Expand Down Expand Up @@ -177,6 +178,12 @@ public void parseCommand_comment() throws Exception {
assertEquals(new CommentCommand(INDEX_FIRST_PERSON, new Comment("good communication")), command);
}

@Test
public void parseCommand_findFreePerson() throws Exception {
assertTrue(parser.parseCommand(FindFreePersonCommand.COMMAND_WORD) instanceof FindFreePersonCommand);
assertTrue(parser.parseCommand(FindFreePersonCommand.COMMAND_WORD + " 3") instanceof FindFreePersonCommand);
}

@Test
public void parseCommand_unrecognisedInput_throwsParseException() {
assertThrows(ParseException.class, String.format(MESSAGE_INVALID_COMMAND_FORMAT, HelpCommand.MESSAGE_USAGE), ()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package seedu.address.model.person;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import seedu.address.testutil.PersonBuilder;

class PersonHasNoTaskPredicateTest {

@Test
public void equals() {
PersonHasNoTaskPredicate firstPredicate = new PersonHasNoTaskPredicate();
PersonHasNoTaskPredicate secondPredicate = new PersonHasNoTaskPredicate();

// same object -> returns true
assertTrue(firstPredicate.equals(firstPredicate));

// different types -> returns false
assertFalse(firstPredicate.equals(1));

// null -> returns false
assertFalse(firstPredicate.equals(null));

// same class -> returns true, since there's no state to differ
assertTrue(firstPredicate.equals(secondPredicate));
}

@Test
public void test_personIsNotBusy_returnsFalse() {
// Person is not busy
PersonHasNoTaskPredicate predicate = new PersonHasNoTaskPredicate();
assertFalse(predicate.test(new PersonBuilder().build()));
}

@Test
public void toStringMethod() {
PersonHasNoTaskPredicate predicate = new PersonHasNoTaskPredicate();

String expected = "PersonHasNoTaskPredicate";
assertTrue(predicate.toString().contains(expected));
}
}

0 comments on commit 7ea9711

Please sign in to comment.