-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathDuke.java
More file actions
177 lines (158 loc) · 7.7 KB
/
Copy pathDuke.java
File metadata and controls
177 lines (158 loc) · 7.7 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
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
import java.util.ArrayList;
import java.util.Scanner;
public class Duke {
private static final String horizontalBorder = "_________________________________\n";
private static final String INVALID_TODO_INPUT = " ☹ OOPS!!! The description of a todo cannot be empty.\n";
private static final String INVALID_DEADLINE_INPUT = "☹ OOPS!!! Please use proper deadline formatting: deadline {task} /by {time}\n";
private static final String INVALID_EVENT_INPUT = "☹ OOPS!!! Please use proper event formatting: event {task} /at {time}\n";
private static final String INVALID_ACCESS_EMPTY_TASKLIST = "☹ OOPS!!! Task does not exist. Initialise a task first, then try again\n";
private static final String INVALID_USER_INPUT = "☹ OOPS!!! Please use one of these keywords: {deadline, event, todo} followed by \\\"by\\\" and \\\"at\\\" for deadline and event tasks respectively.\n";
private Tasklist tasklist;
private Duke(){
this.tasklist = new Tasklist();
}
private Duke(Tasklist tasklist){
this.tasklist = tasklist;
}
private static String welcomeMessage(){
return horizontalBorder + "Hello! I'm Duke\nWhat can I do for you?\n" + horizontalBorder;
}
private static String byeMessage(){
return horizontalBorder + "Bye. Hope to see you again soon!\n" + horizontalBorder;
}
private String listContents(){
return horizontalBorder + this.tasklist + horizontalBorder;
}
public String addTaskMessage(String taskString) {
return horizontalBorder + "Got it. I've added this task:\n" + taskString + "\n" + "Now you have " + this.tasklist.getCount() + " tasks in the list.\n" + horizontalBorder;
}
public String taskNotFoundMessage(){
return "☹ OOPS!!! Task does not exist. Try another number between 1 and " + this.tasklist.getCount() + "\n";
}
private String markDoneMessage(int position) throws DukeException{
boolean isTaskMarked = this.tasklist.markTaskAtPos(position);
if (isTaskMarked) {
Task currentTask = this.tasklist.getTask(position);
return horizontalBorder + "Nice! I've marked this task as done:\n" + currentTask + "\n" + horizontalBorder;
} else if (this.tasklist.getCount() == 0) {
throw new DukeException(INVALID_ACCESS_EMPTY_TASKLIST);
} else {
throw new DukeException(taskNotFoundMessage());
}
}
private String unmarkDoneMessage(int position) throws DukeException {
boolean isTaskUnmarked = this.tasklist.unmarkTaskAtPos(position);
if (isTaskUnmarked) {
Task currentTask = this.tasklist.getTask(position);
return horizontalBorder + "OK, I've marked this task as not done yet:\n" + currentTask + "\n" + horizontalBorder;
} else if (this.tasklist.getCount() == 0) {
throw new DukeException(INVALID_ACCESS_EMPTY_TASKLIST);
} else {
throw new DukeException(taskNotFoundMessage());
}
}
private String deleteTaskMessage(int position) throws DukeException {
try {
Task deletedTask = this.tasklist.deleteTaskAtPos(position);
return horizontalBorder + "Noted. I've removed this task:\n" + deletedTask + "\n" + "" + "Now you have " + this.tasklist.getCount() + " tasks in the list.\n" + horizontalBorder;
} catch (IndexOutOfBoundsException e){
if (this.tasklist.getCount() == 0){
throw new DukeException(INVALID_ACCESS_EMPTY_TASKLIST);
} else {
throw new DukeException(taskNotFoundMessage());
}
}
}
private boolean isInteger(String value) {
try {
Integer.parseInt(value);
return true;
} catch (NumberFormatException e) {
return false;
}
}
private String makeToDoFromInput(String input) throws DukeException {
String description = input.substring("todo".length()).strip();
if (!description.equals("")) {
ToDo newToDo = new ToDo(description);
this.tasklist.add(newToDo);
return addTaskMessage(newToDo.toString());
} else {
throw new DukeException(INVALID_TODO_INPUT);
}
}
private String makeDeadlineFromInput(String input) throws DukeException {
String[] stringArray = input.substring("deadline".length()).strip().split("/by");
if (stringArray.length > 1) {
Deadline newDeadline = new Deadline(stringArray[0].strip(), stringArray[1].strip());
this.tasklist.add(newDeadline);
return addTaskMessage(newDeadline.toString());
} else {
throw new DukeException(INVALID_DEADLINE_INPUT);
}
}
private String makeEventFromInput(String input) throws DukeException{
String[] stringArray = input.substring("event".length()).strip().split("/at");
if (stringArray.length > 1) {
Event newEvent = new Event(stringArray[0].strip(), stringArray[1].strip());
this.tasklist.add(newEvent);
return addTaskMessage(newEvent.toString());
} else {
throw new DukeException(INVALID_EVENT_INPUT);
}
}
public void run(){
System.out.println(welcomeMessage());
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
boolean exitNow = false;
while(!exitNow) {
try {
String[] commandList = s.strip().split(" ");
String command = commandList[0].toLowerCase();
if (command.equals("bye") && commandList.length == 1) {
exitNow = true;
System.out.println(byeMessage());
} else if (command.equals("list") && commandList.length == 1) {
System.out.println(listContents());
} else if (command.equals("mark") && commandList.length > 1 && isInteger(commandList[1])) {
int taskIndexNum = Integer.parseInt(commandList[1]);
System.out.println(markDoneMessage(taskIndexNum));
} else if (command.equals("unmark") && commandList.length > 1 && isInteger(commandList[1])) {
int taskIndexNum = Integer.parseInt(commandList[1]);
System.out.println(unmarkDoneMessage(taskIndexNum));
} else if (command.equals("deadline")) {
System.out.println(makeDeadlineFromInput(s));
} else if (command.equals("event")) {
System.out.println(makeEventFromInput(s));
} else if (command.equals("todo")) {
System.out.println(makeToDoFromInput(s));
} else if (command.equals("delete") && commandList.length > 1 && isInteger(commandList[1])){
int taskIndexNum = Integer.parseInt(commandList[1]);
System.out.println(deleteTaskMessage(taskIndexNum));
} else if (!s.strip().equals("")) {
System.out.println(horizontalBorder + INVALID_USER_INPUT + horizontalBorder);
}
} catch (DukeException e) {
System.out.println(horizontalBorder + e.getMessage() + horizontalBorder);
} finally {
if (!exitNow) {
s = scan.nextLine();
}
}
}
scan.close();
}
public static void main(String[] args) {
Duke sampleDuke = new Duke(new Tasklist());
sampleDuke.run();
// ArrayList<String> a = new ArrayList<>();
// a.add("lol");
// a.add("gan");
// a.add("lasso");
// a.remove(1);
// for (int i = 0; i < a.size(); i++){
// System.out.println(a.get(i));
// }
}
}