Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103T-F15-2#49 from juliantayyc/add-stu…
Browse files Browse the repository at this point in the history
…dent-grade

Add student grade
  • Loading branch information
btbrandon authored Oct 10, 2024
2 parents e4a99f3 + f4bb6ef commit 9df9e6b
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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&#41**)
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!

Expand Down
108 changes: 108 additions & 0 deletions src/main/java/seedu/address/logic/commands/GradeCommand.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -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/");
}
51 changes: 51 additions & 0 deletions src/main/java/seedu/address/logic/parser/GradeCommandParser.java
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());
}

}
32 changes: 32 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/person/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Module {
private Grade grade;

/**
* Constructs an {@code Module}.
* Constructs a {@code Module}.
*
* @param module A valid Module.
*/
Expand Down

0 comments on commit 9df9e6b

Please sign in to comment.