From cb3d04fb79a91cc9b6600e94fcf2bf56fb65c88b Mon Sep 17 00:00:00 2001 From: Mahidharah Date: Thu, 26 Oct 2023 19:27:41 +0800 Subject: [PATCH] no message --- .../logic/commands/DeleteClientCommand.java | 69 +++++++++++++++++++ .../parser/DeleteClientCommandParser.java | 30 ++++++++ 2 files changed, 99 insertions(+) create mode 100644 src/main/java/seedu/address/logic/commands/DeleteClientCommand.java create mode 100644 src/main/java/seedu/address/logic/parser/DeleteClientCommandParser.java diff --git a/src/main/java/seedu/address/logic/commands/DeleteClientCommand.java b/src/main/java/seedu/address/logic/commands/DeleteClientCommand.java new file mode 100644 index 00000000000..36dfceb5666 --- /dev/null +++ b/src/main/java/seedu/address/logic/commands/DeleteClientCommand.java @@ -0,0 +1,69 @@ +package seedu.address.logic.commands; + +import seedu.address.commons.core.index.Index; +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.client.Client; + +import java.util.List; + +import static java.util.Objects.requireNonNull; + +/** + * Deletes a developer identified using it's displayed index from the address book. + */ +public class DeleteClientCommand extends Command { + + public static final String COMMAND_WORD = "delete-client"; + + public static final String MESSAGE_USAGE = COMMAND_WORD + + ": Deletes the client identified by the index number used in the displayed developer list.\n" + + "Parameters: INDEX (must be a positive integer)\n" + + "Example: " + COMMAND_WORD + " 1"; + + public static final String MESSAGE_DELETE_CLIENT_SUCCESS = "Deleted Client: %1$s"; + + private final Index targetIndex; + + public DeleteClientCommand (Index targetIndex) { + this.targetIndex = targetIndex; + } + + @Override + public CommandResult execute(Model model) throws CommandException { + requireNonNull(model); + List lastShownList = model.getFilteredClientList(); + + if (targetIndex.getZeroBased() >= lastShownList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_CLIENT_DISPLAYED_INDEX); + } + + Client clientToDelete = lastShownList.get(targetIndex.getZeroBased()); + model.deleteClient(clientToDelete); + return new CommandResult(String.format(MESSAGE_DELETE_CLIENT_SUCCESS, Messages.format(clientToDelete)),TabIndex.Client); + } + + @Override + public boolean equals(Object other) { + if (other == this) { + return true; + } + + // instanceof handles nulls + if (!(other instanceof DeleteClientCommand)) { + return false; + } + + DeleteClientCommand otherDeleteClientCommand = (DeleteClientCommand) other; + return targetIndex.equals(otherDeleteClientCommand.targetIndex); + } + + @Override + public String toString() { + return new ToStringBuilder(this) + .add("targetIndex", targetIndex) + .toString(); + } +} diff --git a/src/main/java/seedu/address/logic/parser/DeleteClientCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteClientCommandParser.java new file mode 100644 index 00000000000..19dba593233 --- /dev/null +++ b/src/main/java/seedu/address/logic/parser/DeleteClientCommandParser.java @@ -0,0 +1,30 @@ +package seedu.address.logic.parser; + +import seedu.address.commons.core.index.Index; +import seedu.address.logic.commands.DeleteClientCommand; +import seedu.address.logic.commands.DeleteDeveloperCommand; +import seedu.address.logic.parser.exceptions.ParseException; + +import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; + +/** + * Parses input arguments and creates a new DeleteDeveloperCommand object + */ +public class DeleteClientCommandParser implements Parser { + + /** + * Parses the given {@code String} of arguments in the context of the DeleteDeveloperCommand + * and returns a DeleteDeveloperCommand object for execution. + * @throws ParseException if the user input does not conform the expected format + */ + public DeleteClientCommand parse(String args) throws ParseException { + try { + Index index = ParserUtil.parseIndex(args); + return new DeleteClientCommand(index); + } catch (ParseException pe) { + throw new ParseException( + String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteClientCommand.MESSAGE_USAGE), pe); + } + } + +}