Skip to content

Commit

Permalink
Merge pull request #191 from AY2223S1-CS2103T-W16-4/feat-edit-commands
Browse files Browse the repository at this point in the history
Fix edit commands
  • Loading branch information
sikai00 authored Oct 27, 2022
2 parents 60bdee5 + 852e7fc commit d9aaace
Show file tree
Hide file tree
Showing 33 changed files with 469 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public class AddMeetingCommand extends Command {
+ PREFIX_START_TIME + "START TIME "
+ PREFIX_END_TIME + "END TIME "
+ PREFIX_DESCRIPTION + "DESCRIPTION ";
public static final String MESSAGE_DUPLICATE_MEETING = "This meeting already exists in MyInsuRec";
public static final String MESSAGE_CLIENT_NOT_FOUND = "A client named %s could not be found";
public static final String MESSAGE_CONFLICTING_MEETING =
"This meeting conflicts with another that exists in MyInsuRec";
public static final String MESSAGE_MEETING_NOT_FOUND = "A meeting %s could not be found";

private final MeetingDate meetingDate;
private final MeetingTime meetingStartTime;
Expand Down Expand Up @@ -63,7 +64,7 @@ public CommandResult execute(Model model) throws CommandException {
Meeting meetingToAdd = new Meeting(clientToUpdate, description, meetingDate, meetingStartTime, meetingEndTime);

if (model.hasMeeting(meetingToAdd)) {
throw new CommandException(MESSAGE_DUPLICATE_MEETING);
throw new CommandException(MESSAGE_CONFLICTING_MEETING);
}

clientToUpdate.addMeeting(meetingToAdd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
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_PRODUCT;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_CLIENTS;

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

import javafx.collections.ObservableList;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
Expand All @@ -28,14 +28,15 @@
import seedu.address.model.client.Email;
import seedu.address.model.client.Name;
import seedu.address.model.client.Phone;
import seedu.address.model.meeting.Meeting;
import seedu.address.model.product.Product;

/**
* Edits the details of an existing client in MyInsuRec.
*/
public class EditCommand extends Command {
public class EditClientCommand extends Command {

public static final String COMMAND_WORD = "edit";
public static final String COMMAND_WORD = "editClient";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the client identified "
+ "by the index number used in the displayed client list. "
Expand Down Expand Up @@ -63,7 +64,7 @@ public class EditCommand extends Command {
* @param index of the client in the filtered client list to edit
* @param editClientDescriptor details to edit the client with
*/
public EditCommand(Index index, EditClientDescriptor editClientDescriptor) {
public EditClientCommand(Index index, EditClientDescriptor editClientDescriptor) {
requireNonNull(index);
requireNonNull(editClientDescriptor);

Expand All @@ -73,9 +74,7 @@ public EditCommand(Index index, EditClientDescriptor editClientDescriptor) {

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

ObservableList<Client> lastShownList = model.getFilteredClientList();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_CLIENT_DISPLAYED_INDEX);
}
Expand All @@ -95,8 +94,7 @@ public CommandResult execute(Model model) throws CommandException {
}

model.setClient(clientToEdit, editedClient);
model.updateFilteredClientList(PREDICATE_SHOW_ALL_CLIENTS);
return new CommandResult(String.format(MESSAGE_EDIT_CLIENT_SUCCESS, editedClient));
return new CommandResult(String.format(MESSAGE_EDIT_CLIENT_SUCCESS, editedClient), CommandSpecific.CLIENT);
}

/**
Expand All @@ -119,8 +117,21 @@ private static Client createEditedClient(Client clientToEdit, EditClientDescript
: editClientDescriptor.getBirthday();
Set<Product> updatedProducts = editClientDescriptor.getProducts().orElse(clientToEdit.getProducts());

return new Client(updatedName, updatedPhone, updatedEmail, updatedAddress,
Client client = new Client(updatedName, updatedPhone, updatedEmail, updatedAddress,
updatedBirthday, updatedProducts);

// update client in each meeting this client has
List<Meeting> meetings = clientToEdit.getMeetings();

Meeting meeting;
Meeting updatedMeeting;
for (int i = 0; i < meetings.size(); i++) {
meeting = meetings.get(i);
updatedMeeting = new Meeting(client, meeting.getDescription(), meeting.getMeetingDate(),
meeting.getMeetingStartTime(), meeting.getMeetingEndTime());
client.addMeeting(updatedMeeting);
}
return client;
}

@Override
Expand All @@ -131,12 +142,12 @@ public boolean equals(Object other) {
}

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

// state check
EditCommand e = (EditCommand) other;
EditClientCommand e = (EditClientCommand) other;
return index.equals(e.index)
&& editClientDescriptor.equals(e.editClientDescriptor);
}
Expand Down Expand Up @@ -166,7 +177,6 @@ public EditClientDescriptor(EditClientDescriptor toCopy) {
address = toCopy.address;
birthday = toCopy.birthday;
products = toCopy.products;

}

/**
Expand Down
212 changes: 212 additions & 0 deletions src/main/java/seedu/address/logic/commands/EditMeetingCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_END_TIME_BEFORE_START_TIME;
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_END_TIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_INDEX;
import static seedu.address.logic.parser.CliSyntax.PREFIX_START_TIME;

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.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.client.Client;
import seedu.address.model.meeting.Description;
import seedu.address.model.meeting.Meeting;
import seedu.address.model.meeting.MeetingDate;
import seedu.address.model.meeting.MeetingTime;


/**
* Edits the details of an existing meeting in MyInsuRec.
*/
public class EditMeetingCommand extends Command {

public static final String COMMAND_WORD = "editMeeting";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the meeting identified "
+ "by the index number used in the displayed meeting list. \n"
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) \n"
+ "[" + PREFIX_DATE + "DATE] "
+ "[" + PREFIX_START_TIME + "START TIME] "
+ "[" + PREFIX_END_TIME + "END TIME] "
+ "[" + PREFIX_DESCRIPTION + "DESCRIPTION]\n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_INDEX + "1 "
+ PREFIX_DATE + "23122022 ";

public static final String MESSAGE_EDIT_MEETING_SUCCESS = "Edited Meeting: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_MEETING = "This meeting already exists in MyInsuRec.";

private final Index index;
private final EditMeetingDescriptor editMeetingDescriptor;

/**
* @param index of the meeting in the filtered meeting list to edit
* @param editMeetingDescriptor details to edit the meeting with
*/
public EditMeetingCommand(Index index, EditMeetingDescriptor editMeetingDescriptor) {
requireNonNull(index);
requireNonNull(editMeetingDescriptor);

this.index = index;
this.editMeetingDescriptor = new EditMeetingDescriptor(editMeetingDescriptor);
}

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

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

Meeting meetingToEdit = lastShownList.get(index.getZeroBased());
Meeting editedMeeting = createEditedMeeting(meetingToEdit, editMeetingDescriptor);

if (model.hasMeeting(editedMeeting)) {
throw new CommandException(MESSAGE_DUPLICATE_MEETING);
}

// update meeting list
model.setMeeting(meetingToEdit, editedMeeting);

// update meeting in client
Client client = meetingToEdit.getClient();
client.removeMeeting(meetingToEdit);
client.addMeeting(editedMeeting);

return new CommandResult(String.format(MESSAGE_EDIT_MEETING_SUCCESS, editedMeeting), CommandSpecific.MEETING);
}

/**
* Creates and returns a {@code Meeting} with the details of {@code meetingToEdit}
* edited with {@code EditMeetingDescriptor}.
*/
private static Meeting createEditedMeeting(Meeting meetingToEdit, EditMeetingDescriptor editMeetingDescriptor)
throws CommandException {
assert meetingToEdit != null;

MeetingDate updatedDate = editMeetingDescriptor.getDate().orElse(meetingToEdit.getMeetingDate());
Description updatedDescription = editMeetingDescriptor.getDescription().orElse(meetingToEdit.getDescription());
MeetingTime updatedEndTime = editMeetingDescriptor.getEndTime().orElse(meetingToEdit.getMeetingEndTime());
MeetingTime updatedStartTime = editMeetingDescriptor.getStartTime().orElse(meetingToEdit.getMeetingStartTime());
if (updatedEndTime.isBefore(updatedStartTime)) {
throw new CommandException(MESSAGE_END_TIME_BEFORE_START_TIME);
}
Meeting meeting = new Meeting(meetingToEdit.getClient(), updatedDescription, updatedDate,
updatedStartTime, updatedEndTime);
return meeting;
}

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

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

// state check
EditMeetingCommand e = (EditMeetingCommand) other;
return index.equals(e.index)
&& editMeetingDescriptor.equals(e.editMeetingDescriptor);
}

/**
* Stores the details to edit the meeting with. Each non-empty field value will replace the
* corresponding field value of the meeting.
*/
public static class EditMeetingDescriptor {
private MeetingDate date;
private MeetingTime startTime;
private MeetingTime endTime;
private Description description;

public EditMeetingDescriptor() {}

/**
* Copy constructor.
*/
public EditMeetingDescriptor(EditMeetingDescriptor toCopy) {
date = toCopy.date;
startTime = toCopy.startTime;
endTime = toCopy.endTime;
description = toCopy.description;
}

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

public void setDate(MeetingDate date) {
this.date = date;
}

public Optional<MeetingDate> getDate() {
return Optional.ofNullable(date);
}

public void setStartTime(MeetingTime startTime) {
this.startTime = startTime;
}

public Optional<MeetingTime> getStartTime() {
return Optional.ofNullable(startTime);
}

public void setEndTime(MeetingTime endTime) {
this.endTime = endTime;
}

public Optional<MeetingTime> getEndTime() {
return Optional.ofNullable(endTime);
}

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

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


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

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

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

return getDate().equals(e.getDate())
&& getEndTime().equals(e.getEndTime())
&& getStartTime().equals(e.getStartTime())
&& getDescription().equals(e.getDescription());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.EditCommand;
import seedu.address.logic.commands.EditCommand.EditClientDescriptor;
import seedu.address.logic.commands.EditClientCommand;
import seedu.address.logic.commands.EditClientCommand.EditClientDescriptor;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.client.Birthday;
import seedu.address.model.product.Product;

/**
* Parses input arguments and creates a new EditCommand object
* Parses input arguments and creates a new EditClientCommand object
*/
public class EditCommandParser implements Parser<EditCommand> {
public class EditClientCommandParser implements Parser<EditClientCommand> {

/**
* Parses the given {@code String} of arguments in the context of the EditCommand
* and returns an EditCommand object for execution.
* Parses the given {@code String} of arguments in the context of the EditClientCommand
* and returns an EditClientCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public EditCommand parse(String args) throws ParseException {
public EditClientCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_INDEX, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
Expand All @@ -45,7 +45,7 @@ public EditCommand parse(String args) throws ParseException {
try {
index = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_INDEX).get());
} catch (ParseException | NoSuchElementException e) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), e);
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditClientCommand.MESSAGE_USAGE), e);
}

EditClientDescriptor editClientDescriptor = new EditClientDescriptor();
Expand Down Expand Up @@ -73,10 +73,10 @@ public EditCommand parse(String args) throws ParseException {


if (!editClientDescriptor.isAnyFieldEdited()) {
throw new ParseException(EditCommand.MESSAGE_NOT_EDITED);
throw new ParseException(EditClientCommand.MESSAGE_NOT_EDITED);
}

return new EditCommand(index, editClientDescriptor);
return new EditClientCommand(index, editClientDescriptor);
}

/**
Expand Down
Loading

0 comments on commit d9aaace

Please sign in to comment.