forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
/
EditAutoExpenseCommand.java
281 lines (240 loc) · 9.72 KB
/
EditAutoExpenseCommand.java
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
package seedu.guilttrip.logic.commands.editcommands;
import static java.util.Objects.requireNonNull;
import static seedu.guilttrip.commons.core.Messages.MESSAGE_INVALID_CATEGORY;
import static seedu.guilttrip.logic.parser.CliSyntax.PREFIX_AMOUNT;
import static seedu.guilttrip.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.guilttrip.logic.parser.CliSyntax.PREFIX_DESC;
import static seedu.guilttrip.logic.parser.CliSyntax.PREFIX_FREQ;
import static seedu.guilttrip.logic.parser.CliSyntax.PREFIX_TAG;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import seedu.guilttrip.commons.core.Messages;
import seedu.guilttrip.commons.core.index.Index;
import seedu.guilttrip.commons.util.CollectionUtil;
import seedu.guilttrip.logic.CommandHistory;
import seedu.guilttrip.logic.commands.Command;
import seedu.guilttrip.logic.commands.CommandResult;
import seedu.guilttrip.logic.commands.exceptions.CommandException;
import seedu.guilttrip.model.Model;
import seedu.guilttrip.model.entry.Amount;
import seedu.guilttrip.model.entry.AutoExpense;
import seedu.guilttrip.model.entry.Category;
import seedu.guilttrip.model.entry.Date;
import seedu.guilttrip.model.entry.Description;
import seedu.guilttrip.model.tag.Tag;
import seedu.guilttrip.model.util.Frequency;
/**
* Edits the details of an existing entry in the guilttrip book.
*/
public class EditAutoExpenseCommand extends Command {
public static final String COMMAND_WORD = "editAutoExp";
public static final String ONE_LINER_DESC = COMMAND_WORD + ": Edits the details of the Expense identified "
+ "by the index number used in the displayed Expenses list. ";
public static final String MESSAGE_USAGE = ONE_LINER_DESC
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ PREFIX_DESC + "DESCRIPTION "
+ PREFIX_AMOUNT + "AMOUNT "
+ PREFIX_FREQ + "FREQUENCY "
+ PREFIX_DATE + "DATE "
+ "[" + PREFIX_TAG + "TAG]...\n"
+ "Example: "
+ COMMAND_WORD + " 1 " + PREFIX_AMOUNT + "5.60";
public static final String MESSAGE_EDIT_ENTRY_SUCCESS = "Edited Auto Expense: %1$s";
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_ENTRY = "This entry already exists in the guiltTripper!";
private final Index index;
private final EditAutoExpenseDescriptor editAutoExpenseDescriptor;
/**
* @param index of the autoExpense in the filtered expense AutoExpense list to edit
* @param editAutoExpenseDescriptor details to edit the entry with
*/
public EditAutoExpenseCommand(Index index, EditAutoExpenseDescriptor editAutoExpenseDescriptor) {
requireNonNull(index);
requireNonNull(editAutoExpenseDescriptor);
this.index = index;
this.editAutoExpenseDescriptor = new EditAutoExpenseDescriptor(editAutoExpenseDescriptor);
}
//TODO:YJ confirm if still setEntry?
@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
List<AutoExpense> lastShownList = model.getFilteredAutoExpenses();
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_ENTRY_DISPLAYED_INDEX);
}
AutoExpense entryToEdit = lastShownList.get(index.getZeroBased());
AutoExpense editedEntry = createEditedAutoExpense(entryToEdit, editAutoExpenseDescriptor);
if (!model.hasCategory(editedEntry.getCategory())) {
throw new CommandException(MESSAGE_INVALID_CATEGORY);
}
if (!entryToEdit.isSameEntry(editedEntry) && model.hasAutoExpense(editedEntry)) {
throw new CommandException(MESSAGE_DUPLICATE_ENTRY);
}
model.setAutoExpense(entryToEdit, editedEntry);
model.commitGuiltTrip();
return new CommandResult(String.format(MESSAGE_EDIT_ENTRY_SUCCESS, editedEntry));
}
/**
* Creates and returns a {@code Person} with the details of {@code personToEdit}
* edited with {@code editPersonDescriptor}.
*/
private static AutoExpense createEditedAutoExpense(AutoExpense expenseToEdit,
EditAutoExpenseDescriptor editAutoExpenseDescriptor) {
assert expenseToEdit != null;
Description updatedDescription = editAutoExpenseDescriptor.getDesc().orElse(expenseToEdit.getDesc());
Amount updatedAmount = editAutoExpenseDescriptor.getAmount().orElse(expenseToEdit.getAmount());
Frequency updatedFrequency = editAutoExpenseDescriptor.getFrequency().orElse(expenseToEdit.getFrequency());
Date updatedDate = editAutoExpenseDescriptor.getDate().orElse(expenseToEdit.getDate());
Category updatedCategory = editAutoExpenseDescriptor.getCategory().orElse(expenseToEdit.getCategory());
Set<Tag> updatedTags = editAutoExpenseDescriptor.getTags().orElse(expenseToEdit.getTags());
return new AutoExpense(updatedCategory, updatedDescription, updatedAmount, updatedTags, updatedFrequency,
updatedDate);
}
@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}
// instanceof handles nulls
if (!(other instanceof EditAutoExpenseCommand)) {
return false;
}
// state check
EditAutoExpenseCommand e = (EditAutoExpenseCommand) other;
return index.equals(e.index) && editAutoExpenseDescriptor.equals(e.editAutoExpenseDescriptor);
}
/**
* Stores the details to edit the entry with. Each non-empty field value will replace the
* corresponding field value of the entry.
*/
public static class EditAutoExpenseDescriptor {
private Description desc;
private Amount amt;
private Set<Tag> tags;
private Date date;
private Frequency freq;
private Category cat;
public EditAutoExpenseDescriptor() {
}
/**
* Copy constructor.
* A defensive copy of {@code tags} is used internally.
*/
public EditAutoExpenseDescriptor(EditAutoExpenseDescriptor toCopy) {
setDesc(toCopy.desc);
setAmount(toCopy.amt);
setTags(toCopy.tags);
setDate(toCopy.date);
setFrequency(toCopy.freq);
setCategory(toCopy.cat);
}
/**
* Returns true if at least one field is edited.
*/
public boolean isAnyFieldEdited() {
return CollectionUtil.isAnyNonNull(desc, amt, tags);
}
/**
* @param desc the desc to set
*/
public void setDesc(Description desc) {
this.desc = desc;
}
public Optional<Description> getDesc() {
return Optional.ofNullable(desc);
}
/**
* @param amt the amt to set
*/
public void setAmount(Amount amt) {
this.amt = amt;
}
public Optional<Amount> getAmount() {
return Optional.ofNullable(amt);
}
/**
* Sets the date.
*
* @param date the date to set
*/
public void setDate(Date date) {
this.date = date;
}
/**
* Get the Optional Date.
*
* @return the date
*/
public Optional<Date> getDate() {
return Optional.ofNullable(date);
}
/**
* Set the Frequency.
*
* @param freq the freq to set
*/
public void setFrequency(Frequency freq) {
this.freq = freq;
}
/**
* Get the Optional Frequency.
*
* @return the freq
*/
public Optional<Frequency> getFrequency() {
return Optional.ofNullable(freq);
}
/**
* Set the Category.
*
* @param cat the cat to set
*/
public void setCategory(Category cat) {
this.cat = cat;
}
/**
* Get the Optional Category.
*
* @return the cat
*/
public Optional<Category> getCategory() {
return Optional.ofNullable(cat);
}
/**
* Sets {@code tags} to this object's {@code tags}.
* A defensive copy of {@code tags} is used internally.
*/
public void setTags(Set<Tag> tags) {
this.tags = (tags != null) ? new HashSet<>(tags) : null;
}
/**
* Returns an unmodifiable tag set, which throws {@code UnsupportedOperationException}
* if modification is attempted.
* Returns {@code Optional#empty()} if {@code tags} is null.
*/
public Optional<Set<Tag>> getTags() {
return (tags != null) ? Optional.of(Collections.unmodifiableSet(tags)) : Optional.empty();
}
@Override
public boolean equals(Object other) {
// short circuit if same object
if (other == this) {
return true;
}
// instanceof handles nulls
if (!(other instanceof EditAutoExpenseDescriptor)) {
return false;
}
// state check
EditAutoExpenseDescriptor e = (EditAutoExpenseDescriptor) other;
return getDesc().equals(e.getDesc()) && getAmount().equals(e.getAmount()) && getTags().equals(e.getTags())
&& getFrequency().equals(e.getFrequency()) && getCategory().equals(e.getCategory())
&& getDate().equals(e.getDate());
}
}
}