Skip to content

Commit

Permalink
Add delete function without tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SebasFok committed Oct 31, 2023
1 parent 96518e7 commit 1280f14
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ public void start() {
view.handleAddMessage(isSuccessful);
break;
}
case "delete": {
String module = words[1].toUpperCase();
String deleteMessage = student.getSchedule().deleteModule(module);
view.handleDeleteMessage(deleteMessage);
break;
}
case "schedule": {
student.getSchedule().printMainModuleList();
break;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/duke/models/logic/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ public static ArrayList<String> getModulePrereqBasedOnCourse(String moduleCode,

}

/**
* Checks if a module with the given module code exists in the NUSMods database.
*
* @param moduleCode The module code to check for existence.
* @return `true` if the module exists, `false` if the module does not exist.
*/
public static boolean doesModuleExist(String moduleCode) {
JSONObject moduleInfo = getFullModuleInfo(moduleCode);
return (!(moduleInfo == null));
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/duke/models/schema/ModuleList.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,15 @@ public int getNumberOfModules() {
return numberOfModules;
}

/**
* Changes the number of modules by the specified difference.
*
* @param difference The difference by which to change the number of modules.
* A positive value increases the number, while a negative value decreases it.
*/
public void changeNumberOfModules(int difference) {
numberOfModules += difference;
}


}
93 changes: 93 additions & 0 deletions src/main/java/seedu/duke/models/schema/Schedule.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,58 @@
package seedu.duke.models.schema;

import java.util.ArrayList;
import java.util.List;

import static seedu.duke.models.logic.Api.satisfiesAllPrereq;

/**
* The `Schedule` class represents a student's course schedule and extends the `ModuleList` class.
* It allows a student to manage and manipulate their enrolled modules across multiple semesters.
*/
public class Schedule extends ModuleList {

private static final int MAXIMUM_SEMESTERS = 8;
private static final String MISSING_MODULE = "Module is not in schedule.";
private static final String DEPENDENT_MODULE = "Unable to delete module. This module is a prerequisite for ";
private static final String INVALID_MODULE = "Please select a valid module";
private static final String DELETE_MODULE = "Module Successfully Deleted";
protected int[] modulesPerSem;

/**
* Constructs a new `Schedule` with the provided modules and distribution across semesters.
*
* @param modules A string containing module codes representing the student's schedule.
* @param modulesPerSem An array indicating the distribution of modules across semesters.
*/
public Schedule(String modules, int[] modulesPerSem) {
super(modules);
this.modulesPerSem = modulesPerSem;
}

/**
* Constructs a new, empty `Schedule` with no modules and a default semester distribution.
*/
public Schedule() {
super();
this.modulesPerSem = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
}

/**
* Retrieves the maximum number of semesters allowed in a student's course schedule.
*
* @return The maximum number of semesters allowed.
*/
public static int getMaximumSemesters() {
return MAXIMUM_SEMESTERS;
}

/**
* Adds a module to the student's course schedule at the specified target semester.
*
* @param module The module code to be added.
* @param targetSem The target semester for adding the module.
* @return `true` if the module is successfully added, `false` if the addition is not possible.
*/
public boolean addModule(String module, int targetSem) {

if (targetSem < 1 || targetSem > MAXIMUM_SEMESTERS) {
Expand All @@ -41,6 +71,7 @@ public boolean addModule(String module, int targetSem) {
if (satisfiesAllPrereq(module, completedModules)) {
this.getMainModuleList().add(indexToAdd, module);
modulesPerSem[targetSem - 1] += 1;
changeNumberOfModules(1);
printMainModuleList();
return true;
}
Expand All @@ -51,6 +82,68 @@ public boolean addModule(String module, int targetSem) {
return false;
}

/**
* Deletes a module from the student's course schedule.
*
* @param module The module code to be deleted from the schedule.
* @return One of the following strings:
* - If the module is successfully deleted: {@value #DELETE_MODULE}
* - If the specified module does not exist in the schedule: {@value #MISSING_MODULE}
* - If another module is dependent on the specified module:
* {@value #DEPENDENT_MODULE} followed by the dependent module's code.
* - If the specified module is invalid: {@value #INVALID_MODULE}
*/
public String deleteModule(String module) {

int targetIndex = getMainModuleList().indexOf(module);

if (targetIndex == -1) {
return MISSING_MODULE;
}

int targetSem = 1;
int moduleCount = modulesPerSem[0];

while ((moduleCount - 1) < targetIndex) {
moduleCount += modulesPerSem[targetSem];
targetSem += 1;
}

int nextSemStartingIndex = moduleCount;

int lastModuleIndex = getNumberOfModules() - 1;

List<String> completedModulesArray = getMainModuleList().subList(0, nextSemStartingIndex);
ModuleList completedModules = new ModuleList(String.join(" ", completedModulesArray));
completedModules.getMainModuleList().remove(module);

List<String> modulesAheadArray;
try {
modulesAheadArray = getMainModuleList().subList(nextSemStartingIndex, lastModuleIndex + 1);
} catch (IndexOutOfBoundsException | IllegalArgumentException e) {
modulesAheadArray = new ArrayList<>();
}

try {
for (String moduleAhead : modulesAheadArray){
if (!satisfiesAllPrereq(moduleAhead, completedModules)) {
return DEPENDENT_MODULE + moduleAhead;
}
}
} catch (IllegalArgumentException e) {
return INVALID_MODULE;
}

getMainModuleList().remove(module);
modulesPerSem[targetSem - 1] -= 1;
changeNumberOfModules(-1);

return DELETE_MODULE;
}

/**
* Prints the student's course schedule, displaying modules organized by semesters.
*/
@Override
public void printMainModuleList() {
int moduleCounter = 0;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/seedu/duke/utils/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ public static boolean isValidInput(String command, String[] words) {
}
break;
}
case "delete": {
if (words.length != 2) {
ErrorHandler.invalidDeleteFormat();
return false;
}
if (!doesModuleExist(words[1].toUpperCase())) {
ErrorHandler.invalidModule();
return false;
}
break;
}
case "test2": {
if (words.length < 21) {
return false;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/duke/views/CommandLineView.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void handleMajorMessage(int userInputLength, Major major) {
displayMessage("Major " + major + " selected!");
}

public void handleDeleteMessage(String deleteMessage) {
displayMessage(deleteMessage);
}

public void handleAddMessage(boolean isSuccessful) {
if (isSuccessful) {
displayMessage("Module Successfully Added");
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/duke/views/ErrorHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public static void invalidSemester() {
public static void invalidModule() {
System.out.println("Please select a valid module");
}

public static void invalidDeleteFormat() {
System.out.println("Please delete a module using this format: delete [module code]");
}
}

0 comments on commit 1280f14

Please sign in to comment.