diff --git a/src/main/java/seedu/address/logic/commands/CalculateCAPCommand.java b/src/main/java/seedu/address/logic/commands/CalculateCAPCommand.java new file mode 100644 index 00000000000..7e62a91e775 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/CalculateCAPCommand.java @@ -0,0 +1,28 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; + +/** + * Deletes a person identified using it's displayed index from the address book. + */ +public class CalculateCAPCommand extends Command { + + public static final String COMMAND_WORD = "calculate CAP"; + public static final String MESSAGE_CALCULATION_SUCCESS = "Calculated value: %1$s"; + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + + int modularCredits = model.totalModularCredits(); + + Float gradePointsByUnits = model.totalGradePointsByUnits(); + + Float calculatedCAPValue = gradePointsByUnits / modularCredits; + + return new CommandResult(String.format(MESSAGE_CALCULATION_SUCCESS, calculatedCAPValue)); + } +} \ No newline at end of file diff --git a/src/main/java/seedu/address/logic/commands/CalculateCommand.java b/src/main/java/seedu/address/logic/commands/CalculateCommand.java deleted file mode 100644 index 8520763a69d..00000000000 --- a/src/main/java/seedu/address/logic/commands/CalculateCommand.java +++ /dev/null @@ -1,48 +0,0 @@ -package seedu.address.logic.commands; - -import static java.util.Objects.requireNonNull; -import static seedu.address.logic.parser.CliSyntax.PREFIX_CODE; -import static seedu.address.logic.parser.CliSyntax.PREFIX_FUNCTION; -import static seedu.address.logic.parser.CliSyntax.PREFIX_FUNCTIONS; - -import java.util.List; - -import seedu.address.commons.util.ToStringBuilder; -import seedu.address.logic.Messages; -import seedu.address.logic.commands.exceptions.CommandException; -import seedu.address.model.Model; -import seedu.address.model.module.Module; -import seedu.address.model.module.ModuleCode; - - -/** - * Deletes a person identified using it's displayed index from the address book. - */ -public class CalculateCommand extends Command { - - public static final String COMMAND_WORD = "calculate"; - - public static final String MESSAGE_USAGE = COMMAND_WORD - + ": Calculates the CAP or MC (Modular Credits). " - + "Parameters: " + PREFIX_FUNCTION + "function " - + "Example: " + COMMAND_WORD + " " + PREFIX_FUNCTION + "CAP "; - - public static final String MESSAGE_CALCULATION_SUCCESS = "Calculated value: %1$s"; - public static final String MESSAGE_NOT_FOUND_MODULE = "There are no modules so no calculation is done."; - - @Override - public CommandResult execute(Model model) throws CommandException { - requireNonNull(model); - - float calculatedValue; - - List lastShownList = model.getFilteredModuleList(); - - if (lastShownList.isEmpty()) { - throw new CommandException(MESSAGE_NOT_FOUND_MODULE); - } - - model.deleteModule(targetMod); - return new CommandResult(String.format(MESSAGE_CALCULATION_SUCCESS, Messages.format(targetMod))); - } -} \ No newline at end of file diff --git a/src/main/java/seedu/address/logic/commands/CalculateMCCommand.java b/src/main/java/seedu/address/logic/commands/CalculateMCCommand.java new file mode 100644 index 00000000000..77afcab2b13 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/CalculateMCCommand.java @@ -0,0 +1,20 @@ +package seedu.address.logic.commands; + +import static java.util.Objects.requireNonNull; + +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; + +public class CalculateMCCommand extends Command { + public static final String COMMAND_WORD = "calculate MC"; + public static final String MESSAGE_CALCULATION_SUCCESS = "Calculated value: %1$s"; + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + + int modularCredits = model.totalModularCredits(); + + return new CommandResult(String.format(MESSAGE_CALCULATION_SUCCESS, modularCredits)); + } +} diff --git a/src/main/java/seedu/address/logic/parser/AddressBookParser.java b/src/main/java/seedu/address/logic/parser/AddressBookParser.java index ac62782aae4..d64f763898e 100644 --- a/src/main/java/seedu/address/logic/parser/AddressBookParser.java +++ b/src/main/java/seedu/address/logic/parser/AddressBookParser.java @@ -9,7 +9,8 @@ import seedu.address.commons.core.LogsCenter; import seedu.address.logic.commands.AddCommand; -import seedu.address.logic.commands.CalculateCommand; +import seedu.address.logic.commands.CalculateCAPCommand; +import seedu.address.logic.commands.CalculateMCCommand; import seedu.address.logic.commands.ClearCommand; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.DeleteCommand; @@ -78,8 +79,12 @@ public Command parseCommand(String userInput) throws ParseException { case HelpCommand.COMMAND_WORD: return new HelpCommand(); - case CalculateCommand.COMMAND_WORD: - return new CalculateCommand(); + case CalculateCAPCommand.COMMAND_WORD: + return new CalculateCAPCommand(); + + case CalculateMCCommand.COMMAND_WORD: + return new CalculateMCCommand(); + default: logger.finer("This user input caused a ParseException: " + userInput); diff --git a/src/main/java/seedu/address/logic/parser/CalculateCommandParser.java b/src/main/java/seedu/address/logic/parser/CalculateCommandParser.java deleted file mode 100644 index c7bd9999ce9..00000000000 --- a/src/main/java/seedu/address/logic/parser/CalculateCommandParser.java +++ /dev/null @@ -1,30 +0,0 @@ -package seedu.address.logic.parser; - -import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; - -import seedu.address.logic.commands.CalculateCommand; -import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.model.module.ModuleCode; - -/** - * Parses input arguments and creates a new DeleteCommand object - */ -public class DeleteCommandParser implements Parser { - - /** - * Parses the given {@code String} of arguments in the context of the DeleteCommand - * and returns a DeleteCommand object for execution. - * @throws ParseException if the user input does not conform the expected format - */ - public CalculateCommand parse(String args) throws ParseException { - try { - ModuleCode code = ParserUtil.parseCode(args); - return new DeleteCommand(code); - - } catch (ParseException pe) { - throw new ParseException( - String.format(MESSAGE_INVALID_COMMAND_FORMAT, CalculateCommand.MESSAGE_USAGE), pe); - } - } - -} diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index 518ee6b6011..45414d513ea 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -16,6 +16,5 @@ public class CliSyntax { public static final Prefix PREFIX_YEAR = new Prefix("y/"); public static final Prefix PREFIX_SEMESTER = new Prefix("s/"); public static final Prefix PREFIX_GRADE = new Prefix("g/"); - public static final Prefix PREFIX_FUNCTION = new Prefix("f/"); } diff --git a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java index a2e5ce75402..3a80819cc33 100644 --- a/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/DeleteCommandParser.java @@ -18,7 +18,7 @@ public class DeleteCommandParser implements Parser { */ public DeleteCommand parse(String args) throws ParseException { try { - ModuleCode code = ParserUtil.parseModuleCode(args); + ModuleCode code = ParserUtil.parseCode(args); return new DeleteCommand(code); } catch (ParseException pe) { diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index 6dea1a32ff2..5c2418354ab 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -108,6 +108,23 @@ public Module findUsingCode(ModuleCode code) { return modules.find(code); } + /** + * Calculates and returns the total modular credits of all modules in the collection. + * + * @return The total modular credits of all modules in the collection. + */ + public int totalModularCredits() { + return modules.modularCredits(); + } + + /** + * Calculates and returns the total grade points weighted by modular credits of all modules in the collection. + * + * @return The total grade points weighted by modular credits of all modules in the collection as a floating-point number. + */ + public Float totalGradePointsByUnits() { + return modules.gradePointsWithUnits(); + } //// util methods diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 6b83aeb34b2..16b9d38099f 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -86,6 +86,20 @@ public interface Model { */ Module findModuleUsingCode(ModuleCode code); + /** + * Calculates and returns the total modular credits of all modules in the address book. + * + * @return The total modular credits of all modules in the address book. + */ + int totalModularCredits(); + + /** + * Calculates and returns the total grade points weighted by modular credits of all modules. + * + * @return The total grade points weighted by modular credits as a floating-point number. + */ + Float totalGradePointsByUnits(); + /** Returns an unmodifiable view of the filtered person list */ ObservableList getFilteredModuleList(); diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index 116f887f872..8c7c21193d3 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -121,6 +121,16 @@ public Module findModuleUsingCode(ModuleCode code) { return addressBook.findUsingCode(code); } + @Override + public int totalModularCredits() { + return addressBook.totalModularCredits(); + } + + @Override + public Float totalGradePointsByUnits() { + return addressBook.totalGradePointsByUnits(); + } + //=========== Filtered Person List Accessors ============================================================= /** diff --git a/src/main/java/seedu/address/model/function/CAP.java b/src/main/java/seedu/address/model/function/CAP.java deleted file mode 100644 index 03b4ee836ac..00000000000 --- a/src/main/java/seedu/address/model/function/CAP.java +++ /dev/null @@ -1,4 +0,0 @@ -package seedu.address.model.function; - -public class CAP { -} diff --git a/src/main/java/seedu/address/model/function/ModularCredits.java b/src/main/java/seedu/address/model/function/ModularCredits.java deleted file mode 100644 index 3b64d819e5e..00000000000 --- a/src/main/java/seedu/address/model/function/ModularCredits.java +++ /dev/null @@ -1,4 +0,0 @@ -package seedu.address.model.function; - -public class ModularCredits { -} diff --git a/src/main/java/seedu/address/model/module/Grade.java b/src/main/java/seedu/address/model/module/Grade.java index 23a53d63fc3..99ebd0b7bd4 100644 --- a/src/main/java/seedu/address/model/module/Grade.java +++ b/src/main/java/seedu/address/model/module/Grade.java @@ -3,6 +3,7 @@ import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.AppUtil.checkArgument; +import java.awt.datatransfer.FlavorEvent; import java.util.Arrays; import java.util.stream.Collectors; @@ -94,6 +95,10 @@ public String toString() { return grade.getGrade(); } + public Float gradePoint() { + return grade.getGradePoint(); + } + @Override public boolean equals(Object other) { if (other == this) { diff --git a/src/main/java/seedu/address/model/module/UniqueModuleList.java b/src/main/java/seedu/address/model/module/UniqueModuleList.java index e9095d05f46..595472af3c9 100644 --- a/src/main/java/seedu/address/model/module/UniqueModuleList.java +++ b/src/main/java/seedu/address/model/module/UniqueModuleList.java @@ -108,6 +108,36 @@ public Module find(ModuleCode code) { throw new ModuleNotFoundException(); } + /** + * Calculates and returns the total modular credits of all modules in the internal list. + * + * @return The total modular credits of all modules in the internal list. + */ + public int modularCredits() { + Module[] mods = new Module[internalList.size()]; + mods = internalList.toArray(mods); + int modularCredits = 0; + for (int i = 0; i < internalList.size(); i++) { + modularCredits += mods[i].getModularCredit().hashCode(); + } + return modularCredits; + } + + /** + * Calculates and returns the total grade points weighted by the modular credits of all modules in the internal list. + * + * @return The total grade points weighted by modular credits as a floating-point number. + */ + public Float gradePointsWithUnits() { + Module[] mods = new Module[internalList.size()]; + mods = internalList.toArray(mods); + float gradePoints = 0; + for (int i = 0; i < internalList.size(); i++) { + gradePoints += mods[i].getGrade().gradePoint() * mods[i].getModularCredit().hashCode(); + } + return gradePoints; + } + /** * Replaces the contents of this list with {@code modules}. * {@code modules} must not contain duplicate modules. diff --git a/src/test/java/seedu/address/logic/commands/CalculateCAPCommandTest.java b/src/test/java/seedu/address/logic/commands/CalculateCAPCommandTest.java new file mode 100644 index 00000000000..815f5db5f9a --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/CalculateCAPCommandTest.java @@ -0,0 +1,27 @@ +package seedu.address.logic.commands; + +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.testutil.TypicalModules.getTypicalAddressBook; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.testutil.TypicalModules; + +/** + * Contains integration tests (interaction with the Model) and unit tests for CalculateCAPCommand. + */ +public class CalculateCAPCommandTest { + + private Model model; + private Model expectedModel; + @BeforeEach + public void setUp() { + model = new ModelManager(TypicalModules.getTypicalAddressBook(), new UserPrefs()); + expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + } + +} diff --git a/src/test/java/seedu/address/logic/commands/CalculateCommandTest.java b/src/test/java/seedu/address/logic/commands/CalculateCommandTest.java deleted file mode 100644 index 0099c1f36b9..00000000000 --- a/src/test/java/seedu/address/logic/commands/CalculateCommandTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package seedu.address.logic.commands; - -import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; -import static seedu.address.logic.commands.CalculateCommand.MESSAGE_NOT_FOUND_MODULE; -import static seedu.address.testutil.TypicalModules.CS2030S; -import static seedu.address.testutil.TypicalModules.getTypicalAddressBook; -import static seedu.address.testutil.TypicalModules.getTypicalModules; - -import org.junit.jupiter.api.Test; - -import seedu.address.model.Model; -import seedu.address.model.ModelManager; -import seedu.address.model.UserPrefs; - -/** - * Contains integration tests (interaction with the Model) and unit tests for RemarkCommand. - */ -public class CalculateCommandTest { - - private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); - - @Test - public void execute() { - assertCommandFailure(new CalculateCommand(), model, MESSAGE_NOT_FOUND_MODULE); - } - -} diff --git a/src/test/java/seedu/address/logic/commands/CalculateMCCommandTest.java b/src/test/java/seedu/address/logic/commands/CalculateMCCommandTest.java new file mode 100644 index 00000000000..66b9e6bd2e1 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/CalculateMCCommandTest.java @@ -0,0 +1,27 @@ +package seedu.address.logic.commands; + +import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; +import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; +import seedu.address.testutil.TypicalModules; + +/** + * Contains integration tests (interaction with the Model) and unit tests for CalculateMCCommand. + */ +public class CalculateMCCommandTest { + private Model model; + private Model expectedModel; + @BeforeEach + public void setUp() { + model = new ModelManager(TypicalModules.getTypicalAddressBook(), new UserPrefs()); + expectedModel = new ModelManager(model.getAddressBook(), new UserPrefs()); + } + + +} diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index ded0992e1e4..56911633ef0 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -14,7 +14,8 @@ import org.junit.jupiter.api.Test; import seedu.address.logic.commands.AddCommand; -import seedu.address.logic.commands.CalculateCommand; +import seedu.address.logic.commands.CalculateCAPCommand; +import seedu.address.logic.commands.CalculateMCCommand; import seedu.address.logic.commands.ClearCommand; import seedu.address.logic.commands.DeleteCommand; import seedu.address.logic.commands.EditCommand; @@ -92,8 +93,13 @@ public void parseCommand_list() throws Exception { } @Test - public void parseCommand_calculate() throws Exception { - assertTrue(parser.parseCommand(CalculateCommand.COMMAND_WORD) instanceof CalculateCommand); + public void parseCommand_calculateCAP() throws Exception { + assertTrue(parser.parseCommand(CalculateCAPCommand.COMMAND_WORD) instanceof CalculateCAPCommand); + } + + @Test + public void parseCommand_calculateMC() throws Exception { + assertTrue(parser.parseCommand(CalculateMCCommand.COMMAND_WORD) instanceof CalculateMCCommand); } @Test