-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new class CreateHomeworkCommandParser that parses user input and creates a new CreateHomeworkCommand object. The parser validates the user input and throws a ParseException if the input does not conform to the expected format. The CreateHomeworkCommand contains a NameContainsKeywordsPredicate, a homework name, and a deadline.
- Loading branch information
Showing
31 changed files
with
373 additions
and
109 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
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
83 changes: 83 additions & 0 deletions
83
src/main/java/seedu/address/logic/commands/CreateHomeworkCommand.java
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,83 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.student.Homework; | ||
import seedu.address.model.student.NameContainsKeywordsPredicate; | ||
import seedu.address.model.student.Student; | ||
|
||
/** | ||
* Adds an assignment to a student. | ||
*/ | ||
public class CreateHomeworkCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "assign-homework"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds an assignment to a student.\n" | ||
+ "Parameters: " | ||
+ "s/STUDENT_NAME " | ||
+ "hw/HOMEWORK_NAME " | ||
+ "d/DEADLINE\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ "s/John Doe " | ||
+ "as/Math Homework " | ||
+ "d/2023-03-01T12:00"; | ||
|
||
public static final String MESSAGE_SUCCESS = "New homework added: %1$s"; | ||
public static final String MESSAGE_STUDENT_NOT_FOUND = "The specified student cannot be found in the address book"; | ||
|
||
private final String homeworkName; | ||
private final LocalDateTime deadline; | ||
private final NameContainsKeywordsPredicate predicate; | ||
|
||
/** | ||
* Creates a CreateHomeworkCommand to add the specified assignment to the specified student. | ||
*/ | ||
public CreateHomeworkCommand(NameContainsKeywordsPredicate predicate, String homeworkName, LocalDateTime deadline) { | ||
requireNonNull(homeworkName); | ||
requireNonNull(deadline); | ||
requireNonNull(predicate); | ||
|
||
this.homeworkName = homeworkName; | ||
this.deadline = deadline; | ||
this.predicate = predicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
model.updateFilteredStudentList(predicate); | ||
|
||
List<Student> studentList = model.getFilteredStudentList(); | ||
|
||
if (deadline.isBefore(LocalDateTime.now())) { | ||
throw new CommandException("Deadline must be in the future."); | ||
} | ||
|
||
Homework homework = new Homework(homeworkName, deadline); | ||
|
||
// Add the Homework to the target student's list of Assignments | ||
for (Student student : studentList) { | ||
student.addHomework(homework); | ||
} | ||
|
||
return new CommandResult( | ||
String.format(Messages.MESSAGE_HOMEWORK_ADDED_SUCCESS, homework, model.getFilteredStudentList())); | ||
} | ||
|
||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof CreateHomeworkCommand // instanceof handles nulls | ||
&& predicate.equals(((CreateHomeworkCommand) other).predicate) | ||
&& homeworkName.equals(((CreateHomeworkCommand) other).homeworkName) | ||
&& deadline.equals(((CreateHomeworkCommand) other).deadline)); | ||
} | ||
} |
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
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
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
52 changes: 52 additions & 0 deletions
52
src/main/java/seedu/address/logic/parser/CreateHomeworkCommandParser.java
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,52 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_DEADLINE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_HOMEWORK; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.CreateHomeworkCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.student.NameContainsKeywordsPredicate; | ||
|
||
/** | ||
* Parses input arguments and creates a new CreateHomeworkCommand object | ||
*/ | ||
public class CreateHomeworkCommandParser implements Parser<CreateHomeworkCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the CreateHomeworkCommand | ||
* and returns a CreateHomeworkCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public CreateHomeworkCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
|
||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_HOMEWORK, PREFIX_DEADLINE); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_HOMEWORK, PREFIX_DEADLINE) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
CreateHomeworkCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
String homeworkName = argMultimap.getValue(PREFIX_HOMEWORK).get(); | ||
LocalDateTime deadline = ParserUtil.parseDeadline(argMultimap.getValue(PREFIX_DEADLINE).get()); | ||
List<String> nameKeywords = argMultimap.getAllValues(PREFIX_NAME); | ||
|
||
return new CreateHomeworkCommand(new NameContainsKeywordsPredicate(nameKeywords), | ||
homeworkName, deadline); | ||
} | ||
|
||
/** | ||
* Returns true if all prefixes are present in the given {@code ArgumentMultimap}. | ||
*/ | ||
private static boolean arePrefixesPresent(ArgumentMultimap argumentMultimap, Prefix... prefixes) { | ||
return Stream.of(prefixes).allMatch(prefix -> argumentMultimap.getValue(prefix).isPresent()); | ||
} | ||
} |
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
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
31 changes: 0 additions & 31 deletions
31
src/main/java/seedu/address/model/student/AssignmentContainsKeywordPredicate.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.