Skip to content

Commit

Permalink
Add MC and CAP calculation commands
Browse files Browse the repository at this point in the history
  • Loading branch information
yyyaohhh committed Oct 17, 2023
1 parent b63486b commit e94eb94
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -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));
}
}
48 changes: 0 additions & 48 deletions src/main/java/seedu/address/logic/commands/CalculateCommand.java

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/java/seedu/address/logic/commands/CalculateMCCommand.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
11 changes: 8 additions & 3 deletions src/main/java/seedu/address/logic/parser/AddressBookParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

This file was deleted.

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DeleteCommandParser implements Parser<DeleteCommand> {
*/
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) {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Module> getFilteredModuleList();

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 =============================================================

/**
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/seedu/address/model/function/CAP.java

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions src/main/java/seedu/address/model/module/Grade.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/seedu/address/model/module/UniqueModuleList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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());
}


}
Loading

0 comments on commit e94eb94

Please sign in to comment.