Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EditScheduleCommand and ListScheduleCommand #159

Merged
merged 8 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.event.Event;
import seedu.address.model.grade.Grade;
import seedu.address.model.person.Person;

Expand Down Expand Up @@ -43,6 +44,11 @@ public interface Logic {
*/
ObservableList<Appointment> getFilteredAppointmentList();

/**
* Returns an unmodifiable view of the filtered list of events
*/
ObservableList<Event> getFilteredEventList();

/**
* Returns an unmodifiable view of the filtered list of grades
*/
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.event.Event;
import seedu.address.model.grade.Grade;
import seedu.address.model.person.Person;
import seedu.address.storage.Storage;
Expand Down Expand Up @@ -78,6 +79,11 @@ public ObservableList<Grade> getFilteredGradeList() {
return model.getFilteredGradeList();
}

@Override
public ObservableList<Event> getFilteredEventList() {
return model.getFilteredEventList();
}

@Override
public Path getAddressBookFilePath() {
return model.getAddressBookFilePath();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package seedu.address.logic.commands.schedulecommands;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIME_FROM;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIME_TO;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TITLE;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_SCHEDULE;

import java.util.List;
import java.util.Optional;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.appointment.AppointmentDateTime;
import seedu.address.model.schedule.Description;
import seedu.address.model.schedule.Schedule;
import seedu.address.model.schedule.Title;

/**
* Edits the details of an existing schedule in the schedule list.
*/
public class EditScheduleCommand extends Command {

public static final String COMMAND_WORD = "edit_schedule";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the schedule identified "
+ "by the index number used in the displayed schedule list. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_TITLE + "TITLE] "
+ "[" + PREFIX_DATE + "DATE] "
+ "[" + PREFIX_TIME_FROM + "TIME FROM] "
+ "[" + PREFIX_TIME_TO + "TIME TO] "
+ "[" + PREFIX_DESCRIPTION + "DESCRIPTION]\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_TITLE + "Science Tuition Homework "
+ PREFIX_DATE + "2021-3-20";

public static final String MESSAGE_EDIT_SCHEDULE_SUCCESS = "Edited Schedule: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_SCHEDULE = "This Schedule already exists.";

private final Index index;
private final EditScheduleDescriptor editScheduleDescriptor;

/**
* @param index of the schedule in the filtered schedule list to edit
*/
public EditScheduleCommand(Index index, EditScheduleDescriptor editScheduleDescriptor) {
requireNonNull(index);
requireNonNull(editScheduleDescriptor);

this.index = index;
this.editScheduleDescriptor = editScheduleDescriptor;
}

/**
* Creates and returns a {@code Person} with the details of {@code personToEdit}
* edited with {@code editPersonDescriptor}.
*/
private static Schedule createEditedSchedule(Schedule scheduleToEdit,
EditScheduleDescriptor editScheduleDescriptor) {
assert scheduleToEdit != null;

Title updatedTitle =
editScheduleDescriptor.getTitle().orElse(scheduleToEdit.getTitle());
AppointmentDateTime updatedTimeFrom =
editScheduleDescriptor.getTimeFrom().orElse(scheduleToEdit.getTimeFrom());
AppointmentDateTime updatedTimeTo =
editScheduleDescriptor.getTimeTo().orElse(scheduleToEdit.getTimeTo());
Description updatedDescription =
editScheduleDescriptor.getDescription().orElse(scheduleToEdit.getDescription());

return new Schedule(updatedTitle, updatedTimeFrom, updatedTimeTo, updatedDescription);
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Schedule> lastShownList = model.getFilteredScheduleList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_SCHEDULE_DISPLAYED_INDEX);
}

Schedule scheduleToEdit = lastShownList.get(index.getZeroBased());
Schedule editedSchedule = createEditedSchedule(scheduleToEdit, editScheduleDescriptor);

if (!scheduleToEdit.equals(editedSchedule) && model.hasSchedule(editedSchedule)) {
throw new CommandException(MESSAGE_DUPLICATE_SCHEDULE);
}

model.setSchedule(scheduleToEdit, editedSchedule);
model.updateFilteredScheduleList(PREDICATE_SHOW_ALL_SCHEDULE);
return new CommandResult(String.format(MESSAGE_EDIT_SCHEDULE_SUCCESS, editedSchedule));
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EditScheduleCommand)) {
return false;
}

// state check
EditScheduleCommand e = (EditScheduleCommand) other;
return index.equals(e.index) && editScheduleDescriptor.equals(e.editScheduleDescriptor);
}

/**
* Stores the details to edit the schedule with. Each non-empty field value will replace the
* corresponding field value of the schedule.
*/
public static class EditScheduleDescriptor {
private Title title;
private AppointmentDateTime timeFrom;
private AppointmentDateTime timeTo;
private Description description;

public EditScheduleDescriptor() {
}

/**
* Copy constructor.
*/
public EditScheduleDescriptor(EditScheduleDescriptor toCopy) {
setTitle(toCopy.title);
setTimeFrom(toCopy.timeFrom);
setTimeTo(toCopy.timeTo);
setDescription(toCopy.description);
}

/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(title, timeFrom, timeTo, description);
}

public Optional<Title> getTitle() {
return Optional.ofNullable(title);
}

public void setTitle(Title title) {
this.title = title;
}

public Optional<AppointmentDateTime> getTimeFrom() {
return Optional.ofNullable(this.timeFrom);
}

public void setTimeFrom(AppointmentDateTime timeFrom) {
this.timeFrom = timeFrom;
}

public Optional<AppointmentDateTime> getTimeTo() {
return Optional.ofNullable(this.timeTo);
}

public void setTimeTo(AppointmentDateTime timeTo) {
this.timeTo = timeTo;
}

public Optional<Description> getDescription() {
return Optional.ofNullable(description);
}

public void setDescription(Description description) {
this.description = description;
}

@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof EditScheduleDescriptor)) {
return false;
}

// state check
EditScheduleDescriptor e = (EditScheduleDescriptor) other;

return getTitle().equals(e.getTitle())
&& getTimeFrom().equals(e.getTimeFrom())
&& getTimeTo().equals(e.getTimeTo())
&& getDescription().equals(e.getDescription());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package seedu.address.logic.commands.schedulecommands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_SCHEDULE;

import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.model.Model;

/**
* Lists all schedules in the schedule list to the user.
*/
public class ListScheduleCommand extends Command {

public static final String COMMAND_WORD = "list_schedules";

public static final String MESSAGE_SUCCESS = "Listed all schedules";


@Override
public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredScheduleList(PREDICATE_SHOW_ALL_SCHEDULE);
return new CommandResult(MESSAGE_SUCCESS);
}
}
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/logic/parser/TutorTrackerParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import seedu.address.logic.commands.gradecommands.ListGradeCommand;
import seedu.address.logic.commands.schedulecommands.AddScheduleCommand;
import seedu.address.logic.commands.schedulecommands.DeleteScheduleCommand;
import seedu.address.logic.commands.schedulecommands.EditScheduleCommand;
import seedu.address.logic.commands.schedulecommands.ListScheduleCommand;
import seedu.address.logic.parser.appointmentparser.AddAppointmentCommandParser;
import seedu.address.logic.parser.appointmentparser.DeleteAppointmentCommandParser;
import seedu.address.logic.parser.appointmentparser.EditAppointmentCommandParser;
Expand All @@ -53,6 +55,7 @@
import seedu.address.logic.parser.gradeparser.EditGradeCommandParser;
import seedu.address.logic.parser.scheduleparser.AddScheduleCommandParser;
import seedu.address.logic.parser.scheduleparser.DeleteScheduleCommandParser;
import seedu.address.logic.parser.scheduleparser.EditScheduleCommandParser;

/**
* Parses user input.
Expand Down Expand Up @@ -167,9 +170,16 @@ public Command parseCommand(String userInput) throws ParseException {
/* Schedule Commands */
case AddScheduleCommand.COMMAND_WORD:
return new AddScheduleCommandParser().parse(arguments);

case EditScheduleCommand.COMMAND_WORD:
return new EditScheduleCommandParser().parse(arguments);

case DeleteScheduleCommand.COMMAND_WORD:
return new DeleteScheduleCommandParser().parse(arguments);

case ListScheduleCommand.COMMAND_WORD:
return new ListScheduleCommand();

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package seedu.address.logic.parser.scheduleparser;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIME_FROM;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TIME_TO;
import static seedu.address.logic.parser.CliSyntax.PREFIX_TITLE;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.schedulecommands.EditScheduleCommand;
import seedu.address.logic.commands.schedulecommands.EditScheduleCommand.EditScheduleDescriptor;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new EditScheduleCommand object
*/
public class EditScheduleCommandParser implements Parser<EditScheduleCommand> {

/**
* Parses the given {@code String} of arguments in the context of the EditScheduleCommand
* and returns an EditScheduleCommand object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
public EditScheduleCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_TITLE, PREFIX_DATE,
PREFIX_TIME_FROM, PREFIX_TIME_TO, PREFIX_DESCRIPTION);

Index index;
try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
EditScheduleCommand.MESSAGE_USAGE), pe);
}

EditScheduleDescriptor editScheduleDescriptor = new EditScheduleDescriptor();
if (argMultimap.getValue(PREFIX_TITLE).isPresent()) {
editScheduleDescriptor.setTitle(ParserUtil.parseTitle(argMultimap.getValue(PREFIX_TITLE).get()));
}

// TODO: Implement better handling of date and times (and combinations)
if (argMultimap.getValue(PREFIX_DATE).isPresent() && argMultimap.getValue(PREFIX_TIME_FROM).isPresent()) {
String dateString = argMultimap.getValue(PREFIX_DATE).get();
String timeFromString = argMultimap.getValue(PREFIX_TIME_FROM).get();
editScheduleDescriptor.setTimeFrom(ParserUtil.parseDateTime(dateString + " " + timeFromString));
}

if (argMultimap.getValue(PREFIX_DATE).isPresent() && argMultimap.getValue(PREFIX_TIME_TO).isPresent()) {
String dateString = argMultimap.getValue(PREFIX_DATE).get();
String timeToString = argMultimap.getValue(PREFIX_TIME_TO).get();
editScheduleDescriptor.setTimeTo(ParserUtil.parseDateTime(dateString + " " + timeToString));
}

if (argMultimap.getValue(PREFIX_DESCRIPTION).isPresent()) {
editScheduleDescriptor
.setDescription(ParserUtil.parseDescription(argMultimap.getValue(PREFIX_DESCRIPTION).get()));
}

if (!editScheduleDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditScheduleCommand.MESSAGE_NOT_EDITED);
}

return new EditScheduleCommand(index, editScheduleDescriptor);
}
}
Loading