Skip to content

Commit

Permalink
Merge pull request #106 from beatricecst/branch-itemPrice
Browse files Browse the repository at this point in the history
Created price class and edited add command to include price
  • Loading branch information
samuelmui8 authored Oct 26, 2023
2 parents 2e00fa2 + feaa674 commit c268c91
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 31 deletions.
25 changes: 24 additions & 1 deletion src/main/java/seedu/address/commons/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static boolean containsWordIgnoreCase(String sentence, String word) {
String[] wordsInPreppedSentence = preppedSentence.split("\\s+");

return Arrays.stream(wordsInPreppedSentence)
.anyMatch(preppedWord::equalsIgnoreCase);
.anyMatch(preppedWord::equalsIgnoreCase);
}

/**
Expand Down Expand Up @@ -65,4 +65,27 @@ public static boolean isNonZeroUnsignedInteger(String s) {
return false;
}
}

/**
* Returns true if {@code s} represents a non-zero unsigned double
* e.g. 1, 2, 3, ..., {@code Double.MAX_VALUE} <br>
* Will return false for any other non-null string input
* e.g. empty string, "-1", "0", "+1", and " 2 " (untrimmed), "3 0" (contains whitespace), "1 a" (contains letters)
* @throws NullPointerException if {@code s} is null.
*/
public static boolean isNonZeroUnsignedDouble(String s) {
requireNonNull(s);

try {
// Check if the value has exactly two decimal places
String[] parts = s.split("\\.");
if (parts.length != 2 || parts[1].length() != 2) {
return false;
}
double value = Double.parseDouble(s);
return value > 0 && !s.startsWith("+"); // "+1" is successfully parsed by Integer#parseInt(String)
} catch (NumberFormatException nfe) {
return false;
}
}
}
23 changes: 14 additions & 9 deletions src/main/java/seedu/address/logic/commands/AddItemCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ITEM;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRICE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STALL;

import java.util.List;
Expand All @@ -12,6 +13,7 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.item.Item;
import seedu.address.model.item.Price;
import seedu.address.model.stall.Stall;

/**
Expand All @@ -22,25 +24,28 @@ public class AddItemCommand extends Command {
public static final String COMMAND_WORD = "add-item";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Adds an item to the address book. "
+ "Parameters: "
+ PREFIX_STALL + "STALL_INDEX "
+ PREFIX_ITEM + "ITEM_NAME \n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_STALL + "1 "
+ PREFIX_ITEM + "Chicken Rice";
+ "Parameters: "
+ PREFIX_STALL + "STALL_INDEX "
+ PREFIX_ITEM + "ITEM_NAME \n"
+ "Example: " + COMMAND_WORD + " "
+ PREFIX_STALL + "1 "
+ PREFIX_ITEM + "Chicken Rice"
+ PREFIX_PRICE + "5.50 ";

public static final String MESSAGE_SUCCESS = "New Item added: %1$s";
public static final String MESSAGE_DUPLICATE_ITEM = "This Item already exists in this Stall";
private final Item toAdd;
private final Index stallIndex;
private final Price price;

/**
* Creates an AddItemCommand to add the specified {@code Item}
*/
public AddItemCommand(Index stallIndex, Item item) {
public AddItemCommand(Index stallIndex, Item item, Price price) {
requireNonNull(item);
this.toAdd = item;
this.stallIndex = stallIndex;
this.price = price;
}

@Override
Expand Down Expand Up @@ -80,7 +85,7 @@ public boolean equals(Object other) {
@Override
public String toString() {
return new ToStringBuilder(this)
.add("toAdd", toAdd)
.toString();
.add("toAdd", toAdd)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ITEM;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRICE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STALL;

import java.util.stream.Stream;
Expand All @@ -11,6 +12,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.item.Item;
import seedu.address.model.item.ItemName;
import seedu.address.model.item.Price;


/**
Expand All @@ -25,20 +27,21 @@ public class AddItemCommandParser implements Parser<AddItemCommand> {
*/
public AddItemCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_STALL, PREFIX_ITEM);
ArgumentTokenizer.tokenize(args, PREFIX_STALL, PREFIX_ITEM, PREFIX_PRICE);

if (!arePrefixesPresent(argMultimap, PREFIX_STALL, PREFIX_ITEM)
|| !argMultimap.getPreamble().isEmpty()) {
if (!arePrefixesPresent(argMultimap, PREFIX_STALL, PREFIX_ITEM, PREFIX_PRICE)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddItemCommand.MESSAGE_USAGE));
}

argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_STALL, PREFIX_ITEM);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_STALL, PREFIX_ITEM, PREFIX_PRICE);
Index stallIndex = ParserUtil.parseStallIndex(argMultimap.getValue(PREFIX_STALL).get());
ItemName itemName = ParserUtil.parseItemName(argMultimap.getValue(PREFIX_ITEM).get());
Price price = ParserUtil.parsePrice(argMultimap.getValue(PREFIX_PRICE).get());

Item item = new Item(itemName);

return new AddItemCommand(stallIndex, item);
return new AddItemCommand(stallIndex, item, price);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class CliSyntax {
public static final Prefix PREFIX_DESCRIPTION = new Prefix("d/");
public static final Prefix PREFIX_ITEM = new Prefix("i/");
public static final Prefix PREFIX_STALL = new Prefix("s/");
public static final Prefix PREFIX_PRICE = new Prefix("p/");

}
21 changes: 21 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import seedu.address.commons.util.StringUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.item.ItemName;
import seedu.address.model.item.Price;
import seedu.address.model.review.Description;
import seedu.address.model.review.Rating;
import seedu.address.model.stall.Location;
Expand All @@ -22,6 +23,9 @@ public class ParserUtil {
public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer.";
public static final String MESSAGE_INVALID_STALL_INDEX = "Stall index is not a non-zero unsigned integer.";
public static final String MESSAGE_INVALID_ITEM_INDEX = "Item index is not a non-zero unsigned integer.";
public static final String MESSAGE_INVALID_ITEM_PRICE = "Item price is not a non-zero "
+ "unsigned double with 2 decimal places.";


/**
* Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be
Expand Down Expand Up @@ -106,6 +110,23 @@ public static Index parseStallIndex(String oneBasedIndex) throws ParseException
return Index.fromOneBased(Integer.parseInt(trimmedIndex));
}

/**
* Parses a {@code String stallIndex} into an {@code Index}.
* Leading and trailing whitespaces will be trimmed.
*
* @param price The price of the item.
* @return The parsed price.
* @throws ParseException if the given {@code price} is invalid.
*/
public static Price parsePrice(String price) throws ParseException {
String trimmedPrice = price.trim();
double value = Double.parseDouble(trimmedPrice);
if (!StringUtil.isNonZeroUnsignedDouble(trimmedPrice)) {
throw new ParseException(MESSAGE_INVALID_ITEM_PRICE);
}
return new Price(value);
}

/**
* Parses a {@code String itemIndex} into an {@code Index}.
* Leading and trailing whitespaces will be trimmed.
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/model/item/Price.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.address.model.item;

import static java.util.Objects.requireNonNull;

/**
* Represent an item's price.
*/
public class Price {
public static final String MESSAGE_CONSTRAINTS =
"Price should not be blank";

private double price;

/**
* Constructs a {@code Price}.
*
* @param price A valid name.
*/
public Price(double price) {
requireNonNull(price);
this.price = price;
}

@Override
public String toString() {
return String.valueOf(price);
}

}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/ui/StallDetailsCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class StallDetailsCard extends UiPart<Region> {
private Label menuItem;

/**
* Creates a {@code PersonCode} with the given {@code Stall} and index to display.
* Creates a {@code StallDetailsCard} with the given {@code Stall} and index to display.
*/
public StallDetailsCard(Stall stall, int displayedIndex) {
super(FXML);
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/seedu/address/commons/util/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public void isNonZeroUnsignedInteger() {
assertTrue(StringUtil.isNonZeroUnsignedInteger("10"));
}

@Test
public void isNonZeroUnsignedDouble() {
assertTrue(StringUtil.isNonZeroUnsignedDouble("12.34"));
assertFalse(StringUtil.isNonZeroUnsignedDouble("12.345"));
assertFalse(StringUtil.isNonZeroUnsignedDouble("invalid"));
}


//---------------- Tests for containsWordIgnoreCase --------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_ITEM;
import static seedu.address.logic.parser.CliSyntax.PREFIX_LOCATION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PRICE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_STALL;
import static seedu.address.testutil.Assert.assertThrows;

Expand Down Expand Up @@ -41,16 +42,21 @@ public class CommandTestUtil {
public static final String VALID_STALL_INDEX_1 = "1";
public static final String VALID_ITEM_NAME_NASI_LEMAK = "Nasi Lemak";
public static final String VALID_ITEM_NAME_CHICKEN_RICE = "Chicken Rice";
public static final double VALID_ITEM_PRICE = 5.50;
public static final double INVALID_ITEM_PRICE = 5.555;

// item index 1 is valid
public static final String VALID_STALL_INDEX_DESC = " " + PREFIX_STALL + VALID_STALL_INDEX_1;
// item index 1 is valid
public static final String VALID_ITEM_INDEX_DESC = " " + PREFIX_ITEM + VALID_STALL_INDEX_1;
public static final String VALID_PRICE_DESC = " " + PREFIX_PRICE + VALID_ITEM_PRICE;
public static final String ITEM_DESC_NASI_LEMAK = " " + PREFIX_ITEM + VALID_ITEM_NAME_NASI_LEMAK;
public static final String ITEM_DESC_CHICKEN_RICE = " " + PREFIX_ITEM + VALID_ITEM_NAME_CHICKEN_RICE;
public static final String INVALID_ITEM_NAME_DESC = " " + PREFIX_ITEM + " ";
public static final String INVALID_STALL_INDEX_DESC = " " + PREFIX_STALL + " ";
public static final String INVALID_ITEM_INDEX_DESC = " " + PREFIX_ITEM + " ";
public static final String INVALID_ITEM_INDEX_DESC_NEGATIVE = " " + PREFIX_ITEM + "-1";
public static final String INVALID_ITEM_PRICE_DESC = " " + PREFIX_PRICE + INVALID_ITEM_PRICE;

//Others
public static final String PREAMBLE_WHITESPACE = "\t \r \n";
Expand Down
Loading

0 comments on commit c268c91

Please sign in to comment.