forked from nus-cs2103-AY2324S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 4
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 #62 from justinlengch/updated-lesson-class
Update lesson class to use arraylist, and add schedule lesson command
- Loading branch information
Showing
30 changed files
with
498 additions
and
230 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 |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
import static seedu.address.logic.parser.CliSyntax.PREFIX_LESSON; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_SUBJECT; | ||
|
||
import seedu.address.commons.util.ToStringBuilder; | ||
|
@@ -28,17 +27,14 @@ public class AddCommand extends Command { | |
+ PREFIX_PHONE + "PHONE " | ||
+ PREFIX_EMAIL + "EMAIL " | ||
+ PREFIX_ADDRESS + "ADDRESS " | ||
+ PREFIX_SUBJECT + "SUBJECT " | ||
+ PREFIX_REMARK + "REMARK " | ||
+ "[" + PREFIX_LESSON + "LESSON]...\n" | ||
+ PREFIX_SUBJECT + "SUBJECT \n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_NAME + "John Doe " | ||
+ PREFIX_PHONE + "98765432 " | ||
+ PREFIX_EMAIL + "[email protected] " | ||
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 " | ||
+ PREFIX_SUBJECT + "Maths " | ||
+ PREFIX_REMARK + "He is a slow learner. " | ||
+ PREFIX_LESSON + "Maths|23-05-2024|10:00|1"; | ||
+ PREFIX_LESSON + "Maths|2021-03-01|10:00"; | ||
|
||
public static final String MESSAGE_SUCCESS = "New student added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_STUDENT = "This student already exists in the address book"; | ||
|
84 changes: 84 additions & 0 deletions
84
src/main/java/seedu/address/logic/commands/AddLessonCommand.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,84 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STUDENTS; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.student.Lesson; | ||
import seedu.address.model.student.Student; | ||
|
||
|
||
/** | ||
* Adds a lesson to the student identified using it's displayed index from the address book. | ||
*/ | ||
public class AddLessonCommand extends Command { | ||
public static final String COMMAND_WORD = "schedule"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Adds a lesson to the student identified " | ||
+ "by the index number used in the last person listing. " | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "l/ [LESSON]\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ "l/ 20-02-2002|10:00"; | ||
public static final String MESSAGE_ADD_LESSON_SUCCESS = "Scheduled lesson to student: %1$s"; | ||
private final Index index; | ||
private final LocalDate date; | ||
private final LocalTime time; | ||
private Integer isCompleted; | ||
/*** | ||
* Creates a AddLessonCommand to add the lesson to the specified {@code Person}, | ||
* without the isCompleted field. | ||
*/ | ||
public AddLessonCommand(Index index, LocalDate date, LocalTime time) { | ||
requireAllNonNull(index, date, time); | ||
this.index = index; | ||
this.date = date; | ||
this.time = time; | ||
} | ||
/*** | ||
* Creates a AddLessonCommand to add the lesson to the specified {@code Person}, | ||
* with the isCompleted field. | ||
*/ | ||
public AddLessonCommand(Index index, LocalDate date, LocalTime time, int isCompleted) { | ||
this(index, date, time); | ||
this.isCompleted = isCompleted; | ||
} | ||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
List<Student> lastShownList = model.getFilteredStudentList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX); | ||
} | ||
|
||
Student studentToEdit = lastShownList.get(index.getZeroBased()); | ||
List<Lesson> lessonList = new ArrayList<>(studentToEdit.getLessons()); | ||
String studentToEditSubject = studentToEdit.getSubject().value; | ||
if (isCompleted != null) { | ||
lessonList.add(new Lesson(studentToEditSubject, this.date, this.time, this.isCompleted)); | ||
} else { | ||
lessonList.add(new Lesson(studentToEditSubject, this.date, this.time)); | ||
} | ||
Student editedStudent = new Student(studentToEdit.getName(), studentToEdit.getPhone(), studentToEdit.getEmail(), | ||
studentToEdit.getAddress(), studentToEdit.getSubject(), | ||
studentToEdit.getRemark(), lessonList); | ||
|
||
model.setStudent(studentToEdit, editedStudent); | ||
model.updateFilteredStudentList(PREDICATE_SHOW_ALL_STUDENTS); | ||
|
||
return new CommandResult(generateSuccessMessage(editedStudent)); | ||
} | ||
|
||
private String generateSuccessMessage(Student editedStudent) { | ||
return String.format(MESSAGE_ADD_LESSON_SUCCESS, editedStudent); | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
src/main/java/seedu/address/logic/parser/AddLessonCommandParser.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,82 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_LESSON; | ||
import static seedu.address.model.student.Lesson.DATE_FORMATTER; | ||
import static seedu.address.model.student.Lesson.TIME_FORMATTER; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
import java.time.format.DateTimeParseException; | ||
|
||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.commons.exceptions.IllegalValueException; | ||
import seedu.address.logic.commands.AddLessonCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new AddLessonCommand object | ||
*/ | ||
public class AddLessonCommandParser implements Parser<AddLessonCommand> { | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the AddLessonCommand | ||
* and returns an AddLessonCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public AddLessonCommand parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_LESSON); | ||
|
||
Index index; | ||
try { | ||
index = ParserUtil.parseIndex(argMultimap.getPreamble()); | ||
} catch (IllegalValueException ive) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
AddLessonCommand.MESSAGE_USAGE), ive); | ||
} | ||
|
||
String lesson = argMultimap.getValue(PREFIX_LESSON).orElse(""); | ||
|
||
if (!isValidLesson(lesson)) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
AddLessonCommand.MESSAGE_USAGE)); | ||
} | ||
// split lesson into its attributes based on "|" character | ||
String[] lessonDetails = lesson.trim().split("\\|"); | ||
LocalDate dateDetail = LocalDate.parse(lessonDetails[0], DATE_FORMATTER); | ||
LocalTime timeDetail = LocalTime.parse(lessonDetails[1], TIME_FORMATTER); | ||
int isCompleted; | ||
if (lessonDetails.length == 3) { | ||
isCompleted = Integer.parseInt(lessonDetails[3]); | ||
return new AddLessonCommand(index, dateDetail, timeDetail, isCompleted); | ||
} else { | ||
return new AddLessonCommand(index, dateDetail, timeDetail); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the lesson is in the correct format. | ||
* @param lesson the lesson to be checked | ||
* @return true if the lesson is in the correct format, false otherwise | ||
*/ | ||
public boolean isValidLesson(String lesson) { | ||
String[] lessonDetails = lesson.trim().split("\\|"); | ||
if (lessonDetails.length != 2 && lessonDetails.length != 3) { | ||
return false; | ||
} | ||
try { | ||
LocalDate.parse(lessonDetails[0], DATE_FORMATTER); | ||
LocalTime.parse(lessonDetails[1], TIME_FORMATTER); | ||
if (lessonDetails.length == 3) { | ||
int isCompleted = Integer.parseInt(lessonDetails[2]); | ||
if (isCompleted != 0 && isCompleted != 1) { | ||
return false; | ||
} | ||
} | ||
} catch (DateTimeParseException | NumberFormatException e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
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.