Skip to content

Commit

Permalink
Merge pull request #80 from Gavzzz/v1.3
Browse files Browse the repository at this point in the history
Fix bugs for sorting offerlist, listinglist and clientlist and add commands for client
  • Loading branch information
jeromehjj authored Oct 21, 2022
2 parents 0e6259c + b997440 commit 26f91a9
Show file tree
Hide file tree
Showing 31 changed files with 1,164 additions and 48 deletions.
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = "The person index provided is invalid";
public static final String MESSAGE_INVALID_CLIENT_DISPLAYED_INDEX = "The client index provided is invalid";
public static final String MESSAGE_INVALID_LISTING_ID = "The listing ID provided is invalid";
public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!";
public static final String MESSAGE_CLIENTS_LISTED_OVERVIEW = "%1$d clients listed!";

}
4 changes: 4 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.offer.Offer;
import seedu.address.model.person.Client;
import seedu.address.model.person.Person;

/**
Expand All @@ -34,6 +35,9 @@ public interface Logic {
/** Returns an unmodifiable view of the filtered list of persons */
ObservableList<Person> getFilteredPersonList();

/** Returns an unmodifiable view of the filtered list of clients */
ObservableList<Client> getFilteredClientList();

/** Returns an unmodifiable view of the filtered list of offers*/
ObservableList<Offer> getFilteredOfferList();

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.offer.Offer;
import seedu.address.model.person.Client;
import seedu.address.model.person.Person;
import seedu.address.storage.Storage;

Expand Down Expand Up @@ -65,6 +66,11 @@ public ObservableList<Person> getFilteredPersonList() {
return model.getFilteredPersonList();
}

@Override
public ObservableList<Client> getFilteredClientList() {
return model.getFilteredClientList();
}

@Override
public ObservableList<Offer> getFilteredOfferList() {
return model.getFilteredOfferList();
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/seedu/address/logic/commands/AddClientCommand.java
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));
}
}

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 src/main/java/seedu/address/logic/commands/EditClientCommand.java
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());
}
}
}

Loading

0 comments on commit 26f91a9

Please sign in to comment.