-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathDuke.java
More file actions
189 lines (155 loc) · 6.9 KB
/
Duke.java
File metadata and controls
189 lines (155 loc) · 6.9 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
178
179
180
181
182
183
184
185
186
187
188
189
import java.util.*;
public class Duke {
static void level1() {
System.out.println("Hello! I'm Duke\n" + "What can I do for you?");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
while (!input.equals("bye")) {
System.out.println(input);
input = sc.nextLine();
}
System.out.println("Bye. Hope to see you again soon!");
}
static void level2() {
System.out.println("Hello! I'm Duke\n" + "What can I do for you?");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
List<String> lst = new ArrayList<>();
while (!input.equals("bye")) {
if (!input.equals("list")) {
lst.add(input);
System.out.println("added: " + input);
} else {
for (int i = 1; i <= lst.size(); i++) {
System.out.println(i + ". " + lst.get(i-1));
}
}
input = sc.nextLine();
}
System.out.println("Bye. Hope to see you again soon!");
}
static void level3() {
List<Task> lst = new ArrayList<>();
lst.add(new Task("read book"));
lst.add(new Task("return book"));
lst.add(new Task("buy bread"));
lst.get(0).markAsDone();
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
String[] inputArray = input.split(" ");
while (true) {
if (input.equals("list")) {
System.out.println("Here are the tasks in your list:");
for (int i = 1; i <= lst.size(); i++) {
System.out.println(i + "." + lst.get(i - 1));
}
} else if (inputArray[0].equals("done")) {
System.out.println("Nice! I've marked this task as done:");
lst.get(Integer.parseInt(inputArray[1]) - 1).markAsDone();
System.out.println(" " + lst.get(Integer.parseInt(inputArray[1]) - 1));
}
input = sc.nextLine();
inputArray = input.split(" ");
}
}
static void level4() throws DukeException {
List<Task> lst = new ArrayList<>();
lst.add(new ToDoTask("read book"));
lst.add(new DeadlineTask("return book", "June 6th"));
lst.add(new EventTask("project meeting", "Aug 6th 2-4pm"));
lst.add(new ToDoTask("join sports club"));
lst.get(0).markAsDone();
lst.get(3).markAsDone();
Scanner sc = new Scanner(System.in);
String input;
String[] inputArray;
while (sc.hasNextLine()) {
input = sc.nextLine();
inputArray = input.split(" ");
if (inputArray[0].equals("list")) {
System.out.println("Here are the tasks in your list:");
for (int i = 1; i <= lst.size(); i++) {
System.out.println(i + "." + lst.get(i - 1));
}
} else if (inputArray[0].equals("todo") || inputArray[0].equals("deadline") || inputArray[0].equals("event")) { //adding a task
Task task = new Task("");
String[] inputArr1;
String[] inputArr2;
if (inputArray[0].equals("todo")) {
if (inputArray.length == 1) {
throw new DukeException("☹ OOPS!!! The description of a todo cannot be empty.");
}
inputArr1 = input.split("todo ");
task = new ToDoTask(inputArr1[1]);
} else if (inputArray[0].equals("deadline")) {
if (inputArray.length == 1) {
throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.");
}
inputArr1 = input.split("deadline ");
inputArr2 = inputArr1[1].split(" /by ");
task = new DeadlineTask(inputArr2[0], inputArr2[1]);
} else if (inputArray[0].equals("event")) {
if (inputArray.length == 1) {
throw new DukeException("☹ OOPS!!! The description of a deadline cannot be empty.");
}
inputArr1 = input.split("event ");
inputArr2 = inputArr1[1].split(" /at ");
task = new EventTask(inputArr2[0], inputArr2[1]);
}
lst.add(task);
System.out.println("Got it. I've added this task: ");
System.out.println(" " + task);
System.out.println("Now you have " + lst.size() + " tasks in the list.");
} else {
throw new DukeException("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
}
}
static void level6() throws DukeException {
List<Task> lst = new ArrayList<>();
lst.add(new ToDoTask("read book"));
lst.add(new DeadlineTask("return book", "June 6th"));
lst.add(new EventTask("project meeting", "Aug 6th 2-4pm"));
lst.add(new ToDoTask("join sports club"));
lst.add(new ToDoTask("borrow book"));
lst.get(0).markAsDone();
lst.get(1).markAsDone();
lst.get(3).markAsDone();
Scanner sc = new Scanner(System.in);
String input;
String[] inputArray;
while (sc.hasNextLine()) {
input = sc.nextLine();
inputArray = input.split(" ");
if (inputArray[0].equals("list")) {
System.out.println("Here are the tasks in your list:");
for (int i = 1; i <= lst.size(); i++) {
System.out.println(i + "." + lst.get(i - 1));
}
} else if (inputArray[0].equals("delete")) { //deleting a task
if (inputArray.length == 1) {
throw new DukeException("☹ OOPS!!! The task number has not been specified.");
} else if (Integer.parseInt(inputArray[1]) > lst.size()) {
throw new DukeException("☹ OOPS!!! This task number does not exist.");
}
System.out.println("Noted. I've removed this task:");
System.out.println(" " + lst.get(Integer.parseInt(inputArray[1]) - 1));
lst.remove(Integer.parseInt(inputArray[1]) - 1);
System.out.println("Now you have " + lst.size() + " tasks in the list.");
}
}
}
public static void main(String[] args) {
try {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
level6();
} catch (DukeException e) {
System.out.println(e);
}
}
}