From 7de1b06a01d721415707d0b291209c2a780ee86e Mon Sep 17 00:00:00 2001 From: lrongyi Date: Thu, 7 Nov 2024 17:31:50 +0800 Subject: [PATCH 1/5] Revert back to index for listing commands (WIP) --- .../java/seedu/address/logic/Messages.java | 6 +- .../AddBuyersToListingCommand.java | 60 ++++++++-------- .../listingcommands/DeleteListingCommand.java | 32 +++++---- .../listingcommands/EditListingCommand.java | 70 ++++++++++--------- .../RemoveBuyersFromListingCommand.java | 67 ++++++++++-------- .../AddBuyersToListingCommandParser.java | 32 +++++---- .../DeleteListingCommandParser.java | 5 +- .../EditListingCommandParser.java | 11 +-- .../RemoveBuyersFromListingCommandParser.java | 24 +++++-- 9 files changed, 173 insertions(+), 134 deletions(-) diff --git a/src/main/java/seedu/address/logic/Messages.java b/src/main/java/seedu/address/logic/Messages.java index b084831f748..dbf54c13260 100644 --- a/src/main/java/seedu/address/logic/Messages.java +++ b/src/main/java/seedu/address/logic/Messages.java @@ -15,9 +15,11 @@ public class Messages { public static final String MESSAGE_NOT_NUMBER = "Please enter a number for the Price and Area"; 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_COMMAND_FORMAT = "Invalid command format!\n%1$s"; public static final String MESSAGE_INVALID_PERSON_DISPLAYED_INDEX = - "The person index provided is invalid"; + "The person index provided is invalid!"; + public static final String MESSAGE_INVALID_LISTING_DISPLAYED_INDEX = + "The listing index provided is invalid!"; public static final String MESSAGE_PERSONS_LISTED_OVERVIEW = "%1$d persons listed!"; public static final String MESSAGE_LISTINGS_LISTED_OVERVIEW = "%1$d listings listed!"; diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommand.java index 4cb3a5506b9..a1bef162cfe 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommand.java @@ -3,8 +3,11 @@ import static java.util.Objects.requireNonNull; import java.util.HashSet; +import java.util.List; import java.util.Set; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; @@ -13,6 +16,7 @@ import seedu.address.model.person.Buyer; import seedu.address.model.person.Name; import seedu.address.model.person.Person; +import seedu.address.model.person.Role; /** * Adds buyers to an existing listing in the system. @@ -21,29 +25,29 @@ public class AddBuyersToListingCommand extends Command { public static final String COMMAND_WORD = "addlistingbuyers"; - public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds buyers to the listing identified by its name. " - + "Parameters: LISTING_NAME buyer/BUYER_NAME [buyer/MORE_BUYER_NAMES]...\n" - + "Example: " + COMMAND_WORD + " Warton House buyer/Alice buyer/Bob"; + public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds buyers to the listing identified by its index. " + + "Parameters: LISTING_INDEX buyer/BUYER_INDEX [buyer/MORE_BUYER_INDEXES]...\n" + + "Example: " + COMMAND_WORD + " 1 buyer/1 buyer/3"; public static final String MESSAGE_ADD_BUYERS_SUCCESS = "Buyers added to listing: %1$s"; public static final String MESSAGE_LISTING_NOT_FOUND = "The specified listing name does not exist."; - public static final String MESSAGE_DUPLICATE_BUYERS = "Some buyers are already associated with this listing."; + public static final String MESSAGE_DUPLICATE_BUYERS = "%1$s is already associated with this listing."; public static final String MESSAGE_BUYER_NOT_FOUND = "The specified buyer %1$s does not exist in the client list."; public static final String MESSAGE_PERSON_NOT_BUYER = "The specified person %1$s is not a buyer."; - private final Name listingName; - private final Set buyersToAdd; + private final Index index; + private final Set buyersToAdd; /** * Constructs an {@code AddBuyersToListingCommand}. * - * @param listingName The name of the listing to which buyers will be added. - * @param buyersToAdd The names of the buyers to add to the listing. + * @param index The index of the listing to which buyers will be added. + * @param buyersToAdd The indexes of the buyers to add to the listing. */ - public AddBuyersToListingCommand(Name listingName, Set buyersToAdd) { - requireNonNull(listingName); + public AddBuyersToListingCommand(Index index, Set buyersToAdd) { + requireNonNull(index); requireNonNull(buyersToAdd); - this.listingName = listingName; + this.index = index; this.buyersToAdd = new HashSet<>(buyersToAdd); } @@ -51,36 +55,36 @@ public AddBuyersToListingCommand(Name listingName, Set buyersToAdd) { public CommandResult execute(Model model) throws CommandException { requireNonNull(model); - if (!model.hasListingOfName(listingName)) { - throw new CommandException(MESSAGE_LISTING_NOT_FOUND); + int zeroBased = index.getZeroBased(); + List lastShownListingList = model.getFilteredListingList(); + if (zeroBased >= lastShownListingList.size() || zeroBased < 0) { + throw new CommandException(Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } - Listing listingToEdit = model.getListingByName(listingName); + Listing listingToEdit = model.getFilteredListingList().get(zeroBased); Set existingBuyers = new HashSet<>(listingToEdit.getBuyers()); Set newBuyers = new HashSet<>(); + List lastShownPersonList = model.getFilteredPersonList(); - for (Name buyerName : buyersToAdd) { - - if (!model.hasPersonOfName(buyerName)) { - throw new CommandException(String.format(MESSAGE_BUYER_NOT_FOUND, buyerName)); + for (Index buyerIndex : buyersToAdd) { + int zeroBasedBuyer = buyerIndex.getZeroBased(); + if (zeroBasedBuyer >= lastShownPersonList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } - Person buyer = model.getPersonByName(buyerName); + Person buyerToAdd = lastShownPersonList.get(zeroBasedBuyer); // Check if the person is actually an instance of Buyer - if (!(buyer instanceof Buyer)) { - throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, buyerName)); + if (!buyerToAdd.getRole().equals(Role.BUYER)) { + throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, buyerToAdd.getName())); } // Add the buyer to newBuyers set only if not already in existingBuyers - if (!existingBuyers.contains(buyer)) { - newBuyers.add(buyer); + if (!existingBuyers.contains(buyerToAdd)) { + newBuyers.add(buyerToAdd); } - } - if (newBuyers.isEmpty()) { - throw new CommandException(MESSAGE_DUPLICATE_BUYERS); } existingBuyers.addAll(newBuyers); @@ -96,7 +100,7 @@ public CommandResult execute(Model model) throws CommandException { ); model.setListing(listingToEdit, updatedListing); - return new CommandResult(String.format(MESSAGE_ADD_BUYERS_SUCCESS, listingName)); + return new CommandResult(String.format(MESSAGE_ADD_BUYERS_SUCCESS, Messages.format(listingToEdit))); } @Override @@ -110,7 +114,7 @@ public boolean equals(Object other) { } AddBuyersToListingCommand otherCommand = (AddBuyersToListingCommand) other; - return listingName.equals(otherCommand.listingName) + return index.equals(otherCommand.index) && buyersToAdd.equals(otherCommand.buyersToAdd); } } diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/DeleteListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/DeleteListingCommand.java index abcd5d1f1e1..5ae607771af 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/DeleteListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/DeleteListingCommand.java @@ -4,12 +4,13 @@ import java.util.List; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; import seedu.address.model.listing.Listing; -import seedu.address.model.person.Name; /** * Deletes a listing identified by its name. @@ -19,17 +20,17 @@ public class DeleteListingCommand extends Command { public static final String COMMAND_WORD = "deletelisting"; public static final String MESSAGE_USAGE = COMMAND_WORD - + ": Deletes the listing specified by its name.\n" - + "Parameters: LISTING_NAME\n" - + "Example: " + COMMAND_WORD + " Warton House"; + + ": Deletes the listing specified by its index.\n" + + "Parameters: LISTING_INDEX\n" + + "Example: " + COMMAND_WORD + " 1"; public static final String MESSAGE_DELETE_LISTING_SUCCESS = "Successfully deleted listing: %1$s"; public static final String MESSAGE_LISTING_NOT_FOUND = "This listing does not exist in EZSTATE"; - private final Name targetName; + private final Index targetIndex; - public DeleteListingCommand(Name targetName) { - this.targetName = targetName; + public DeleteListingCommand(Index targetIndex) { + this.targetIndex = targetIndex; } @Override @@ -37,14 +38,15 @@ public CommandResult execute(Model model) throws CommandException { requireNonNull(model); List lastShownList = model.getFilteredListingList(); - Listing listingToDelete = model.getListingByName(targetName); - - if (lastShownList.contains(listingToDelete)) { - model.deleteListing(listingToDelete); - return new CommandResult(String.format(MESSAGE_DELETE_LISTING_SUCCESS, listingToDelete.getName())); - } else { - throw new CommandException(MESSAGE_LISTING_NOT_FOUND); + int zeroBased = targetIndex.getZeroBased(); + if (zeroBased >= lastShownList.size() || zeroBased < 0) { + throw new CommandException(Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } + + Listing listingToDelete = lastShownList.get(targetIndex.getZeroBased()); + + model.deleteListing(listingToDelete); + return new CommandResult(String.format(MESSAGE_DELETE_LISTING_SUCCESS, Messages.format(listingToDelete))); } @Override @@ -58,6 +60,6 @@ public boolean equals(Object other) { } DeleteListingCommand otherCommand = (DeleteListingCommand) other; - return targetName.equals(otherCommand.targetName); + return targetIndex.equals(otherCommand.targetIndex); } } diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/EditListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/EditListingCommand.java index 4d4587b1fb6..dde5841f5bc 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/EditListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/EditListingCommand.java @@ -12,6 +12,7 @@ import java.util.Objects; import java.util.Optional; +import seedu.address.commons.core.index.Index; import seedu.address.commons.util.CollectionUtil; import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; @@ -36,15 +37,15 @@ public class EditListingCommand extends Command { public static final String COMMAND_WORD = "editlisting"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the listing identified " - + "by the listing name. Buyers cannot be edited using this command. " + + "by the listing index. Buyers cannot be edited using this command. " + "Use addlistingbuyers or removelistingbuyers to manage buyers.\n" - + "Parameters: LISTING_NAME " + + "Parameters: LISTING_INDEX " + "[" + PREFIX_NAME + "NAME] " + "[" + PREFIX_PRICE + "PRICE] " + "[" + PREFIX_AREA + "AREA] " + "[" + PREFIX_ADDRESS + "ADDRESS] " + "[" + PREFIX_REGION + "REGION]...\n" - + "Example: " + COMMAND_WORD + " ListingName " + + "Example: " + COMMAND_WORD + " 2 " + PREFIX_PRICE + "450000 " + PREFIX_AREA + "1200"; @@ -53,45 +54,46 @@ public class EditListingCommand extends Command { public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided."; public static final String MESSAGE_INVALID_LISTING_NAME = "The specified listing name is invalid."; - private final Name listingName; + private final Index listingIndex; private final EditListingDescriptor editListingDescriptor; /** - * @param listingName of the listing in the filtered listing list to edit + * @param listingIndex of the listing in the filtered listing list to edit * @param editListingDescriptor details to edit the listing with */ - public EditListingCommand(Name listingName, EditListingDescriptor editListingDescriptor) { - requireNonNull(listingName); + public EditListingCommand(Index listingIndex, EditListingDescriptor editListingDescriptor) { + requireNonNull(listingIndex); requireNonNull(editListingDescriptor); - this.listingName = listingName; + this.listingIndex = listingIndex; this.editListingDescriptor = new EditListingDescriptor(editListingDescriptor); } @Override public CommandResult execute(Model model) throws CommandException { requireNonNull(model); - List lastShownList = model.getFilteredListingList(); + List lastShownListingList = model.getFilteredListingList(); - Listing listingToEdit = lastShownList.stream() - .filter(listing -> listing.getName().equals(listingName)) - .findFirst() - .orElse(null); - - if (listingToEdit == null) { - throw new CommandException(MESSAGE_INVALID_LISTING_NAME); + int zeroBasedListing = listingIndex.getZeroBased(); + if (zeroBasedListing >= lastShownListingList.size() || zeroBasedListing < 0) { + throw new CommandException(Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } + Listing listingToEdit = lastShownListingList.get(zeroBasedListing); + + Optional sellerIndex = editListingDescriptor.getSellerIndex(); - Optional seller = editListingDescriptor.getSellerName() - .flatMap(name -> Optional.ofNullable(model.getPersonByName(name))); - if (seller.isPresent()) { - if (!(seller.get() instanceof Seller)) { - throw new CommandException("The specified person is not a seller."); + if (sellerIndex.isPresent()) { + List lastShownPersonList = model.getFilteredPersonList(); + int zeroBasedPerson = sellerIndex.get().getZeroBased(); + if (zeroBasedPerson >= lastShownPersonList.size() || zeroBasedPerson < 0) { + throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } - } else if (editListingDescriptor.getSellerName().isPresent()) { - throw new CommandException("Seller not found in the system."); + Person seller = lastShownPersonList.get(zeroBasedPerson); } + + + Listing editedListing = createEditedListing( listingToEdit, editListingDescriptor, @@ -151,14 +153,14 @@ public boolean equals(Object other) { return false; } - return listingName.equals(otherEditCommand.listingName) + return listingIndex.equals(otherEditCommand.listingIndex) && editListingDescriptor.equals(otherEditCommand.editListingDescriptor); } @Override public String toString() { return new ToStringBuilder(this) - .add("listingName", listingName) + .add("listingIndex", listingIndex) .add("editListingDescriptor", editListingDescriptor) .toString(); } @@ -173,7 +175,7 @@ public static class EditListingDescriptor { private Area area; private Address address; private Region region; - private Name sellerName; + private Index sellerIndex; public EditListingDescriptor() {} @@ -190,11 +192,11 @@ public EditListingDescriptor(EditListingDescriptor toCopy) { setArea(toCopy.area); setAddress(toCopy.address); setRegion(toCopy.region); - setSellerName(toCopy.sellerName); + setSellerIndex(toCopy.sellerIndex); } public boolean isAnyFieldEdited() { - return CollectionUtil.isAnyNonNull(name, price, area, address, region, sellerName); + return CollectionUtil.isAnyNonNull(name, price, area, address, region, sellerIndex); } public void setName(Name name) { @@ -236,12 +238,12 @@ public void setRegion(Region region) { public Optional getRegion() { return Optional.ofNullable(region); } - public void setSellerName(Name sellerName) { - this.sellerName = sellerName; + public void setSellerIndex(Index sellerIndex) { + this.sellerIndex = sellerIndex; } - public Optional getSellerName() { - return Optional.ofNullable(sellerName); + public Optional getSellerIndex() { + return Optional.ofNullable(sellerIndex); } @@ -261,7 +263,7 @@ public boolean equals(Object other) { && Objects.equals(area, otherDescriptor.area) && Objects.equals(address, otherDescriptor.address) && Objects.equals(region, otherDescriptor.region) - && Objects.equals(sellerName, otherDescriptor.sellerName); + && Objects.equals(sellerIndex, otherDescriptor.sellerIndex); } @Override @@ -272,7 +274,7 @@ public String toString() { .add("area", area) .add("address", address) .add("region", region) - .add("seller", sellerName) + .add("seller", sellerIndex) .toString(); } } diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java index 6032f87316c..75372908920 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java @@ -3,8 +3,12 @@ import static java.util.Objects.requireNonNull; import java.util.HashSet; +import java.util.List; import java.util.Set; +import java.util.stream.Collectors; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; import seedu.address.logic.commands.Command; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; @@ -13,6 +17,7 @@ import seedu.address.model.person.Buyer; import seedu.address.model.person.Name; import seedu.address.model.person.Person; +import seedu.address.model.person.Role; /** * Removes buyers from an existing listing in the system. @@ -22,11 +27,12 @@ public class RemoveBuyersFromListingCommand extends Command { public static final String COMMAND_WORD = "removelistingbuyers"; public static final String MESSAGE_USAGE = COMMAND_WORD + ": Removes buyers from the listing identified by its " - + "name. " - + "Parameters: LISTING_NAME buyer/BUYER_NAME [buyer/MORE_BUYER_NAMES]...\n" - + "Example: " + COMMAND_WORD + " Warton House buyer/Alice buyer/Bob"; + + "index. " + + "Parameters: LISTING_INDEX buyer/BUYER_INDEX [buyer/MORE_BUYER_INDEXES]...\n" + + "Example: " + COMMAND_WORD + " 1 buyer/1 buyer/3"; - public static final String MESSAGE_REMOVE_BUYERS_SUCCESS = "Buyers removed from listing: %1$s"; + public static final String MESSAGE_REMOVE_BUYERS_SUCCESS = "Buyers removed from listing: %1$s.\n" + + "Removed buyers: %2$s"; public static final String MESSAGE_LISTING_NOT_FOUND = "The specified listing name does not exist."; public static final String MESSAGE_EMPTY_SET = "Please provide valid buyers"; public static final String MESSAGE_PERSON_NOT_BUYER = "The client %1$s is not registered as a buyer."; @@ -34,20 +40,20 @@ public class RemoveBuyersFromListingCommand extends Command { "The specified buyer %1$s is not a buyer of the listing %2$s."; public static final String MESSAGE_BUYER_NOT_FOUND = "The specified buyer %1$s does not exist in the client list."; - private final Name listingName; - private final Set buyersToRemove; + private final Index index; + private final Set buyersToRemove; /** * Constructs a {@code RemoveBuyersFromListingCommand}. * - * @param listingName The name of the listing from which buyers will be removed. - * @param buyersToRemove The names of the buyers to remove from the listing. + * @param index The index of the listing from which buyers will be removed. + * @param buyersToRemove The indexes of the buyers to remove from the listing. */ - public RemoveBuyersFromListingCommand(Name listingName, Set buyersToRemove) { - requireNonNull(listingName); + public RemoveBuyersFromListingCommand(Index index, Set buyersToRemove) { + requireNonNull(index); requireNonNull(buyersToRemove); - this.listingName = listingName; + this.index = index; this.buyersToRemove = new HashSet<>(buyersToRemove); } @@ -55,34 +61,37 @@ public RemoveBuyersFromListingCommand(Name listingName, Set buyersToRemove public CommandResult execute(Model model) throws CommandException { requireNonNull(model); - - if (!model.hasListingOfName(listingName)) { - throw new CommandException(MESSAGE_LISTING_NOT_FOUND); + int zeroBased = index.getZeroBased(); + List lastShownListingList = model.getFilteredListingList(); + if (zeroBased >= lastShownListingList.size() || zeroBased < 0) { + throw new CommandException(Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } - Listing listingToEdit = model.getListingByName(listingName); + + Listing listingToEdit = model.getFilteredListingList().get(zeroBased); Set existingBuyers = new HashSet<>(listingToEdit.getBuyers()); Set buyersToRemoveSet = new HashSet<>(); + List lastShownPersonList = model.getFilteredPersonList(); - for (Name buyerName : buyersToRemove) { - - - if (!model.hasPersonOfName(buyerName)) { - throw new CommandException(String.format(MESSAGE_BUYER_NOT_FOUND, buyerName)); + for (Index buyerIndex : buyersToRemove) { + int zeroBasedBuyer = buyerIndex.getZeroBased(); + if (zeroBasedBuyer >= lastShownPersonList.size()) { + throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } - Person buyer = model.getPersonByName(buyerName); + Person buyerToRemove = lastShownPersonList.get(zeroBasedBuyer); // Check if the person is actually an instance of Buyer - if (!(buyer instanceof Buyer)) { - throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, buyer.getName())); + if (!buyerToRemove.getRole().equals(Role.BUYER)) { + throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, buyerToRemove.getName())); } - if (!existingBuyers.contains(buyer)) { - throw new CommandException(String.format(MESSAGE_NOT_BUYER_FOR_LISTING, buyer.getName(), listingName)); + if (!existingBuyers.contains(buyerToRemove)) { + throw new CommandException(String.format(MESSAGE_NOT_BUYER_FOR_LISTING, + buyerToRemove.getName(), listingToEdit)); } - buyersToRemoveSet.add(buyer); + buyersToRemoveSet.add(buyerToRemove); } if (buyersToRemoveSet.isEmpty()) { @@ -103,7 +112,9 @@ public CommandResult execute(Model model) throws CommandException { ); model.setListing(listingToEdit, updatedListing); - return new CommandResult(String.format(MESSAGE_REMOVE_BUYERS_SUCCESS, listingName)); + String removedNames = buyersToRemoveSet.stream() + .map(buyer -> buyer.getName().toString()).collect(Collectors.joining(", ")); + return new CommandResult(String.format(MESSAGE_REMOVE_BUYERS_SUCCESS, listingToEdit.getName(), removedNames)); } @Override @@ -117,7 +128,7 @@ public boolean equals(Object other) { } RemoveBuyersFromListingCommand otherCommand = (RemoveBuyersFromListingCommand) other; - return listingName.equals(otherCommand.listingName) + return index.equals(otherCommand.index) && buyersToRemove.equals(otherCommand.buyersToRemove); } } diff --git a/src/main/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParser.java b/src/main/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParser.java index 4f013314ea0..f7b70014b33 100644 --- a/src/main/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParser.java @@ -6,6 +6,9 @@ import java.util.HashSet; import java.util.Set; +import seedu.address.commons.core.index.Index; +import seedu.address.commons.util.StringUtil; +import seedu.address.logic.Messages; import seedu.address.logic.commands.listingcommands.AddBuyersToListingCommand; import seedu.address.logic.parser.ArgumentMultimap; import seedu.address.logic.parser.ArgumentTokenizer; @@ -23,26 +26,29 @@ public class AddBuyersToListingCommandParser implements Parser buyerIndexes = new HashSet<>(); - // Parse buyer names with the "buyer/" prefix - Set buyerNames = new HashSet<>(); - for (String buyerNameStr : argMultimap.getAllValues(PREFIX_BUYER)) { - buyerNames.add(ParserUtil.parseName(buyerNameStr)); + for (String buyerIndexStr : argMultimap.getAllValues(PREFIX_BUYER)) { + if (!StringUtil.isNonZeroUnsignedInteger(buyerIndexStr.trim())) { + throw new ParseException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + buyerIndexes.add(ParserUtil.parseIndex(buyerIndexStr)); } - // Check if there are buyer names; otherwise, throw an error - if (buyerNames.isEmpty()) { + // Check if there are buyer indexes; otherwise, throw an error + if (buyerIndexes.isEmpty()) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddBuyersToListingCommand.MESSAGE_USAGE)); } - return new AddBuyersToListingCommand(listingName, buyerNames); + return new AddBuyersToListingCommand(index, buyerIndexes); } } diff --git a/src/main/java/seedu/address/logic/parser/listingcommandparsers/DeleteListingCommandParser.java b/src/main/java/seedu/address/logic/parser/listingcommandparsers/DeleteListingCommandParser.java index 79ec27c342e..8c336fc13df 100644 --- a/src/main/java/seedu/address/logic/parser/listingcommandparsers/DeleteListingCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/listingcommandparsers/DeleteListingCommandParser.java @@ -2,6 +2,7 @@ import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; +import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.listingcommands.DeleteListingCommand; import seedu.address.logic.parser.ArgumentMultimap; import seedu.address.logic.parser.ArgumentTokenizer; @@ -26,8 +27,8 @@ public class DeleteListingCommandParser implements Parser public DeleteListingCommand parse(String args) throws ParseException { ArgumentMultimap argumentMultimap = ArgumentTokenizer.tokenize(args); try { - Name name = ParserUtil.parseName(argumentMultimap.getPreamble()); - return new DeleteListingCommand(name); + Index index = ParserUtil.parseIndex(args); + return new DeleteListingCommand(index); } catch (ParseException pe) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteListingCommand.MESSAGE_USAGE), pe); diff --git a/src/main/java/seedu/address/logic/parser/listingcommandparsers/EditListingCommandParser.java b/src/main/java/seedu/address/logic/parser/listingcommandparsers/EditListingCommandParser.java index 50cf2097881..c00ba062fa5 100644 --- a/src/main/java/seedu/address/logic/parser/listingcommandparsers/EditListingCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/listingcommandparsers/EditListingCommandParser.java @@ -9,6 +9,7 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_REGION; import static seedu.address.logic.parser.CliSyntax.PREFIX_SELLER; +import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.listingcommands.EditListingCommand; import seedu.address.logic.commands.listingcommands.EditListingCommand.EditListingDescriptor; import seedu.address.logic.parser.ArgumentMultimap; @@ -36,10 +37,10 @@ public EditListingCommand parse(String args) throws ParseException { argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PRICE, PREFIX_AREA, PREFIX_ADDRESS, PREFIX_REGION, PREFIX_SELLER); - Name currentListingName; + Index currentListingIndex; try { - currentListingName = ParserUtil.parseName(argMultimap.getPreamble()); + currentListingIndex = ParserUtil.parseIndex(argMultimap.getPreamble()); } catch (ParseException pe) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditListingCommand.MESSAGE_USAGE), pe); @@ -62,14 +63,14 @@ public EditListingCommand parse(String args) throws ParseException { editListingDescriptor.setRegion(ParserUtil.parseRegion(argMultimap.getValue(PREFIX_REGION).get())); } if (argMultimap.getValue(PREFIX_SELLER).isPresent()) { - Name sellerName = ParserUtil.parseName(argMultimap.getValue(PREFIX_SELLER).get()); - editListingDescriptor.setSellerName(sellerName); + Index sellerIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_SELLER).get()); + editListingDescriptor.setSellerIndex(sellerIndex); } if (!editListingDescriptor.isAnyFieldEdited()) { throw new ParseException(EditListingCommand.MESSAGE_NOT_EDITED); } - return new EditListingCommand(currentListingName, editListingDescriptor); + return new EditListingCommand(currentListingIndex, editListingDescriptor); } } diff --git a/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java b/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java index 236f03173f7..3dfacbfe6b3 100644 --- a/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java @@ -6,6 +6,9 @@ import java.util.HashSet; import java.util.Set; +import seedu.address.commons.core.index.Index; +import seedu.address.commons.util.StringUtil; +import seedu.address.logic.Messages; import seedu.address.logic.commands.listingcommands.RemoveBuyersFromListingCommand; import seedu.address.logic.parser.ArgumentMultimap; import seedu.address.logic.parser.ArgumentTokenizer; @@ -28,7 +31,7 @@ public class RemoveBuyersFromListingCommandParser implements Parser buyerNames = new HashSet<>(); - for (String buyerName : argMultimap.getAllValues(PREFIX_BUYER)) { - buyerNames.add(ParserUtil.parseName(buyerName)); + // Parse index + String indexOneBased = argMultimap.getPreamble().trim(); + if (!StringUtil.isNonZeroUnsignedInteger(indexOneBased)) { + throw new ParseException(Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); + } + Index index = ParserUtil.parseIndex(indexOneBased); + + // Parse buyer indexes + Set buyerIndexes = new HashSet<>(); + for (String buyerIndex : argMultimap.getAllValues(PREFIX_BUYER)) { + buyerIndexes.add(ParserUtil.parseIndex(buyerIndex)); } // Check if at least one buyer is specified - if (buyerNames.isEmpty()) { + if (buyerIndexes.isEmpty()) { throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemoveBuyersFromListingCommand.MESSAGE_USAGE)); } - return new RemoveBuyersFromListingCommand(listingName, buyerNames); + return new RemoveBuyersFromListingCommand(index, buyerIndexes); } } From 7fa0ae125f7799d65c5e7829d893e2368bb9268c Mon Sep 17 00:00:00 2001 From: lrongyi Date: Thu, 7 Nov 2024 19:57:27 +0800 Subject: [PATCH 2/5] Revert listing commands back to Index (TODO) Complete tests --- .../listingcommands/AddListingCommand.java | 77 +++++++++++-------- .../listingcommands/EditListingCommand.java | 13 +--- .../RemoveBuyersFromListingCommand.java | 5 +- .../AddListingCommandParser.java | 11 ++- .../AddBuyersToListingCommandTest.java | 38 +++++---- .../address/testutil/TypicalIndexes.java | 10 +++ 6 files changed, 94 insertions(+), 60 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/AddListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/AddListingCommand.java index 4332f82e9be..84eb6bc9807 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/AddListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/AddListingCommand.java @@ -1,6 +1,5 @@ package seedu.address.logic.commands.listingcommands; -import static java.util.Objects.isNull; import static java.util.Objects.requireNonNull; import static seedu.address.commons.util.CollectionUtil.requireAllNonNull; import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; @@ -15,6 +14,7 @@ import java.util.List; import java.util.Set; +import seedu.address.commons.core.index.Index; import seedu.address.commons.util.ToStringBuilder; import seedu.address.logic.Messages; import seedu.address.logic.commands.Command; @@ -29,6 +29,7 @@ import seedu.address.model.person.Buyer; import seedu.address.model.person.Name; import seedu.address.model.person.Person; +import seedu.address.model.person.Role; import seedu.address.model.person.Seller; /** @@ -45,74 +46,84 @@ public class AddListingCommand extends Command { + PREFIX_AREA + "AREA " + PREFIX_ADDRESS + "ADDRESS " + PREFIX_REGION + "REGION " - + PREFIX_SELLER + "SELLER " - + "[" + PREFIX_BUYER + "BUYER]...\n" + + PREFIX_SELLER + "SELLER INDEX " + + "[" + PREFIX_BUYER + "BUYER INDEX]...\n" + "Example: " + COMMAND_WORD + " " + PREFIX_NAME + "Warton House " + PREFIX_PRICE + "400000 " + PREFIX_AREA + "1000 " + PREFIX_ADDRESS + "123 PASIR RIS (S)123456 " + PREFIX_REGION + "east " - + PREFIX_SELLER + "Sean Dias " - + PREFIX_BUYER + "Rong yi " - + PREFIX_BUYER + "Wen Xuan "; + + PREFIX_SELLER + "2 " + + PREFIX_BUYER + "1 " + + PREFIX_BUYER + "3 "; public static final String MESSAGE_SUCCESS = "New listing added: %1$s"; - public static final String MESSAGE_NOT_SELLER = "The seller specified is not a seller"; - public static final String MESSAGE_NOT_BUYER = "The buyer(s) specified is not a buyer"; - public static final String MESSAGE_DUPLICATE_LISTING = "This listing already exists in EZSTATE"; + public static final String MESSAGE_NOT_SELLER = "The seller index specified is not a seller:\n" + + "%d. %s"; + public static final String MESSAGE_NOT_BUYER = "The buyer index specified is not a buyer:\n" + + "%d. %s"; + public static final String MESSAGE_INVALID_SELLER_INDEX = "The seller index provided is invalid!"; + public static final String MESSAGE_INVALID_BUYER_INDEX = "The buyer index (%d) provided is invalid!"; + public static final String MESSAGE_DUPLICATE_LISTING = "This listing already exists in EZSTATE!"; private final Name listingName; private final Price price; private final Area area; private final Address address; private final Region region; - private final Name seller; + private final Index sellerIndex; - private final Set buyers; + private final Set buyerIndexes; /** * Creates an AddListingCommand to add the specified {@code Listing} */ public AddListingCommand(Name listingName, Price price, Area area, Address address, Region region, - Name seller, Set buyers) { - requireAllNonNull(listingName, price, area, address, region, seller); + Index sellerIndex, Set buyerIndexes) { + requireAllNonNull(listingName, price, area, address, region, sellerIndex); this.listingName = listingName; this.price = price; this.area = area; this.address = address; this.region = region; - this.seller = seller; - this.buyers = buyers; + this.sellerIndex = sellerIndex; + this.buyerIndexes = buyerIndexes; } - // needs SLAP @Override public CommandResult execute(Model model) throws CommandException { requireNonNull(model); List lastShownList = model.getFilteredPersonList(); - - Person seller = model.getPersonByName(this.seller); - - if (!lastShownList.contains(seller)) { - throw new CommandException(Messages.MESSAGE_INVALID_PERSON_INPUT); + int zeroBasedSeller = sellerIndex.getZeroBased(); + int oneBasedSeller = sellerIndex.getOneBased(); + if (zeroBasedSeller >= lastShownList.size() || zeroBasedSeller < 0) { + throw new CommandException(MESSAGE_INVALID_SELLER_INDEX); } - if (!(seller instanceof Seller)) { - throw new CommandException(MESSAGE_NOT_SELLER); + Person seller = lastShownList.get(zeroBasedSeller); + + if (!seller.getRole().equals(Role.SELLER)) { + throw new CommandException(String.format(MESSAGE_NOT_SELLER, + oneBasedSeller, seller.getName())); } Set personBuyers = new HashSet<>(); - if (!isNull(buyers)) { - for (Name b : buyers) { - Person buyer = model.getPersonByName(b); - if (!lastShownList.contains(buyer)) { - throw new CommandException(Messages.MESSAGE_INVALID_PERSON_INPUT); + if (!buyerIndexes.isEmpty()) { + for (Index buyerIndex : buyerIndexes) { + int zeroBasedBuyer = buyerIndex.getZeroBased(); + int oneBasedBuyer = buyerIndex.getOneBased(); + if (zeroBasedBuyer >= lastShownList.size() || zeroBasedBuyer < 0) { + throw new CommandException(String.format(MESSAGE_INVALID_BUYER_INDEX,oneBasedBuyer)); } - if (!(buyer instanceof Buyer)) { - throw new CommandException(MESSAGE_NOT_BUYER); + + Person buyer = lastShownList.get(zeroBasedBuyer); + + if (!buyer.getRole().equals(Role.BUYER)) { + throw new CommandException(String.format(MESSAGE_NOT_BUYER, + oneBasedBuyer, buyer.getName())); } personBuyers.add(buyer); @@ -146,8 +157,8 @@ public boolean equals(Object other) { && area.equals(otherCommand.area) && address.equals(otherCommand.address) && region.equals(otherCommand.region) - && seller.equals(otherCommand.seller) - && buyers.equals(otherCommand.buyers); + && sellerIndex.equals(otherCommand.sellerIndex) + && buyerIndexes.equals(otherCommand.buyerIndexes); } @Override @@ -155,7 +166,7 @@ public String toString() { return new ToStringBuilder(this) .add("toAdd", this.listingName) .add("address", this.address) - .add("seller", this.seller) + .add("seller", this.sellerIndex) .toString(); } } diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/EditListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/EditListingCommand.java index dde5841f5bc..601695a9800 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/EditListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/EditListingCommand.java @@ -82,6 +82,7 @@ public CommandResult execute(Model model) throws CommandException { Optional sellerIndex = editListingDescriptor.getSellerIndex(); + Listing editedListing; if (sellerIndex.isPresent()) { List lastShownPersonList = model.getFilteredPersonList(); int zeroBasedPerson = sellerIndex.get().getZeroBased(); @@ -89,17 +90,11 @@ public CommandResult execute(Model model) throws CommandException { throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } Person seller = lastShownPersonList.get(zeroBasedPerson); + editedListing = createEditedListing(listingToEdit, editListingDescriptor, seller); + } else { + editedListing = createEditedListing(listingToEdit, editListingDescriptor, listingToEdit.getSeller()); } - - - - Listing editedListing = createEditedListing( - listingToEdit, - editListingDescriptor, - seller.orElse(listingToEdit.getSeller()) - ); - if (isIdentifierChanged(listingToEdit, editedListing) && model.canEditListing(listingToEdit, editedListing)) { throw new CommandException(MESSAGE_DUPLICATE_LISTING); } diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java index 75372908920..8409e6c2304 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java @@ -35,7 +35,7 @@ public class RemoveBuyersFromListingCommand extends Command { + "Removed buyers: %2$s"; public static final String MESSAGE_LISTING_NOT_FOUND = "The specified listing name does not exist."; public static final String MESSAGE_EMPTY_SET = "Please provide valid buyers"; - public static final String MESSAGE_PERSON_NOT_BUYER = "The client %1$s is not registered as a buyer."; + public static final String MESSAGE_PERSON_NOT_BUYER = "The buyer specified is not a buyer: %d"; public static final String MESSAGE_NOT_BUYER_FOR_LISTING = "The specified buyer %1$s is not a buyer of the listing %2$s."; public static final String MESSAGE_BUYER_NOT_FOUND = "The specified buyer %1$s does not exist in the client list."; @@ -75,6 +75,7 @@ public CommandResult execute(Model model) throws CommandException { for (Index buyerIndex : buyersToRemove) { int zeroBasedBuyer = buyerIndex.getZeroBased(); + int oneBasedBuyer = buyerIndex.getOneBased(); if (zeroBasedBuyer >= lastShownPersonList.size()) { throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } @@ -83,7 +84,7 @@ public CommandResult execute(Model model) throws CommandException { // Check if the person is actually an instance of Buyer if (!buyerToRemove.getRole().equals(Role.BUYER)) { - throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, buyerToRemove.getName())); + throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, oneBasedBuyer)); } if (!existingBuyers.contains(buyerToRemove)) { diff --git a/src/main/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParser.java b/src/main/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParser.java index 8c676d203a5..eb3d33f6b41 100644 --- a/src/main/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParser.java @@ -9,9 +9,11 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_REGION; import static seedu.address.logic.parser.CliSyntax.PREFIX_SELLER; +import java.util.HashSet; import java.util.Set; import java.util.stream.Stream; +import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.listingcommands.AddListingCommand; import seedu.address.logic.parser.ArgumentMultimap; import seedu.address.logic.parser.ArgumentTokenizer; @@ -52,10 +54,13 @@ public AddListingCommand parse(String args) throws ParseException { Area area = ParserUtil.parseArea(argMultimap.getValue(PREFIX_AREA).get()); Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()); Region region = ParserUtil.parseRegion(argMultimap.getValue(PREFIX_REGION).get()); - Name sellerName = ParserUtil.parseName(argMultimap.getValue(PREFIX_SELLER).get()); - Set buyerNameList = ParserUtil.parseNames(argMultimap.getAllValues(PREFIX_BUYER)); + Index sellerIndex = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_SELLER).get()); + Set buyerIndexSet = new HashSet<>(); + for (String buyerIndex : argMultimap.getAllValues(PREFIX_BUYER)) { + buyerIndexSet.add(ParserUtil.parseIndex(buyerIndex)); + } - return new AddListingCommand(name, price, area, address, region, sellerName, buyerNameList); + return new AddListingCommand(name, price, area, address, region, sellerIndex, buyerIndexSet); } /** diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommandTest.java index c959e7389bc..daab5da595a 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommandTest.java @@ -5,6 +5,13 @@ import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIFTH_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_FOURTH_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_PERSON; +import static seedu.address.testutil.TypicalIndexes.LISTING_INDEX_OUT_OF_BOUNDS; +import static seedu.address.testutil.TypicalIndexes.PERSON_INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalListings.PASIR_RIS; import static seedu.address.testutil.TypicalListings.SIMEI; import static seedu.address.testutil.TypicalListings.TAMPINES; @@ -20,6 +27,7 @@ import org.junit.jupiter.api.Test; +import seedu.address.commons.core.index.Index; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; @@ -31,56 +39,60 @@ public class AddBuyersToListingCommandTest { + private static final Index VALID_LISTING_INDEX = INDEX_FIRST_LISTING; private static final Listing VALID_LISTING = PASIR_RIS; - private static final Name VALID_LISTING_NAME = VALID_LISTING.getName(); + private static final Name VALID_LISTING_NAME = PASIR_RIS.getName(); private static final Name OTHER_LISTING_NAME = TAMPINES.getName(); private static final Name INVALID_LISTING_NAME = SIMEI.getName(); private static final Name SELLER_NAME = ALICE.getName(); private static final Set SELLER = Set.of(SELLER_NAME); - private static final Name VALID_BUYER_NAME = ELLE.getName(); private static final Set EXISTING_BUYERS = Set.of(GEORGE.getName()); - private static final Set VALID_BUYER_NAMES = Set.of(VALID_BUYER_NAME); - private static final Name INVALID_BUYER_NAME = HOON.getName(); - private static final Set INVALID_BUYERS = Set.of(INVALID_BUYER_NAME); + private static final Set VALID_BUYER_INDEXES = Set.of(INDEX_FOURTH_PERSON, INDEX_FIFTH_PERSON); + private static final Set INVALID_BUYER_INDEXES = Set.of(INDEX_FIRST_PERSON); + private static final Set BUYER_INDEXES_OUT_OF_BOUNDS = Set.of(PERSON_INDEX_OUT_OF_BOUNDS); private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); @Test public void constructor_nullPerson_throwsNullPointerException() { - assertThrows(NullPointerException.class, () -> new AddBuyersToListingCommand(VALID_LISTING_NAME, + assertThrows(NullPointerException.class, () -> new AddBuyersToListingCommand(INDEX_FIRST_LISTING, null)); } @Test public void constructor_nullListing_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> new AddBuyersToListingCommand(null, - VALID_BUYER_NAMES)); + VALID_BUYER_INDEXES)); } @Test - public void execute_buyerAddedToListing_success() { + public void execute_buyerAddedToListingUnfilteredList_success() { AddBuyersToListingCommand addBuyersToListingCommand = - new AddBuyersToListingCommand(VALID_LISTING_NAME, VALID_BUYER_NAMES); + new AddBuyersToListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); + String expectedMessage = String.format(AddBuyersToListingCommand.MESSAGE_ADD_BUYERS_SUCCESS, VALID_LISTING_NAME); + Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); - Listing editedListing = new ListingBuilder(VALID_LISTING).withBuyers(ELLE, GEORGE, DANIEL).build(); + Listing editedListing = new ListingBuilder(VALID_LISTING).withBuyers().build(); + expectedModel.setListing(VALID_LISTING, editedListing); + assertCommandSuccess(addBuyersToListingCommand, model, expectedMessage, expectedModel); } @Test public void execute_buyerNotInClientList_throwsCommandException() { AddBuyersToListingCommand addBuyersToListingCommand = - new AddBuyersToListingCommand(VALID_LISTING_NAME, INVALID_BUYERS); + new AddBuyersToListingCommand(VALID_LISTING_INDEX, BUYER_INDEXES_OUT_OF_BOUNDS); assertCommandFailure(addBuyersToListingCommand, model, String.format(AddBuyersToListingCommand.MESSAGE_BUYER_NOT_FOUND, INVALID_BUYER_NAME)); } @Test - public void execute_listingNotInListingList_throwsCommandException() { + public void execute_listingIndexOutOfBoundsUnfilteredList_throwsCommandException() { AddBuyersToListingCommand addBuyersToListingCommand = - new AddBuyersToListingCommand(INVALID_LISTING_NAME, VALID_BUYER_NAMES); + new AddBuyersToListingCommand(LISTING_INDEX_OUT_OF_BOUNDS, VALID_BUYER_INDEXES); assertCommandFailure(addBuyersToListingCommand, model, AddBuyersToListingCommand.MESSAGE_LISTING_NOT_FOUND); } diff --git a/src/test/java/seedu/address/testutil/TypicalIndexes.java b/src/test/java/seedu/address/testutil/TypicalIndexes.java index f621aebd0e1..59351c60237 100644 --- a/src/test/java/seedu/address/testutil/TypicalIndexes.java +++ b/src/test/java/seedu/address/testutil/TypicalIndexes.java @@ -9,7 +9,17 @@ public class TypicalIndexes { public static final Index INDEX_FIRST_PERSON = Index.fromOneBased(1); public static final Index INDEX_SECOND_PERSON = Index.fromOneBased(2); public static final Index INDEX_THIRD_PERSON = Index.fromOneBased(3); + public static final Index INDEX_FOURTH_PERSON = Index.fromOneBased(4); + public static final Index INDEX_FIFTH_PERSON = Index.fromOneBased(5); + public static final Index INDEX_SIXTH_PERSON = Index.fromOneBased(6); + public static final Index INDEX_SEVENTH_PERSON = Index.fromOneBased(7); + public static final Index PERSON_INDEX_OUT_OF_BOUNDS = Index.fromOneBased(8); public static final Index INDEX_FIRST_LISTING = Index.fromOneBased(1); public static final Index INDEX_SECOND_LISTING = Index.fromOneBased(2); public static final Index INDEX_THIRD_LISTING = Index.fromOneBased(3); + public static final Index INDEX_FOURTH_LISTING = Index.fromOneBased(4); + public static final Index INDEX_FIFTH_LISTING = Index.fromOneBased(5); + public static final Index INDEX_SIXTH_LISTING = Index.fromOneBased(6); + public static final Index INDEX_SEVENTH_LISTING = Index.fromOneBased(7); + public static final Index LISTING_INDEX_OUT_OF_BOUNDS = Index.fromOneBased(8); } From 0ba52dc94f4733306bcf837d0fdb67ee525ad538 Mon Sep 17 00:00:00 2001 From: Sean Dias Date: Thu, 7 Nov 2024 22:16:38 +0800 Subject: [PATCH 3/5] Change back TypicalIndexes --- .../java/seedu/address/testutil/TypicalIndexes.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/test/java/seedu/address/testutil/TypicalIndexes.java b/src/test/java/seedu/address/testutil/TypicalIndexes.java index 7beac84a2d7..59351c60237 100644 --- a/src/test/java/seedu/address/testutil/TypicalIndexes.java +++ b/src/test/java/seedu/address/testutil/TypicalIndexes.java @@ -9,9 +9,17 @@ public class TypicalIndexes { public static final Index INDEX_FIRST_PERSON = Index.fromOneBased(1); public static final Index INDEX_SECOND_PERSON = Index.fromOneBased(2); public static final Index INDEX_THIRD_PERSON = Index.fromOneBased(3); - public static final Index PERSON_INDEX_OUT_OF_BOUNDS = Index.fromOneBased(4); + public static final Index INDEX_FOURTH_PERSON = Index.fromOneBased(4); + public static final Index INDEX_FIFTH_PERSON = Index.fromOneBased(5); + public static final Index INDEX_SIXTH_PERSON = Index.fromOneBased(6); + public static final Index INDEX_SEVENTH_PERSON = Index.fromOneBased(7); + public static final Index PERSON_INDEX_OUT_OF_BOUNDS = Index.fromOneBased(8); public static final Index INDEX_FIRST_LISTING = Index.fromOneBased(1); public static final Index INDEX_SECOND_LISTING = Index.fromOneBased(2); public static final Index INDEX_THIRD_LISTING = Index.fromOneBased(3); - public static final Index LISTING_INDEX_OUT_OF_BOUNDS = Index.fromOneBased(4); + public static final Index INDEX_FOURTH_LISTING = Index.fromOneBased(4); + public static final Index INDEX_FIFTH_LISTING = Index.fromOneBased(5); + public static final Index INDEX_SIXTH_LISTING = Index.fromOneBased(6); + public static final Index INDEX_SEVENTH_LISTING = Index.fromOneBased(7); + public static final Index LISTING_INDEX_OUT_OF_BOUNDS = Index.fromOneBased(8); } From 763bb0cf2c17218b1bf3e10bb937ac64b899abfc Mon Sep 17 00:00:00 2001 From: lrongyi Date: Thu, 7 Nov 2024 23:10:06 +0800 Subject: [PATCH 4/5] Change tests to account for Index --- .../AddBuyersToListingCommand.java | 7 +- .../RemoveBuyersFromListingCommand.java | 3 +- .../RemoveBuyersFromListingCommandParser.java | 8 -- .../logic/commands/CommandTestUtil.java | 6 +- .../AddBuyersToListingCommandTest.java | 103 ++++++++++------- .../AddListingCommandTest.java | 88 ++++++++------- .../EditListingCommandTest.java | 41 ++++--- .../EditListingDescriptorTest.java | 2 +- .../RemoveBuyersFromListingCommandTest.java | 105 +++++++++++------- .../AddBuyersToListingCommandParserTest.java | 79 ++++++------- .../AddListingCommandParserTest.java | 6 +- .../EditListingCommandParserTest.java | 46 ++++---- ...oveBuyersFromListingCommandParserTest.java | 81 ++++++++------ 13 files changed, 313 insertions(+), 262 deletions(-) diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommand.java index a1bef162cfe..0d64d3469f2 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommand.java @@ -33,7 +33,8 @@ public class AddBuyersToListingCommand extends Command { public static final String MESSAGE_LISTING_NOT_FOUND = "The specified listing name does not exist."; public static final String MESSAGE_DUPLICATE_BUYERS = "%1$s is already associated with this listing."; public static final String MESSAGE_BUYER_NOT_FOUND = "The specified buyer %1$s does not exist in the client list."; - public static final String MESSAGE_PERSON_NOT_BUYER = "The specified person %1$s is not a buyer."; + public static final String MESSAGE_PERSON_NOT_BUYER = "The specified person is not a buyer:\n" + + "%d. %s"; private final Index index; private final Set buyersToAdd; @@ -69,6 +70,7 @@ public CommandResult execute(Model model) throws CommandException { for (Index buyerIndex : buyersToAdd) { int zeroBasedBuyer = buyerIndex.getZeroBased(); + int oneBasedBuyer = buyerIndex.getOneBased(); if (zeroBasedBuyer >= lastShownPersonList.size()) { throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } @@ -77,7 +79,8 @@ public CommandResult execute(Model model) throws CommandException { // Check if the person is actually an instance of Buyer if (!buyerToAdd.getRole().equals(Role.BUYER)) { - throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, buyerToAdd.getName())); + throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, + oneBasedBuyer, buyerToAdd.getName())); } // Add the buyer to newBuyers set only if not already in existingBuyers diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java index 8409e6c2304..f63a7b9551f 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java @@ -35,7 +35,8 @@ public class RemoveBuyersFromListingCommand extends Command { + "Removed buyers: %2$s"; public static final String MESSAGE_LISTING_NOT_FOUND = "The specified listing name does not exist."; public static final String MESSAGE_EMPTY_SET = "Please provide valid buyers"; - public static final String MESSAGE_PERSON_NOT_BUYER = "The buyer specified is not a buyer: %d"; + public static final String MESSAGE_PERSON_NOT_BUYER = "The specified person is not a buyer:\n" + + "%d. %s"; public static final String MESSAGE_NOT_BUYER_FOR_LISTING = "The specified buyer %1$s is not a buyer of the listing %2$s."; public static final String MESSAGE_BUYER_NOT_FOUND = "The specified buyer %1$s does not exist in the client list."; diff --git a/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java b/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java index 3dfacbfe6b3..ad2fd438ed6 100644 --- a/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java @@ -31,14 +31,6 @@ public class RemoveBuyersFromListingCommandParser implements Parser SELLER = Set.of(SELLER_NAME); - private static final Set EXISTING_BUYERS = Set.of(GEORGE.getName()); private static final Set VALID_BUYER_INDEXES = Set.of(INDEX_FOURTH_PERSON, INDEX_FIFTH_PERSON); - private static final Set INVALID_BUYER_INDEXES = Set.of(INDEX_FIRST_PERSON); + private static final Set OTHER_BUYER_INDEXES = Set.of(INDEX_SIXTH_PERSON, INDEX_SEVENTH_PERSON); + private static final Person FOURTH_BUYER = DANIEL; + private static final Person FIFTH_BUYER = ELLE; + private static final Set SELLER_INDEX = Set.of(INDEX_FIRST_PERSON); private static final Set BUYER_INDEXES_OUT_OF_BOUNDS = Set.of(PERSON_INDEX_OUT_OF_BOUNDS); private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); @@ -74,7 +75,7 @@ public void execute_buyerAddedToListingUnfilteredList_success() { VALID_LISTING_NAME); Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); - Listing editedListing = new ListingBuilder(VALID_LISTING).withBuyers().build(); + Listing editedListing = new ListingBuilder(VALID_LISTING).withBuyers(DANIEL, ELLE).build(); expectedModel.setListing(VALID_LISTING, editedListing); @@ -82,66 +83,88 @@ public void execute_buyerAddedToListingUnfilteredList_success() { } @Test - public void execute_buyerNotInClientList_throwsCommandException() { + void execute_buyerAddedToListingFilteredList_success() { + showListingAtIndex(model, INDEX_FIRST_LISTING); AddBuyersToListingCommand addBuyersToListingCommand = - new AddBuyersToListingCommand(VALID_LISTING_INDEX, BUYER_INDEXES_OUT_OF_BOUNDS); + new AddBuyersToListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); + + String expectedMessage = String.format(AddBuyersToListingCommand.MESSAGE_ADD_BUYERS_SUCCESS, + VALID_LISTING_NAME); + + Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); + Listing editedListing = new ListingBuilder(VALID_LISTING).withBuyers(DANIEL, ELLE).build(); + + expectedModel.setListing(VALID_LISTING, editedListing); + showListingAtIndex(model, INDEX_FIRST_LISTING); + assertCommandSuccess(addBuyersToListingCommand, model, expectedMessage,expectedModel); + } + + @Test + public void execute_buyerIndexOutOfBoundsUnfilteredPersonList_throwsCommandException() { + AddBuyersToListingCommand addBuyersToListingCommand = + new AddBuyersToListingCommand(INDEX_FIRST_LISTING, BUYER_INDEXES_OUT_OF_BOUNDS); assertCommandFailure(addBuyersToListingCommand, model, - String.format(AddBuyersToListingCommand.MESSAGE_BUYER_NOT_FOUND, INVALID_BUYER_NAME)); + Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + @Test + public void execute_buyerIndexOutOfBoundsFilteredPersonList_throwsCommandException() { + showPersonAtIndex(model, INDEX_THIRD_PERSON); + AddBuyersToListingCommand addBuyersToListingCommand = + new AddBuyersToListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); + assertCommandFailure(addBuyersToListingCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } @Test - public void execute_listingIndexOutOfBoundsUnfilteredList_throwsCommandException() { + public void execute_listingIndexOutOfBoundsUnfilteredListingList_throwsCommandException() { AddBuyersToListingCommand addBuyersToListingCommand = new AddBuyersToListingCommand(LISTING_INDEX_OUT_OF_BOUNDS, VALID_BUYER_INDEXES); - assertCommandFailure(addBuyersToListingCommand, model, AddBuyersToListingCommand.MESSAGE_LISTING_NOT_FOUND); + assertCommandFailure(addBuyersToListingCommand, model, Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } @Test - public void execute_existingBuyerOfListing_throwsCommandException() { + public void execute_listingIndexOutOfBoundsFilteredListingList_throwsCommandException() { + showListingAtIndex(model, INDEX_SECOND_LISTING); AddBuyersToListingCommand addBuyersToListingCommand = - new AddBuyersToListingCommand(VALID_LISTING_NAME, EXISTING_BUYERS); - assertCommandFailure(addBuyersToListingCommand, model, AddBuyersToListingCommand.MESSAGE_DUPLICATE_BUYERS); + new AddBuyersToListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); + assertCommandFailure(addBuyersToListingCommand, model, Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } @Test public void execute_inputSeller_throwsCommandException() { AddBuyersToListingCommand addBuyersToListingCommand = - new AddBuyersToListingCommand(VALID_LISTING_NAME, SELLER); + new AddBuyersToListingCommand(INDEX_FIRST_LISTING, SELLER_INDEX); assertCommandFailure(addBuyersToListingCommand, model, String.format(AddBuyersToListingCommand.MESSAGE_PERSON_NOT_BUYER, SELLER_NAME)); } @Test public void equals() { - Buyer alice = new PersonBuilder().withName("Alice").buildBuyer(); - Buyer bob = new PersonBuilder().withName("Bob").buildBuyer(); - Set setBuyer1 = Set.of(alice.getName()); - Set setBuyer2 = Set.of(bob.getName()); - AddBuyersToListingCommand addAliceToListingCommand = - new AddBuyersToListingCommand(VALID_LISTING_NAME, setBuyer1); - AddBuyersToListingCommand addBobToListingCommand = - new AddBuyersToListingCommand(VALID_LISTING_NAME, setBuyer2); + + AddBuyersToListingCommand validAddBuyersToListingCommand = + new AddBuyersToListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); + + AddBuyersToListingCommand otherAddBuyersToListingCommand = + new AddBuyersToListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); // same object -> returns true - assertTrue(addAliceToListingCommand.equals(addAliceToListingCommand)); + assertTrue(validAddBuyersToListingCommand.equals(validAddBuyersToListingCommand)); // same values -> returns true - AddBuyersToListingCommand addAliceToListingCommandCopy = - new AddBuyersToListingCommand(VALID_LISTING_NAME, setBuyer1); - assertTrue(addAliceToListingCommand.equals(addAliceToListingCommandCopy)); + assertTrue(validAddBuyersToListingCommand.equals(otherAddBuyersToListingCommand)); // different types -> returns false - assertFalse(addAliceToListingCommand.equals(1)); + assertFalse(validAddBuyersToListingCommand.equals(1)); // null -> returns false - assertFalse(addAliceToListingCommand.equals(null)); + assertFalse(validAddBuyersToListingCommand.equals(null)); // different person -> returns false - assertFalse(addAliceToListingCommand.equals(addBobToListingCommand)); + otherAddBuyersToListingCommand = new AddBuyersToListingCommand(INDEX_FIRST_LISTING, OTHER_BUYER_INDEXES); + assertFalse(validAddBuyersToListingCommand.equals(otherAddBuyersToListingCommand)); - // different listingName -> returns false - AddBuyersToListingCommand addToDifferentListing = - new AddBuyersToListingCommand(OTHER_LISTING_NAME, setBuyer1); - assertFalse(addAliceToListingCommand.equals(addToDifferentListing)); + // different listingIndex -> returns false + otherAddBuyersToListingCommand = new AddBuyersToListingCommand(OTHER_LISTING_INDEX, VALID_BUYER_INDEXES); + assertFalse(validAddBuyersToListingCommand.equals(otherAddBuyersToListingCommand)); } } diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java index 8787e990592..c4e3046495c 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java @@ -5,6 +5,12 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIFTH_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_FOURTH_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_SIXTH_PERSON; import static seedu.address.testutil.TypicalListings.PASIR_RIS; import static seedu.address.testutil.TypicalListings.TAMPINES; import static seedu.address.testutil.TypicalPersons.ALICE; @@ -40,43 +46,43 @@ public class AddListingCommandTest { @Test public void constructor_nulllistingName_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> new AddListingCommand(null, PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName())))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON)))); } @Test public void constructor_nullPrice_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> new AddListingCommand(PASIR_RIS.getName(), null, - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName())))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON)))); } @Test public void constructor_nullArea_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - null, PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName())))); + null, PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON)))); } @Test public void constructor_nullAddress_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), null, PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName())))); + PASIR_RIS.getArea(), null, PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON)))); } @Test public void constructor_nullRegion_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), null, ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName())))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), null, INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON)))); } @Test public void constructor_nullSeller_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), null, - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName())))); + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON)))); } @Test @@ -84,7 +90,7 @@ public void constructor_nullBuyers_success() throws CommandException { ModelStubAcceptingListingAdded modelStub = new ModelStubAcceptingListingAdded(); modelStub.addPerson(ALICE); CommandResult commandResult = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, null).execute(modelStub); Listing listingWithNoBuyers = new ListingBuilder(PASIR_RIS).withBuyers().build(); @@ -111,8 +117,8 @@ public void execute_listingAcceptedByModel_addSuccessful() throws CommandExcepti @Test public void execute_duplicateListing_throwsCommandException() { AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); ModelStub modelStub = new ModelStubWithListing(PASIR_RIS); modelStub.addPerson(ALICE); modelStub.addPerson(DANIEL); @@ -125,8 +131,8 @@ public void execute_duplicateListing_throwsCommandException() { @Test public void execute_buyerAsSeller_throwsCommandException() { AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), FIONA.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIFTH_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); ModelStub modelStub = new ModelStubWithListing(PASIR_RIS); modelStub.addPerson(FIONA); modelStub.addPerson(DANIEL); @@ -139,8 +145,8 @@ public void execute_buyerAsSeller_throwsCommandException() { @Test public void execute_sellerDoesNotExist_throwsCommandException() { AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), FIONA.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIFTH_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); ModelStub modelStub = new ModelStubWithListing(PASIR_RIS); modelStub.addPerson(DANIEL); modelStub.addPerson(GEORGE); @@ -152,11 +158,11 @@ public void execute_sellerDoesNotExist_throwsCommandException() { @Test public void execute_buyerDoesNotExist_throwsCommandException() { AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), FIONA.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIFTH_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); ModelStub modelStub = new ModelStubWithListing(PASIR_RIS); modelStub.addPerson(ALICE); - // DANIEL does not exist + // DANIEL does not exist (FOURTH PERSON) modelStub.addPerson(GEORGE); assertThrows(CommandException.class, @@ -167,20 +173,20 @@ public void execute_buyerDoesNotExist_throwsCommandException() { @Test public void equals() { AddListingCommand addPasirRisCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); AddListingCommand addTampinesCommand = new AddListingCommand(TAMPINES.getName(), TAMPINES.getPrice(), - TAMPINES.getArea(), TAMPINES.getAddress(), TAMPINES.getRegion(), BENSON.getName(), - new HashSet<>(List.of(DANIEL.getName(), ELLE.getName()))); + TAMPINES.getArea(), TAMPINES.getAddress(), TAMPINES.getRegion(), INDEX_SECOND_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_FIFTH_PERSON))); // same object -> return true assertTrue(addPasirRisCommand.equals(addPasirRisCommand)); // same values -> returns true AddListingCommand addPasirRisCommandCopy = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); assertTrue(addPasirRisCommand.equals(addPasirRisCommandCopy)); // different values -> return false @@ -194,41 +200,41 @@ public void equals() { // any one value is different -> return false AddListingCommand different = new AddListingCommand(PASIR_RIS.getName(), TAMPINES.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); assertFalse(addPasirRisCommand.equals(different)); different = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - new Area("50"), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + new Area("50"), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); assertFalse(addPasirRisCommand.equals(different)); different = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), TAMPINES.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), TAMPINES.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); assertFalse(addPasirRisCommand.equals(different)); different = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), Region.WEST, ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), Region.WEST, INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); assertFalse(addPasirRisCommand.equals(different)); different = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), BENSON.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_SECOND_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); assertFalse(addPasirRisCommand.equals(different)); different = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON))); assertFalse(addPasirRisCommand.equals(different)); } @Test public void toStringMethod() { AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), ALICE.getName(), - new HashSet<>(List.of(DANIEL.getName(), GEORGE.getName()))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); String expected = AddListingCommand.class.getCanonicalName() + "{toAdd=" + PASIR_RIS.getName() + ", address=" + PASIR_RIS.getAddress() diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/EditListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/EditListingCommandTest.java index 4080ca58c9c..c077cbecc14 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/EditListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/EditListingCommandTest.java @@ -5,7 +5,12 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.CommandTestUtil.showListingAtIndex; import static seedu.address.logic.commands.CommandTestUtil.showListingWithName; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIFTH_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_LISTING; +import static seedu.address.testutil.TypicalIndexes.LISTING_INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalListings.PASIR_RIS; import static seedu.address.testutil.TypicalListings.SENGKANG; import static seedu.address.testutil.TypicalListings.SIMEI; @@ -29,14 +34,17 @@ import seedu.address.testutil.ListingBuilder; public class EditListingCommandTest { + private static final Listing FIRST_LISTING = PASIR_RIS; + private static final Listing FIFTH_LISTING = SENGKANG; private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); @Test public void execute_allFieldsSpecifiedUnfilteredList_success() { Listing editedListing = new ListingBuilder(SIMEI).withBuyers(SENGKANG.getBuyers() .toArray(new Person[0])).build(); + EditListingDescriptor descriptor = new EditListingDescriptorBuilder(editedListing).build(); - EditListingCommand editListingCommand = new EditListingCommand(SENGKANG.getName(), descriptor); + EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIFTH_LISTING, descriptor); String expectedMessage = String.format(EditListingCommand.MESSAGE_EDIT_LISTING_SUCCESS, Messages.format(editedListing)); @@ -52,7 +60,7 @@ public void execute_allFieldsSpecifiedUnfilteredList_success() { public void execute_someFieldsSpecifiedUnfilteredList_success() { Listing editedListing = new ListingBuilder(SENGKANG).withArea("50").withSeller(ALICE).build(); EditListingDescriptor descriptor = new EditListingDescriptorBuilder(editedListing).build(); - EditListingCommand editListingCommand = new EditListingCommand(SENGKANG.getName(), descriptor); + EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIFTH_LISTING, descriptor); String expectedMessage = String.format(EditListingCommand.MESSAGE_EDIT_LISTING_SUCCESS, Messages.format(editedListing)); @@ -65,7 +73,7 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() { @Test public void execute_noFieldSpecifiedUnfilteredList_success() { - EditListingCommand editListingCommand = new EditListingCommand(SENGKANG.getName(), + EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIFTH_LISTING, new EditListingDescriptor()); Listing editedListing = SENGKANG; @@ -84,7 +92,7 @@ public void execute_filteredList_success() { Listing editedListing = new ListingBuilder(SIMEI).withBuyers(PASIR_RIS.getBuyers() .toArray(new Person[0])).build(); EditListingDescriptor descriptor = new EditListingDescriptorBuilder(editedListing).build(); - EditListingCommand editListingCommand = new EditListingCommand(PASIR_RIS.getName(), descriptor); + EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIRST_LISTING, descriptor); String expectedMessage = String.format(EditListingCommand.MESSAGE_EDIT_LISTING_SUCCESS, Messages.format(editedListing)); @@ -103,7 +111,7 @@ public void execute_duplicateListingName_failure() { Listing editedListing = PASIR_RIS; EditListingDescriptor descriptor = new EditListingDescriptorBuilder(editedListing) .withAddress(SENGKANG.getAddress()).build(); - EditListingCommand editListingCommand = new EditListingCommand(SENGKANG.getName(), descriptor); + EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIFTH_LISTING, descriptor); assertCommandFailure(editListingCommand, model, EditListingCommand.MESSAGE_DUPLICATE_LISTING); } @@ -113,26 +121,25 @@ public void execute_duplicateListingAddress_failure() { Listing editedListing = PASIR_RIS; EditListingDescriptor descriptor = new EditListingDescriptorBuilder(editedListing) .withName(SENGKANG.getName()).build(); - EditListingCommand editListingCommand = new EditListingCommand(SENGKANG.getName(), descriptor); + EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIFTH_LISTING, descriptor); assertCommandFailure(editListingCommand, model, EditListingCommand.MESSAGE_DUPLICATE_LISTING); } @Test - public void execute_invalidListingUnfilteredList_failure() { - Listing notInListings = SIMEI; + public void execute_listingIndexOutOfBoundsUnfilteredList_failure() { EditListingDescriptor descriptor = new EditListingDescriptorBuilder().build(); - EditListingCommand editListingCommand = new EditListingCommand(notInListings.getName(), descriptor); + EditListingCommand editListingCommand = new EditListingCommand(LISTING_INDEX_OUT_OF_BOUNDS, descriptor); assertCommandFailure(editListingCommand, model, EditListingCommand.MESSAGE_INVALID_LISTING_NAME); } @Test public void execute_invalidListingFilteredList_failure() { - showListingWithName(model, PASIR_RIS.getName()); - Listing notInList = TAMPINES; + showListingAtIndex(model, INDEX_SECOND_LISTING); + EditListingDescriptor descriptor = new EditListingDescriptorBuilder().build(); - EditListingCommand editListingCommand = new EditListingCommand(notInList.getName(), descriptor); + EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIRST_LISTING, descriptor); assertCommandFailure(editListingCommand, model, EditListingCommand.MESSAGE_INVALID_LISTING_NAME); } @@ -141,12 +148,12 @@ public void execute_invalidListingFilteredList_failure() { public void equals() { EditListingDescriptor editListingDescriptor = new EditListingDescriptorBuilder(PASIR_RIS) .withName(SIMEI.getName()).build(); - final EditListingCommand standardCommand = new EditListingCommand(PASIR_RIS.getName(), editListingDescriptor); + final EditListingCommand standardCommand = new EditListingCommand(INDEX_FIRST_LISTING, editListingDescriptor); // same values -> returns true EditListingDescriptor copyDescriptor = new EditListingDescriptorBuilder(PASIR_RIS) .withName(SIMEI.getName()).build(); - EditListingCommand commandWithSameValues = new EditListingCommand(PASIR_RIS.getName(), copyDescriptor); + EditListingCommand commandWithSameValues = new EditListingCommand(INDEX_FIRST_LISTING, copyDescriptor); assertTrue(standardCommand.equals(commandWithSameValues)); // same object -> returns true @@ -159,20 +166,20 @@ public void equals() { assertFalse(standardCommand.equals(new ClearCommand())); // different name -> returns false - EditListingCommand differentName = new EditListingCommand(TAMPINES.getName(), editListingDescriptor); + EditListingCommand differentName = new EditListingCommand(INDEX_SECOND_LISTING, editListingDescriptor); assertFalse(standardCommand.equals(differentName)); // different descriptor -> returns false EditListingDescriptor otherDescriptor = new EditListingDescriptorBuilder(TAMPINES) .withName(SIMEI.getName()).build(); - EditListingCommand differentDescriptor = new EditListingCommand(PASIR_RIS.getName(), otherDescriptor); + EditListingCommand differentDescriptor = new EditListingCommand(INDEX_FIRST_LISTING, otherDescriptor); assertFalse(standardCommand.equals(differentDescriptor)); } @Test public void toStringMethod() { EditListingDescriptor editListingDescriptor = new EditListingDescriptor(); - EditListingCommand editListingCommand = new EditListingCommand(PASIR_RIS.getName(), editListingDescriptor); + EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIRST_LISTING, editListingDescriptor); String expected = EditListingCommand.class.getCanonicalName() + "{listingName=" + PASIR_RIS.getName() + ", editListingDescriptor=" + editListingDescriptor + "}"; assertEquals(expected, editListingCommand.toString()); diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/EditListingDescriptorTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/EditListingDescriptorTest.java index 8a8e42a1e6f..918f361292f 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/EditListingDescriptorTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/EditListingDescriptorTest.java @@ -75,7 +75,7 @@ public void toStringMethod() { + editListingDescriptor.getArea().orElse(null) + ", address=" + editListingDescriptor.getAddress().orElse(null) + ", region=" + editListingDescriptor.getRegion().orElse(null) + ", seller=" - + editListingDescriptor.getSellerName().orElse(null) + "}"; + + editListingDescriptor.getSellerIndex().orElse(null) + "}"; assertEquals(expected, editListingDescriptor.toString()); } diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommandTest.java index c03ecb5e02e..f9a95c6da0f 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommandTest.java @@ -4,13 +4,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.CommandTestUtil.showListingAtIndex; +import static seedu.address.logic.commands.CommandTestUtil.showPersonAtIndex; import static seedu.address.testutil.Assert.assertThrows; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIFTH_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_FOURTH_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_SIXTH_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_PERSON; +import static seedu.address.testutil.TypicalIndexes.LISTING_INDEX_OUT_OF_BOUNDS; +import static seedu.address.testutil.TypicalIndexes.PERSON_INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalListings.PASIR_RIS; -import static seedu.address.testutil.TypicalListings.SIMEI; -import static seedu.address.testutil.TypicalListings.TAMPINES; import static seedu.address.testutil.TypicalListings.getTypicalListings; -import static seedu.address.testutil.TypicalPersons.ALICE; -import static seedu.address.testutil.TypicalPersons.AMY; import static seedu.address.testutil.TypicalPersons.DANIEL; import static seedu.address.testutil.TypicalPersons.ELLE; import static seedu.address.testutil.TypicalPersons.GEORGE; @@ -21,6 +27,8 @@ import org.junit.jupiter.api.Test; +import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; @@ -29,37 +37,36 @@ import seedu.address.testutil.ListingBuilder; public class RemoveBuyersFromListingCommandTest { - - private static final Set VALID_BUYERS = Set.of(DANIEL.getName()); - private static final Set INVALID_BUYERS = Set.of(AMY.getName()); - private static final Name SELLER_NAME = ALICE.getName(); - private static final Set SELLER = Set.of(SELLER_NAME); - private static final Set EMPTY_SET = new HashSet<>(); - private static final Set NOT_BUYER_OF_LISTING = Set.of(ELLE.getName()); + private static final Set VALID_BUYER_INDEXES = Set.of(INDEX_FOURTH_PERSON); + private static final Set OTHER_BUYER_INDEXES = Set.of(INDEX_SIXTH_LISTING); + private static final Name FOURTH_BUYER_NAME = DANIEL.getName(); + private static final Set BUYER_INDEXES_OUT_OF_BOUNDS = Set.of(PERSON_INDEX_OUT_OF_BOUNDS); + private static final Set SELLER_INDEX = Set.of(INDEX_FIRST_LISTING); + private static final Set EMPTY_SET = new HashSet<>(); + private static final Set NOT_BUYER_OF_LISTING = Set.of(INDEX_FIFTH_PERSON); private static final Listing VALID_LISTING = PASIR_RIS; private static final Name VALID_LISTING_NAME = PASIR_RIS.getName(); - private static final Name INVALID_LISTING_NAME = SIMEI.getName(); private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); @Test public void constructor_nullListingName_throwsNullPointerException() { assertThrows(NullPointerException.class, () -> new RemoveBuyersFromListingCommand(null, - VALID_BUYERS)); + VALID_BUYER_INDEXES)); } @Test public void constructor_nullBuyersToRemove_throwsNullPointerException() { - assertThrows(NullPointerException.class, () -> new RemoveBuyersFromListingCommand(VALID_LISTING_NAME, + assertThrows(NullPointerException.class, () -> new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, null)); } @Test public void execute_validValues_success() { RemoveBuyersFromListingCommand removeBuyersFromListingCommand = - new RemoveBuyersFromListingCommand(VALID_LISTING_NAME, VALID_BUYERS); + new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); String expectedMessage = String.format(RemoveBuyersFromListingCommand.MESSAGE_REMOVE_BUYERS_SUCCESS, - VALID_LISTING_NAME); + VALID_LISTING_NAME, FOURTH_BUYER_NAME); Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); Listing editedListing = new ListingBuilder(VALID_LISTING).withBuyers(GEORGE).build(); @@ -68,33 +75,50 @@ public void execute_validValues_success() { } @Test - public void execute_buyerNotInClientList_throwsCommandException() { + public void execute_buyerIndexOutOfBoundsUnfilteredPersonList_throwsCommandException() { RemoveBuyersFromListingCommand removeBuyersFromListingCommand = - new RemoveBuyersFromListingCommand(VALID_LISTING_NAME, INVALID_BUYERS); + new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, BUYER_INDEXES_OUT_OF_BOUNDS); assertCommandFailure(removeBuyersFromListingCommand, model, - String.format(RemoveBuyersFromListingCommand.MESSAGE_BUYER_NOT_FOUND, AMY.getName())); + Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); + } + + @Test + public void execute_buyerIndexOutOfBoundsFilteredPersonList_throwsCommandException() { + showPersonAtIndex(model, INDEX_THIRD_PERSON); + RemoveBuyersFromListingCommand removeBuyersFromListingCommand = + new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); + assertCommandFailure(removeBuyersFromListingCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } @Test - public void execute_listingNotInListingList_throwsCommandException() { + public void execute_listingIndexOutOfBoundsUnfilteredListingList_throwsCommandException() { RemoveBuyersFromListingCommand removeBuyersFromListingCommand = - new RemoveBuyersFromListingCommand(INVALID_LISTING_NAME, VALID_BUYERS); + new RemoveBuyersFromListingCommand(LISTING_INDEX_OUT_OF_BOUNDS, VALID_BUYER_INDEXES); assertCommandFailure(removeBuyersFromListingCommand, model, - RemoveBuyersFromListingCommand.MESSAGE_LISTING_NOT_FOUND); + Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); + } + + @Test + public void execute_listingIndexOutOfBoundsFilteredListingList_throwsCommandException() { + showListingAtIndex(model, INDEX_SECOND_LISTING); + RemoveBuyersFromListingCommand removeBuyersFromListingCommand = + new RemoveBuyersFromListingCommand(LISTING_INDEX_OUT_OF_BOUNDS, VALID_BUYER_INDEXES); + assertCommandFailure(removeBuyersFromListingCommand, model, + Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } @Test public void execute_inputSeller_throwsCommandException() { RemoveBuyersFromListingCommand removeBuyersFromListingCommand = - new RemoveBuyersFromListingCommand(VALID_LISTING_NAME, SELLER); + new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, SELLER_INDEX); assertCommandFailure(removeBuyersFromListingCommand, model, - String.format(RemoveBuyersFromListingCommand.MESSAGE_PERSON_NOT_BUYER, SELLER_NAME)); + String.format(RemoveBuyersFromListingCommand.MESSAGE_PERSON_NOT_BUYER, SELLER_INDEX)); } @Test public void execute_notBuyerOfListing_throwsCommandException() { RemoveBuyersFromListingCommand removeBuyersFromListingCommand = - new RemoveBuyersFromListingCommand(VALID_LISTING_NAME, NOT_BUYER_OF_LISTING); + new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, NOT_BUYER_OF_LISTING); assertCommandFailure(removeBuyersFromListingCommand, model, String.format(RemoveBuyersFromListingCommand.MESSAGE_NOT_BUYER_FOR_LISTING, ELLE.getName(), VALID_LISTING_NAME)); @@ -103,40 +127,39 @@ public void execute_notBuyerOfListing_throwsCommandException() { @Test public void execute_emptySet_throwsCommandException() { RemoveBuyersFromListingCommand removeBuyersFromListingCommand = - new RemoveBuyersFromListingCommand(VALID_LISTING_NAME, EMPTY_SET); + new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, EMPTY_SET); assertCommandFailure(removeBuyersFromListingCommand, model, RemoveBuyersFromListingCommand.MESSAGE_EMPTY_SET); } @Test public void equals() { - RemoveBuyersFromListingCommand removeBuyersFromPasirRis = - new RemoveBuyersFromListingCommand(PASIR_RIS.getName(), Set.of(DANIEL.getName())); + RemoveBuyersFromListingCommand validRemoveBuyersFromListingCommand = + new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); - RemoveBuyersFromListingCommand removeBuyersFromTampines = - new RemoveBuyersFromListingCommand(TAMPINES.getName(), Set.of(DANIEL.getName())); + RemoveBuyersFromListingCommand otherRemoveBuyersFromListingCommand = + new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); // same object -> returns true - assertTrue(removeBuyersFromPasirRis.equals(removeBuyersFromPasirRis)); + assertTrue(validRemoveBuyersFromListingCommand.equals(validRemoveBuyersFromListingCommand)); // same values -> returns true - RemoveBuyersFromListingCommand copy = new RemoveBuyersFromListingCommand(PASIR_RIS.getName(), - Set.of(DANIEL.getName())); - - assertTrue(removeBuyersFromPasirRis.equals(copy)); + assertTrue(validRemoveBuyersFromListingCommand.equals(otherRemoveBuyersFromListingCommand)); // null -> returns false - assertFalse(removeBuyersFromPasirRis.equals(null)); + assertFalse(validRemoveBuyersFromListingCommand.equals(null)); // different type -> returns false - assertFalse(removeBuyersFromPasirRis.equals(4)); + assertFalse(validRemoveBuyersFromListingCommand.equals(4)); // different Listing -> returns false - assertFalse(removeBuyersFromPasirRis.equals(removeBuyersFromTampines)); + otherRemoveBuyersFromListingCommand = new RemoveBuyersFromListingCommand(INDEX_SECOND_LISTING, + VALID_BUYER_INDEXES); + assertFalse(validRemoveBuyersFromListingCommand.equals(otherRemoveBuyersFromListingCommand)); // different Buyer -> returns false - RemoveBuyersFromListingCommand differentBuyer = new RemoveBuyersFromListingCommand(PASIR_RIS.getName(), - Set.of(GEORGE.getName())); - assertFalse(removeBuyersFromPasirRis.equals(differentBuyer)); + otherRemoveBuyersFromListingCommand = new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, + OTHER_BUYER_INDEXES); + assertFalse(validRemoveBuyersFromListingCommand.equals(otherRemoveBuyersFromListingCommand)); } } diff --git a/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParserTest.java b/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParserTest.java index 294d29301e7..c53af687e00 100644 --- a/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParserTest.java @@ -4,44 +4,44 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.logic.parser.CliSyntax.PREFIX_BUYER; -import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; import java.util.HashSet; import java.util.Set; import org.junit.jupiter.api.Test; +import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.listingcommands.AddBuyersToListingCommand; import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.model.person.Name; public class AddBuyersToListingCommandParserTest { - private static final String VALID_LISTING_NAME = "Kent Ridge Condo"; - private static final String VALID_BUYER_NAME_1 = "John Doe"; - private static final String VALID_BUYER_NAME_2 = "Jane Smith"; - private static final String INVALID_BUYER_NAME = ""; + private static final String VALID_LISTING_INDEX = "1"; + private static final String INVALID_LISTING_INDEX = "-1"; + private static final String VALID_BUYER_INDEX_1 = "1"; + private static final String VALID_BUYER_INDEX_2 = "2"; + private static final String INVALID_BUYER_INDEX = "-1"; private final AddBuyersToListingCommandParser parser = new AddBuyersToListingCommandParser(); @Test public void parse_allFieldsPresent_success() throws Exception { - String userInput = VALID_LISTING_NAME + " " - + PREFIX_BUYER + VALID_BUYER_NAME_1 + " " - + PREFIX_BUYER + VALID_BUYER_NAME_2; + String userInput = VALID_LISTING_INDEX + " " + + PREFIX_BUYER + VALID_BUYER_INDEX_1 + " " + + PREFIX_BUYER + VALID_BUYER_INDEX_2; - Set expectedBuyerNames = new HashSet<>(); - expectedBuyerNames.add(new Name(VALID_BUYER_NAME_1)); - expectedBuyerNames.add(new Name(VALID_BUYER_NAME_2)); + Set expectedBuyerIndexes = new HashSet<>(); + expectedBuyerIndexes.add(Index.fromOneBased(Integer.parseInt(VALID_BUYER_INDEX_1))); + expectedBuyerIndexes.add(Index.fromOneBased(Integer.parseInt(VALID_BUYER_INDEX_2))); AddBuyersToListingCommand expectedCommand = - new AddBuyersToListingCommand(new Name(VALID_LISTING_NAME), expectedBuyerNames); + new AddBuyersToListingCommand(Index.fromOneBased(Integer.parseInt(VALID_LISTING_INDEX)), + expectedBuyerIndexes); AddBuyersToListingCommand result = parser.parse(userInput); assertEquals(expectedCommand, result); } @Test - public void parse_missingListingName_throwsParseException() { - String userInput = AddBuyersToListingCommand.COMMAND_WORD - + PREFIX_BUYER + VALID_BUYER_NAME_1; + public void parse_missingListingIndex_throwsParseException() { + String userInput = " " + PREFIX_BUYER + VALID_BUYER_INDEX_1; String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddBuyersToListingCommand.MESSAGE_USAGE); @@ -49,9 +49,17 @@ public void parse_missingListingName_throwsParseException() { } @Test - public void parse_missingBuyerNames_throwsParseException() { + public void parse_invalidListingIndex_throwsParseException() { String userInput = AddBuyersToListingCommand.COMMAND_WORD + " " - + PREFIX_NAME + VALID_LISTING_NAME; + + INVALID_LISTING_INDEX + " " + + PREFIX_BUYER + VALID_BUYER_INDEX_1; + + assertThrows(ParseException.class, () -> parser.parse(userInput)); + } + + @Test + public void parse_missingBuyerIndex_throwsParseException() { + String userInput = VALID_LISTING_INDEX + " " + PREFIX_BUYER; String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddBuyersToListingCommand.MESSAGE_USAGE); @@ -59,42 +67,17 @@ public void parse_missingBuyerNames_throwsParseException() { } @Test - public void parse_invalidBuyerName_throwsParseException() { + public void parse_invalidBuyerIndex_throwsParseException() { String userInput = AddBuyersToListingCommand.COMMAND_WORD + " " - + PREFIX_NAME + VALID_LISTING_NAME + " " - + PREFIX_BUYER + INVALID_BUYER_NAME; + + VALID_LISTING_INDEX + " " + + PREFIX_BUYER + INVALID_BUYER_INDEX; assertThrows(ParseException.class, () -> parser.parse(userInput)); } @Test - public void parse_multipleBuyersPresent_success() throws Exception { - String userInput = VALID_LISTING_NAME + " " - + PREFIX_BUYER + VALID_BUYER_NAME_1 + " " - + PREFIX_BUYER + VALID_BUYER_NAME_2; - - Set expectedBuyerNames = new HashSet<>(); - expectedBuyerNames.add(new Name(VALID_BUYER_NAME_1)); - expectedBuyerNames.add(new Name(VALID_BUYER_NAME_2)); - AddBuyersToListingCommand expectedCommand = - new AddBuyersToListingCommand(new Name(VALID_LISTING_NAME), expectedBuyerNames); - - AddBuyersToListingCommand result = parser.parse(userInput); - assertEquals(expectedCommand, result); - } - @Test - public void parse_emptyListingName_throwsParseException() { - String userInput = " " - + PREFIX_BUYER + VALID_BUYER_NAME_1; - String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, - AddBuyersToListingCommand.MESSAGE_USAGE); - - assertThrows(ParseException.class, () -> parser.parse(userInput), expectedMessage); - } - - @Test - public void parse_emptyBuyerNames_throwsParseException() { - String userInput = VALID_LISTING_NAME; + public void parse_emptyBuyerIndex_throwsParseException() { + String userInput = VALID_LISTING_INDEX; String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddBuyersToListingCommand.MESSAGE_USAGE); diff --git a/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParserTest.java b/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParserTest.java index 68e3bc2f376..5fb73e217d9 100644 --- a/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParserTest.java @@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test; +import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.listingcommands.AddListingCommand; import seedu.address.model.listing.Address; import seedu.address.model.listing.Area; @@ -48,9 +49,8 @@ public void parse_validArgs_returnsAddListingCommand() { new Area(VALID_AREA_PASIR_RIS), new Address(VALID_ADDRESS_PASIR_RIS), Region.fromString(VALID_REGION_PASIR_RIS), - new Name(VALID_SELLER_PASIR_RIS), - new HashSet(Arrays.asList(new Name(VALID_FIRST_BUYER_PASIR_RIS), - new Name(VALID_SECOND_BUYER_PASIR_RIS))))); + Index.fromOneBased(1), + new HashSet(Arrays.asList(Index.fromOneBased(4), Index.fromOneBased(6))))); } @Test diff --git a/src/test/java/seedu/address/logic/parser/listingcommandparsers/EditListingCommandParserTest.java b/src/test/java/seedu/address/logic/parser/listingcommandparsers/EditListingCommandParserTest.java index 1d2a8ba1c2a..ebe184f66cd 100644 --- a/src/test/java/seedu/address/logic/parser/listingcommandparsers/EditListingCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/listingcommandparsers/EditListingCommandParserTest.java @@ -12,6 +12,7 @@ import org.junit.jupiter.api.Test; +import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.listingcommands.EditListingCommand; import seedu.address.logic.commands.listingcommands.EditListingCommand.EditListingDescriptor; import seedu.address.logic.parser.ParserUtil; @@ -19,35 +20,36 @@ import seedu.address.model.person.Name; public class EditListingCommandParserTest { - private static final String VALID_LISTING_NAME_ONE = "Woodlands Block 153"; - private static final String VALID_LISTING_NAME_TWO = "Woodlands Block 154"; + private static final String VALID_LISTING_INDEX = "1"; + private static final String VALID_LISTING_NAME = "Woodlands Block 154"; private static final String VALID_PRICE = "500000"; private static final String VALID_AREA = "1200"; private static final String VALID_ADDRESS = "Woodlands Avenue 6"; private static final String VALID_REGION = "North"; - private static final String VALID_SELLER_NAME = "John Doe"; + private static final String VALID_SELLER_INDEX = "1"; + private static final String OTHER_SELLER_INDEX = "2"; private final EditListingCommandParser parser = new EditListingCommandParser(); @Test public void parse_allFieldsPresent_success() throws Exception { - String userInput = VALID_LISTING_NAME_ONE + " " - + PREFIX_NAME + VALID_LISTING_NAME_TWO + " " + String userInput = VALID_LISTING_INDEX + " " + + PREFIX_NAME + VALID_LISTING_NAME + " " + PREFIX_PRICE + VALID_PRICE + " " + PREFIX_AREA + VALID_AREA + " " + PREFIX_ADDRESS + VALID_ADDRESS + " " + PREFIX_REGION + VALID_REGION + " " - + PREFIX_SELLER + VALID_SELLER_NAME; + + PREFIX_SELLER + VALID_SELLER_INDEX; EditListingDescriptor descriptor = new EditListingDescriptor(); - descriptor.setName(new Name(VALID_LISTING_NAME_TWO)); + descriptor.setName(new Name(VALID_LISTING_NAME)); descriptor.setPrice(ParserUtil.parsePrice(VALID_PRICE)); descriptor.setArea(ParserUtil.parseArea(VALID_AREA)); descriptor.setAddress(ParserUtil.parseAddress(VALID_ADDRESS)); descriptor.setRegion(ParserUtil.parseRegion(VALID_REGION)); - descriptor.setSellerName(new Name(VALID_SELLER_NAME)); + descriptor.setSellerIndex(ParserUtil.parseIndex(VALID_SELLER_INDEX)); EditListingCommand expectedCommand = - new EditListingCommand(new Name(VALID_LISTING_NAME_ONE), descriptor); + new EditListingCommand(Index.fromOneBased(Integer.parseInt(VALID_LISTING_INDEX)), descriptor); EditListingCommand result = parser.parse(userInput); assertEquals(expectedCommand, result); @@ -55,14 +57,14 @@ public void parse_allFieldsPresent_success() throws Exception { @Test public void parse_partialFieldsPresent_success() throws Exception { - String userInput = VALID_LISTING_NAME_ONE + " " + String userInput = VALID_LISTING_INDEX + " " + PREFIX_PRICE + VALID_PRICE; EditListingDescriptor descriptor = new EditListingDescriptor(); descriptor.setPrice(ParserUtil.parsePrice(VALID_PRICE)); EditListingCommand expectedCommand = - new EditListingCommand(new Name(VALID_LISTING_NAME_ONE), descriptor); + new EditListingCommand(Index.fromOneBased(Integer.parseInt(VALID_LISTING_INDEX)), descriptor); EditListingCommand result = parser.parse(userInput); assertEquals(expectedCommand, result); @@ -79,7 +81,7 @@ public void parse_missingListingName_throwsParseException() { @Test public void parse_noFieldsEdited_throwsParseException() { String userInput = EditListingCommand.COMMAND_WORD + " " - + VALID_LISTING_NAME_ONE; + + VALID_LISTING_INDEX; assertThrows(ParseException.class, () -> parser.parse(userInput), EditListingCommand.MESSAGE_NOT_EDITED); } @@ -92,9 +94,9 @@ public void parse_emptyArg_throwsParseException() { @Test public void parse_multipleNames_throwsParseException() { String userInput = EditListingCommand.COMMAND_WORD + " " - + PREFIX_NAME + VALID_LISTING_NAME_ONE + " " - + PREFIX_NAME + VALID_LISTING_NAME_TWO + " " - + PREFIX_NAME + VALID_LISTING_NAME_TWO + " " + + VALID_LISTING_INDEX + " " + + PREFIX_NAME + VALID_LISTING_NAME + " " + + PREFIX_NAME + VALID_LISTING_NAME + " " + PREFIX_PRICE + VALID_PRICE; assertThrows(ParseException.class, () -> parser.parse(userInput), @@ -104,7 +106,7 @@ public void parse_multipleNames_throwsParseException() { @Test public void parse_multiplePrices_throwsParseException() { String userInput = EditListingCommand.COMMAND_WORD + " " - + PREFIX_NAME + VALID_LISTING_NAME_ONE + " " + + PREFIX_NAME + VALID_LISTING_INDEX + " " + PREFIX_PRICE + VALID_PRICE + " " + PREFIX_PRICE + VALID_PRICE; @@ -115,7 +117,7 @@ public void parse_multiplePrices_throwsParseException() { @Test public void parse_multipleAreas_throwsParseException() { String userInput = EditListingCommand.COMMAND_WORD + " " - + PREFIX_NAME + VALID_LISTING_NAME_ONE + " " + + PREFIX_NAME + VALID_LISTING_INDEX + " " + PREFIX_AREA + VALID_AREA + " " + PREFIX_AREA + VALID_AREA; @@ -126,7 +128,7 @@ public void parse_multipleAreas_throwsParseException() { @Test public void parse_multipleAddresses_throwsParseException() { String userInput = EditListingCommand.COMMAND_WORD + " " - + PREFIX_NAME + VALID_LISTING_NAME_ONE + " " + + PREFIX_NAME + VALID_LISTING_INDEX + " " + PREFIX_ADDRESS + VALID_ADDRESS + " " + PREFIX_ADDRESS + VALID_ADDRESS; @@ -137,7 +139,7 @@ public void parse_multipleAddresses_throwsParseException() { @Test public void parse_multipleRegions_throwsParseException() { String userInput = EditListingCommand.COMMAND_WORD + " " - + PREFIX_NAME + VALID_LISTING_NAME_ONE + " " + + PREFIX_NAME + VALID_LISTING_INDEX + " " + PREFIX_REGION + VALID_REGION + " " + PREFIX_REGION + VALID_REGION; @@ -148,9 +150,9 @@ public void parse_multipleRegions_throwsParseException() { @Test public void parse_multipleSellers_throwsParseException() { String userInput = EditListingCommand.COMMAND_WORD + " " - + PREFIX_NAME + VALID_LISTING_NAME_ONE + " " - + PREFIX_SELLER + VALID_SELLER_NAME + " " - + PREFIX_SELLER + VALID_SELLER_NAME; + + VALID_LISTING_INDEX + " " + + PREFIX_SELLER + VALID_SELLER_INDEX + " " + + PREFIX_SELLER + OTHER_SELLER_INDEX; assertThrows(ParseException.class, () -> parser.parse(userInput), String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditListingCommand.MESSAGE_USAGE)); diff --git a/src/test/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParserTest.java b/src/test/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParserTest.java index de69212b50f..7ef3ac996f2 100644 --- a/src/test/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParserTest.java @@ -2,82 +2,93 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.logic.parser.CliSyntax.PREFIX_BUYER; -import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; import java.util.HashSet; import java.util.Set; import org.junit.jupiter.api.Test; +import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.listingcommands.RemoveBuyersFromListingCommand; import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.model.person.Name; + public class RemoveBuyersFromListingCommandParserTest { - private static final String VALID_LISTING_NAME = "Pasir Ris HDB"; - private static final String VALID_BUYER_NAME_1 = "Sean Dias"; - private static final String VALID_BUYER_NAME_2 = "Wen Xuan"; + private static final String VALID_LISTING_INDEX = "1"; + private static final String INVALID_LISTING_INDEX = "-1"; + private static final String VALID_BUYER_INDEX_1 = "1"; + private static final String VALID_BUYER_INDEX_2 = "3"; + private static final String INVALID_BUYER_INDEX = "-1"; private final RemoveBuyersFromListingCommandParser parser = new RemoveBuyersFromListingCommandParser(); @Test public void parse_allFieldsPresent_success() throws Exception { - String userInput = VALID_LISTING_NAME + " " - + PREFIX_BUYER + VALID_BUYER_NAME_1 + " " - + PREFIX_BUYER + VALID_BUYER_NAME_2; + String userInput = VALID_LISTING_INDEX + " " + + PREFIX_BUYER + VALID_BUYER_INDEX_1 + " " + + PREFIX_BUYER + VALID_BUYER_INDEX_2; - Set expectedBuyerNames = new HashSet<>(); - expectedBuyerNames.add(new Name(VALID_BUYER_NAME_1)); - expectedBuyerNames.add(new Name(VALID_BUYER_NAME_2)); - RemoveBuyersFromListingCommand expectedCommand = new RemoveBuyersFromListingCommand( - new Name(VALID_LISTING_NAME), expectedBuyerNames); + Set expectedBuyerIndexes = new HashSet<>(); + expectedBuyerIndexes.add(Index.fromOneBased(Integer.parseInt(VALID_BUYER_INDEX_1))); + expectedBuyerIndexes.add(Index.fromOneBased(Integer.parseInt(VALID_BUYER_INDEX_2))); + RemoveBuyersFromListingCommand expectedCommand = + new RemoveBuyersFromListingCommand(Index.fromOneBased(Integer.parseInt(VALID_LISTING_INDEX)), + expectedBuyerIndexes); RemoveBuyersFromListingCommand result = parser.parse(userInput); assertEquals(expectedCommand, result); } @Test - public void parse_multipleBuyers_success() throws Exception { - String userInput = VALID_LISTING_NAME + " " - + PREFIX_BUYER + VALID_BUYER_NAME_1 + " " - + PREFIX_BUYER + VALID_BUYER_NAME_2; - - Set expectedBuyerNames = new HashSet<>(); - expectedBuyerNames.add(new Name(VALID_BUYER_NAME_1)); - expectedBuyerNames.add(new Name(VALID_BUYER_NAME_2)); - RemoveBuyersFromListingCommand expectedCommand = new RemoveBuyersFromListingCommand( - new Name(VALID_LISTING_NAME), expectedBuyerNames); - - RemoveBuyersFromListingCommand result = parser.parse(userInput); - assertTrue(expectedCommand.equals(result)); + public void parse_missingListingIndex_throwsParseException() { + String userInput = RemoveBuyersFromListingCommand.COMMAND_WORD + " " + + PREFIX_BUYER + VALID_BUYER_INDEX_1; + assertThrows(ParseException.class, () -> parser.parse(userInput), + String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemoveBuyersFromListingCommand.MESSAGE_USAGE)); } @Test - public void parse_missingListingName_throwsParseException() { - String userInput = RemoveBuyersFromListingCommand.COMMAND_WORD - + PREFIX_BUYER + VALID_BUYER_NAME_1; - assertThrows(ParseException.class, () -> parser.parse(userInput), - String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemoveBuyersFromListingCommand.MESSAGE_USAGE)); + public void parse_invalidListingIndex_throwsParseException() { + String userInput = INVALID_LISTING_INDEX + " " + PREFIX_BUYER + VALID_BUYER_INDEX_1; + assertThrows(ParseException.class, () -> parser.parse(userInput)); } @Test public void parse_noBuyersSpecified_throwsParseException() { String userInput = RemoveBuyersFromListingCommand.COMMAND_WORD + " " - + PREFIX_NAME + VALID_LISTING_NAME; + + VALID_LISTING_INDEX; assertThrows(ParseException.class, () -> parser.parse(userInput), String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemoveBuyersFromListingCommand.MESSAGE_USAGE)); } + @Test + public void parse_missingBuyerIndex_throwsParseException() { + String userInput = VALID_LISTING_INDEX + " " + PREFIX_BUYER; + String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, + RemoveBuyersFromListingCommand.MESSAGE_USAGE); + + assertThrows(ParseException.class, () -> parser.parse(userInput), expectedMessage); + } + + @Test + public void parse_invalidBuyerIndex_throwsParseException() { + String userInput = RemoveBuyersFromListingCommand.COMMAND_WORD + " " + + VALID_LISTING_INDEX + " " + + PREFIX_BUYER + INVALID_BUYER_INDEX; + + assertThrows(ParseException.class, () -> parser.parse(userInput)); + } + @Test public void parse_emptyArg_throwsParseException() { assertThrows(ParseException.class, () -> parser.parse(""), String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemoveBuyersFromListingCommand.MESSAGE_USAGE)); } + @Test - public void parse_emptyBuyerNames_throwsParseException() { - String userInput = VALID_LISTING_NAME; + public void parse_emptyBuyerIndex_throwsParseException() { + String userInput = VALID_LISTING_INDEX; String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, RemoveBuyersFromListingCommand.MESSAGE_USAGE); From 3d2b0e8ec4f808845336694ac1d568d8c4f2e910 Mon Sep 17 00:00:00 2001 From: lrongyi Date: Fri, 8 Nov 2024 01:23:27 +0800 Subject: [PATCH 5/5] Resolve failing tests --- .../address/commons/core/index/Index.java | 5 +++ .../listingcommands/AddListingCommand.java | 1 - .../RemoveBuyersFromListingCommand.java | 5 ++- .../RemoveBuyersFromListingCommandParser.java | 1 - .../AddBuyersToListingCommandTest.java | 21 ++++++---- .../AddListingCommandTest.java | 42 ++++++++----------- .../DeleteListingCommandTest.java | 23 +++++----- .../EditListingCommandTest.java | 42 ++++--------------- .../EditListingDescriptorTest.java | 1 + .../RemoveBuyersFromListingCommandTest.java | 8 +++- .../logic/parser/EzstatesParserTest.java | 10 ++--- .../AddBuyersToListingCommandParserTest.java | 2 +- .../AddListingCommandParserTest.java | 6 ++- .../DeletingListingCommandParserTest.java | 8 ---- .../EditListingDescriptorBuilder.java | 26 ++++++++++-- 15 files changed, 99 insertions(+), 102 deletions(-) diff --git a/src/main/java/seedu/address/commons/core/index/Index.java b/src/main/java/seedu/address/commons/core/index/Index.java index 08e48ae5dc7..7069ee0334e 100644 --- a/src/main/java/seedu/address/commons/core/index/Index.java +++ b/src/main/java/seedu/address/commons/core/index/Index.java @@ -74,4 +74,9 @@ public String toStringOneBased() { public String toStringZeroBased() { return String.format("%d", getZeroBased()); } + + @Override + public int hashCode() { + return Integer.hashCode(zeroBasedIndex); + } } diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/AddListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/AddListingCommand.java index 2a75ab34df0..5b86c7fddc5 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/AddListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/AddListingCommand.java @@ -164,7 +164,6 @@ public String toString() { return new ToStringBuilder(this) .add("toAdd", this.listingName) .add("address", this.address) - .add("seller", this.sellerIndex) .toString(); } } diff --git a/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java b/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java index f4fd8eb397f..59c5a356412 100644 --- a/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java +++ b/src/main/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommand.java @@ -83,12 +83,13 @@ public CommandResult execute(Model model) throws CommandException { // Check if the person is actually an instance of Buyer if (!buyerToRemove.getRole().equals(Role.BUYER)) { - throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, oneBasedBuyer)); + throw new CommandException(String.format(MESSAGE_PERSON_NOT_BUYER, + oneBasedBuyer, buyerToRemove.getName())); } if (!existingBuyers.contains(buyerToRemove)) { throw new CommandException(String.format(MESSAGE_NOT_BUYER_FOR_LISTING, - buyerToRemove.getName(), listingToEdit)); + buyerToRemove.getName(), Messages.format(listingToEdit))); } buyersToRemoveSet.add(buyerToRemove); diff --git a/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java b/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java index ad2fd438ed6..c8a6741f58b 100644 --- a/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/listingcommandparsers/RemoveBuyersFromListingCommandParser.java @@ -15,7 +15,6 @@ import seedu.address.logic.parser.Parser; import seedu.address.logic.parser.ParserUtil; import seedu.address.logic.parser.exceptions.ParseException; -import seedu.address.model.person.Name; /** * Parses input arguments and creates a new {@code RemoveBuyersFromListingCommand} object. diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommandTest.java index f1a5bc4610b..ccd24a33c3b 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/AddBuyersToListingCommandTest.java @@ -14,6 +14,7 @@ import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_LISTING; import static seedu.address.testutil.TypicalIndexes.INDEX_SEVENTH_PERSON; import static seedu.address.testutil.TypicalIndexes.INDEX_SIXTH_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_LISTING; import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_PERSON; import static seedu.address.testutil.TypicalIndexes.LISTING_INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalIndexes.PERSON_INDEX_OUT_OF_BOUNDS; @@ -31,6 +32,7 @@ import seedu.address.commons.core.index.Index; import seedu.address.logic.Messages; +import seedu.address.model.AddressBook; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; @@ -67,12 +69,12 @@ public void constructor_nullListing_throwsNullPointerException() { } @Test - public void execute_buyerAddedToListingUnfilteredList_success() { + public void execute_buyerAddedToListingUnfilteredListingList_success() { AddBuyersToListingCommand addBuyersToListingCommand = new AddBuyersToListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); String expectedMessage = String.format(AddBuyersToListingCommand.MESSAGE_ADD_BUYERS_SUCCESS, - VALID_LISTING_NAME); + Messages.format(VALID_LISTING)); Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); Listing editedListing = new ListingBuilder(VALID_LISTING).withBuyers(DANIEL, ELLE).build(); @@ -83,19 +85,21 @@ public void execute_buyerAddedToListingUnfilteredList_success() { } @Test - void execute_buyerAddedToListingFilteredList_success() { + void execute_buyerAddedToListingFilteredListingList_success() { showListingAtIndex(model, INDEX_FIRST_LISTING); AddBuyersToListingCommand addBuyersToListingCommand = new AddBuyersToListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); String expectedMessage = String.format(AddBuyersToListingCommand.MESSAGE_ADD_BUYERS_SUCCESS, - VALID_LISTING_NAME); + Messages.format(VALID_LISTING)); + + Model expectedModel = new ModelManager(getTypicalAddressBook(), + new UserPrefs(), getTypicalListings()); + showListingAtIndex(expectedModel, INDEX_FIRST_LISTING); - Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); Listing editedListing = new ListingBuilder(VALID_LISTING).withBuyers(DANIEL, ELLE).build(); expectedModel.setListing(VALID_LISTING, editedListing); - showListingAtIndex(model, INDEX_FIRST_LISTING); assertCommandSuccess(addBuyersToListingCommand, model, expectedMessage,expectedModel); } @@ -126,7 +130,7 @@ public void execute_listingIndexOutOfBoundsUnfilteredListingList_throwsCommandEx public void execute_listingIndexOutOfBoundsFilteredListingList_throwsCommandException() { showListingAtIndex(model, INDEX_SECOND_LISTING); AddBuyersToListingCommand addBuyersToListingCommand = - new AddBuyersToListingCommand(INDEX_FIRST_LISTING, VALID_BUYER_INDEXES); + new AddBuyersToListingCommand(INDEX_THIRD_LISTING, VALID_BUYER_INDEXES); assertCommandFailure(addBuyersToListingCommand, model, Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } @@ -135,7 +139,8 @@ public void execute_inputSeller_throwsCommandException() { AddBuyersToListingCommand addBuyersToListingCommand = new AddBuyersToListingCommand(INDEX_FIRST_LISTING, SELLER_INDEX); assertCommandFailure(addBuyersToListingCommand, model, - String.format(AddBuyersToListingCommand.MESSAGE_PERSON_NOT_BUYER, SELLER_NAME)); + String.format(AddBuyersToListingCommand.MESSAGE_PERSON_NOT_BUYER, + INDEX_FIRST_PERSON.getOneBased(), SELLER_NAME)); } @Test diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java index 4fecc7b5432..a272ec1c9da 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/AddListingCommandTest.java @@ -10,6 +10,7 @@ import static seedu.address.testutil.TypicalIndexes.INDEX_FOURTH_PERSON; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; import static seedu.address.testutil.TypicalIndexes.INDEX_SIXTH_PERSON; +import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_PERSON; import static seedu.address.testutil.TypicalListings.PASIR_RIS; import static seedu.address.testutil.TypicalListings.TAMPINES; import static seedu.address.testutil.TypicalPersons.ALICE; @@ -84,12 +85,12 @@ public void constructor_nullSeller_throwsNullPointerException() { } @Test - public void constructor_nullBuyers_success() throws CommandException { + public void constructor_noBuyers_success() throws CommandException { ModelStubAcceptingListingAdded modelStub = new ModelStubAcceptingListingAdded(); modelStub.addPerson(ALICE); CommandResult commandResult = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, - null).execute(modelStub); + new HashSet<>()).execute(modelStub); Listing listingWithNoBuyers = new ListingBuilder(PASIR_RIS).withBuyers().build(); assertEquals(String.format(AddListingCommand.MESSAGE_SUCCESS, Messages.format(listingWithNoBuyers)), @@ -106,7 +107,7 @@ public void execute_listingAcceptedByModel_addSuccessful() throws CommandExcepti CommandResult commandResult = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, - new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))).execute(modelStub); + new HashSet<>(List.of(INDEX_SECOND_PERSON, INDEX_THIRD_PERSON))).execute(modelStub); assertEquals(String.format(AddListingCommand.MESSAGE_SUCCESS, Messages.format(PASIR_RIS)), commandResult.getFeedbackToUser()); assertEquals(Arrays.asList(PASIR_RIS), modelStub.listingsAdded); @@ -116,7 +117,7 @@ public void execute_listingAcceptedByModel_addSuccessful() throws CommandExcepti public void execute_duplicateListing_throwsCommandException() { AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, - new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); + new HashSet<>(List.of(INDEX_SECOND_PERSON, INDEX_THIRD_PERSON))); ModelStub modelStub = new ModelStubWithListing(PASIR_RIS); modelStub.addPerson(ALICE); modelStub.addPerson(DANIEL); @@ -129,15 +130,16 @@ public void execute_duplicateListing_throwsCommandException() { @Test public void execute_buyerAsSeller_throwsCommandException() { AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIFTH_PERSON, - new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_SECOND_PERSON, INDEX_THIRD_PERSON))); ModelStub modelStub = new ModelStubWithListing(PASIR_RIS); modelStub.addPerson(FIONA); modelStub.addPerson(DANIEL); modelStub.addPerson(GEORGE); assertThrows(CommandException.class, - AddListingCommand.MESSAGE_NOT_SELLER, () -> addListingCommand.execute(modelStub)); + String.format(AddListingCommand.MESSAGE_NOT_SELLER, INDEX_FIRST_PERSON.getOneBased(), FIONA.getName()), + () -> addListingCommand.execute(modelStub)); } @Test @@ -150,21 +152,22 @@ public void execute_sellerDoesNotExist_throwsCommandException() { modelStub.addPerson(GEORGE); assertThrows(CommandException.class, - Messages.MESSAGE_INVALID_PERSON_INPUT, () -> addListingCommand.execute(modelStub)); + AddListingCommand.MESSAGE_INVALID_SELLER_INDEX, () -> addListingCommand.execute(modelStub)); } @Test public void execute_buyerDoesNotExist_throwsCommandException() { AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIFTH_PERSON, - new HashSet<>(List.of(INDEX_FOURTH_PERSON, INDEX_SIXTH_PERSON))); + PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, + new HashSet<>(List.of(INDEX_SECOND_PERSON, INDEX_THIRD_PERSON))); ModelStub modelStub = new ModelStubWithListing(PASIR_RIS); modelStub.addPerson(ALICE); - // DANIEL does not exist (FOURTH PERSON) + // DANIEL does not exist (THIRD PERSON) modelStub.addPerson(GEORGE); assertThrows(CommandException.class, - Messages.MESSAGE_INVALID_PERSON_INPUT, () -> addListingCommand.execute(modelStub)); + String.format(AddListingCommand.MESSAGE_INVALID_BUYER_INDEX, INDEX_THIRD_PERSON.getOneBased()), + () -> addListingCommand.execute(modelStub)); } // equals and toString methods @@ -236,22 +239,10 @@ public void toStringMethod() { String expected = AddListingCommand.class.getCanonicalName() + "{toAdd=" + PASIR_RIS.getName() + ", address=" + PASIR_RIS.getAddress() - + ", seller=" + ALICE.getName() + "}"; assertEquals(expected, addListingCommand.toString()); } - @Test - public void execute_buyerNotInList_throwsCommandException() { - AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), - PASIR_RIS.getArea(), PASIR_RIS.getAddress(), PASIR_RIS.getRegion(), INDEX_FIRST_PERSON, - new HashSet<>(List.of(INDEX_FOURTH_PERSON))); - ModelStub modelStub = new ModelStubAcceptingListingAdded(); - modelStub.addPerson(ALICE); - assertThrows(CommandException.class, - Messages.MESSAGE_INVALID_PERSON_INPUT, () -> addListingCommand.execute(modelStub)); - } - @Test public void execute_buyerNotOfTypeBuyer_throwsCommandException() { AddListingCommand addListingCommand = new AddListingCommand(PASIR_RIS.getName(), PASIR_RIS.getPrice(), @@ -262,7 +253,8 @@ public void execute_buyerNotOfTypeBuyer_throwsCommandException() { modelStub.addPerson(BENSON); assertThrows(CommandException.class, - AddListingCommand.MESSAGE_NOT_BUYER, () -> addListingCommand.execute(modelStub)); + String.format(AddListingCommand.MESSAGE_NOT_BUYER, INDEX_SECOND_PERSON.getOneBased(), BENSON.getName()), + () -> addListingCommand.execute(modelStub)); } /** diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/DeleteListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/DeleteListingCommandTest.java index 4adcc6fd369..772df563fbf 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/DeleteListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/DeleteListingCommandTest.java @@ -4,11 +4,13 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; +import static seedu.address.logic.commands.CommandTestUtil.showListingAtIndex; import static seedu.address.testutil.Assert.assertThrows; import static seedu.address.testutil.TypicalIndexes.*; import org.junit.jupiter.api.Test; +import seedu.address.logic.Messages; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.AddressBook; import seedu.address.model.Listings; @@ -29,7 +31,7 @@ public void execute_validListingUnfilteredList_success() { DeleteListingCommand deleteListingCommand = new DeleteListingCommand(INDEX_FIRST_LISTING); String expectedMessage = String.format(DeleteListingCommand.MESSAGE_DELETE_LISTING_SUCCESS, - listingToDelete.getName()); + Messages.format(listingToDelete)); Model expectedModel = new ModelManager(new AddressBook(), new UserPrefs(), model.getListings()); expectedModel.deleteListing(listingToDelete); @@ -38,10 +40,17 @@ public void execute_validListingUnfilteredList_success() { } @Test - public void execute_invalidListingUnfilteredList_throwsCommandException() { + public void execute_listingIndexOutOfBoundsUnfilteredListingList_throwsCommandException() { DeleteListingCommand deleteListingCommand = new DeleteListingCommand(LISTING_INDEX_OUT_OF_BOUNDS); - assertCommandFailure(deleteListingCommand, model, DeleteListingCommand.MESSAGE_LISTING_NOT_FOUND); + assertCommandFailure(deleteListingCommand, model, Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); + } + + @Test + public void execute_listingIndexOutOfBoundsFilteredListingList_throwsCommandException() { + showListingAtIndex(model, INDEX_FIRST_LISTING); + assertThrows(CommandException.class, Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX, () -> + new DeleteListingCommand(INDEX_SECOND_LISTING).execute(model)); } @Test @@ -50,7 +59,7 @@ public void execute_validListingFilteredList_success() { DeleteListingCommand deleteListingCommand = new DeleteListingCommand(INDEX_SECOND_LISTING); String expectedMessage = String.format(DeleteListingCommand.MESSAGE_DELETE_LISTING_SUCCESS, - listingToDelete.getName()); + Messages.format(listingToDelete)); Model expectedModel = new ModelManager(new AddressBook(), new UserPrefs(), model.getListings()); expectedModel.deleteListing(listingToDelete); @@ -58,12 +67,6 @@ public void execute_validListingFilteredList_success() { assertCommandSuccess(deleteListingCommand, model, expectedMessage, expectedModel); } - @Test - public void execute_invalidListingFilteredList_throwsCommandException() { - assertThrows(CommandException.class, DeleteListingCommand.MESSAGE_LISTING_NOT_FOUND, () -> - new DeleteListingCommand(LISTING_INDEX_OUT_OF_BOUNDS).execute(model)); - } - @Test public void equals() { DeleteListingCommand deleteFirstCommand = new DeleteListingCommand(INDEX_FIRST_LISTING); diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/EditListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/EditListingCommandTest.java index be0ff839a58..f68a0b0fe24 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/EditListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/EditListingCommandTest.java @@ -10,6 +10,7 @@ import static seedu.address.logic.commands.CommandTestUtil.showListingWithName; import static seedu.address.testutil.TypicalIndexes.INDEX_FIFTH_LISTING; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_LISTING; +import static seedu.address.testutil.TypicalIndexes.INDEX_FOURTH_LISTING; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_LISTING; import static seedu.address.testutil.TypicalIndexes.LISTING_INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalListings.PASIR_RIS; @@ -24,18 +25,15 @@ import seedu.address.logic.Messages; import seedu.address.logic.commands.ClearCommand; -import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.logic.commands.listingcommands.EditListingCommand.EditListingDescriptor; import seedu.address.model.Listings; import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.UserPrefs; import seedu.address.model.listing.Listing; -import seedu.address.model.person.Name; import seedu.address.model.person.Person; import seedu.address.testutil.EditListingDescriptorBuilder; import seedu.address.testutil.ListingBuilder; -import seedu.address.testutil.PersonBuilder; public class EditListingCommandTest { private static final Listing FIRST_LISTING = PASIR_RIS; @@ -55,6 +53,7 @@ public void execute_allFieldsSpecifiedUnfilteredList_success() { Model expectedModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), new Listings(model.getListings())); + expectedModel.setListing(model.getListingByName(SENGKANG.getName()), editedListing); assertCommandSuccess(editListingCommand, model, expectedMessage, expectedModel); @@ -135,17 +134,17 @@ public void execute_listingIndexOutOfBoundsUnfilteredList_failure() { EditListingDescriptor descriptor = new EditListingDescriptorBuilder().build(); EditListingCommand editListingCommand = new EditListingCommand(LISTING_INDEX_OUT_OF_BOUNDS, descriptor); - assertCommandFailure(editListingCommand, model, EditListingCommand.MESSAGE_INVALID_LISTING_NAME); + assertCommandFailure(editListingCommand, model, Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } @Test - public void execute_invalidListingFilteredList_failure() { - showListingAtIndex(model, INDEX_SECOND_LISTING); + public void execute_listingIndexOutOfBoundsFilteredListingList_failure() { + showListingAtIndex(model, INDEX_FIRST_LISTING); EditListingDescriptor descriptor = new EditListingDescriptorBuilder().build(); - EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIRST_LISTING, descriptor); + EditListingCommand editListingCommand = new EditListingCommand(INDEX_SECOND_LISTING, descriptor); - assertCommandFailure(editListingCommand, model, EditListingCommand.MESSAGE_INVALID_LISTING_NAME); + assertCommandFailure(editListingCommand, model, Messages.MESSAGE_INVALID_LISTING_DISPLAYED_INDEX); } @Test @@ -184,33 +183,8 @@ public void equals() { public void toStringMethod() { EditListingDescriptor editListingDescriptor = new EditListingDescriptor(); EditListingCommand editListingCommand = new EditListingCommand(INDEX_FIRST_LISTING, editListingDescriptor); - String expected = EditListingCommand.class.getCanonicalName() + "{listingName=" + PASIR_RIS.getName() + String expected = EditListingCommand.class.getCanonicalName() + "{listingIndex=" + INDEX_FIRST_LISTING + ", editListingDescriptor=" + editListingDescriptor + "}"; assertEquals(expected, editListingCommand.toString()); } - @Test - public void execute_nonSellerSpecified_throwsCommandException() { - Model model = new ModelManager(); - Person nonSellerPerson = new PersonBuilder().withName("NonSeller").buildSeller(); - model.addPerson(nonSellerPerson); - - EditListingCommand.EditListingDescriptor descriptor = new EditListingDescriptorBuilder() - .withName("NonSeller").build(); - EditListingCommand command = new EditListingCommand(new Name("SampleListing"), descriptor); - - assertThrows(CommandException.class, () -> command.execute(model), - "The specified person is not a seller."); - } - - @Test - public void execute_sellerNotFound_throwsCommandException() { - Model model = new ModelManager(); - - EditListingCommand.EditListingDescriptor descriptor = new EditListingDescriptorBuilder() - .withName("MissingSeller").build(); - EditListingCommand command = new EditListingCommand(new Name("SampleListing"), descriptor); - - assertThrows(CommandException.class, () -> command.execute(model), - "Seller not found in the system."); - } } diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/EditListingDescriptorTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/EditListingDescriptorTest.java index 918f361292f..d70d5a120fd 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/EditListingDescriptorTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/EditListingDescriptorTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; import static seedu.address.testutil.TypicalListings.PASIR_RIS; import static seedu.address.testutil.TypicalListings.TAMPINES; import static seedu.address.testutil.TypicalPersons.BENSON; diff --git a/src/test/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommandTest.java b/src/test/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommandTest.java index f9a95c6da0f..76cbf38140a 100644 --- a/src/test/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/listingcommands/RemoveBuyersFromListingCommandTest.java @@ -17,6 +17,7 @@ import static seedu.address.testutil.TypicalIndexes.PERSON_INDEX_OUT_OF_BOUNDS; import static seedu.address.testutil.TypicalListings.PASIR_RIS; import static seedu.address.testutil.TypicalListings.getTypicalListings; +import static seedu.address.testutil.TypicalPersons.ALICE; import static seedu.address.testutil.TypicalPersons.DANIEL; import static seedu.address.testutil.TypicalPersons.ELLE; import static seedu.address.testutil.TypicalPersons.GEORGE; @@ -42,6 +43,7 @@ public class RemoveBuyersFromListingCommandTest { private static final Name FOURTH_BUYER_NAME = DANIEL.getName(); private static final Set BUYER_INDEXES_OUT_OF_BOUNDS = Set.of(PERSON_INDEX_OUT_OF_BOUNDS); private static final Set SELLER_INDEX = Set.of(INDEX_FIRST_LISTING); + private static final Name SELLER_NAME = ALICE.getName(); private static final Set EMPTY_SET = new HashSet<>(); private static final Set NOT_BUYER_OF_LISTING = Set.of(INDEX_FIFTH_PERSON); private static final Listing VALID_LISTING = PASIR_RIS; @@ -111,8 +113,10 @@ public void execute_listingIndexOutOfBoundsFilteredListingList_throwsCommandExce public void execute_inputSeller_throwsCommandException() { RemoveBuyersFromListingCommand removeBuyersFromListingCommand = new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, SELLER_INDEX); + System.out.println(INDEX_FIRST_LISTING.getOneBased()); + System.out.println(SELLER_NAME); assertCommandFailure(removeBuyersFromListingCommand, model, - String.format(RemoveBuyersFromListingCommand.MESSAGE_PERSON_NOT_BUYER, SELLER_INDEX)); + String.format(RemoveBuyersFromListingCommand.MESSAGE_PERSON_NOT_BUYER, INDEX_FIRST_LISTING.getOneBased(), SELLER_NAME)); } @Test @@ -121,7 +125,7 @@ public void execute_notBuyerOfListing_throwsCommandException() { new RemoveBuyersFromListingCommand(INDEX_FIRST_LISTING, NOT_BUYER_OF_LISTING); assertCommandFailure(removeBuyersFromListingCommand, model, String.format(RemoveBuyersFromListingCommand.MESSAGE_NOT_BUYER_FOR_LISTING, - ELLE.getName(), VALID_LISTING_NAME)); + ELLE.getName(), Messages.format(VALID_LISTING))); } @Test diff --git a/src/test/java/seedu/address/logic/parser/EzstatesParserTest.java b/src/test/java/seedu/address/logic/parser/EzstatesParserTest.java index fe7cdf4a6c5..7bbf025139e 100644 --- a/src/test/java/seedu/address/logic/parser/EzstatesParserTest.java +++ b/src/test/java/seedu/address/logic/parser/EzstatesParserTest.java @@ -180,20 +180,20 @@ public void parseCommand_addListing() throws Exception { + "address/543 Pasir Ris Street 11 " + "p/2000000 " + "region/EAST " - + "seller/ Benson"); + + "seller/ 2"); assertEquals(AddListingCommand.class, command.getClass()); } @Test public void parseCommand_editListing() throws Exception { Command command = - parser.parseCommand("editlisting Kent Ridge Condo n/Kent Ridge HDB"); + parser.parseCommand("editlisting 1 n/Kent Ridge HDB"); assertEquals(EditListingCommand.class, command.getClass()); } @Test public void parseCommand_deleteListing() throws Exception { - Command command = parser.parseCommand("deletelisting Wartion House"); + Command command = parser.parseCommand("deletelisting 1"); assertEquals(DeleteListingCommand.class, command.getClass()); } @@ -212,14 +212,14 @@ public void parseCommand_findListing() throws Exception { @Test public void parseCommand_addBuyersToListing() throws Exception { Command command = - parser.parseCommand("addlistingbuyers Warton House buyer/John Doe"); + parser.parseCommand("addlistingbuyers 1 buyer/2"); assertEquals(AddBuyersToListingCommand.class, command.getClass()); } @Test public void parseCommand_removeBuyersFromListing() throws Exception { Command command = - parser.parseCommand("removelistingbuyers Warton House buyer/John Doe"); + parser.parseCommand("removelistingbuyers 1 buyer/2"); assertEquals(RemoveBuyersFromListingCommand.class, command.getClass()); } diff --git a/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParserTest.java b/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParserTest.java index c53af687e00..163da087e55 100644 --- a/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddBuyersToListingCommandParserTest.java @@ -34,7 +34,7 @@ public void parse_allFieldsPresent_success() throws Exception { AddBuyersToListingCommand expectedCommand = new AddBuyersToListingCommand(Index.fromOneBased(Integer.parseInt(VALID_LISTING_INDEX)), expectedBuyerIndexes); - + System.out.println(expectedCommand); AddBuyersToListingCommand result = parser.parse(userInput); assertEquals(expectedCommand, result); } diff --git a/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParserTest.java b/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParserTest.java index 5fb73e217d9..8f343f6ffc2 100644 --- a/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/listingcommandparsers/AddListingCommandParserTest.java @@ -28,7 +28,9 @@ import org.junit.jupiter.api.Test; import seedu.address.commons.core.index.Index; +import seedu.address.logic.Messages; import seedu.address.logic.commands.listingcommands.AddListingCommand; +import seedu.address.logic.parser.ParserUtil; import seedu.address.model.listing.Address; import seedu.address.model.listing.Area; import seedu.address.model.listing.Price; @@ -59,13 +61,13 @@ public void parse_missingField_throwsParseException() { String userInput = NAME_DESC_PASIR_RIS + PRICE_DESC_PASIR_RIS + AREA_DESC_PASIR_RIS + ADDRESS_DESC_PASIR_RIS + REGION_DESC_PASIR_RIS + SELLER_DESC_PASIR_RIS + " " + PREFIX_BUYER; - assertParseFailure(parser, userInput, Name.MESSAGE_CONSTRAINTS); + assertParseFailure(parser, userInput, ParserUtil.MESSAGE_INVALID_INDEX); // missing seller userInput = NAME_DESC_PASIR_RIS + PRICE_DESC_PASIR_RIS + AREA_DESC_PASIR_RIS + ADDRESS_DESC_PASIR_RIS + REGION_DESC_PASIR_RIS + " " + PREFIX_SELLER + " " + BUYER_DESC_PASIR_RIS; - assertParseFailure(parser, userInput, Name.MESSAGE_CONSTRAINTS); + assertParseFailure(parser, userInput, ParserUtil.MESSAGE_INVALID_INDEX); } @Test diff --git a/src/test/java/seedu/address/logic/parser/listingcommandparsers/DeletingListingCommandParserTest.java b/src/test/java/seedu/address/logic/parser/listingcommandparsers/DeletingListingCommandParserTest.java index e036b33a969..d5bcf5e4d4a 100644 --- a/src/test/java/seedu/address/logic/parser/listingcommandparsers/DeletingListingCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/listingcommandparsers/DeletingListingCommandParserTest.java @@ -4,7 +4,6 @@ import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_LISTING; -import static seedu.address.testutil.TypicalListings.KENT_RIDGE; import org.junit.jupiter.api.Test; @@ -18,13 +17,6 @@ public void parse_validArgs_returnsDeleteCommand() { assertParseSuccess(parser, "1", new DeleteListingCommand(INDEX_FIRST_LISTING)); } - @Test - public void parse_outOfBoundsIndex_throwsParseException() { - // Test an index out of bounds (example: 100) - assertParseFailure(parser, "100", String.format(MESSAGE_INVALID_COMMAND_FORMAT, - DeleteListingCommand.MESSAGE_USAGE)); - } - @Test public void parse_missingField_throwsParseException() { assertParseFailure(parser, "", String.format(MESSAGE_INVALID_COMMAND_FORMAT, diff --git a/src/test/java/seedu/address/testutil/EditListingDescriptorBuilder.java b/src/test/java/seedu/address/testutil/EditListingDescriptorBuilder.java index e39bd114d74..0aa4f8163ef 100644 --- a/src/test/java/seedu/address/testutil/EditListingDescriptorBuilder.java +++ b/src/test/java/seedu/address/testutil/EditListingDescriptorBuilder.java @@ -1,8 +1,15 @@ package seedu.address.testutil; import java.math.BigDecimal; +import java.util.List; +import java.util.OptionalInt; +import java.util.stream.IntStream; +import seedu.address.commons.core.index.Index; import seedu.address.logic.commands.listingcommands.EditListingCommand.EditListingDescriptor; +import seedu.address.model.Model; +import seedu.address.model.ModelManager; +import seedu.address.model.UserPrefs; import seedu.address.model.listing.Address; import seedu.address.model.listing.Area; import seedu.address.model.listing.Listing; @@ -11,12 +18,16 @@ import seedu.address.model.person.Name; import seedu.address.model.person.Person; +import static seedu.address.testutil.TypicalListings.getTypicalListings; +import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; + /** * A utility class to help with building EditListingDescriptor objects. * Used ChatGPT to assist in writing JavaDocs */ public class EditListingDescriptorBuilder { private EditListingDescriptor descriptor; + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalListings()); /** * Creates a new {@code EditListingDescriptorBuilder} with an empty {@code EditListingDescriptor}. @@ -44,7 +55,8 @@ public EditListingDescriptorBuilder(Listing listing) { descriptor.setAddress(listing.getAddress()); descriptor.setArea(listing.getArea()); descriptor.setRegion(listing.getRegion()); - descriptor.setSellerName(listing.getSeller().getName()); + System.out.println(findSellerIndexByName(listing.getSeller().getName())); + descriptor.setSellerIndex(Index.fromZeroBased(findSellerIndexByName(listing.getSeller().getName()))); } /** @@ -164,7 +176,7 @@ public EditListingDescriptorBuilder withRegion(Region region) { * @return This EditListingDescriptorBuilder object for method chaining. */ public EditListingDescriptorBuilder withSeller(Name sellerName) { - descriptor.setSellerName(sellerName); + descriptor.setSellerIndex(Index.fromZeroBased(findSellerIndexByName(sellerName))); return this; } @@ -175,10 +187,18 @@ public EditListingDescriptorBuilder withSeller(Name sellerName) { * @return This EditListingDescriptorBuilder object for method chaining. */ public EditListingDescriptorBuilder withSeller(Person seller) { - descriptor.setSellerName(seller.getName()); + descriptor.setSellerIndex(Index.fromZeroBased(findSellerIndexByName(seller.getName()))); return this; } + private int findSellerIndexByName(Name sellerName) { + List filteredList = model.getFilteredPersonList(); + + return IntStream.range(0, filteredList.size()) + .filter(x -> filteredList.get(x).getName().equals(sellerName)) + .findFirst().orElse(-1); + } + /** * Builds and returns the {@code EditListingDescriptor}. *