Skip to content

Commit

Permalink
Merge pull request #62 from justinlengch/updated-lesson-class
Browse files Browse the repository at this point in the history
Update lesson class to use arraylist, and add schedule lesson command
  • Loading branch information
justinlengch authored Apr 3, 2024
2 parents c579d87 + 5c1807f commit 7166431
Show file tree
Hide file tree
Showing 30 changed files with 498 additions and 230 deletions.
8 changes: 2 additions & 6 deletions src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down
84 changes: 84 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddLessonCommand.java
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);
}
}
19 changes: 7 additions & 12 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STUDENTS;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
Expand Down Expand Up @@ -101,7 +98,7 @@ private static Student createEditedStudent(Student studentToEdit, EditStudentDes
Phone updatedPhone = editStudentDescriptor.getPhone().orElse(studentToEdit.getPhone());
Email updatedEmail = editStudentDescriptor.getEmail().orElse(studentToEdit.getEmail());
Address updatedAddress = editStudentDescriptor.getAddress().orElse(studentToEdit.getAddress());
Set<Lesson> updatedLessons = editStudentDescriptor.getLessons().orElse(studentToEdit.getLessons());
List<Lesson> updatedLessons = editStudentDescriptor.getLessons().orElse(studentToEdit.getLessons());
Remark updatedRemark = studentToEdit.getRemark();
Subject updatedSubject = editStudentDescriptor.getSubject().orElse(studentToEdit.getSubject());

Expand Down Expand Up @@ -142,7 +139,7 @@ public static class EditStudentDescriptor {
private Phone phone;
private Email email;
private Address address;
private Set<Lesson> lessons;
private List<Lesson> lessons;
private Subject subject;
private Remark remark;
public EditStudentDescriptor() {}
Expand All @@ -159,7 +156,6 @@ public EditStudentDescriptor(EditStudentDescriptor toCopy) {
setLessons(toCopy.lessons);
setSubject(toCopy.subject);
setRemark(toCopy.remark);
setLessons(toCopy.lessons);
}

public void setRemark(Remark remark) {
Expand Down Expand Up @@ -213,17 +209,16 @@ public Optional<Address> getAddress() {
* Sets {@code lessons} to this object's {@code lessons}.
* A defensive copy of {@code lessons} is used internally.
*/
public void setLessons(Set<Lesson> lessons) {
this.lessons = (lessons != null) ? new HashSet<>(lessons) : null;
public void setLessons(List<Lesson> lessons) {
this.lessons = lessons;
}

/**
* Returns an unmodifiable lesson set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns an modifiable lesson list,
* Returns {@code Optional#empty()} if {@code lessons} is null.
*/
public Optional<Set<Lesson>> getLessons() {
return (lessons != null) ? Optional.of(Collections.unmodifiableSet(lessons)) : Optional.empty();
public Optional<List<Lesson>> getLessons() {
return Optional.ofNullable(lessons);
}

public void setSubject(Subject subject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public CommandResult execute(Model model) throws CommandException {
Student studentToEdit = lastShownList.get(index.getZeroBased());
Student editedStudent = new Student(studentToEdit.getName(), studentToEdit.getPhone(), studentToEdit.getEmail(),
studentToEdit.getAddress(), studentToEdit.getSubject(),
studentToEdit.getRemark(), studentToEdit.getLessons());
this.remark, studentToEdit.getLessons());

model.setStudent(studentToEdit, editedStudent);
model.updateFilteredStudentList(PREDICATE_SHOW_ALL_STUDENTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SUBJECT;

import java.util.Set;
import java.util.List;
import java.util.stream.Stream;

import seedu.address.logic.commands.AddCommand;
Expand Down Expand Up @@ -49,9 +49,9 @@ public AddCommand parse(String args) throws ParseException {
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Lesson> lessonList = ParserUtil.parseLessons(argMultimap.getAllValues(PREFIX_LESSON));
Subject subject = ParserUtil.parseSubject(argMultimap.getValue(PREFIX_SUBJECT).get());
Remark remark = new Remark(argMultimap.getValue(PREFIX_REMARK).orElse("")); // default value
Remark remark = new Remark(""); // default value
List<Lesson> lessonList = ParserUtil.parseLessons((argMultimap.getAllValues(PREFIX_LESSON)));
Student student = new Student(name, phone, email, address, subject, remark, lessonList);
return new AddCommand(student);
}
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddLessonCommand;
import seedu.address.logic.commands.ClearCommand;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.DeleteCommand;
Expand Down Expand Up @@ -81,6 +82,10 @@ public Command parseCommand(String userInput) throws ParseException {
case RemarkCommand.COMMAND_WORD:
return new RemarkCommandParser().parse(arguments);

case AddLessonCommand.COMMAND_WORD:
return new AddLessonCommandParser().parse(arguments);


default:
logger.finer("This user input caused a ParseException: " + userInput);
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
Expand Down Expand Up @@ -74,18 +74,18 @@ public EditCommand parse(String args) throws ParseException {
}

/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty.
* If {@code tags} contain only one element which is an empty string, it will be parsed into a
* {@code Set<Tag>} containing zero tags.
* Parses {@code Collection<String> lessons} into a {@code List<Lesson>} if {@code lessons} is non-empty.
* If {@code lessons} contain only one element which is an empty string, it will be parsed into a
* {@code List<Lesson>} containing zero lessons.
*/
private Optional<Set<Lesson>> parseLessonsForEdit(Collection<String> lessons) throws ParseException {
private Optional<List<Lesson>> parseLessonsForEdit(Collection<String> lessons) throws ParseException {
assert lessons != null;

if (lessons.isEmpty()) {
return Optional.empty();
}
Collection<String> lessonSet = lessons.size() == 1 && lessons.contains("") ? Collections.emptySet() : lessons;
return Optional.of(ParserUtil.parseLessons(lessonSet));
Collection<String> lessonList = lessons.size() == 1 && lessons.contains("") ? Collections.emptyList() : lessons;
return Optional.of(ParserUtil.parseLessons(lessonList));
}

}
Loading

0 comments on commit 7166431

Please sign in to comment.