forked from nus-cs2103-AY1920S1/addressbook-level3
-
Notifications
You must be signed in to change notification settings - Fork 5
/
CopyCardNumberCommand.java
83 lines (69 loc) · 3.11 KB
/
CopyCardNumberCommand.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
package seedu.address.logic.commands;
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CVC;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DESCRIPTION;
import java.util.List;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ClipboardUtil;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.card.Card;
import seedu.address.model.card.Cvc;
import seedu.address.model.card.Description;
/**
* Copies card number from card identified.
*/
public class CopyCardNumberCommand extends Command {
public static final String COMMAND_WORD = "copy";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ " : Opens and copies the card number of the card identified by "
+ "the description or index used in the display list. \nChecks against "
+ "provided CVC as second level of security\n"
+ "Parameters: d/DESCRIPTION v/CVC\n"
+ "Example: " + COMMAND_WORD + " " + PREFIX_DESCRIPTION + "POSB Debit "
+ PREFIX_CVC + "023\n"
+ "Parameters: INDEX v/CVC\n"
+ "Example: " + COMMAND_WORD + " 1 " + PREFIX_CVC + "023";
public static final String MESSAGE_SUCCESS = "Copied card number of %s to clipboard";
private final Cvc cvc;
private final Description description;
private final Index targetIndex;
public CopyCardNumberCommand(Cvc cvc, Description description) {
this.cvc = cvc;
this.description = description;
this.targetIndex = null;
}
public CopyCardNumberCommand(Cvc cvc, Index index) {
this.cvc = cvc;
this.description = null;
this.targetIndex = index;
}
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Card> lastShownList = model.getFilteredCardList();
assert((description != null) || (targetIndex != null));
Card cardToRead;
if (targetIndex != null) {
if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_CARD_DISPLAYED_INDEX);
}
cardToRead = lastShownList.get(targetIndex.getZeroBased());
} else {
if (!lastShownList.contains(new Card(description))) {
throw new CommandException(Messages.MESSAGE_INVALID_CARD_DISPLAYED);
}
cardToRead = lastShownList.get(lastShownList.indexOf(new Card(description)));
}
if (!cardToRead.getNonEncryptedCvc().equals(cvc.getNonEncryptedCvc())) {
throw new CommandException(Messages.MESSAGE_INVALID_CVC_DISPLAYED);
}
ClipboardUtil.copyToClipboard(cardToRead.getCardNumberWithoutDashes(), null);
return CommandResult.builder(String.format(MESSAGE_SUCCESS, cardToRead.getDescription()))
.read()
.setObject(cardToRead)
.setIndex(targetIndex)
.build();
}
}