Skip to content

Commit

Permalink
Fix mark bugs - invalid mark does not throw error
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelinglow committed Apr 4, 2024
1 parent 6ec2926 commit cb0bc8c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/appointment/Mark.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public String toString() {
return Boolean.toString(isMarked);
}

/**
* Returns true if a given string is a valid appointment mark.
*/
public static boolean isValidMark(String test) {
if (!test.equalsIgnoreCase("true") && !test.equalsIgnoreCase("false")) {
return false;
}
return true;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/seedu/address/storage/JsonAdaptedAppointment.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class JsonAdaptedAppointment {
private final String endTime;
private final String appointmentType;
private final String note;
private final boolean isMarked;
private final String isMarked;
/**
* Constructs a {@code JsonAdaptedAppointment} with the given appointment details.
*/
Expand All @@ -35,7 +35,7 @@ public JsonAdaptedAppointment(@JsonProperty("nric") String nric,
@JsonProperty("date") String date,
@JsonProperty("startTime") String startTime, @JsonProperty("endTime") String endTime,
@JsonProperty("appointmentType") String appointmentType,
@JsonProperty("note") String note, @JsonProperty("mark") boolean isMarked) {
@JsonProperty("note") String note, @JsonProperty("mark") String isMarked) {
this.nric = nric;
this.date = date;
this.startTime = startTime;
Expand All @@ -55,7 +55,7 @@ public JsonAdaptedAppointment(Appointment source) {
endTime = source.getEndTime().toString();
appointmentType = source.getAppointmentType().typeName;
note = source.getNote().note;
isMarked = source.getMark().isMarked;
isMarked = source.getMark().toString();
}

/**
Expand Down Expand Up @@ -118,7 +118,15 @@ public Appointment toModelType() throws IllegalValueException {
}
final Note modelNote = new Note(note);

final Mark modelMarked = new Mark(isMarked);
if (isMarked == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Mark.class.getSimpleName()));
}

if (!Mark.isValidMark(isMarked)) {
throw new IllegalValueException(Mark.MESSAGE_CONSTRAINTS);
}

final Mark modelMarked = new Mark(Boolean.parseBoolean(isMarked));

Appointment newAppt = new Appointment(modelNric, modelDate, modelTimePeriod,
modelAppointmentType, modelNote, modelMarked);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import seedu.address.commons.core.date.Date;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.appointment.AppointmentType;
import seedu.address.model.appointment.Mark;
import seedu.address.model.appointment.Note;
import seedu.address.model.appointment.Time;
import seedu.address.model.appointment.TimePeriod;
Expand All @@ -30,7 +31,7 @@ public class JsonAdaptedAppointmentTest {
private static final String VALID_END_TIME = ALICE_APPT.getEndTime().toString();
private static final String VALID_TYPE = ALICE_APPT.getAppointmentType().typeName;
private static final String VALID_NOTE = ALICE_APPT.getNote().note;
private static final boolean VALID_MARK = ALICE_APPT.getMark().isMarked;
private static final String VALID_MARK = ALICE_APPT.getMark().toString();
@Test
public void toModelType_validAppointmentDetails_returnsAppointment() throws Exception {
JsonAdaptedAppointment appointment = new JsonAdaptedAppointment(ALICE_APPT);
Expand Down Expand Up @@ -142,14 +143,31 @@ public void toModelType_nullNote_throwsIllegalValueException() {
@Test
public void toModelType_convertTrueMark_succeeds() {
JsonAdaptedAppointment appointment = new JsonAdaptedAppointment(VALID_NRIC, VALID_DATE,
VALID_START_TIME, VALID_END_TIME, VALID_TYPE, VALID_NOTE, true);
VALID_START_TIME, VALID_END_TIME, VALID_TYPE, VALID_NOTE, "true");
assertDoesNotThrow(appointment::toModelType); //does not throw any exceptions
}

@Test
public void toModelType_convertFalseMark_succeeds() {
JsonAdaptedAppointment appointment = new JsonAdaptedAppointment(VALID_NRIC, VALID_DATE,
VALID_START_TIME, VALID_END_TIME, VALID_TYPE, VALID_NOTE, false);
VALID_START_TIME, VALID_END_TIME, VALID_TYPE, VALID_NOTE, "false");
assertDoesNotThrow(appointment::toModelType); //does not throw any exceptions
}

@Test
public void toModelType_nullMark_throwsIllegalValueException() {
JsonAdaptedAppointment appointment = new JsonAdaptedAppointment(VALID_NRIC, VALID_DATE,
VALID_START_TIME, VALID_END_TIME, VALID_TYPE, VALID_NOTE, null);
String expectedMessage = String.format(MISSING_FIELD_MESSAGE_FORMAT,
Mark.class.getSimpleName());
assertThrows(IllegalValueException.class, expectedMessage, appointment::toModelType);
}

@Test
public void toModelType_invalidMark_throwsIllegalValueException() {
JsonAdaptedAppointment appointment = new JsonAdaptedAppointment(VALID_NRIC, VALID_DATE,
VALID_START_TIME, VALID_END_TIME, VALID_TYPE, VALID_NOTE, "test");
String expectedMessage = Mark.MESSAGE_CONSTRAINTS;
assertThrows(IllegalValueException.class, expectedMessage, appointment::toModelType);
}
}

0 comments on commit cb0bc8c

Please sign in to comment.