Skip to content

Commit 241d80c

Browse files
committed
Implement more OOP
Most of Duke's logic was coded into the chatbox function. Seperated logic into different components to make full use of OOP in Java
1 parent 28b62bf commit 241d80c

6 files changed

Lines changed: 445 additions & 5 deletions

File tree

src/main/java/Duke.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ public static void main(String[] args) {
1717
}
1818

1919
public static void chatBot(){
20+
21+
Ui ui = new Ui();
22+
ui.greeting();
23+
24+
Storage st = new Storage("tasklist");
25+
TaskList tl = st.loadTaskList();
26+
27+
ui.getInput(tl);
28+
29+
st.saveTaskList(tl);
30+
31+
//===============================================
32+
33+
/*
2034
System.out.println("Hello! I'm Duke");
2135
System.out.println("What can I do for you?");
2236
@@ -157,12 +171,12 @@ public static void chatBot(){
157171
}
158172
159173
saveTaskList(tl);
160-
System.out.println("Bye. Hope to see you again soon!");
174+
System.out.println("Bye. Hope to see you again soon!"); */
161175

162176

163177
}
164178

165-
private static TaskList loadTaskList(){
179+
/* private static TaskList loadTaskList(){
166180
File f = new File("tasklist");
167181
try {
168182
if (f.createNewFile()) {
@@ -253,11 +267,11 @@ private static void saveTaskList(TaskList tl){
253267
254268
}
255269
256-
}
270+
} */
257271

258272
}
259273

260-
class Task {
274+
/* class Task {
261275
private String name;
262276
private char type;
263277
private LocalDate dateTime;
@@ -366,4 +380,4 @@ public Task getTask(int i) {
366380
return taskList.get(i);
367381
}
368382
369-
}
383+
} */

src/main/java/Parser.java

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.time.*;
4+
import java.time.format.DateTimeParseException;
5+
import java.time.format.DateTimeFormatter;
6+
7+
public class Parser {
8+
9+
private boolean end;
10+
11+
public Parser() {
12+
end = false;
13+
}
14+
15+
public boolean checkEnd() {
16+
return end;
17+
}
18+
19+
public String process(String in, TaskList tl){
20+
21+
try {
22+
23+
String res = "";
24+
25+
String[] split = in.split("\\s");
26+
27+
String cmd = split[0];
28+
29+
switch(cmd) {
30+
case "bye":
31+
end = true;
32+
res = "Bye. Hope to see you again soon!";
33+
break;
34+
case "list":
35+
res = "Here are the tasks in your list:\n";
36+
res += tl.printList();
37+
break;
38+
case "done":
39+
if (split.length <= 1) {
40+
throw new DukeException("OOPS!!! A number needs to be specified.");
41+
}
42+
43+
try {
44+
Task t = tl.markDone(Integer.parseInt(split[1]));
45+
46+
res = "Nice! I've marked this task as done:\n";
47+
res += t;
48+
} catch(NumberFormatException ne) {
49+
throw new DukeException("OOPS!!! A number needs to be specified.");
50+
}
51+
52+
break;
53+
case "todo":
54+
split = in.split("todo\\s");
55+
56+
if (split.length <= 1) {
57+
throw new DukeException("OOPS!!! The description of a todo cannot be empty.");
58+
}
59+
60+
Task td = new Task(split[1]);
61+
tl.add(td);
62+
63+
res = "Got it. I've added this task:\n";
64+
res += td + "\n";
65+
res += "Now you have "+tl.count()+" tasks in the list.";
66+
break;
67+
case "deadline":
68+
split = in.split("deadline\\s");
69+
70+
if (split.length <= 1) {
71+
throw new DukeException("OOPS!!! The description of a deadline cannot be empty.");
72+
}
73+
74+
split = split[1].split("\\s/by\\s");
75+
76+
if (split.length <= 1) {
77+
throw new DukeException("OOPS!!! A date and time is needed.");
78+
}
79+
80+
LocalDate dt = LocalDate.now();
81+
82+
try {
83+
84+
dt = LocalDate.parse(split[1]);
85+
86+
} catch(DateTimeParseException dte) {
87+
throw new DukeException("OOPS!!! The date format should be in YYYY-MM-DD");
88+
}
89+
90+
Task dl = new Task(split[0], 'D', dt);
91+
tl.add(dl);
92+
93+
res = "Got it. I've added this task:\n";
94+
res += dl + "\n";
95+
res += "Now you have "+tl.count()+" tasks in the list.\n";
96+
break;
97+
case "event":
98+
split = in.split("event\\s");
99+
100+
if (split.length <= 1) {
101+
throw new DukeException("OOPS!!! The description of a event cannot be empty.");
102+
}
103+
104+
split = split[1].split("\\s/at\\s");
105+
106+
if (split.length <= 1) {
107+
throw new DukeException("OOPS!!! A date and time is needed.");
108+
}
109+
110+
LocalDate dtt = LocalDate.now();
111+
112+
try {
113+
114+
dtt = LocalDate.parse(split[1]);
115+
116+
} catch(DateTimeParseException e) {
117+
throw new DukeException("OOPS!!! The date format should be in YYYY-MM-DD");
118+
}
119+
120+
Task ev = new Task(split[0], 'E', dtt);
121+
tl.add(ev);
122+
123+
res = "Got it. I've added this task:\n";
124+
res += ev + "\n";
125+
res += "Now you have "+tl.count()+" tasks in the list.\n";
126+
break;
127+
case "delete":
128+
if (split.length <= 1) {
129+
throw new DukeException("OOPS!!! A number needs to be specified.");
130+
}
131+
132+
try {
133+
Task t = tl.remove(Integer.parseInt(split[1]));
134+
135+
res = "Noted. I've removed this task\n";
136+
res += t + "\n";
137+
res += "Now you have "+tl.count()+" tasks in the list.\n";
138+
} catch(NumberFormatException ne) {
139+
throw new DukeException("OOPS!!! A number needs to be specified.");
140+
}
141+
142+
break;
143+
default:
144+
throw new DukeException("OOPS!!! I'm sorry, but I don't know what that means :-(");
145+
146+
}
147+
148+
return res;
149+
150+
} catch(DukeException de) {
151+
return de.getMessage();
152+
}
153+
154+
155+
}
156+
157+
}

src/main/java/Storage.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.time.*;
4+
import java.time.format.DateTimeParseException;
5+
import java.time.format.DateTimeFormatter;
6+
7+
public class Storage {
8+
9+
private File f;
10+
11+
public Storage(String filename) {
12+
f = new File(filename);
13+
}
14+
15+
public TaskList loadTaskList() {
16+
17+
try {
18+
if (f.createNewFile()) {
19+
return new TaskList();
20+
} else {
21+
22+
BufferedReader br = new BufferedReader(new FileReader(f));
23+
24+
TaskList tl = new TaskList();
25+
26+
String line;
27+
while((line = br.readLine()) != null) {
28+
String[] split = line.split("\\|");
29+
30+
// Create new task for each line
31+
char type = split[0].charAt(0);
32+
33+
boolean done = false;
34+
if (split[1].equals("D")) {
35+
done = true;
36+
}
37+
38+
String name = split[2];
39+
40+
if (type == 'D' || type == 'E') {
41+
LocalDate dateTime = LocalDate.parse(split[3]);
42+
43+
Task t = new Task(name, type, dateTime);
44+
if (done)
45+
t.mark();
46+
47+
tl.add(t);
48+
49+
} else {
50+
Task t = new Task(name);
51+
if (done)
52+
t.mark();
53+
54+
tl.add(t);
55+
}
56+
}
57+
58+
return tl;
59+
60+
61+
}
62+
63+
} catch(IOException e) {
64+
System.out.println(e.toString());
65+
return new TaskList();
66+
}
67+
68+
}
69+
70+
public void saveTaskList(TaskList tl){
71+
72+
try {
73+
FileWriter fw = new FileWriter(f, false);
74+
75+
for (int i = 0; i < tl.count(); i++) {
76+
77+
Task t = tl.getTask(i);
78+
79+
String line = "" + t.getType() + "|";
80+
if (t.getDone()) {
81+
line += "D|";
82+
} else {
83+
line += "ND|";
84+
}
85+
86+
line += t.getName();
87+
88+
if (t.getType() == 'D' | t.getType() == 'E') {
89+
line += "|" + t.getDate();
90+
}
91+
92+
line += "\n";
93+
94+
fw.write(line);
95+
96+
}
97+
98+
fw.flush();
99+
fw.close();
100+
101+
} catch (IOException e) {
102+
System.out.println("Error writing to file!");
103+
104+
}
105+
106+
}
107+
108+
}

src/main/java/Task.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.time.*;
4+
import java.time.format.DateTimeParseException;
5+
import java.time.format.DateTimeFormatter;
6+
7+
public class Task {
8+
private String name;
9+
private char type;
10+
private LocalDate dateTime;
11+
private boolean done;
12+
13+
public Task(String n, char t, LocalDate dt) {
14+
name = n;
15+
type = t;
16+
dateTime = dt;
17+
done = false;
18+
}
19+
20+
public Task(String n) {
21+
name = n;
22+
type = 'T';
23+
done = false;
24+
}
25+
26+
public char getType() {
27+
return type;
28+
}
29+
30+
public String getName() {
31+
return name;
32+
}
33+
34+
public String getDate() {
35+
return dateTime.toString();
36+
}
37+
38+
public boolean getDone() {
39+
return done;
40+
}
41+
42+
public void mark() {
43+
done = true;
44+
}
45+
46+
public String toString() {
47+
48+
String str = "["+type+"]";
49+
50+
if (done)
51+
str += "[X] "+name;
52+
else
53+
str += "[ ] "+name;
54+
55+
switch (type) {
56+
case 'D':
57+
str += " (by: "+dateTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy"))+")";
58+
break;
59+
case 'E':
60+
str += " (at: "+dateTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy"))+")";
61+
break;
62+
}
63+
64+
return str;
65+
66+
}
67+
}

0 commit comments

Comments
 (0)