forked from nus-cs2103-AY1718S2/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex2id.patch
318 lines (276 loc) · 14.1 KB
/
index2id.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
From 2500cab53d2b6fe8bf79405900d67b63e49c791f Mon Sep 17 00:00:00 2001
From: Chu Qing Hao <[email protected]>
Date: Sun, 15 Apr 2018 15:39:09 +0800
Subject: [PATCH 1/3] Change delete order to use ID instead of index
---
.../address/logic/commands/DeleteOrderCommand.java | 32 ++++++++++++----------
.../logic/parser/DeleteOrderCommandParser.java | 4 +--
.../seedu/address/logic/parser/ParserUtil.java | 13 +++++++++
3 files changed, 32 insertions(+), 17 deletions(-)
diff --git a/src/main/java/seedu/address/logic/commands/DeleteOrderCommand.java b/src/main/java/seedu/address/logic/commands/DeleteOrderCommand.java
index 2945aa0..32da637 100644
--- a/src/main/java/seedu/address/logic/commands/DeleteOrderCommand.java
+++ b/src/main/java/seedu/address/logic/commands/DeleteOrderCommand.java
@@ -11,36 +11,38 @@ import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.order.Order;
import seedu.address.model.order.exceptions.OrderNotFoundException;
+//@@author qinghao1
/**
- * Deletes a order identified using it's last displayed index from the address book.
+ * Deletes a order identified using its id from the address book.
*/
public class DeleteOrderCommand extends UndoableCommand {
public static final String COMMAND_WORD = "deleteorder";
public static final String MESSAGE_USAGE = COMMAND_WORD
- + ": Deletes the order identified by the index number used in the last order listing.\n"
- + "Parameters: INDEX (must be a positive integer)\n"
+ + ": Deletes the order identified by its id.\n"
+ + "Parameters: ID (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";
public static final String MESSAGE_DELETE_ORDER_SUCCESS = "Deleted Order: %1$s";
+ public static final String MESSAGE_INVALID_ORDER = "The order is invalid. Check that the order ID is correct.";
- private final Index targetIndex;
+ private final int targetID;
private Order orderToDelete;
- public DeleteOrderCommand(Index targetIndex) {
- this.targetIndex = targetIndex;
+ public DeleteOrderCommand(int targetID) {
+ this.targetID = targetID;
}
@Override
public CommandResult executeUndoableCommand() {
- requireNonNull(orderToDelete);
try {
+ requireNonNull(orderToDelete);
model.deleteOrder(orderToDelete);
- } catch (OrderNotFoundException onfe) {
- throw new AssertionError("The target order cannot be missing");
+ } catch (NullPointerException | OrderNotFoundException e) {
+ throw new AssertionError(MESSAGE_INVALID_ORDER);
}
return new CommandResult(String.format(MESSAGE_DELETE_ORDER_SUCCESS, orderToDelete));
@@ -49,19 +51,19 @@ public class DeleteOrderCommand extends UndoableCommand {
@Override
protected void preprocessUndoableCommand() throws CommandException {
List<Order> lastShownList = model.getFilteredOrderList();
-
- if (targetIndex.getZeroBased() >= lastShownList.size()) {
- throw new CommandException(Messages.MESSAGE_INVALID_ORDER_DISPLAYED_INDEX);
+ orderToDelete = null;
+ //There should only be one order that matches the ID
+ for(Order order : lastShownList) {
+ if(order.getId() == targetID)
+ orderToDelete = order;
}
-
- orderToDelete = lastShownList.get(targetIndex.getZeroBased());
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeleteCommand // instanceof handles nulls
- && this.targetIndex.equals(((DeleteOrderCommand) other).targetIndex) // state check
+ && this.targetID == ((DeleteOrderCommand) other).targetID // state check
&& Objects.equals(this.orderToDelete, ((DeleteOrderCommand) other).orderToDelete));
}
}
diff --git a/src/main/java/seedu/address/logic/parser/DeleteOrderCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteOrderCommandParser.java
index c64d346..0a5623f 100644
--- a/src/main/java/seedu/address/logic/parser/DeleteOrderCommandParser.java
+++ b/src/main/java/seedu/address/logic/parser/DeleteOrderCommandParser.java
@@ -19,8 +19,8 @@ public class DeleteOrderCommandParser implements Parser<DeleteOrderCommand> {
*/
public DeleteOrderCommand parse(String args) throws ParseException {
try {
- Index index = ParserUtil.parseIndex(args);
- return new DeleteOrderCommand(index);
+ int id = ParserUtil.parseID(args);
+ return new DeleteOrderCommand(id);
} catch (IllegalValueException ive) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteOrderCommand.MESSAGE_USAGE));
diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java
index 901a531..e981b13 100644
--- a/src/main/java/seedu/address/logic/parser/ParserUtil.java
+++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java
@@ -28,6 +28,7 @@ import seedu.address.model.tag.Tag;
public class ParserUtil {
public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer.";
+ public static final String MESSAGE_INVALID_ID = "ID is invalid! Must be non-zero unsigned integer";
public static final String MESSAGE_INSUFFICIENT_PARTS = "Number of parts must be more than 1.";
/**
@@ -43,6 +44,18 @@ public class ParserUtil {
return Index.fromOneBased(Integer.parseInt(trimmedIndex));
}
+ /**
+ * Parses ID field and returns an int which is the ID.
+ * @throws IllegalValueException if the specified ID is invalid (not non-zero unsigned integer).
+ */
+ public static int parseID(String idString) throws IllegalValueException {
+ String trimmedID = idString.trim();
+ if (!StringUtil.isNonZeroUnsignedInteger(trimmedID)) {
+ throw new IllegalValueException(MESSAGE_INVALID_ID);
+ }
+ return Integer.parseInt(trimmedID);
+ }
+
/**
* Parses a {@code String name} into a {@code Name}.
* Leading and trailing whitespaces will be trimmed.
--
2.15.1 (Apple Git-101)
From 866cb62991907ffc37df27afdba475323fe05c79 Mon Sep 17 00:00:00 2001
From: Chu Qing Hao <[email protected]>
Date: Sun, 15 Apr 2018 15:45:52 +0800
Subject: [PATCH 2/3] Change deleteproduct to use id instead of index
---
.../logic/commands/DeleteProductCommand.java | 33 ++++++++++++----------
.../logic/parser/DeleteProductCommandParser.java | 4 +--
2 files changed, 20 insertions(+), 17 deletions(-)
diff --git a/src/main/java/seedu/address/logic/commands/DeleteProductCommand.java b/src/main/java/seedu/address/logic/commands/DeleteProductCommand.java
index 10ce501..c1b2e46 100644
--- a/src/main/java/seedu/address/logic/commands/DeleteProductCommand.java
+++ b/src/main/java/seedu/address/logic/commands/DeleteProductCommand.java
@@ -11,36 +11,38 @@ import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.product.Product;
import seedu.address.model.product.exceptions.ProductNotFoundException;
+//@@qinghao1
/**
- * Deletes a product identified using it's last displayed index from the address book.
+ * Deletes a product identified using its ID.
*/
public class DeleteProductCommand extends UndoableCommand {
public static final String COMMAND_WORD = "deleteproduct";
public static final String MESSAGE_USAGE = COMMAND_WORD
- + ": Deletes the product identified by the index number used in the last product listing.\n"
- + "Parameters: INDEX (must be a positive integer)\n"
+ + ": Deletes the product identified by its ID.\n"
+ + "Parameters: ID (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " 1";
public static final String MESSAGE_DELETE_PRODUCT_SUCCESS = "Deleted Product: %1$s";
+ public static final String MESSAGE_INVALID_PRODUCT = "The product is invalid. Check that the product ID is correct.";
- private final Index targetIndex;
+ private int targetID;
private Product productToDelete;
- public DeleteProductCommand(Index targetIndex) {
- this.targetIndex = targetIndex;
+ public DeleteProductCommand(int targetID) {
+ this.targetID = targetID;
}
@Override
public CommandResult executeUndoableCommand() {
- requireNonNull(productToDelete);
try {
+ requireNonNull(productToDelete);
model.deleteProduct(productToDelete);
- } catch (ProductNotFoundException pnfe) {
- throw new AssertionError("The target product cannot be missing");
+ } catch (NullPointerException|ProductNotFoundException e) {
+ throw new AssertionError(MESSAGE_INVALID_PRODUCT);
}
return new CommandResult(String.format(MESSAGE_DELETE_PRODUCT_SUCCESS, productToDelete));
@@ -49,19 +51,20 @@ public class DeleteProductCommand extends UndoableCommand {
@Override
protected void preprocessUndoableCommand() throws CommandException {
List<Product> lastShownList = model.getFilteredProductList();
-
- if (targetIndex.getZeroBased() >= lastShownList.size()) {
- throw new CommandException(Messages.MESSAGE_INVALID_PRODUCT_DISPLAYED_INDEX);
+ productToDelete = null;
+ //There should only be one product that matches the ID
+ for(Product product : lastShownList) {
+ if(product.getId() == targetID) {
+ productToDelete = product;
+ }
}
-
- productToDelete = lastShownList.get(targetIndex.getZeroBased());
}
@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof DeleteCommand // instanceof handles nulls
- && this.targetIndex.equals(((DeleteProductCommand) other).targetIndex) // state check
+ && this.targetID == (((DeleteProductCommand) other).targetID) // state check
&& Objects.equals(this.productToDelete, ((DeleteProductCommand) other).productToDelete));
}
}
diff --git a/src/main/java/seedu/address/logic/parser/DeleteProductCommandParser.java b/src/main/java/seedu/address/logic/parser/DeleteProductCommandParser.java
index 4e40fd7..e1ef2e5 100644
--- a/src/main/java/seedu/address/logic/parser/DeleteProductCommandParser.java
+++ b/src/main/java/seedu/address/logic/parser/DeleteProductCommandParser.java
@@ -19,8 +19,8 @@ public class DeleteProductCommandParser implements Parser<DeleteProductCommand>
*/
public DeleteProductCommand parse(String args) throws ParseException {
try {
- Index index = ParserUtil.parseIndex(args);
- return new DeleteProductCommand(index);
+ int id = ParserUtil.parseID(args);
+ return new DeleteProductCommand(id);
} catch (IllegalValueException ive) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, DeleteProductCommand.MESSAGE_USAGE));
--
2.15.1 (Apple Git-101)
From 2a126c4a5c1f8f61478c25077ad63119317c0cff Mon Sep 17 00:00:00 2001
From: Chu Qing Hao <[email protected]>
Date: Sun, 15 Apr 2018 15:54:41 +0800
Subject: [PATCH 3/3] Fix bugs
---
.../java/seedu/address/logic/commands/DeleteOrderCommand.java | 9 ++++++---
.../java/seedu/address/logic/commands/DeleteProductCommand.java | 9 ++++++---
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/src/main/java/seedu/address/logic/commands/DeleteOrderCommand.java b/src/main/java/seedu/address/logic/commands/DeleteOrderCommand.java
index 32da637..3e60d14 100644
--- a/src/main/java/seedu/address/logic/commands/DeleteOrderCommand.java
+++ b/src/main/java/seedu/address/logic/commands/DeleteOrderCommand.java
@@ -38,11 +38,11 @@ public class DeleteOrderCommand extends UndoableCommand {
@Override
public CommandResult executeUndoableCommand() {
+ requireNonNull(orderToDelete);
try {
- requireNonNull(orderToDelete);
model.deleteOrder(orderToDelete);
- } catch (NullPointerException | OrderNotFoundException e) {
- throw new AssertionError(MESSAGE_INVALID_ORDER);
+ } catch (OrderNotFoundException e) {
+ throw new AssertionError("The target order cannot be missing.");
}
return new CommandResult(String.format(MESSAGE_DELETE_ORDER_SUCCESS, orderToDelete));
@@ -57,6 +57,9 @@ public class DeleteOrderCommand extends UndoableCommand {
if(order.getId() == targetID)
orderToDelete = order;
}
+ if(orderToDelete == null) {
+ throw new CommandException(MESSAGE_INVALID_ORDER);
+ }
}
@Override
diff --git a/src/main/java/seedu/address/logic/commands/DeleteProductCommand.java b/src/main/java/seedu/address/logic/commands/DeleteProductCommand.java
index c1b2e46..d0b0eb1 100644
--- a/src/main/java/seedu/address/logic/commands/DeleteProductCommand.java
+++ b/src/main/java/seedu/address/logic/commands/DeleteProductCommand.java
@@ -38,11 +38,11 @@ public class DeleteProductCommand extends UndoableCommand {
@Override
public CommandResult executeUndoableCommand() {
+ requireNonNull(productToDelete);
try {
- requireNonNull(productToDelete);
model.deleteProduct(productToDelete);
- } catch (NullPointerException|ProductNotFoundException e) {
- throw new AssertionError(MESSAGE_INVALID_PRODUCT);
+ } catch (ProductNotFoundException e) {
+ throw new AssertionError("The product cannot be missing.");
}
return new CommandResult(String.format(MESSAGE_DELETE_PRODUCT_SUCCESS, productToDelete));
@@ -58,6 +58,9 @@ public class DeleteProductCommand extends UndoableCommand {
productToDelete = product;
}
}
+ if(productToDelete == null) {
+ throw new CommandException(MESSAGE_INVALID_PRODUCT);
+ }
}
@Override
--
2.15.1 (Apple Git-101)