Skip to content

Commit

Permalink
Merge pull request #65 from izruff/add-attendance-support
Browse files Browse the repository at this point in the history
Integrate attendances with UI and update tests
  • Loading branch information
Oceankoh authored Oct 19, 2024
2 parents 981186f + 688bf22 commit cbda40f
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 46 deletions.
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public static String format(Person person) {
// Append the GradeList (exams and scores)
builder.append("; Exams: ")
.append(person.getGradeList());
// Append the AttendanceList (dates and attendance)
builder.append("; Attendances: ")
.append(person.getAttendanceList());
return builder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class MarkAttendanceCommand extends Command {
+ PREFIX_DATE + "DATETIME "
+ PREFIX_ATTENDANCE + "ATTENDANCE \n"
+ "Example: \n"
+ COMMAND_WORD + " 1" + PREFIX_DATE + "31/01/2024 12:00 " + PREFIX_ATTENDANCE + "present \n"
+ COMMAND_WORD + " 1" + PREFIX_DATE + "31/01/2024 12:00 " + PREFIX_ATTENDANCE + "absent \n";
+ COMMAND_WORD + " 1" + PREFIX_DATE + "31/01/2024 12:00 " + PREFIX_ATTENDANCE + "Attended \n"
+ COMMAND_WORD + " 1" + PREFIX_DATE + "31/01/2024 12:00 " + PREFIX_ATTENDANCE + "Absent \n";

public static final String MESSAGE_MARK_ATTENDANCE_SUCCESS = "Person: %1$s marked as %2$s on %3$s";

Expand Down Expand Up @@ -67,7 +67,7 @@ public CommandResult execute(Model model) throws CommandException {
model.setPerson(studentToMark, studentMarked);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(
String.format(MESSAGE_MARK_ATTENDANCE_SUCCESS, studentMarked.getName(), classDate));
String.format(MESSAGE_MARK_ATTENDANCE_SUCCESS, studentMarked.getName(), attendance, classDate));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.MarkAttendanceCommand;
import seedu.address.logic.commands.UnmarkAttendanceCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
Expand Down Expand Up @@ -85,6 +87,12 @@ public Command parseCommand(String userInput) throws ParseException {
case DeleteGradeCommand.COMMAND_WORD:
return new DeleteGradeCommandParser().parse(arguments);

case MarkAttendanceCommand.COMMAND_WORD:
return new MarkAttendanceCommandParser().parse(arguments);

case UnmarkAttendanceCommand.COMMAND_WORD:
return new UnmarkAttendanceCommandParser().parse(arguments);

default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/seedu/address/model/person/AttendanceList.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,16 @@ public AttendanceList removeAttendance(LocalDateTime date) {
}
Map<LocalDateTime, Attendance> newAttendanceList = new TreeMap<>(attendanceList);
newAttendanceList.remove(date);
return new AttendanceList(attendanceList);
return new AttendanceList(newAttendanceList);
}

/**
* Returns a copy of the map from {@code LocalDateTime} to {@code Attendance} in this {@code AttendanceList}
*
* @return A map from {@code LocalDateTime} to {@code Attendance} representing all the attendances.
*/
public Map<LocalDateTime, Attendance> getMap() {
return new TreeMap<>(attendanceList);
}

@Override
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/seedu/address/storage/JsonAdaptedAttendance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package seedu.address.storage;

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

import seedu.address.model.person.Attendance;

/**
* Jackson-friendly version of {@link Attendance}.
*/
public class JsonAdaptedAttendance {

public static final String MISSING_FIELD_MESSAGE_FORMAT = "Attendance's %s field is missing!";

private final boolean hasAttended;

/**
* Constructs a {@code JsonAdaptedAttendance} with the given attendance details.
*/
@JsonCreator
public JsonAdaptedAttendance(@JsonProperty("hasAttended") boolean hasAttended) {
this.hasAttended = hasAttended;
}

public JsonAdaptedAttendance(Attendance source) {
hasAttended = source.hasAttended();
}

/**
* Converts this Jackson-friendly adapted attendance object into the model's {@code Attendance} object.
*/
public Attendance toModelType() {
return new Attendance(hasAttended);
}
}
18 changes: 15 additions & 3 deletions src/main/java/seedu/address/storage/JsonAdaptedPerson.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package seedu.address.storage;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;

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

import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Attendance;
import seedu.address.model.person.AttendanceList;
import seedu.address.model.person.Email;
import seedu.address.model.person.Grade;
Expand All @@ -33,14 +37,16 @@ class JsonAdaptedPerson {
private final String address;
private final List<JsonAdaptedTag> tags = new ArrayList<>();
private final List<JsonAdaptedGrade> grades = new ArrayList<>();
private final Map<LocalDateTime, JsonAdaptedAttendance> attendances = new TreeMap<>();

/**
* Constructs a {@code JsonAdaptedPerson} with the given person details.
*/
@JsonCreator
public JsonAdaptedPerson(@JsonProperty("name") String name, @JsonProperty("phone") String phone,
@JsonProperty("email") String email, @JsonProperty("address") String address,
@JsonProperty("tags") List<JsonAdaptedTag> tags, @JsonProperty("grades") List<JsonAdaptedGrade> grades) {
@JsonProperty("tags") List<JsonAdaptedTag> tags, @JsonProperty("grades") List<JsonAdaptedGrade> grades,
@JsonProperty("attendances") Map<LocalDateTime, JsonAdaptedAttendance> attendances) {
this.name = name;
this.phone = phone;
this.email = email;
Expand All @@ -51,6 +57,9 @@ public JsonAdaptedPerson(@JsonProperty("name") String name, @JsonProperty("phone
if (grades != null) {
this.grades.addAll(grades);
}
if (attendances != null) {
this.attendances.putAll(attendances);
}
}

/**
Expand All @@ -67,6 +76,8 @@ public JsonAdaptedPerson(Person source) {
grades.addAll(source.getGradeList().getList().stream() // Convert GradeList to JSON
.map(JsonAdaptedGrade::new)
.collect(Collectors.toList()));
source.getAttendanceList().getMap().forEach((key, value) ->
attendances.put(key, new JsonAdaptedAttendance(value)));
}

/**
Expand Down Expand Up @@ -120,8 +131,9 @@ public Person toModelType() throws IllegalValueException {
}
final GradeList modelGradeList = new GradeList(convertedGrades);

// TODO: Store attendanceList properly
final AttendanceList modelAttendancelist = new AttendanceList();
final Map<LocalDateTime, Attendance> convertedAttendances = new TreeMap<>();
attendances.forEach((key, value) -> convertedAttendances.put(key, value.toModelType()));
final AttendanceList modelAttendancelist = new AttendanceList(convertedAttendances);

return new Person(modelName, modelPhone, modelEmail, modelAddress, modelTags, modelGradeList,
modelAttendancelist);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/ui/PersonCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class PersonCard extends UiPart<Region> {
private FlowPane tags;
@FXML
private Label grades;
@FXML
private Label attendances;

/**
* Creates a {@code PersonCode} with the given {@code Person} and index to display.
Expand All @@ -58,6 +60,7 @@ public PersonCard(Person person, int displayedIndex) {
.sorted(Comparator.comparing(tag -> tag.tagName))
.forEach(tag -> tags.getChildren().add(new Label(tag.tagName)));
grades.setText(formatGrades(person.getGradeList().toString())); // Format and set grades
attendances.setText(formatAttendances(person.getAttendanceList().toString())); // Format and set attendances
}

/**
Expand All @@ -66,4 +69,11 @@ public PersonCard(Person person, int displayedIndex) {
private String formatGrades(String gradeList) {
return "Grades:\n" + gradeList;
}

/**
* Formats the attendance list to display properly in the UI.
*/
private String formatAttendances(String attendanceList) {
return "Attendances:\n" + (attendanceList.isEmpty() ? "No attendance records" : attendanceList);
}
}
1 change: 1 addition & 0 deletions src/main/resources/view/PersonListCard.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<Label fx:id="address" styleClass="cell_small_label" text="\$address" />
<Label fx:id="email" styleClass="cell_small_label" text="\$email" />
<Label fx:id="grades" styleClass="cell_small_label" text="No grades available" />
<Label fx:id="attendances" styleClass="cell_small_label" text="No attendances available" />
</VBox>
</GridPane>
</HBox>
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,31 @@
"email" : "[email protected]",
"address" : "123, Jurong West Ave 6, #08-111",
"tags" : [ "friends" ],
"grades" : []
"grades" : [],
"attendances": {}
}, {
"name" : "Benson Meier",
"phone" : "98765432",
"email" : "[email protected]",
"address" : "311, Clementi Ave 2, #02-25",
"tags" : [ "owesMoney", "friends" ],
"grades" : []
"grades" : [],
"attendances": {}
}, {
"name" : "Carl Kurz",
"phone" : "95352563",
"email" : "[email protected]",
"address" : "wall street",
"tags" : [ ],
"grades" : []
"grades" : [],
"attendances" : {
"2024-01-01T12:00" : {
"hasAttended" : true
},
"2024-01-08T12:00" : {
"hasAttended" : false
}
}
}, {
"name" : "Daniel Meier",
"phone" : "87652533",
Expand All @@ -38,7 +48,15 @@
"score": 90,
"weightage": 30
}
]
],
"attendances" : {
"2024-01-01T12:00" : {
"hasAttended" : true
},
"2024-01-08T12:00" : {
"hasAttended" : false
}
}
}, {
"name" : "Elle Meyer",
"phone" : "9482224",
Expand All @@ -56,7 +74,15 @@
"score": 90,
"weightage": 30
}
]
],
"attendances" : {
"2024-01-01T12:00" : {
"hasAttended" : true
},
"2024-01-08T12:00" : {
"hasAttended" : false
}
}
}, {
"name" : "Fiona Kunz",
"phone" : "9482427",
Expand All @@ -74,7 +100,15 @@
"score": 90,
"weightage": 30
}
]
],
"attendances" : {
"2024-02-14T10:00" : {
"hasAttended" : false
},
"2024-02-21T10:00" : {
"hasAttended" : true
}
}
}, {
"name" : "George Best",
"phone" : "9482442",
Expand All @@ -92,6 +126,14 @@
"score": 92,
"weightage": 25
}
]
],
"attendances" : {
"2024-02-14T10:00" : {
"hasAttended" : false
},
"2024-02-21T10:00" : {
"hasAttended" : true
}
}
} ]
}
18 changes: 18 additions & 0 deletions src/test/java/seedu/address/storage/JsonAdaptedAttendanceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package seedu.address.storage;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

import seedu.address.model.person.Attendance;

public class JsonAdaptedAttendanceTest {
private static final Attendance VALID_ATTENDANCE = new Attendance(true);
private static final boolean VALID_HAS_ATTENDED = true;

@Test
public void toModelType_validAttendanceDetails_returnAttendance() {
JsonAdaptedAttendance attendance = new JsonAdaptedAttendance(VALID_HAS_ATTENDED);
assertEquals(VALID_ATTENDANCE, attendance.toModelType());
}
}
Loading

0 comments on commit cbda40f

Please sign in to comment.