Skip to content

Commit

Permalink
Merge pull request #132 from yican95/feature/discount-item
Browse files Browse the repository at this point in the history
v1.3 discount-item
  • Loading branch information
azhikai authored Oct 24, 2018
2 parents 17644eb + baadc90 commit a6a6a17
Show file tree
Hide file tree
Showing 24 changed files with 513 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import seedu.address.model.menu.Item;

/**
* Adds an item to the address book.
* Adds an item to the menu.
*/
public class AddItemCommand extends Command {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package seedu.address.logic.commands.menu;

import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PERCENT;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_ITEMS;

import java.util.List;

import seedu.address.commons.core.EventsCenter;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.events.ui.DisplayItemListRequestEvent;
import seedu.address.logic.CommandHistory;
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.menu.Item;
import seedu.address.model.menu.Price;

/**
* Gives an existing item in the menu a discount.
*/
public class DiscountItemCommand extends Command {

public static final String COMMAND_WORD = "discount-item";

public static final String COMMAND_ALIAS = "dci";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Gives the item identified "
+ "by the index number used in the displayed item list a discount based on the percent. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "[" + PREFIX_PERCENT + "PRICE] "
+ "Example: " + COMMAND_WORD + " 1 "
+ PREFIX_PERCENT + "20";

public static final String MESSAGE_DISCOUNT_ITEM_SUCCESS = "Discounted Item: %1$s";

private final Index index;
private final double percent;

/**
* @param index of the item in the filtered item list to edit
* @param percent the percent of the discount
*/
public DiscountItemCommand(Index index, double percent) {
requireNonNull(index);
requireNonNull(percent);

this.index = index;
this.percent = percent;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
List<Item> lastShownList = model.getFilteredItemList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_ITEM_DISPLAYED_INDEX);
}

Item itemToDiscount = lastShownList.get(index.getZeroBased());
Item discountedItem = createDiscountedItem(itemToDiscount, percent);

model.updateItem(itemToDiscount, discountedItem);
model.updateFilteredItemList(PREDICATE_SHOW_ALL_ITEMS);
model.commitAddressBook();
EventsCenter.getInstance().post(new DisplayItemListRequestEvent());
return new CommandResult(String.format(MESSAGE_DISCOUNT_ITEM_SUCCESS, discountedItem));
}

/**
* Creates and returns a {@code Item} with the details of {@code itemToEdit}
* edited with {@code editItemDescriptor}.
*/
public static Item createDiscountedItem(Item itemToEdit, double percent) {
assert itemToEdit != null;
double originalValue = itemToEdit.getPrice().getOriginalValue();
Price updatedPrice = new Price(String.format("%.2f", originalValue));
updatedPrice.setValue(percent);

return new Item(itemToEdit.getName(), updatedPrice, itemToEdit.getRemark(), itemToEdit.getTags());
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DiscountItemCommand // instanceof handles nulls
&& index.equals(((DiscountItemCommand) other).index) // state check
&& percent == ((DiscountItemCommand) other).percent); // state check
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import seedu.address.logic.commands.menu.AddItemCommand;
import seedu.address.logic.commands.menu.ClearMenuCommand;
import seedu.address.logic.commands.menu.DeleteItemCommand;
import seedu.address.logic.commands.menu.DiscountItemCommand;
import seedu.address.logic.commands.menu.EditItemCommand;
import seedu.address.logic.commands.menu.FilterMenuCommand;
import seedu.address.logic.commands.menu.FindItemCommand;
Expand All @@ -54,6 +55,7 @@
import seedu.address.logic.parser.ingredients.EditIngredientCommandParser;
import seedu.address.logic.parser.menu.AddItemCommandParser;
import seedu.address.logic.parser.menu.DeleteItemCommandParser;
import seedu.address.logic.parser.menu.DiscountItemCommandParser;
import seedu.address.logic.parser.menu.EditItemCommandParser;
import seedu.address.logic.parser.menu.FilterMenuCommandParser;
import seedu.address.logic.parser.menu.FindItemCommandParser;
Expand Down Expand Up @@ -224,6 +226,10 @@ public Command parseCommand(String userInput) throws ParseException {
case TodaySpecialCommand.COMMAND_ALIAS:
return new TodaySpecialCommand();

case DiscountItemCommand.COMMAND_WORD:
case DiscountItemCommand.COMMAND_ALIAS:
return new DiscountItemCommandParser().parse(arguments);

case ClearMenuCommand.COMMAND_WORD:
case ClearMenuCommand.COMMAND_ALIAS:
return new ClearMenuCommand();
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 @@ -31,6 +31,7 @@ public class CliSyntax {

/*Prefix definitions for menu management */
public static final Prefix PREFIX_PRICE = new Prefix("p/");
public static final Prefix PREFIX_PERCENT = new Prefix("dp/");

/*Prefix definitions for reservation management */
public static final Prefix PREFIX_PAX = new Prefix("px/");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package seedu.address.logic.parser.menu;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PERCENT;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.menu.DiscountItemCommand;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new DiscountItemCommand object
*/
public class DiscountItemCommandParser implements Parser<DiscountItemCommand> {
public static final String MESSAGE_PERCENT_CONSTRAINTS =
"Percent should only contain numbers, and it should be at most 2 digits";
public static final String PERCENT_VALIDATION_REGEX = "\\d{0,2}";

/**
* Parses the given {@code String} of arguments in the context of the DiscountItemCommand
* and returns an DiscountItemCommand object for execution.
* @throws ParseException if the user input does not conform the expected format
*/
public DiscountItemCommand parse(String args) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_PERCENT);

Index index;

try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DiscountItemCommand.MESSAGE_USAGE),
pe);
}

if (!argMultimap.getValue(PREFIX_PERCENT).isPresent()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, DiscountItemCommand.MESSAGE_USAGE));
}
String trimmedPercent = argMultimap.getValue(PREFIX_PERCENT).get().trim();
if (!isValidPercent(trimmedPercent)) {
throw new ParseException(MESSAGE_PERCENT_CONSTRAINTS);
}
double percent = Double.parseDouble(trimmedPercent);

return new DiscountItemCommand(index, percent);
}

/**
* Returns true if a given string is a valid percent.
*/
private static boolean isValidPercent(String test) {
return test.matches(PERCENT_VALIDATION_REGEX);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import seedu.address.logic.parser.exceptions.ParseException;

/**
* Parses input arguments and creates a new FindItemCommand object
* Parses input arguments and creates a new SortMenuCommand object
*/
public class SortMenuCommandParser implements Parser<SortMenuCommand> {

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/menu/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public Set<Tag> getTags() {
}

/**
* Returns true if both persons of the same name have at least one other identity field that is the same.
* This defines a weaker notion of equality between two persons.
* Returns true if both items of the same name have at least one other identity field that is the same.
* This defines a weaker notion of equality between two items.
*/
public boolean isSameItem(Item otherItem) {
if (otherItem == this) {
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/seedu/address/model/menu/Price.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ public class Price {
"Price should only contain numbers, and it should be at most 2 decimal place";
private static final String DECIMAL_PLACE_REGEX = "\\d{0,2}";
public static final String PRICE_VALIDATION_REGEX = "\\d+" + ".?" + DECIMAL_PLACE_REGEX;
private final double value;
private static final double MAX_PERCENT = 100.0;
private double value;
private final double originalValue;

/**
* Constructs a {@code Price}.
Expand All @@ -23,6 +25,19 @@ public class Price {
public Price(String price) {
requireNonNull(price);
checkArgument(isValidPrice(price), MESSAGE_PRICE_CONSTRAINTS);
originalValue = Double.parseDouble(price);
value = Double.parseDouble(price);
}

/**
* Constructs a {@code Price}.
*
* @param price A valid price.
*/
public Price(String price, String originalPrice) {
requireNonNull(price);
checkArgument(isValidPrice(price), MESSAGE_PRICE_CONSTRAINTS);
originalValue = Double.parseDouble(originalPrice);
value = Double.parseDouble(price);
}

Expand All @@ -37,6 +52,14 @@ public double getValue() {
return value;
}

public double getOriginalValue() {
return originalValue;
}

public void setValue(double percent) {
value = originalValue * ((MAX_PERCENT - percent) / MAX_PERCENT);
}

@Override
public String toString() {
//return valueInString;
Expand All @@ -47,7 +70,8 @@ public String toString() {
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Price // instanceof handles nulls
&& value == ((Price) other).value); // state check
&& value == ((Price) other).value // state check
&& originalValue == ((Price) other).originalValue);
}

@Override
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/seedu/address/storage/elements/XmlAdaptedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import seedu.address.storage.XmlAdaptedTag;

/**
* JAXB-friendly version of the Person.
* JAXB-friendly version of the Item.
*/
public class XmlAdaptedItem {

Expand All @@ -29,6 +29,8 @@ public class XmlAdaptedItem {
@XmlElement(required = true)
private String price;
@XmlElement(required = true)
private String originalPrice;
@XmlElement(required = true)
private String remark;

@XmlElement
Expand All @@ -46,6 +48,7 @@ public XmlAdaptedItem() {}
public XmlAdaptedItem(String name, String price, String remark, List<XmlAdaptedTag> tagged) {
this.name = name;
this.price = price;
this.originalPrice = price;
this.remark = remark;
if (tagged != null) {
this.tagged = new ArrayList<>(tagged);
Expand All @@ -60,6 +63,7 @@ public XmlAdaptedItem(String name, String price, String remark, List<XmlAdaptedT
public XmlAdaptedItem(Item source) {
name = source.getName().toString();
price = source.getPrice().toString();
originalPrice = String.format("%.2f", source.getPrice().getOriginalValue());
remark = source.getRemark().toString();
tagged = source.getTags().stream()
.map(XmlAdaptedTag::new)
Expand Down Expand Up @@ -91,7 +95,13 @@ public Item toModelType() throws IllegalValueException {
if (!Price.isValidPrice(price)) {
throw new IllegalValueException(Price.MESSAGE_PRICE_CONSTRAINTS);
}
final Price modelPrice = new Price(price);
if (originalPrice == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Price.class.getSimpleName()));
}
if (!Price.isValidPrice(originalPrice)) {
throw new IllegalValueException(Price.MESSAGE_PRICE_CONSTRAINTS);
}
final Price modelPrice = new Price(price, originalPrice);

if (remark == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Remark.class.getSimpleName()));
Expand All @@ -115,6 +125,7 @@ public boolean equals(Object other) {
XmlAdaptedItem otherItem = (XmlAdaptedItem) other;
return Objects.equals(name, otherItem.name)
&& Objects.equals(price, otherItem.price)
&& Objects.equals(originalPrice, otherItem.originalPrice)
&& tagged.equals(otherItem.tagged);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
<items>
<name>Cheese Burger</name>
<price>3</price>
<originalPrice>3</originalPrice>
<remark></remark>
</items>

<!-- Item with same identity as Cheese Burger -->
<items>
<name>Cheese Burger</name>
<price>3</price>
<originalPrice>3</originalPrice>
<remark></remark>
</items>

Expand Down
Loading

0 comments on commit a6a6a17

Please sign in to comment.