forked from nus-cs2103-AY1819S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/issue-98
- Loading branch information
Showing
24 changed files
with
513 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/main/java/seedu/address/logic/commands/menu/DiscountItemCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/main/java/seedu/address/logic/parser/menu/DiscountItemCommandParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.