Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103T-F15-2#61 from JYL27/branch-AddModule
Browse files Browse the repository at this point in the history
Add Module Command
  • Loading branch information
btbrandon authored Oct 16, 2024
2 parents a7b2f02 + de8ef8d commit 8dbf3fd
Show file tree
Hide file tree
Showing 23 changed files with 809 additions and 53 deletions.
21 changes: 12 additions & 9 deletions src/main/java/seedu/address/logic/commands/GradeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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 static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -36,15 +37,14 @@ public class GradeCommand extends Command {

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";
public static final String MESSAGE_MODULE_NOT_FOUND = "The student does not take the module %1$s";

private final Module module;
private final StudentId studentId;
private final Grade grade;

/**
* Creates an AddCommand to add the specified {@code Person}
* Creates a GradeCommand to assign a grade to the specified {@code Module}
*/
public GradeCommand(StudentId studentId, Module module, Grade grade) {
requireNonNull(studentId);
Expand Down Expand Up @@ -74,13 +74,14 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_PERSON_NOT_FOUND);
}

ArrayList<Module> moduleGrades = person.getModuleGrades();
if (moduleGrades.contains(module)) {
throw new CommandException(String.format(DUPLICATE_MODULE, module));
ArrayList<Module> modules = person.getModules();
if (!modules.contains(module)) {
throw new CommandException(String.format(MESSAGE_MODULE_NOT_FOUND, module.value));
}

person.addModuleGrade(module, grade);

Person updatedPerson = person.setModuleGrade(module, grade);
model.setPerson(person, updatedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_SUCCESS, module));
}

Expand All @@ -102,7 +103,9 @@ public boolean equals(Object other) {
@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", module)
.add("studentId", studentId)
.add("module", module)
.add("toAdd", grade)
.toString();
}
}
103 changes: 103 additions & 0 deletions src/main/java/seedu/address/logic/commands/ModuleCommand.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import seedu.address.logic.commands.GradeCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ModuleCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -81,6 +82,9 @@ public Command parseCommand(String userInput) throws ParseException {
case HelpCommand.COMMAND_WORD:
return new HelpCommand();

case ModuleCommand.COMMAND_WORD:
return new ModuleCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import seedu.address.model.person.StudentId;

/**
* Parses input arguments and creates a new AddCommand object
* Parses input arguments and creates a new GradeCommand 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.
* Parses the given {@code String} of arguments in the context of the GradeCommand
* and returns a GradeCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public GradeCommand parse(String args) throws ParseException {
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/seedu/address/logic/parser/ModuleCommandParser.java
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());
}

}
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/model/person/Grade.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Grade {
"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "D+", "D", "F"
};

public final String grade;
public final String value;

/**
* Constructs an {@code Grade}.
Expand All @@ -27,7 +27,7 @@ public class Grade {
public Grade(String grade) {
requireNonNull(grade);
checkArgument(isValidGrade(grade), MESSAGE_CONSTRAINTS);
this.grade = grade.toUpperCase(); // Store as uppercase
this.value = grade.toUpperCase(); // Store as uppercase
}

/**
Expand All @@ -45,7 +45,7 @@ public static boolean isValidGrade(String test) {

@Override
public String toString() {
return grade;
return value;
}

@Override
Expand All @@ -60,11 +60,11 @@ public boolean equals(Object other) {
}

Grade otherGrade = (Grade) other;
return grade.equals(otherGrade.grade);
return value.equals(otherGrade.value);
}

@Override
public int hashCode() {
return grade.hashCode();
return value.hashCode();
}
}
10 changes: 5 additions & 5 deletions src/main/java/seedu/address/model/person/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Module {
*/
public static final String VALIDATION_REGEX = "^[a-zA-Z0-9]+$";

public final String module;
public final String value;
private Grade grade;

/**
Expand All @@ -29,7 +29,7 @@ public class Module {
public Module(String module) {
requireNonNull(module);
checkArgument(isValidModule(module), MESSAGE_CONSTRAINTS);
this.module = module;
this.value = module;
}

/**
Expand All @@ -45,7 +45,7 @@ public void setGrade(Grade grade) {

@Override
public String toString() {
return module + ": " + (grade == null ? "Grade not assigned" : grade.toString());
return value + ": " + (grade == null ? "Grade not assigned" : grade.toString());
}

@Override
Expand All @@ -60,11 +60,11 @@ public boolean equals(Object other) {
}

Module otherModule = (Module) other;
return module.equals(otherModule.module);
return value.equals(otherModule.value);
}

@Override
public int hashCode() {
return module.hashCode();
return value.hashCode();
}
}
Loading

0 comments on commit 8dbf3fd

Please sign in to comment.