1
1
package io .xpire .logic .commands ;
2
2
3
- import static io .xpire .commons .core .Messages .MESSAGE_REPLENISH_SHIFT_SUCCESS ;
4
3
import static io .xpire .commons .util .CollectionUtil .requireAllNonNull ;
4
+ import static io .xpire .logic .commands .util .CommandUtil .MESSAGE_REPLENISH_SHIFT_SUCCESS ;
5
5
import static io .xpire .model .ListType .REPLENISH ;
6
6
import static io .xpire .model .ListType .XPIRE ;
7
7
11
11
import io .xpire .commons .core .Messages ;
12
12
import io .xpire .commons .core .index .Index ;
13
13
import io .xpire .logic .commands .exceptions .CommandException ;
14
+ import io .xpire .logic .commands .util .CommandUtil ;
14
15
import io .xpire .logic .parser .exceptions .ParseException ;
15
16
import io .xpire .model .ListType ;
16
17
import io .xpire .model .Model ;
@@ -35,6 +36,7 @@ private enum DeleteMode { ITEM, QUANTITY, TAGS }
35
36
36
37
public static final String COMMAND_WORD = "delete" ;
37
38
public static final String COMMAND_SHORTHAND = "d" ;
39
+ public static final String MESSAGE_UNKNOWN_DELETE_MODE = "Unknown Delete mode." ;
38
40
39
41
public static final String MESSAGE_USAGE =
40
42
"Three formats available for " + COMMAND_WORD + ":\n "
@@ -50,15 +52,13 @@ private enum DeleteMode { ITEM, QUANTITY, TAGS }
50
52
public static final String MESSAGE_DELETE_ITEM_SUCCESS = "Deleted item: %s" ;
51
53
public static final String MESSAGE_DELETE_TAGS_SUCCESS = "Deleted tags from item: %s" ;
52
54
public static final String MESSAGE_DELETE_QUANTITY_SUCCESS = "Reduced quantity by %s from item: %s" ;
53
- public static final String MESSAGE_DELETE_QUANTITY_FAILURE = "Invalid quantity specified. \n "
54
- + "Quantity must be positive and less than or equals to item's quantity." ;
55
55
56
56
private final Index targetIndex ;
57
57
private final Set <Tag > tagSet ;
58
58
private final Quantity quantity ;
59
59
private final DeleteMode mode ;
60
60
private final ListType listType ;
61
- private Item item = null ;
61
+ private Item item ;
62
62
private String result = "" ;
63
63
64
64
public DeleteCommand (ListType listType , Index targetIndex ) {
@@ -92,7 +92,7 @@ public CommandResult execute(Model model, StateManager stateManager) throws Comm
92
92
ObservableList <? extends Item > currentList = model .getCurrentList ();
93
93
94
94
if (this .targetIndex .getZeroBased () >= currentList .size ()) {
95
- throw new CommandException (Messages .MESSAGE_INVALID_ITEM_DISPLAYED_INDEX );
95
+ throw new CommandException (Messages .MESSAGE_INVALID_INDEX );
96
96
}
97
97
98
98
Item targetItem = currentList .get (this .targetIndex .getZeroBased ());
@@ -106,22 +106,22 @@ public CommandResult execute(Model model, StateManager stateManager) throws Comm
106
106
case QUANTITY :
107
107
return executeDeleteQuantity (model , targetItem , stateManager );
108
108
default :
109
- throw new CommandException (Messages . MESSAGE_UNKNOWN_DELETE_MODE );
109
+ throw new CommandException (MESSAGE_UNKNOWN_DELETE_MODE );
110
110
}
111
111
}
112
112
113
113
/**
114
114
* Executes the command and returns the result message.
115
115
*
116
116
* @param model model {@code Model} which the command should operate on.
117
- * @param targetItem target item to reduce the quantity of,
117
+ * @param targetItem target item to reduce the quantity of.
118
118
* @return feedback message of the operation result for display.
119
119
* @throws CommandException If an error occurs during command execution.
120
120
*/
121
121
private CommandResult executeDeleteQuantity (Model model , Item targetItem , StateManager stateManager )
122
- throws CommandException , ParseException {
122
+ throws CommandException {
123
123
assert this .quantity != null ;
124
- XpireItem updatedItem = reduceItemQuantity (new XpireItem (( XpireItem ) targetItem ) , this .quantity );
124
+ XpireItem updatedItem = CommandUtil . reduceItemQuantity (( XpireItem ) targetItem , this .quantity );
125
125
stateManager .saveState (new ModifiedState (model ));
126
126
model .setItem (listType , targetItem , updatedItem );
127
127
// transfer item to replenish list
@@ -132,7 +132,7 @@ private CommandResult executeDeleteQuantity(Model model, Item targetItem, StateM
132
132
setShowInHistory (true );
133
133
return new CommandResult (this .result );
134
134
}
135
- this .result = String .format (MESSAGE_DELETE_QUANTITY_SUCCESS , quantity .toString (), targetItem );
135
+ this .result = String .format (MESSAGE_DELETE_QUANTITY_SUCCESS , quantity .toString (), targetItem . getName () );
136
136
setShowInHistory (true );
137
137
return new CommandResult (this .result );
138
138
}
@@ -161,8 +161,7 @@ private CommandResult executeDeleteTags(Model model, Item targetItem, StateManag
161
161
return new CommandResult (this .result );
162
162
}
163
163
164
- /**
165
- * Executes the command and returns the result message.
164
+ /** Executes the command and returns the result message.
166
165
*
167
166
* @param model model {@code Model} which the command should operate on.
168
167
* @param targetItem target item to delete completely.
@@ -220,25 +219,6 @@ private Item removeTagsFromReplenishItem(Item targetReplenishItem, Set<Tag> tagS
220
219
return targetReplenishItem ;
221
220
}
222
221
223
- /**
224
- * Reduces xpireItem's quantity by amount specified.
225
- *
226
- * @param targetXpireItem XpireItem which amount will be reduced.
227
- * @param reduceByQuantity Quantity to be reduced.
228
- * @return The new XpireItem with its quantity reduced.
229
- * @throws ParseException if
230
- */
231
- private XpireItem reduceItemQuantity (XpireItem targetXpireItem , Quantity reduceByQuantity ) throws CommandException ,
232
- ParseException {
233
- Quantity originalQuantity = targetXpireItem .getQuantity ();
234
- if (originalQuantity .isLessThan (reduceByQuantity )) {
235
- throw new CommandException (MESSAGE_DELETE_QUANTITY_FAILURE );
236
- }
237
- Quantity updatedQuantity = originalQuantity .deductQuantity (reduceByQuantity );
238
- targetXpireItem .setQuantity (updatedQuantity );
239
- return targetXpireItem ;
240
- }
241
-
242
222
/**
243
223
* Shifts Item to ReplenishList.
244
224
*/
0 commit comments