Skip to content

Commit

Permalink
Merge pull request #193
Browse files Browse the repository at this point in the history
Fix bugs: Check duplicate task
  • Loading branch information
NereusWB922 authored Oct 30, 2023
2 parents d742fa2 + b4aed85 commit bfc5e4b
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 120 deletions.
2 changes: 0 additions & 2 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ public class Messages {

public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_DUPLICATE_FIELDS =
"Multiple values specified for the following single-valued field(s): ";

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

/**
* Enum of create task categories.
*/
public enum Category {
NONE, ALLSTU, ALLGRP
}
120 changes: 87 additions & 33 deletions src/main/java/seedu/address/logic/commands/CreateDeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,31 @@ public class CreateDeadlineCommand extends Command {

public static final String MESSAGE_SUCCESS_ALL_STUDENTS =
"New Deadline task added to all students in group: %1$s";
public static final String MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING =
"Warning: Some student(s) already have the task. \n"
+ "New Deadline task has been added to the rest.";
public static final String MESSAGE_SUCCESS_ALL_GROUPS =
"New Deadline task added to all groups in root: %1$s";
public static final String MESSAGE_SUCCESS_ALL_GROUPS_WITH_WARNING =
"Warning: Some group(s) already have the task. \n"
+ "New Deadline task has been added to the rest.";
public static final String MESSAGE_DUPLICATE_DEADLINE_TASK =
"This Deadline task has already been allocated";
public static final String MESSAGE_PATH_NOT_FOUND = "Path does not exist in ProfBook.";
public static final String MESSAGE_NOT_TASK_MANAGER = "Cannot create task for this path.";
public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "All stu flag is only allowed for group path";
public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "All Group flag is only allowed for root path";
public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "AllStu flag is only allowed for root and group path";
public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "AllGrp flag is only allowed for root path";
public static final String MESSAGE_ALL_CHILDREN_HAVE_TASK = "All %1$ss already have the task.";

private final AbsolutePath path;
private final Deadline deadline;
private String category = null;

/**
* Creates an CreateDeadlineCommand to add the Deadline Task for a specified {@code Student} or {@code Group}
*/
public CreateDeadlineCommand(AbsolutePath path, Deadline deadline) {
requireAllNonNull(path, deadline);
this.path = path;
this.deadline = deadline;
}
private Category category;

/**
* Creates an CreateDeadlineCommand to add the Deadline Task for a specified {@code Student} or {@code Group}
* User has input a category as well.
*/
public CreateDeadlineCommand(AbsolutePath path, Deadline deadline, String category) {
public CreateDeadlineCommand(AbsolutePath path, Deadline deadline, Category category) {
requireAllNonNull(path, deadline, category);
this.path = path;
this.deadline = deadline;
Expand All @@ -82,44 +80,100 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_PATH_NOT_FOUND);
}

if (this.category == null) {
// Check target path is task manager
if (!model.hasTaskListInPath(path)) {
throw new CommandException(MESSAGE_NOT_TASK_MANAGER);
}
switch(category) {
case NONE:
return handleNone(model);
case ALLSTU:
return handleAllStu(model);
default:
return handleAllGrp(model);
}
}

TaskOperation target = model.taskOperation(path);
private CommandResult handleNone(Model model) throws CommandException {
// Check target path is task manager
if (!model.hasTaskListInPath(path)) {
throw new CommandException(MESSAGE_NOT_TASK_MANAGER);
}

// Check duplicate deadline
if (target.hasTask(this.deadline)) {
throw new CommandException(MESSAGE_DUPLICATE_DEADLINE_TASK);
}
TaskOperation target = model.taskOperation(path);

target.addTask(this.deadline);
model.updateList();
// Check duplicate deadline
if (target.hasTask(this.deadline)) {
throw new CommandException(MESSAGE_DUPLICATE_DEADLINE_TASK);
}

return new CommandResult(String.format(MESSAGE_SUCCESS, this.deadline));
target.addTask(this.deadline);
model.updateList();

return new CommandResult(String.format(MESSAGE_SUCCESS, this.deadline));
}

private CommandResult handleAllStu(Model model) throws CommandException {
if (path.isStudentDirectory()) {
throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU);
}

if (this.category.equals("allStu")) {
if (!path.isGroupDirectory()) {
throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU);
}
if (path.isGroupDirectory()) {
ChildOperation<Student> groupOper = model.groupChildOperation(path);

// Check whether all children already have the task
if (groupOper.checkIfAllChildrenHaveTask(deadline, 1)) {
throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student"));
}

// Check whether at least one of the children has the task
boolean warning = false;
if (groupOper.checkIfAnyChildHasTask(deadline, 1)) {
warning = true;
}

groupOper.addTaskToAllChildren(deadline, 1);
model.updateList();
return new CommandResult(MESSAGE_SUCCESS_ALL_STUDENTS);
return new CommandResult(
warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS);
}

ChildOperation<Group> operation = model.rootChildOperation();

// Check whether all children already have the task
if (operation.checkIfAllChildrenHaveTask(deadline, 2)) {
throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student"));
}

// Check whether at least one of the children has the task
boolean warning = false;
if (operation.checkIfAnyChildHasTask(deadline, 2)) {
warning = true;
}

operation.addTaskToAllChildren(deadline, 2);
model.updateList();
return new CommandResult(
warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS);
}

private CommandResult handleAllGrp(Model model) throws CommandException {
if (!path.isRootDirectory()) {
throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_GROUP);
}

ChildOperation<Group> rootOper = model.rootChildOperation();

// Check whether all children already have the task
if (rootOper.checkIfAllChildrenHaveTask(deadline, 1)) {
throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "group"));
}

// Check whether at least one of the children has the task
boolean warning = false;
if (rootOper.checkIfAnyChildHasTask(deadline, 1)) {
warning = true;
}

rootOper.addTaskToAllChildren(deadline, 1);
model.updateList();

return new CommandResult(MESSAGE_SUCCESS_ALL_GROUPS);
return new CommandResult(warning ? MESSAGE_SUCCESS_ALL_GROUPS_WITH_WARNING : MESSAGE_SUCCESS_ALL_GROUPS);
}

/**
Expand Down
121 changes: 88 additions & 33 deletions src/main/java/seedu/address/logic/commands/CreateTodoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,26 @@ public class CreateTodoCommand extends Command {
"This ToDo task has already been allocated to this group in ProfBook";
public static final String MESSAGE_SUCCESS_ALL_STUDENTS =
"New ToDo task added to all students in group: %1$s";
public static final String MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING =
"Warning: Some student(s) already have the task. \n"
+ "New ToDo task has been added to the rest.";
public static final String MESSAGE_SUCCESS_ALL_GROUPS =
"New ToDo task added to all groups in root: %1$s";

public static final String MESSAGE_SUCCESS_ALL_GROUPS_WITH_WARNING =
"Warning: Some group(s) already have the task. \n"
+ "New ToDo task has been added to the rest.";
public static final String MESSAGE_ERROR = "Invalid target encountered while creating this todo task";
public static final String MESSAGE_SUCCESS = "New ToDo task has been added to: %1$s";
public static final String MESSAGE_PATH_NOT_FOUND = "Path does not exist in ProfBook.";
public static final String MESSAGE_NOT_TASK_MANAGER = "Cannot create task for this path.";
public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "All stu flag is only allowed for group path";
public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "All Group flag is only allowed for root path";
public static final String MESSAGE_INVALID_PATH_FOR_ALL_STU = "AllStu flag is only allowed for root and group path";
public static final String MESSAGE_INVALID_PATH_FOR_ALL_GROUP = "AllGrp flag is only allowed for root path";
public static final String MESSAGE_ALL_CHILDREN_HAVE_TASK = "All %1$ss already have the task.";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": student";

private final AbsolutePath target;
private final ToDo todo;
private String category = null;

/**
* Constructs a {@code CreateTodoCommand} with the specified relative path and "ToDo" task details.
*
* @param relativePath The relative path to the group where the "ToDo" task will be added.
* @param todo The details of the "ToDo" task to be created.
*/
public CreateTodoCommand(AbsolutePath target, ToDo todo) {
requireAllNonNull(target, todo);
this.target = target;
this.todo = todo;
}
private Category category;

/**
* Constructs a {@code CreateTodoCommand} with the specified absolute path and "ToDo" task details.
Expand All @@ -63,7 +57,7 @@ public CreateTodoCommand(AbsolutePath target, ToDo todo) {
* @param todo The details of the "ToDo" task to be created.
* @param category The specific category of people to add ToDo task to each.
*/
public CreateTodoCommand(AbsolutePath target, ToDo todo, String category) {
public CreateTodoCommand(AbsolutePath target, ToDo todo, Category category) {
requireAllNonNull(target, todo, category);
this.target = target;
this.todo = todo;
Expand All @@ -80,35 +74,96 @@ public CreateTodoCommand(AbsolutePath target, ToDo todo, String category) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
if (this.category == null) {
TaskOperation taskOperation = model.taskOperation(target);
if (taskOperation.hasTask(this.todo)) {
throw new CommandException(MESSAGE_DUPLICATE_TODO_TASK_STUDENT);
}
taskOperation.addTask(this.todo);
model.updateList();
return new CommandResult(String.format(MESSAGE_SUCCESS, target));

switch(category) {
case NONE:
return handleNone(model);
case ALLSTU:
return handleAllStu(model);
default:
return handleAllGrp(model);
}
}

if (this.category.equals("allStu")) {
if (!target.isGroupDirectory()) {
throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU);
}
private CommandResult handleNone(Model model) throws CommandException {
// Check path exists in ProfBook
if (!model.hasPath(target)) {
throw new CommandException(MESSAGE_PATH_NOT_FOUND);
}

TaskOperation taskOperation = model.taskOperation(target);
if (taskOperation.hasTask(this.todo)) {
throw new CommandException(MESSAGE_DUPLICATE_TODO_TASK_STUDENT);
}
taskOperation.addTask(this.todo);
model.updateList();
return new CommandResult(String.format(MESSAGE_SUCCESS, target));
}

private CommandResult handleAllStu(Model model) throws CommandException {
if (target.isStudentDirectory()) {
throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_STU);
}

if (target.isGroupDirectory()) {
ChildOperation<Student> groupOper = model.groupChildOperation(target);

// Check whether all children already have the task
if (groupOper.checkIfAllChildrenHaveTask(todo, 1)) {
throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student"));
}

// Check whether at least one of the children has the task
boolean warning = false;
if (groupOper.checkIfAnyChildHasTask(todo, 1)) {
warning = true;
}

groupOper.addTaskToAllChildren(todo, 1);
model.updateList();
return new CommandResult(MESSAGE_SUCCESS_ALL_STUDENTS);
return new CommandResult(
warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS);
}

ChildOperation<Group> operation = model.rootChildOperation();

// Check whether all children already have the task
if (operation.checkIfAllChildrenHaveTask(todo, 2)) {
throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "student"));
}

// Check whether at least one of the children has the task
boolean warning = false;
if (operation.checkIfAnyChildHasTask(todo, 2)) {
warning = true;
}

operation.addTaskToAllChildren(todo, 2);
model.updateList();
return new CommandResult(
warning ? MESSAGE_SUCCESS_ALL_STUDENTS_WITH_WARNING : MESSAGE_SUCCESS_ALL_STUDENTS);
}

private CommandResult handleAllGrp(Model model) throws CommandException {
if (!target.isRootDirectory()) {
throw new CommandException(MESSAGE_INVALID_PATH_FOR_ALL_GROUP);
}
ChildOperation<Group> rootOper = model.rootChildOperation();
rootOper.addTaskToAllChildren(todo, 1);

model.updateList();
// Check whether all children already have the task
if (rootOper.checkIfAllChildrenHaveTask(todo, 1)) {
throw new CommandException(String.format(MESSAGE_ALL_CHILDREN_HAVE_TASK, "group"));
}

// Check whether at least one of the children has the task
boolean warning = false;
if (rootOper.checkIfAnyChildHasTask(todo, 1)) {
warning = true;
}

return new CommandResult(MESSAGE_SUCCESS_ALL_GROUPS);
rootOper.addTaskToAllChildren(todo, 1);
model.updateList();
return new CommandResult(warning ? MESSAGE_SUCCESS_ALL_GROUPS_WITH_WARNING : MESSAGE_SUCCESS_ALL_GROUPS);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.time.LocalDateTime;

import seedu.address.logic.commands.Category;
import seedu.address.logic.commands.CreateDeadlineCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.path.AbsolutePath;
Expand Down Expand Up @@ -50,10 +51,9 @@ public CreateDeadlineCommand parse(String args, AbsolutePath currPath) throws Pa
Deadline deadline = new Deadline(argMultimap.getValue(OPTION_DESC).get(), by);

if (argMultimap.getValue(OPTION_ALL).isEmpty()) {
return new CreateDeadlineCommand(targetPath, deadline);
} else {
String category = ParserUtil.parseCategory(argMultimap.getValue(OPTION_ALL).get());
return new CreateDeadlineCommand(targetPath, deadline, category);
return new CreateDeadlineCommand(targetPath, deadline, Category.NONE);
}
Category category = ParserUtil.parseCategory(argMultimap.getValue(OPTION_ALL).get());
return new CreateDeadlineCommand(targetPath, deadline, category);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.CliSyntax.OPTION_ALL;
import static seedu.address.logic.parser.CliSyntax.OPTION_DESC;

import seedu.address.logic.commands.Category;
import seedu.address.logic.commands.CreateTodoCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.path.AbsolutePath;
Expand Down Expand Up @@ -47,10 +48,9 @@ public CreateTodoCommand parse(String args, AbsolutePath currPath) throws ParseE
ToDo todo = new ToDo(argMultimap.getValue(OPTION_DESC).get());

if (argMultimap.getValue(OPTION_ALL).isEmpty()) {
return new CreateTodoCommand(targetPath, todo);
} else {
String category = ParserUtil.parseCategory(argMultimap.getValue(OPTION_ALL).get());
return new CreateTodoCommand(targetPath, todo, category);
return new CreateTodoCommand(targetPath, todo, Category.NONE);
}
Category category = ParserUtil.parseCategory(argMultimap.getValue(OPTION_ALL).get());
return new CreateTodoCommand(targetPath, todo, category);
}
}
Loading

0 comments on commit bfc5e4b

Please sign in to comment.