forked from nus-cs2103-AY2324S1/tp
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from rocketninja7/model
Add Module class with preliminary testing
- Loading branch information
Showing
11 changed files
with
701 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package seedu.address.model.module; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
/** | ||
* Represents a Module's description in the system. | ||
* Guarantees: immutable; is valid as declared in {@link #isValidDescription(String)} | ||
*/ | ||
public class Description { | ||
public static final String MESSAGE_CONSTRAINTS = | ||
"Description should only contain alphanumeric characters and spaces, and it should not be blank"; | ||
|
||
/* | ||
* The first character of the lecturer's name must not be a whitespace, | ||
* otherwise " " (a blank string) becomes a valid input. | ||
*/ | ||
public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*"; | ||
|
||
public final String description; | ||
|
||
/** | ||
* Constructs a {@code Description}. | ||
* | ||
* @param description A valid description. | ||
*/ | ||
public Description(String description) { | ||
requireNonNull(description); | ||
checkArgument(isValidDescription(description), MESSAGE_CONSTRAINTS); | ||
this.description = description; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid description. | ||
*/ | ||
public static boolean isValidDescription(String test) { | ||
return test.matches(VALIDATION_REGEX); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return description; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof Description)) { | ||
return false; | ||
} | ||
|
||
Description otherName = (Description) other; | ||
return description.equals(otherName.description); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return description.hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package seedu.address.model.module; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Represents a student's grade in a Module in the system. | ||
* Guarantees: immutable; is valid as declared in {@link #isValidGrade(String)} | ||
*/ | ||
public class Grade { | ||
// We define an enum inside a class to allow error messages to be abstracted out. | ||
/** | ||
* An enum to represent possible grades. | ||
*/ | ||
public enum GradeEnum { | ||
// Based on https://www.nus.edu.sg/registrar/academic-information-policies/undergraduate-students/modular-system | ||
A_PLUS("A+", 5.0f), | ||
A("A", 5.0f), | ||
A_MINUS("A-", 4.5f), | ||
B_PLUS("B+", 4.0f), | ||
B("B", 3.5f), | ||
B_MINUS("B-", 3.0f), | ||
C_PLUS("C+", 2.5f), | ||
C("C", 2.0f), | ||
D_PLUS("D+", 1.5f), | ||
D("D", 1.0f), | ||
F("F", 0.0f), | ||
EXEMPTED("EXE", null), | ||
INCOMPLETE("IC", null), | ||
IN_PROGRESS("IP", null), | ||
WITHDRAWN("W", null); | ||
|
||
private final String grade; | ||
private final Float gradePoint; | ||
|
||
GradeEnum(String grade, Float gradePoint) { | ||
this.grade = grade; | ||
this.gradePoint = gradePoint; | ||
} | ||
|
||
/** | ||
* Converts a {@code String} to {@code GradeEnum} | ||
* @param grade The {@code String} representation of a grade. | ||
* @return The corresponding {@code GradeEnum} | ||
*/ | ||
public static GradeEnum fromString(String grade) { | ||
for (GradeEnum gradeEnum: GradeEnum.values()) { | ||
if (grade.equals(gradeEnum.grade)) { | ||
return gradeEnum; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public String getGrade() { | ||
return this.grade; | ||
} | ||
} | ||
|
||
public static final String MESSAGE_CONSTRAINTS = "Grade should only be the following: " | ||
+ String.join(", ", | ||
Arrays.stream(GradeEnum.values()).map(GradeEnum::getGrade) | ||
.collect(Collectors.toList())); | ||
|
||
private final GradeEnum grade; | ||
|
||
/** | ||
* Constructs a {@code Lecturer}. | ||
* | ||
* @param grade A valid lecturer's name. | ||
*/ | ||
public Grade(String grade) { | ||
requireNonNull(grade); | ||
checkArgument(isValidGrade(grade), MESSAGE_CONSTRAINTS); | ||
this.grade = GradeEnum.fromString(grade); | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid grade. | ||
*/ | ||
public static boolean isValidGrade(String test) { | ||
return GradeEnum.fromString(test) != null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return grade.getGrade(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof Grade)) { | ||
return false; | ||
} | ||
|
||
Grade otherGrade = (Grade) other; | ||
return grade.equals(otherGrade.grade); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return grade.getGrade().hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package seedu.address.model.module; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.AppUtil.checkArgument; | ||
|
||
/** | ||
* Represents a lecturer in the system. | ||
* Guarantees: immutable; is valid as declared in {@link #isValidLecturer(String)} | ||
*/ | ||
public class Lecturer { | ||
|
||
public static final String MESSAGE_CONSTRAINTS = | ||
"Lecturer names should only contain alphanumeric characters and spaces, and it should not be blank"; | ||
|
||
/* | ||
* The first character of the lecturer's name must not be a whitespace, | ||
* otherwise " " (a blank string) becomes a valid input. | ||
*/ | ||
public static final String VALIDATION_REGEX = "[\\p{Alnum}][\\p{Alnum} ]*"; | ||
|
||
public final String lecturerName; | ||
|
||
/** | ||
* Constructs a {@code Lecturer}. | ||
* | ||
* @param name A valid lecturer's name. | ||
*/ | ||
public Lecturer(String name) { | ||
requireNonNull(name); | ||
checkArgument(isValidLecturer(name), MESSAGE_CONSTRAINTS); | ||
lecturerName = name; | ||
} | ||
|
||
/** | ||
* Returns true if a given string is a valid name. | ||
*/ | ||
public static boolean isValidLecturer(String test) { | ||
return test.matches(VALIDATION_REGEX); | ||
} | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return lecturerName; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof Lecturer)) { | ||
return false; | ||
} | ||
|
||
Lecturer otherName = (Lecturer) other; | ||
return lecturerName.equals(otherName.lecturerName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return lecturerName.hashCode(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package seedu.address.model.module; | ||
|
||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Represents a Module in the system. | ||
* Guarantees: details are present and not null, field values are validated, immutable. | ||
*/ | ||
public class Module { | ||
// Identity fields | ||
private final Name name; | ||
private final ModuleCode moduleCode; | ||
|
||
// Data fields | ||
private final Description description; | ||
private final List<Lecturer> lecturers; | ||
private final Year yearTaken; | ||
private final Semester semesterTaken; | ||
private final Grade grade; | ||
|
||
/** | ||
* Every field must be present and not null. | ||
*/ | ||
public Module( | ||
Name name, ModuleCode moduleCode, Description description, List<Lecturer> lecturers, | ||
Year yearTaken, Semester semesterTaken, Grade grade) { | ||
requireAllNonNull(name, moduleCode, description, lecturers, yearTaken, semesterTaken, grade); | ||
this.name = name; | ||
this.moduleCode = moduleCode; | ||
this.description = description; | ||
this.lecturers = lecturers; | ||
this.yearTaken = yearTaken; | ||
this.semesterTaken = semesterTaken; | ||
this.grade = grade; | ||
} | ||
|
||
public Name getName() { | ||
return name; | ||
} | ||
|
||
public ModuleCode getModuleCode() { | ||
return moduleCode; | ||
} | ||
|
||
public Description getDescription() { | ||
return description; | ||
} | ||
|
||
public List<Lecturer> getLecturers() { | ||
return lecturers; | ||
} | ||
|
||
public Year getYearTaken() { | ||
return yearTaken; | ||
} | ||
|
||
public Semester getSemesterTaken() { | ||
return semesterTaken; | ||
} | ||
|
||
public Grade getGrade() { | ||
return grade; | ||
} | ||
|
||
/** | ||
* Checks if two modules are the same module. | ||
* @param otherModule the other module to check. | ||
* @return true if the modules are the same, false otherwise. | ||
*/ | ||
public boolean isSameModule(Module otherModule) { | ||
if (otherModule == this) { | ||
return true; | ||
} | ||
|
||
return otherModule != null && otherModule.getModuleCode().equals(getModuleCode()); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof Module)) { | ||
return false; | ||
} | ||
|
||
Module otherModule = (Module) other; | ||
return isSameModule(otherModule); | ||
} | ||
} |
Oops, something went wrong.