From f6b4677a80331d32fb0938ce95829b0c41d47ec5 Mon Sep 17 00:00:00 2001 From: Julian Date: Wed, 9 Oct 2024 21:56:53 +0800 Subject: [PATCH 1/3] Fix syntax in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 695554daec6..91fc721873c 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ school LMS (Learning Management Systems). ## Getting Started -[//]: For the detailed documentation of this project, see the **[EduContacts Product Website](https://ay2425s1-cs2103t-f15-2.github.io/tp/index.html)**) +For the detailed documentation of this project, see the **[EduContacts Product Website](https://ay2425s1-cs2103t-f15-2.github.io/tp/index.html)** Our product website is currently in development. Check back soon! From 2919069408b837c0576552caa1c3828a3ed364ab Mon Sep 17 00:00:00 2001 From: Julian Date: Thu, 10 Oct 2024 03:15:57 +0800 Subject: [PATCH 2/3] Add grade command logic (excluding storage) --- .../address/logic/commands/GradeCommand.java | 110 ++++++++++++++++++ .../logic/parser/AddressBookParser.java | 4 + .../seedu/address/logic/parser/CliSyntax.java | 3 +- .../logic/parser/GradeCommandParser.java | 52 +++++++++ .../address/logic/parser/ParserUtil.java | 32 +++++ .../seedu/address/model/person/Module.java | 2 +- 6 files changed, 201 insertions(+), 2 deletions(-) create mode 100644 src/main/java/seedu/address/logic/commands/GradeCommand.java create mode 100644 src/main/java/seedu/address/logic/parser/GradeCommandParser.java diff --git a/src/main/java/seedu/address/logic/commands/GradeCommand.java b/src/main/java/seedu/address/logic/commands/GradeCommand.java new file mode 100644 index 00000000000..17abb71f407 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/GradeCommand.java @@ -0,0 +1,110 @@ +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.Person; +import seedu.address.model.person.StudentId; +import seedu.address.model.person.Module; + +/** + * 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 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 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(); + } +} diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index 3149ee07e0b..f52bbc06bed 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -15,6 +15,7 @@ import seedu.address.logic.commands.EditCommand; import seedu.address.logic.commands.ExitCommand; import seedu.address.logic.commands.FindCommand; +import seedu.address.logic.commands.GradeCommand; import seedu.address.logic.commands.HelpCommand; import seedu.address.logic.commands.ListCommand; import seedu.address.logic.parser.exceptions.ParseException; @@ -62,6 +63,9 @@ public Command parseCommand(String userInput) throws ParseException { case DeleteCommand.COMMAND_WORD: return new DeleteCommandParser().parse(arguments); + case GradeCommand.COMMAND_WORD: + return new GradeCommandParser().parse(arguments); + case ClearCommand.COMMAND_WORD: return new ClearCommand(); diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index f2214d37079..c07c781e7fb 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -13,5 +13,6 @@ public class CliSyntax { public static final Prefix PREFIX_ADDRESS = new Prefix("a/"); public static final Prefix PREFIX_COURSE = new Prefix("c/"); public static final Prefix PREFIX_TAG = new Prefix("t/"); - + public static final Prefix PREFIX_MODULE = new Prefix("m/"); + public static final Prefix PREFIX_GRADE = new Prefix("g/"); } diff --git a/src/main/java/seedu/address/logic/parser/GradeCommandParser.java b/src/main/java/seedu/address/logic/parser/GradeCommandParser.java new file mode 100644 index 00000000000..3c790a61912 --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/GradeCommandParser.java @@ -0,0 +1,52 @@ +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.Module; +import seedu.address.model.person.StudentId; +import seedu.address.model.person.Grade; + +/** + * Parses input arguments and creates a new AddCommand object + */ +public class GradeCommandParser implements Parser { + + /** + * 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()); + } + +} diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index fc0cc76c505..20d1f0e9e67 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -12,6 +12,8 @@ import seedu.address.model.person.Address; import seedu.address.model.person.Course; import seedu.address.model.person.Email; +import seedu.address.model.person.Grade; +import seedu.address.model.person.Module; import seedu.address.model.person.Name; import seedu.address.model.person.Phone; import seedu.address.model.person.StudentId; @@ -153,4 +155,34 @@ public static Course parseCourse(String course) throws ParseException { } return new Course(trimmedCourse); } + + /** + * Parses a {@code String module} into a {@code Module}. + * Leading and trailing whitespaces will be trimmed. + * + * @throws ParseException if the given {@code course} is invalid. + */ + public static Module parseModule(String module) throws ParseException { + requireNonNull(module); + String trimmedModule = module.trim(); + if (!Module.isValidModule(trimmedModule)) { + throw new ParseException(Module.MESSAGE_CONSTRAINTS); + } + return new Module(trimmedModule); + } + + /** + * Parses a {@code String grade} into a {@code Grade}. + * Leading and trailing whitespaces will be trimmed. + * + * @throws ParseException if the given {@code grade} is invalid. + */ + public static Grade parseGrade(String grade) throws ParseException { + requireNonNull(grade); + String trimmedGrade = grade.trim(); + if (!Grade.isValidGrade(trimmedGrade)) { + throw new ParseException(Grade.MESSAGE_CONSTRAINTS); + } + return new Grade(trimmedGrade); + } } diff --git a/src/main/java/seedu/address/model/person/Module.java b/src/main/java/seedu/address/model/person/Module.java index b4334c27412..dbe9550566e 100644 --- a/src/main/java/seedu/address/model/person/Module.java +++ b/src/main/java/seedu/address/model/person/Module.java @@ -22,7 +22,7 @@ public class Module { private Grade grade; /** - * Constructs an {@code Module}. + * Constructs a {@code Module}. * * @param module A valid Module. */ From f4bb6ef98c718523f4236027be89b8456d741c33 Mon Sep 17 00:00:00 2001 From: Julian Date: Thu, 10 Oct 2024 22:35:49 +0800 Subject: [PATCH 3/3] Fix checkstyle issues --- src/main/java/seedu/address/logic/commands/GradeCommand.java | 4 +--- .../java/seedu/address/logic/parser/GradeCommandParser.java | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/GradeCommand.java b/src/main/java/seedu/address/logic/commands/GradeCommand.java index 17abb71f407..9cdf1fd94cf 100644 --- a/src/main/java/seedu/address/logic/commands/GradeCommand.java +++ b/src/main/java/seedu/address/logic/commands/GradeCommand.java @@ -1,12 +1,10 @@ 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; @@ -14,9 +12,9 @@ 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; -import seedu.address.model.person.Module; /** * Assigns a course-specific grade to a student. diff --git a/src/main/java/seedu/address/logic/parser/GradeCommandParser.java b/src/main/java/seedu/address/logic/parser/GradeCommandParser.java index 3c790a61912..54d964544d8 100644 --- a/src/main/java/seedu/address/logic/parser/GradeCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/GradeCommandParser.java @@ -7,12 +7,11 @@ 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; -import seedu.address.model.person.Grade; /** * Parses input arguments and creates a new AddCommand object