Skip to content

Commit

Permalink
Merge pull request #275 from lrongyi/changeToIndex
Browse files Browse the repository at this point in the history
Revert the Listing Commands back to using Index
  • Loading branch information
wang-h-z authored Nov 8, 2024
2 parents acb7c2d + b3863c9 commit 6987a39
Show file tree
Hide file tree
Showing 28 changed files with 671 additions and 572 deletions.
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/commons/core/index/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ public String toStringOneBased() {
public String toStringZeroBased() {
return String.format("%d", getZeroBased());
}

@Override
public int hashCode() {
return Integer.hashCode(zeroBasedIndex);
}
}
6 changes: 4 additions & 2 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
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;
import seedu.address.model.Model;
import seedu.address.model.listing.Listing;
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.
Expand All @@ -21,66 +23,69 @@ 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. "
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds buyers to the listing identified by its index. "
+ "Parameters: LISTING_NAME buy/BUYER_NAME [buy/MORE_BUYER_NAMES]...\n"
+ "Example: " + COMMAND_WORD + " Warton House buy/Alice buyer/Bob";
+ "Example: " + COMMAND_WORD + " 1 buy/1 buy/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<Name> buyersToAdd;
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<Index> 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<Name> buyersToAdd) {
requireNonNull(listingName);
public AddBuyersToListingCommand(Index index, Set<Index> buyersToAdd) {
requireNonNull(index);
requireNonNull(buyersToAdd);

this.listingName = listingName;
this.index = index;
this.buyersToAdd = new HashSet<>(buyersToAdd);
}

@Override
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<Listing> 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<Person> existingBuyers = new HashSet<>(listingToEdit.getBuyers());
Set<Person> newBuyers = new HashSet<>();
List<Person> 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();
int oneBasedBuyer = buyerIndex.getOneBased();
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,
oneBasedBuyer, 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);
Expand All @@ -96,7 +101,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
Expand All @@ -110,7 +115,7 @@ public boolean equals(Object other) {
}

AddBuyersToListingCommand otherCommand = (AddBuyersToListingCommand) other;
return listingName.equals(otherCommand.listingName)
return index.equals(otherCommand.index)
&& buyersToAdd.equals(otherCommand.buyersToAdd);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -26,10 +26,9 @@
import seedu.address.model.listing.Listing;
import seedu.address.model.listing.Price;
import seedu.address.model.listing.Region;
import seedu.address.model.person.Buyer;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.Seller;
import seedu.address.model.person.Role;

/**
* Adds a listing to the address book.
Expand All @@ -45,74 +44,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<Name> buyers;
private final Set<Index> 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<Name> buyers) {
requireAllNonNull(listingName, price, area, address, region, seller);
Index sellerIndex, Set<Index> 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<Person> 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<Person> 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);
Expand Down Expand Up @@ -146,16 +155,15 @@ 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
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", this.listingName)
.add("address", this.address)
.add("seller", this.seller)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,32 +20,33 @@ 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
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

List<Listing> 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
Expand All @@ -58,6 +60,6 @@ public boolean equals(Object other) {
}

DeleteListingCommand otherCommand = (DeleteListingCommand) other;
return targetName.equals(otherCommand.targetName);
return targetIndex.equals(otherCommand.targetIndex);
}
}
Loading

0 comments on commit 6987a39

Please sign in to comment.