-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecipher.java
More file actions
59 lines (49 loc) · 1.44 KB
/
Copy pathDecipher.java
File metadata and controls
59 lines (49 loc) · 1.44 KB
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
package CE2;
/** This class will help to decode what kind of command entered and its relevant information
* and whether or not a valid command is entered
*/
public class Decipher {
private String[] stringArr;
public Decipher(String command) {
stringArr = command.split(" ", 2);
}
public String getCommandType() {
return stringArr[0];
}
public String getDescription() {
return stringArr[1];
}
// Verify the amount of parts to the command and then call the appropriate method
public boolean isGoodCommand() {
if (this.getCommandParts() == 2) {
return isValidTwoPartsCommand();
} else {
return isValidOnePartCommand();
}
}
private int getCommandParts() {
return stringArr.length;
}
// Checks whether the command is valid one-part command
private boolean isValidOnePartCommand() {
return getCommandType().equals("display") || getCommandType().equals("clear") ||
getCommandType().equals("exit") || getCommandType().equals("sort");
}
// Checks whether the command is a valid two-parts command
private boolean isValidTwoPartsCommand() {
boolean isValid = true;
if (getCommandType().equals("delete") || getCommandType().equals("add") ||
getCommandType().equals("search")) {
try {
Integer.parseInt(this.getDescription());
} catch (NumberFormatException e) {
if (this.getCommandType().equals("delete")) {
isValid = false;
}
}
return isValid;
} else {
return false;
}
}
}