Skip to content

Commit

Permalink
Add more fields to Module View (#72)
Browse files Browse the repository at this point in the history
* Refactor tests to use builder classes

* Add prerequisite field to ArchivedModule

* Add semesterData to ArchivedModule

* Fix regression

* Add SemesterDetail to ArchivedModule

* Add preclusion to ArchivedModule

* Refactor ModuleViewPanel to make it scrollable

* Cleanup Ui
  • Loading branch information
geshuming committed Oct 28, 2019
1 parent 74934f9 commit c72b157
Show file tree
Hide file tree
Showing 21 changed files with 562 additions and 61 deletions.
22 changes: 21 additions & 1 deletion src/main/java/seedu/module/model/module/ArchivedModule.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package seedu.module.model.module;

import java.util.Objects;
import java.util.Optional;

/**
* Represents an Archived Module. An Archived Module is an Object containing data on a module
Expand All @@ -12,14 +13,33 @@ public class ArchivedModule implements Module {
private final String moduleCode;
private final String title;
private final String description;
private final Optional<String> prerequisite;
private final Optional<String> preclusion;
private final SemesterDetailList semesterDetails;

/**
* Every field must be present and not null.
*/
public ArchivedModule(String moduleCode, String title, String description) {
public ArchivedModule(String moduleCode, String title, String description, String prerequisite,
String preclusion, SemesterDetailList semesterDetails) {
this.moduleCode = moduleCode;
this.title = title;
this.description = description;
this.prerequisite = Optional.ofNullable(prerequisite);
this.preclusion = Optional.ofNullable(preclusion);
this.semesterDetails = semesterDetails;
}

public Optional<String> getPreclusion() {
return preclusion;
}

public SemesterDetailList getSemesterDetails() {
return semesterDetails;
}

public Optional<String> getPrerequisite() {
return prerequisite;
}

public String getModuleCode() {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/seedu/module/model/module/Module.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.module.model.module;

import java.util.Optional;

/**
* Represents a Module.
*/
Expand All @@ -11,4 +13,9 @@ public interface Module {

String getDescription();

Optional<String> getPrerequisite();

Optional<String> getPreclusion();

SemesterDetailList getSemesterDetails();
}
59 changes: 59 additions & 0 deletions src/main/java/seedu/module/model/module/SemesterDetail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package seedu.module.model.module;

import java.time.LocalDateTime;
import java.util.Optional;

/**
* Represents semester details pertaining to a module.
*/
public class SemesterDetail {

private final int semester;
private final Optional<LocalDateTime> examDate; // ISO standard datetime format
private final int examDuration; // duration in minutes
private final boolean offered; // whether the module is offered in this semester

/**
* Use this constructor to indicate the semester is offered.
* @apiNote examDate may be null to indicate no exams for this semester.
*/
public SemesterDetail(int semester, LocalDateTime examDate, int examDuration) {
this.semester = semester;
this.examDate = Optional.ofNullable(examDate);
this.examDuration = examDuration;
this.offered = true;
}

public SemesterDetail(int semester) {
this.semester = semester;
this.examDate = Optional.empty();
this.examDuration = 0;
this.offered = false;
}

public boolean isOffered() {
return offered;
}

public int getExamDuration() {
return examDuration;
}

public Optional<LocalDateTime> getExamDate() {
return examDate;
}

public int getSemester() {
return semester;
}

// Compares 2 objects based on the identify field {@code semester}.
@Override
public boolean equals(Object o) {
if (o instanceof SemesterDetail) {
return ((SemesterDetail) o).getSemester() == this.semester;
}

return false;
}
}
53 changes: 53 additions & 0 deletions src/main/java/seedu/module/model/module/SemesterDetailList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package seedu.module.model.module;

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

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.module.commons.core.index.Index;

/**
* Represents a list of semester details pertaining to a module.
* There are a total of 4 "semesters", 2 semesters and 2 special terms.
*
* See http://www.nus.edu.sg/registrar/calendar.html
*/
public class SemesterDetailList {

private List<SemesterDetail> semesterDetails;

/**
* Constructs a SemesterDetailList. Contains 4 elements.
* Each SemesterDetail defaults to unoffered status.
*/
public SemesterDetailList() {
this.semesterDetails = new ArrayList<>(4);
for (int i = 1; i <= 4; i++) {
this.semesterDetails.add(new SemesterDetail(i));
}
}

/**
* Constructs and sets the SemesterDetailList with new semester details.
* @param semesterDetails list of semester details, may be an incomplete list.
*/
public SemesterDetailList(List<SemesterDetail> semesterDetails) {
this();
for (SemesterDetail s : semesterDetails) {
// Sets the corresponding semester details
if (this.semesterDetails.contains(s)) {
this.semesterDetails.set(Index.fromOneBased(s.getSemester()).getZeroBased(), s);
}
}
}

/**
* Returns the SemesterDetailList as an Observable List.
*/
public ObservableList<SemesterDetail> getAsObservableList() {
ObservableList<SemesterDetail> observableList = FXCollections.observableArrayList();
observableList.setAll(semesterDetails);
return observableList;
}
}
13 changes: 13 additions & 0 deletions src/main/java/seedu/module/model/module/TrackedModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* Represents a TrackedModule in the ModuleList.
Expand Down Expand Up @@ -34,6 +35,18 @@ public String getDescription() {
return archivedModule.getDescription();
}

public Optional<String> getPrerequisite() {
return archivedModule.getPrerequisite();
}

public Optional<String> getPreclusion() {
return archivedModule.getPreclusion();
}

public SemesterDetailList getSemesterDetails() {
return archivedModule.getSemesterDetails();
}

public String getDeadline() {
String deadlineString = "Deadline: \n";
for (int i = 0; i < deadlineList.size(); i++) {
Expand Down
44 changes: 41 additions & 3 deletions src/main/java/seedu/module/storage/JsonAdaptedArchivedModule.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,73 @@
package seedu.module.storage;

import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.module.commons.core.LogsCenter;
import seedu.module.model.module.ArchivedModule;
import seedu.module.model.module.SemesterDetailList;

/**
* Jackson-friendly version of {@link ArchivedModule}. This class serves only as a reader.
*/
class JsonAdaptedArchivedModule {
private static final Logger logger = LogsCenter.getLogger(JsonAdaptedArchivedModule.class);

private final String moduleCode;
private final String title;
private final String description;
private final String prerequisite;
private final String preclusion;
private final List<JsonAdaptedSemesterDetail> semesterDetails;

/**
* Constructs a {@code JsonAdaptedArchivedModule} with the given module details.
*/
@JsonCreator
public JsonAdaptedArchivedModule(@JsonProperty("moduleCode") String moduleCode, @JsonProperty("title") String title,
@JsonProperty("description") String description) {
@JsonProperty("description") String description, @JsonProperty("prerequisite") String prerequisite,
@JsonProperty("preclusion") String preclusion,
@JsonProperty("semesterData") List<JsonAdaptedSemesterDetail> semesterDetails) {
this.moduleCode = moduleCode;
this.title = title;
this.description = description;
this.prerequisite = prerequisite;
this.preclusion = preclusion;
this.semesterDetails = Optional.ofNullable(semesterDetails).orElse(new ArrayList<>());
}

/**
* Converts this Jackson-friendly adapted module object into the model's {@code ArchivedModel} object.
* Converts this Jackson-friendly adapted module object into the model's {@code ArchivedModule} object.
*/
public ArchivedModule toModelType() {
return new ArchivedModule(moduleCode, title, description);
return new ArchivedModule(moduleCode, title, description, prerequisite, preclusion,
fromListToSemesterDetailList(semesterDetails));
}

/**
* Converts the list of {@code JsonAdaptedSemesterDetail} to {@code SemesterDetailList}.
*/
private SemesterDetailList fromListToSemesterDetailList(List<JsonAdaptedSemesterDetail> semesterDetails) {
return new SemesterDetailList(semesterDetails.stream()
.map(semesterDetail -> {
try {
return semesterDetail.toModelType();
} catch (DateTimeParseException e) {
logger.info(
String.format("ArchivedModule %s has bad semester data.", moduleCode));
return null;
}
})
.filter(semesterDetail -> semesterDetail != null)
.collect(Collectors.toList())
);
}

}
44 changes: 44 additions & 0 deletions src/main/java/seedu/module/storage/JsonAdaptedSemesterDetail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package seedu.module.storage;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

import com.fasterxml.jackson.annotation.JsonProperty;

import seedu.module.model.module.SemesterDetail;

/**
* Jackson-friendly version of {@code SemesterDetail}
*/
public class JsonAdaptedSemesterDetail {
private static final DateTimeFormatter format = DateTimeFormatter.ofPattern(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // format used by NUSmods

private final int semester;
private final String examDate; // ISO standard datetime format
private final int examDuration; // duration in minutes

/**
* Constructs a {@code JsonAdaptedSemesterDetail} with the given parameters.
*/
public JsonAdaptedSemesterDetail(@JsonProperty("semester") int semester,
@JsonProperty("examDate") String examDate,
@JsonProperty("examDuration") int examDuration) {
this.semester = semester;
this.examDate = examDate;
this.examDuration = examDuration;
}

/**
* Converts this Jackson-friendly semester detail object into the model's {@code SemesterDetail} object.
*/
public SemesterDetail toModelType() throws DateTimeParseException {
// null indicates that the semester has no exam
if (examDate == null) {
return new SemesterDetail(semester, null, examDuration);
}

return new SemesterDetail(semester, LocalDateTime.parse(examDate, format), examDuration);
}
}

0 comments on commit c72b157

Please sign in to comment.