forked from nus-cs2103-AY2021S1/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 #133 from VaishakAnand/attendance
Add Attendance command
- Loading branch information
Showing
48 changed files
with
1,850 additions
and
129 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
109 changes: 109 additions & 0 deletions
109
src/main/java/seedu/address/logic/commands/AddAttendanceCommand.java
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,109 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ATTENDANCE_DATE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ATTENDANCE_FEEDBACK; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ATTENDANCE_STATUS; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STUDENTS; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.student.Student; | ||
import seedu.address.model.student.academic.Attendance; | ||
|
||
public class AddAttendanceCommand extends AttendanceCommand { | ||
|
||
public static final String COMMAND_WORD = "add"; | ||
public static final String MESSAGE_USAGE = AttendanceCommand.COMMAND_WORD + " " + COMMAND_WORD | ||
+ ": adds an Attendance to the student identified " | ||
+ "by the index number used in the displayed student list. \n" | ||
+ "Parameters: STUDENT_INDEX (must be a positive integer) " | ||
+ PREFIX_ATTENDANCE_DATE + "LESSON_DATE " + PREFIX_ATTENDANCE_STATUS + "ATTENDANCE_STATUS " | ||
+ PREFIX_ATTENDANCE_FEEDBACK + "FEEDBACK\n" | ||
+ "Example: " + AttendanceCommand.COMMAND_WORD + " " + COMMAND_WORD + " 2 " | ||
+ PREFIX_ATTENDANCE_DATE + "14/02/2020 " + PREFIX_ATTENDANCE_STATUS + "attended " | ||
+ PREFIX_ATTENDANCE_FEEDBACK + "attentive"; | ||
public static final String MESSAGE_SUCCESS = "Attendance added for %s: %s"; | ||
public static final String MESSAGE_INVALID_ATTENDANCE_DATE = | ||
"There is already an existing attendance for the entered date! Please use another date, or delete the " | ||
+ "existing attendance before adding a new one."; | ||
private static Logger logger = Logger.getLogger("Add Attendance Log"); | ||
|
||
private final Index index; | ||
private final Attendance attendanceToAdd; | ||
|
||
/** | ||
* Creates an AddAdditionalDetailCommand to add the specified {@code AdditionalDetail} to the student | ||
* at the specified {@code Index}. | ||
*/ | ||
public AddAttendanceCommand(Index index, Attendance attendanceToAdd) { | ||
requireAllNonNull(index, attendanceToAdd); | ||
this.index = index; | ||
this.attendanceToAdd = attendanceToAdd; | ||
} | ||
|
||
/** | ||
* Executes the command and returns the result message. | ||
* | ||
* @param model {@code Model} which the command should operate on. | ||
* @return feedback message of the operation result for display | ||
* @throws CommandException If an error occurs during command execution. | ||
*/ | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
logger.log(Level.INFO, "Beginning command execution"); | ||
|
||
List<Student> lastShownList = model.getSortedStudentList(); | ||
if (index.getZeroBased() >= lastShownList.size()) { | ||
logger.log(Level.WARNING, "Invalid student index input error"); | ||
throw new CommandException(Messages.MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX); | ||
} | ||
Student studentToAddAttendance = lastShownList.get(index.getZeroBased()); | ||
|
||
List<Attendance> attendanceList = new ArrayList<>(studentToAddAttendance.getAttendance()); | ||
this.updateAttendanceList(attendanceList); | ||
|
||
Student updatedStudent = super.updateStudentAttendance(studentToAddAttendance, attendanceList); | ||
|
||
model.setStudent(studentToAddAttendance, updatedStudent); | ||
model.updateFilteredStudentList(PREDICATE_SHOW_ALL_STUDENTS); | ||
logger.log(Level.INFO, "Execution complete"); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, updatedStudent.getName(), attendanceToAdd)); | ||
} | ||
|
||
private List<Attendance> updateAttendanceList(List<Attendance> attendanceList) throws CommandException { | ||
boolean containsAttendanceAtDate = attendanceList | ||
.stream() | ||
.anyMatch(attendance -> attendance.getLessonDate().equals(attendanceToAdd.getLessonDate())); | ||
|
||
if (containsAttendanceAtDate) { | ||
throw new CommandException(MESSAGE_INVALID_ATTENDANCE_DATE); | ||
} | ||
attendanceList.add(attendanceToAdd); | ||
return attendanceList; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (!(obj instanceof AddAttendanceCommand)) { | ||
return false; | ||
} | ||
|
||
AddAttendanceCommand other = (AddAttendanceCommand) obj; | ||
return index.equals(other.index) && attendanceToAdd.equals(other.attendanceToAdd); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
src/main/java/seedu/address/logic/commands/AttendanceCommand.java
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,31 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.model.student.Student; | ||
import seedu.address.model.student.academic.Academic; | ||
import seedu.address.model.student.academic.Attendance; | ||
|
||
public abstract class AttendanceCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "attendance"; | ||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds or deletes an Attendance from a student in " | ||
+ " Reeve. \n" + "SUPPORTED COMMANDS: add, delete"; | ||
|
||
/** | ||
* Creates a new Student, with the provided attendance. | ||
* @param studentToAddAttendance student to add attendance to. | ||
* @param attendanceList new list of additional attendanceList. | ||
* @return updated Student. | ||
*/ | ||
public Student updateStudentAttendance(Student studentToAddAttendance, List<Attendance> attendanceList) { | ||
Academic academicToAddAttendance = studentToAddAttendance.getAcademic(); | ||
Academic updatedAcademic = new Academic(attendanceList); | ||
Student updatedStudent = new Student(studentToAddAttendance.getName(), studentToAddAttendance.getPhone(), | ||
studentToAddAttendance.getSchool(), studentToAddAttendance.getYear(), studentToAddAttendance.getAdmin(), | ||
studentToAddAttendance.getQuestions(), studentToAddAttendance.getExams(), | ||
updatedAcademic); | ||
|
||
return updatedStudent; | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
src/main/java/seedu/address/logic/commands/DeleteAttendanceCommand.java
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,117 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ATTENDANCE_DATE; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STUDENTS; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Stream; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.student.Student; | ||
import seedu.address.model.student.academic.Attendance; | ||
|
||
public class DeleteAttendanceCommand extends AttendanceCommand { | ||
|
||
public static final String COMMAND_WORD = "delete"; | ||
public static final String MESSAGE_USAGE = AttendanceCommand.COMMAND_WORD + " " + COMMAND_WORD | ||
+ ": deletes an Attendance from the student identified " | ||
+ "by the date of the lesson. \n" | ||
+ "Parameters: STUDENT_INDEX (must be a positive integer) " | ||
+ PREFIX_ATTENDANCE_DATE + "LESSON_DATE\n" | ||
+ "Example: " + AttendanceCommand.COMMAND_WORD + " " + COMMAND_WORD + " 2 " | ||
+ PREFIX_ATTENDANCE_DATE + "14/02/2020"; | ||
public static final String MESSAGE_SUCCESS = "Attendance deleted for %s for the date of %s"; | ||
public static final String MESSAGE_INVALID_ATTENDANCE_DATE = "There is no existing attendance for the entered date"; | ||
|
||
private static Logger logger = Logger.getLogger("Delete Attendance Log"); | ||
|
||
private final Index index; | ||
private final LocalDate attendanceDate; | ||
|
||
/** | ||
* Creates an EditAdditionalDetailCommand to add the specified {@code AdditionalDetail} to the student | ||
* at the specified {@code Index}. | ||
*/ | ||
public DeleteAttendanceCommand(Index index, LocalDate attendanceDate) { | ||
requireAllNonNull(index, attendanceDate); | ||
this.index = index; | ||
this.attendanceDate = attendanceDate; | ||
} | ||
|
||
/** | ||
* Executes the command and returns the result message. | ||
* | ||
* @param model {@code Model} which the command should operate on. | ||
* @return feedback message of the operation result for display | ||
* @throws CommandException If an error occurs during command execution. | ||
*/ | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
logger.log(Level.INFO, "Beginning command execution"); | ||
|
||
List<Student> lastShownList = model.getSortedStudentList(); | ||
if (index.getZeroBased() >= lastShownList.size()) { | ||
logger.log(Level.WARNING, "Invalid student index input error"); | ||
throw new CommandException(Messages.MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX); | ||
} | ||
Student studentToDeleteAttendance = lastShownList.get(index.getZeroBased()); | ||
|
||
List<Attendance> attendanceList = new ArrayList<>(studentToDeleteAttendance.getAttendance()); | ||
List<Attendance> updatedAttendanceList = this.updateAttendanceList(attendanceList); | ||
|
||
Student updatedStudent = super.updateStudentAttendance(studentToDeleteAttendance, updatedAttendanceList); | ||
|
||
model.setStudent(studentToDeleteAttendance, updatedStudent); | ||
model.updateFilteredStudentList(PREDICATE_SHOW_ALL_STUDENTS); | ||
logger.log(Level.INFO, "Execution complete"); | ||
|
||
return new CommandResult(String.format(MESSAGE_SUCCESS, updatedStudent.getName(), getUserInputDateString())); | ||
} | ||
|
||
private List<Attendance> updateAttendanceList(List<Attendance> attendanceList) throws CommandException { | ||
boolean containsAttendanceAtDate = attendanceList | ||
.stream() | ||
.anyMatch(attendance -> attendance.getLessonDate().equals(attendanceDate)); | ||
|
||
if (!containsAttendanceAtDate) { | ||
throw new CommandException(MESSAGE_INVALID_ATTENDANCE_DATE); | ||
} | ||
|
||
Stream<Attendance> matchingAttendance = attendanceList.stream() | ||
.filter(attendance -> attendance.getLessonDate().equals(attendanceDate)); | ||
Attendance attendanceToDelete = matchingAttendance.findFirst().get(); | ||
attendanceList.remove(attendanceToDelete); | ||
|
||
return attendanceList; | ||
} | ||
|
||
private String getUserInputDateString() { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy"); | ||
return attendanceDate.format(formatter); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
|
||
if (!(obj instanceof DeleteAttendanceCommand)) { | ||
return false; | ||
} | ||
|
||
DeleteAttendanceCommand other = (DeleteAttendanceCommand) obj; | ||
return index.equals(other.index) && attendanceDate.equals(other.attendanceDate); | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.