forked from nus-cs2103-AY2324S1/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 #148 from chonguschonguschongus/branch-deleteAppoi…
…ntment Add DeleteAppointment command
- Loading branch information
Showing
10 changed files
with
185 additions
and
0 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
78 changes: 78 additions & 0 deletions
78
src/main/java/seedu/address/logic/commands/DeleteAppointmentCommand.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,78 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.commons.util.ToStringBuilder; | ||
import seedu.address.logic.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.appointment.Appointment; | ||
|
||
/** | ||
* Deletes a person identified using it's displayed index from the address book. | ||
*/ | ||
public class DeleteAppointmentCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "delete-appt"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Deletes the person identified by specified index in the Appointments list.\n" | ||
+ "Parameters: valid integer as shown in Appointments list \n" | ||
+ "Example: " + COMMAND_WORD + " 1"; | ||
|
||
public static final String MESSAGE_DELETE_APPOINTMENT_SUCCESS = "Deleted Appointment: %1$s"; | ||
private static final Logger logger = LogsCenter.getLogger(DeleteAppointmentCommand.class); | ||
|
||
private final int targetIndex; | ||
|
||
public DeleteAppointmentCommand(int targetIndex) { | ||
this.targetIndex = targetIndex; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Appointment> lastShownList = new ArrayList<>(); | ||
lastShownList.addAll(model.getFilteredAppointmentList()); | ||
try { | ||
Appointment target = lastShownList.get(targetIndex - 1); | ||
model.deleteAppointment(target); | ||
List<Appointment> updatedList = new ArrayList<>(); | ||
updatedList.addAll(model.getFilteredAppointmentList()); | ||
assert updatedList.size() < lastShownList.size(); | ||
logger.info("Successfully deleted appointment"); | ||
return new CommandResult(String.format(MESSAGE_DELETE_APPOINTMENT_SUCCESS, Messages.format(target))); | ||
} catch (IndexOutOfBoundsException e) { | ||
logger.warning("Appointment does not exist"); | ||
throw new CommandException(Messages.MESSAGE_APPOINTMENT_NOT_FOUND); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof DeleteAppointmentCommand)) { | ||
return false; | ||
} | ||
|
||
DeleteAppointmentCommand otherDeleteAppointmentCommand = (DeleteAppointmentCommand) other; | ||
return targetIndex == otherDeleteAppointmentCommand.targetIndex; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.add("targetIc", targetIndex) | ||
.toString(); | ||
} | ||
} | ||
|
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
34 changes: 34 additions & 0 deletions
34
src/main/java/seedu/address/logic/parser/DeleteAppointmentCommandParser.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,34 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
|
||
import java.util.logging.Logger; | ||
|
||
import seedu.address.commons.core.LogsCenter; | ||
import seedu.address.logic.commands.DeleteAppointmentCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
|
||
/** | ||
* Parses input arguments and creates a new DeleteCommand object | ||
*/ | ||
public class DeleteAppointmentCommandParser implements Parser<DeleteAppointmentCommand> { | ||
private static final Logger logger = LogsCenter.getLogger(DeleteCommandParser.class); | ||
/** | ||
* Parses the given {@code String} of arguments in the context of the DeleteCommand | ||
* and returns a DeleteCommand object for execution. | ||
* @throws ParseException if the user input does not conform the expected format | ||
*/ | ||
public DeleteAppointmentCommand parse(String args) throws ParseException { | ||
String trimmedArgs = args.trim(); | ||
try { | ||
int index = Integer.parseInt(trimmedArgs); | ||
logger.info("Successfully parsed integer from DeleteAppointmentCommand: " + index); | ||
return new DeleteAppointmentCommand(index); | ||
} catch (NumberFormatException e) { | ||
logger.warning("Invalid user input for delete appointment command"); | ||
throw new ParseException( | ||
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteAppointmentCommand.MESSAGE_USAGE), e); | ||
} | ||
} | ||
|
||
} |
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
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
24 changes: 24 additions & 0 deletions
24
src/test/java/seedu/address/logic/parser/DeleteAppointmentCommandParserTest.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,24 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; | ||
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.address.logic.commands.DeleteAppointmentCommand; | ||
|
||
public class DeleteAppointmentCommandParserTest { | ||
private DeleteAppointmentCommandParser parser = new DeleteAppointmentCommandParser(); | ||
|
||
@Test | ||
public void parseValidArgs_returnsDeleteAppointmentCommand() { | ||
assertParseSuccess(parser, "1", new DeleteAppointmentCommand(1)); | ||
} | ||
|
||
@Test | ||
public void parseInvalidArgs_throwsParseException() { | ||
assertParseFailure(parser, "a", String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
DeleteAppointmentCommand.MESSAGE_USAGE)); | ||
} | ||
} |