forked from AY2425S1-CS2103T-F15-2/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-F15-2#61 from JYL27/branch-AddModule
Add Module Command
- Loading branch information
Showing
23 changed files
with
809 additions
and
53 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
103 changes: 103 additions & 0 deletions
103
src/main/java/seedu/address/logic/commands/ModuleCommand.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,103 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Module; | ||
import seedu.address.model.person.Person; | ||
import seedu.address.model.person.StudentId; | ||
|
||
/** | ||
* Assigns a course-specific grade to a student. | ||
*/ | ||
public class ModuleCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "module"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a module to a student. " | ||
+ "Parameters: " | ||
+ PREFIX_STUDENTID + "ID " | ||
+ PREFIX_MODULE + "MODULE " | ||
+ "\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_STUDENTID + "12345678 " | ||
+ PREFIX_MODULE + "CS2103T "; | ||
|
||
public static final String MESSAGE_SUCCESS = "New module added for Student %1$s"; | ||
public static final String MESSAGE_PERSON_NOT_FOUND = "This person does not exist in the address book"; | ||
public static final String MESSAGE_DUPLICATE_MODULE = "The module %1$s already exists for this student"; | ||
|
||
private final Module module; | ||
private final StudentId studentId; | ||
|
||
/** | ||
* Creates a ModuleCommand to add the specified {@code Module} | ||
*/ | ||
public ModuleCommand(StudentId studentId, Module module) { | ||
requireNonNull(studentId); | ||
requireNonNull(module); | ||
this.module = module; | ||
this.studentId = studentId; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
List<Person> lastShownList = model.getFilteredPersonList(); | ||
|
||
Person person = null; | ||
|
||
for (Person p : lastShownList) { | ||
if (p.getStudentId().equals(studentId)) { | ||
person = p; | ||
break; | ||
} | ||
} | ||
|
||
if (person == null) { | ||
throw new CommandException(MESSAGE_PERSON_NOT_FOUND); | ||
} | ||
|
||
ArrayList<Module> modules = person.getModules(); | ||
if (modules.contains(module)) { | ||
throw new CommandException(String.format(MESSAGE_DUPLICATE_MODULE, module.value)); | ||
} | ||
|
||
Person updatedPerson = person.addModule(module); | ||
model.setPerson(person, updatedPerson); | ||
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, studentId)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof ModuleCommand)) { | ||
return false; | ||
} | ||
|
||
ModuleCommand otherModuleCommand = (ModuleCommand) other; | ||
return module.equals(otherModuleCommand.module); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("studentId", studentId) | ||
.add("toAdd", module) | ||
.toString(); | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
src/main/java/seedu/address/logic/parser/ModuleCommandParser.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,48 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import seedu.address.logic.commands.ModuleCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.Module; | ||
import seedu.address.model.person.StudentId; | ||
|
||
/** | ||
* Parses input arguments and creates a new ModuleCommand object | ||
*/ | ||
public class ModuleCommandParser implements Parser<ModuleCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the ModuleCommand | ||
* and returns a ModuleCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public ModuleCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_STUDENTID, PREFIX_MODULE); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_STUDENTID, PREFIX_MODULE) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ModuleCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_STUDENTID, PREFIX_MODULE); | ||
StudentId studentId = ParserUtil.parseStudentId(argMultimap.getValue(PREFIX_STUDENTID).get()); | ||
Module module = ParserUtil.parseModule(argMultimap.getValue(PREFIX_MODULE).get()); | ||
|
||
return new ModuleCommand(studentId, module); | ||
} | ||
|
||
/** | ||
* Returns true if none of the prefixes contains empty {@code Optional} values 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
Oops, something went wrong.