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#49 from juliantayyc/add-stu…
…dent-grade Add student grade
- Loading branch information
Showing
7 changed files
with
199 additions
and
3 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
108 changes: 108 additions & 0 deletions
108
src/main/java/seedu/address/logic/commands/GradeCommand.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,108 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRADE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_MODULE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_STUDENTID; | ||
|
||
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.Grade; | ||
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 GradeCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "grade"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Assigns a course-specific grade to a student. " | ||
+ "Parameters: " | ||
+ PREFIX_STUDENTID + "ID " | ||
+ PREFIX_MODULE + "MODULE " | ||
+ PREFIX_GRADE + "GRADE " | ||
+ "\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_STUDENTID + "12345678 " | ||
+ PREFIX_MODULE + "CS2103T " | ||
+ PREFIX_GRADE + "A+ "; | ||
|
||
public static final String MESSAGE_SUCCESS = "New grade added for %1$s"; | ||
public static final String MESSAGE_PERSON_NOT_FOUND = "This person does not exist in the address book"; | ||
|
||
public static final String DUPLICATE_MODULE = "The module %1$s already exists for this student"; | ||
|
||
private final Module module; | ||
private final StudentId studentId; | ||
private final Grade grade; | ||
|
||
/** | ||
* Creates an AddCommand to add the specified {@code Person} | ||
*/ | ||
public GradeCommand(StudentId studentId, Module module, Grade grade) { | ||
requireNonNull(studentId); | ||
requireNonNull(module); | ||
requireNonNull(grade); | ||
this.module = module; | ||
this.studentId = studentId; | ||
this.grade = grade; | ||
} | ||
|
||
@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> moduleGrades = person.getModuleGrades(); | ||
if (moduleGrades.contains(module)) { | ||
throw new CommandException(String.format(DUPLICATE_MODULE, module)); | ||
} | ||
|
||
person.addModuleGrade(module, grade); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, module)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof GradeCommand)) { | ||
return false; | ||
} | ||
|
||
GradeCommand otherGradeCommand = (GradeCommand) other; | ||
return module.equals(otherGradeCommand.module); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.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
51 changes: 51 additions & 0 deletions
51
src/main/java/seedu/address/logic/parser/GradeCommandParser.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,51 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_GRADE; | ||
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.GradeCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.person.Grade; | ||
import seedu.address.model.person.Module; | ||
import seedu.address.model.person.StudentId; | ||
|
||
/** | ||
* Parses input arguments and creates a new AddCommand object | ||
*/ | ||
public class GradeCommandParser implements Parser<GradeCommand> { | ||
|
||
/** | ||
* Parses the given {@code String} of arguments in the context of the AddCommand | ||
* and returns an AddCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public GradeCommand parse(String args) throws ParseException { | ||
ArgumentMultimap argMultimap = | ||
ArgumentTokenizer.tokenize(args, PREFIX_STUDENTID, PREFIX_MODULE, PREFIX_GRADE); | ||
|
||
if (!arePrefixesPresent(argMultimap, PREFIX_STUDENTID, PREFIX_MODULE, PREFIX_GRADE) | ||
|| !argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, GradeCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_STUDENTID, PREFIX_MODULE, PREFIX_GRADE); | ||
StudentId studentId = ParserUtil.parseStudentId(argMultimap.getValue(PREFIX_STUDENTID).get()); | ||
Grade grade = ParserUtil.parseGrade(argMultimap.getValue(PREFIX_GRADE).get()); | ||
Module module = ParserUtil.parseModule(argMultimap.getValue(PREFIX_MODULE).get()); | ||
|
||
return new GradeCommand(studentId, module, grade); | ||
} | ||
|
||
/** | ||
* 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