forked from nus-cs2103-AY2223S1/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 #80 from Gavzzz/v1.3
Fix bugs for sorting offerlist, listinglist and clientlist and add commands for client
- Loading branch information
Showing
31 changed files
with
1,164 additions
and
48 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
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
68 changes: 68 additions & 0 deletions
68
src/main/java/seedu/address/logic/commands/AddClientCommand.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,68 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
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_TAG; | ||
|
||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Client; | ||
|
||
/** | ||
* Adds a client to the address book. | ||
*/ | ||
public class AddClientCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "addc"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds a client to the address book. " | ||
+ "Parameters: " | ||
+ PREFIX_NAME + "NAME " | ||
+ PREFIX_PHONE + "PHONE " | ||
+ PREFIX_EMAIL + "EMAIL " | ||
+ PREFIX_ADDRESS + "ADDRESS " | ||
+ "[" + PREFIX_TAG + "TAG]...\n" | ||
+ "Example: " + COMMAND_WORD + " " | ||
+ PREFIX_NAME + "John Doe " | ||
+ PREFIX_PHONE + "98765432 " | ||
+ PREFIX_EMAIL + "[email protected] " | ||
+ PREFIX_ADDRESS + "311, Clementi Ave 2, #02-25 " | ||
+ PREFIX_TAG + "friends " | ||
+ PREFIX_TAG + "owesMoney"; | ||
|
||
public static final String MESSAGE_SUCCESS = "New client added: %1$s"; | ||
public static final String MESSAGE_DUPLICATE_CLIENT = "This client already exists in the address book"; | ||
|
||
private final Client toAdd; | ||
|
||
/** | ||
* Creates an AddCommand to add the specified {@code Client} | ||
*/ | ||
public AddClientCommand(Client client) { | ||
requireNonNull(client); | ||
toAdd = client; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
|
||
if (model.hasClient(toAdd)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_CLIENT); | ||
} | ||
|
||
model.addClient(toAdd); | ||
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof AddClientCommand // instanceof handles nulls | ||
&& toAdd.equals(((AddClientCommand) other).toAdd)); | ||
} | ||
} | ||
|
53 changes: 53 additions & 0 deletions
53
src/main/java/seedu/address/logic/commands/DeleteClientCommand.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,53 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.util.List; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.commons.core.index.Index; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.person.Client; | ||
|
||
/** | ||
* Deletes a client identified using it's displayed index from the address book. | ||
*/ | ||
public class DeleteClientCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "deleteClient"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Deletes the client identified by the index number used in the displayed client 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<Client> 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, clientToDelete)); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
return other == this // short circuit if same object | ||
|| (other instanceof DeleteClientCommand // instanceof handles nulls | ||
&& targetIndex.equals(((DeleteClientCommand) other).targetIndex)); // state check | ||
} | ||
} |
227 changes: 227 additions & 0 deletions
227
src/main/java/seedu/address/logic/commands/EditClientCommand.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,227 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; | ||
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_TAG; | ||
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 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.person.Address; | ||
import seedu.address.model.person.Client; | ||
import seedu.address.model.person.Email; | ||
import seedu.address.model.person.Name; | ||
import seedu.address.model.person.Phone; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Edits the details of an existing client in the address book. | ||
*/ | ||
public class EditClientCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "editClients"; | ||
|
||
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. " | ||
+ "Existing values will be overwritten by the input values.\n" | ||
+ "Parameters: INDEX (must be a positive integer) " | ||
+ "[" + PREFIX_NAME + "NAME] " | ||
+ "[" + PREFIX_PHONE + "PHONE] " | ||
+ "[" + PREFIX_EMAIL + "EMAIL] " | ||
+ "[" + PREFIX_ADDRESS + "ADDRESS] " | ||
+ "[" + PREFIX_TAG + "TAG]...\n" | ||
+ "Example: " + COMMAND_WORD + " 1 " | ||
+ PREFIX_PHONE + "91234567 " | ||
+ PREFIX_EMAIL + "[email protected]"; | ||
|
||
public static final String MESSAGE_EDIT_CLIENT_SUCCESS = "Edited Clients: %1$s"; | ||
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; | ||
public static final String MESSAGE_DUPLICATE_CLIENT = "This client already exists in the address book."; | ||
|
||
private final Index index; | ||
private final EditClientDescriptor editClientDescriptor; | ||
|
||
/** | ||
* @param index of the client in the filtered client list to edit | ||
* @param editClientDescriptor details to edit the client with | ||
*/ | ||
public EditClientCommand(Index index, EditClientDescriptor editClientDescriptor) { | ||
requireNonNull(index); | ||
requireNonNull(editClientDescriptor); | ||
|
||
this.index = index; | ||
this.editClientDescriptor = new EditClientDescriptor(editClientDescriptor); | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
List<Client> lastShownList = model.getFilteredClientList(); | ||
|
||
if (index.getZeroBased() >= lastShownList.size()) { | ||
throw new CommandException(Messages.MESSAGE_INVALID_CLIENT_DISPLAYED_INDEX); | ||
} | ||
|
||
Client clientToEdit = lastShownList.get(index.getZeroBased()); | ||
Client editedClient = createEditedClient(clientToEdit, editClientDescriptor); | ||
|
||
if (!clientToEdit.isSameClient(editedClient) && model.hasClient(editedClient)) { | ||
throw new CommandException(MESSAGE_DUPLICATE_CLIENT); | ||
} | ||
|
||
model.setClient(clientToEdit, editedClient); | ||
model.updateFilteredClientList(PREDICATE_SHOW_ALL_CLIENTS); | ||
return new CommandResult(String.format(MESSAGE_EDIT_CLIENT_SUCCESS, editedClient)); | ||
} | ||
|
||
/** | ||
* Creates and returns a {@code Client} with the details of {@code clientToEdit} | ||
* edited with {@code editClientDescriptor}. | ||
*/ | ||
private static Client createEditedClient(Client clientToEdit, EditClientDescriptor editClientDescriptor) { | ||
assert clientToEdit != null; | ||
|
||
Name updatedName = editClientDescriptor.getName().orElse(clientToEdit.getName()); | ||
Phone updatedPhone = editClientDescriptor.getPhone().orElse(clientToEdit.getPhone()); | ||
Email updatedEmail = editClientDescriptor.getEmail().orElse(clientToEdit.getEmail()); | ||
Address updatedAddress = editClientDescriptor.getAddress().orElse(clientToEdit.getAddress()); | ||
Set<Tag> updatedTags = editClientDescriptor.getTags().orElse(clientToEdit.getTags()); | ||
|
||
return new Client(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EditClientCommand)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
EditClientCommand e = (EditClientCommand) other; | ||
return index.equals(e.index) | ||
&& editClientDescriptor.equals(e.editClientDescriptor); | ||
} | ||
|
||
/** | ||
* Stores the details to edit the client with. Each non-empty field value will replace the | ||
* corresponding field value of the client. | ||
*/ | ||
public static class EditClientDescriptor { | ||
private Name name; | ||
private Phone phone; | ||
private Email email; | ||
private Address address; | ||
private Set<Tag> tags; | ||
|
||
public EditClientDescriptor() {} | ||
|
||
/** | ||
* Copy constructor. | ||
* A defensive copy of {@code tags} is used internally. | ||
*/ | ||
public EditClientDescriptor(EditClientDescriptor toCopy) { | ||
setName(toCopy.name); | ||
setPhone(toCopy.phone); | ||
setEmail(toCopy.email); | ||
setAddress(toCopy.address); | ||
setTags(toCopy.tags); | ||
} | ||
|
||
/** | ||
* Returns true if at least one field is edited. | ||
*/ | ||
public boolean isAnyFieldEdited() { | ||
return CollectionUtil.isAnyNonNull(name, phone, email, address, tags); | ||
} | ||
|
||
public void setName(Name name) { | ||
this.name = name; | ||
} | ||
|
||
public Optional<Name> getName() { | ||
return Optional.ofNullable(name); | ||
} | ||
|
||
public void setPhone(Phone phone) { | ||
this.phone = phone; | ||
} | ||
|
||
public Optional<Phone> getPhone() { | ||
return Optional.ofNullable(phone); | ||
} | ||
|
||
public void setEmail(Email email) { | ||
this.email = email; | ||
} | ||
|
||
public Optional<Email> getEmail() { | ||
return Optional.ofNullable(email); | ||
} | ||
|
||
public void setAddress(Address address) { | ||
this.address = address; | ||
} | ||
|
||
public Optional<Address> getAddress() { | ||
return Optional.ofNullable(address); | ||
} | ||
|
||
/** | ||
* Sets {@code tags} to this object's {@code tags}. | ||
* A defensive copy of {@code tags} is used internally. | ||
*/ | ||
public void setTags(Set<Tag> tags) { | ||
this.tags = (tags != null) ? new HashSet<>(tags) : null; | ||
} | ||
|
||
/** | ||
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException} | ||
* if modification is attempted. | ||
* Returns {@code Optional#empty()} if {@code tags} is null. | ||
*/ | ||
public Optional<Set<Tag>> getTags() { | ||
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
// short circuit if same object | ||
if (other == this) { | ||
return true; | ||
} | ||
|
||
// instanceof handles nulls | ||
if (!(other instanceof EditClientDescriptor)) { | ||
return false; | ||
} | ||
|
||
// state check | ||
EditClientDescriptor e = (EditClientDescriptor) other; | ||
|
||
return getName().equals(e.getName()) | ||
&& getPhone().equals(e.getPhone()) | ||
&& getEmail().equals(e.getEmail()) | ||
&& getAddress().equals(e.getAddress()) | ||
&& getTags().equals(e.getTags()); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.