Skip to content

Commit 5b416f1

Browse files
committed
Finished A-MoreOOP
1 parent 8bcf813 commit 5b416f1

6 files changed

Lines changed: 250 additions & 216 deletions

File tree

data/tasks.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
T@@@1@@@a
2-
T@@@0@@@b
3-
T@@@1@@@c
4-
E@@@0@@@asdfa@@@2020-11-11

src/main/java/Duke.java

Lines changed: 4 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import java.io.File;
2-
import java.io.FileNotFoundException;
3-
import java.io.FileWriter;
42
import java.io.IOException;
53
import java.util.ArrayList;
64
import java.util.Scanner;
@@ -12,219 +10,13 @@
1210
- making task an abstract class
1311
- upload from hard drive default expression
1412
- declared get_date abstract even though todo doesn't use it
13+
- support different date formats and time also
1514
*/
1615

1716
public class Duke {
18-
private static final String SPACE = " ";
19-
private static final String LINE = "\n <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>\n";
20-
private static final ArrayList<Task> storage = new ArrayList<>();
21-
private static int count = 0;
22-
2317
public static void main(String[] args) throws IOException {
24-
Scanner sc = new Scanner(System.in);
25-
greet();
26-
boolean exit_now = false;
27-
check_file_folder_specifications();
28-
29-
while (!exit_now) {
30-
String inp = sc.nextLine();
31-
String[] spl = inp.split(" ", 2);
32-
try {
33-
switch (spl[0]) {
34-
case "todo":
35-
check_spl_length(spl, 2, "todo");
36-
process_todo(spl);
37-
break;
38-
case "deadline":
39-
check_spl_length(spl, 2, "deadline");
40-
String[] spl2 = spl[1].split(" /by ", 2);
41-
check_spl_length(spl2, 2, "deadline");
42-
process_deadline(spl2);
43-
break;
44-
case "event":
45-
check_spl_length(spl, 2, "event");
46-
String[] spl3 =spl[1].split(" /at ", 2);
47-
check_spl_length(spl3, 2, "event");
48-
process_event(spl3);
49-
break;
50-
case "done":
51-
check_spl_length(spl, 2, "done");
52-
is_int(spl[1], "done");
53-
process_done(spl);
54-
break;
55-
case "list":
56-
check_spl_length(spl, 1, "list");
57-
process_list();
58-
break;
59-
case "delete":
60-
check_spl_length(spl, 2, "delete");
61-
is_int(spl[1], "delete");
62-
process_delete(spl);
63-
break;
64-
case "bye":
65-
check_spl_length(spl, 1, "bye");
66-
process_bye();
67-
exit_now = true;
68-
break;
69-
default:
70-
throw new InvalidKeywordException();
71-
}
72-
} catch (InvalidTaskFormatException e) {
73-
e.printMessage();
74-
} catch (InvalidNumberException e) {
75-
e.printMessage();
76-
} catch (InvalidKeywordException e) {
77-
e.printMessage();
78-
}
79-
}
80-
}
81-
82-
static void greet() {
83-
System.out.println(LINE + SPACE + "Heyyoo!! I am Luna :D\n" + SPACE + "What can I do for you today?" + LINE);
84-
}
85-
86-
static void is_int(String s, String task) throws InvalidNumberException {
87-
try {
88-
int x = Integer.parseInt(s);
89-
if (x > count) {
90-
throw new InvalidNumberException(LINE + SPACE + "ERROR! D: The number should be smaller than the total number of tasks in the following task: " + task + LINE);
91-
}
92-
} catch (NumberFormatException e){
93-
throw new InvalidNumberException(LINE + SPACE + "ERROR! D: A number should be added for the following task: " + task + LINE);
94-
}
95-
}
96-
97-
98-
static void check_file_folder_specifications() {
99-
try {
100-
File dir = new File("./data");
101-
dir.mkdir();
102-
File f = new File("./data/tasks.txt");
103-
if (!f.createNewFile()) {
104-
upload_from_hard_drive();
105-
}
106-
107-
} catch (IOException e) {
108-
System.out.println("error in making folder/file");
109-
}
110-
}
111-
112-
static void check_spl_length(String[] spl, int x, String task) throws InvalidTaskFormatException {
113-
if (spl.length != x)
114-
throw new InvalidTaskFormatException(LINE + SPACE + "ERROR! D: The format for the following task is wrong: " + task + LINE);
115-
}
116-
117-
/*
118-
static void process_task(String[] spl) throws InvalidTaskFormatException {
119-
Task new_task;
120-
switch(spl[0]) {
121-
case "todo":
122-
new_task = new todo(spl[1]);
123-
break;
124-
case "deadline":
125-
String[] spl2 = spl[1].split(" ", 2);
126-
new_task = new deadline(spl2[1], spl2[2]);
127-
break;
128-
case "event":
129-
String[] spl3 = spl[1].split(" ", 2);
130-
new_task = new event(spl3[1], spl3[2]);
131-
break;
132-
default:
133-
throw new InvalidTaskFormatException("Wrong");
134-
}
135-
storage.add(new_task);
136-
System.out.println(LINE + SPACE + "Done adding the " + spl[0] + "task: " + storage.get(count - 1) + LINE);
137-
}
138-
139-
*/
140-
141-
static void process_todo(String[] spl) throws InvalidTaskFormatException {
142-
storage.add(new todo(spl[1]));
143-
count++;
144-
System.out.println(LINE + SPACE + "Done adding the TODO Task: " + storage.get(count - 1) + LINE);
18+
Ui.greet();
19+
TaskList.check_file_folder_specifications();
20+
Parser.parse();
14521
}
146-
147-
static void process_deadline(String[] spl) {
148-
storage.add(new deadline(spl[0], LocalDate.parse(spl[1])));
149-
count++;
150-
System.out.println(LINE + SPACE + "Done adding the DEADLINE Task: " + storage.get(count - 1) + LINE);
151-
}
152-
153-
static void process_event(String[] spl) {
154-
storage.add(new event(spl[0], LocalDate.parse(spl[1])));
155-
count++;
156-
System.out.println(LINE + SPACE + "Done adding the EVENT Task: " + storage.get(count - 1) + LINE);
157-
}
158-
159-
static void process_done(String[] spl) {
160-
int number = Integer.parseInt(spl[1]);
161-
Task current = storage.get(number - 1);
162-
current.finished();
163-
System.out.println(LINE + SPACE + "Good job! Another Task completed! I have marked it as done:\n" + SPACE + current + LINE);
164-
}
165-
166-
static void process_list() {
167-
System.out.println(LINE);
168-
for (int i = 1; i <= count; i++) {
169-
System.out.println(SPACE + i + ". " + storage.get(i-1));
170-
}
171-
System.out.println(LINE);
172-
}
173-
174-
static void process_delete(String[] spl) {
175-
int num = Integer.parseInt(spl[1]);
176-
System.out.println(LINE + " Deleted the following task: " + storage.get(num - 1) + LINE);
177-
storage.remove(num-1);
178-
count--;
179-
}
180-
181-
static void process_bye() throws IOException {
182-
upload_to_hard_drive();
183-
System.out.println(LINE + SPACE + "Byee, hope to see you again soon!" + LINE);
184-
}
185-
186-
static void upload_from_hard_drive() throws FileNotFoundException {
187-
File f = new File("./data/tasks.txt");
188-
Scanner s = new Scanner(f);
189-
while (s.hasNext()) {
190-
count++;
191-
String[] spl = s.nextLine().split("@@@", 4);
192-
Task to_add;
193-
switch(spl[0]) {
194-
case "T":
195-
to_add = new todo(spl[2]);
196-
break;
197-
case "D":
198-
to_add = new deadline(spl[2], LocalDate.parse(spl[3]));
199-
break;
200-
case "E":
201-
to_add = new event(spl[2], LocalDate.parse(spl[3]));
202-
break;
203-
default:
204-
to_add = new todo("error");
205-
}
206-
if (spl[1].equals("1")) {
207-
to_add.finished();
208-
}
209-
storage.add(to_add);
210-
}
211-
}
212-
213-
static void upload_to_hard_drive() throws IOException {
214-
FileWriter fw = new FileWriter("./data/tasks.txt");
215-
String between = "@@@";
216-
for (Task t : storage) {
217-
String zero = t.get_initial();
218-
String one = t.get_done();
219-
String two = t.get_description();
220-
if (zero.equals("T")) {
221-
fw.write(zero + between + one + between + two + "\n");
222-
} else {
223-
LocalDate three = t.get_date();
224-
fw.write(zero + between + one + between + two + between + three + "\n");
225-
}
226-
}
227-
fw.close();
228-
}
229-
23022
}

src/main/java/Parser.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.io.IOException;
2+
import java.util.Scanner;
3+
4+
public class Parser {
5+
static void parse() throws IOException {
6+
Scanner sc = new Scanner(System.in);
7+
boolean exit_now = false;
8+
while (!exit_now) {
9+
String inp = sc.nextLine();
10+
String[] spl = inp.split(" ", 2);
11+
try {
12+
switch (spl[0]) {
13+
case "todo":
14+
check_spl_length(spl, 2, "todo");
15+
TaskList.process_todo(spl);
16+
break;
17+
case "deadline":
18+
check_spl_length(spl, 2, "deadline");
19+
String[] spl2 = spl[1].split(" /by ", 2);
20+
check_spl_length(spl2, 2, "deadline");
21+
TaskList.process_deadline(spl2);
22+
break;
23+
case "event":
24+
check_spl_length(spl, 2, "event");
25+
String[] spl3 =spl[1].split(" /at ", 2);
26+
check_spl_length(spl3, 2, "event");
27+
TaskList.process_event(spl3);
28+
break;
29+
case "done":
30+
check_spl_length(spl, 2, "done");
31+
is_int(spl[1]);
32+
TaskList.process_done(spl);
33+
break;
34+
case "list":
35+
check_spl_length(spl, 1, "list");
36+
TaskList.process_list();
37+
break;
38+
case "delete":
39+
check_spl_length(spl, 2, "delete");
40+
is_int(spl[1]);
41+
TaskList.process_delete(spl);
42+
break;
43+
case "bye":
44+
check_spl_length(spl, 1, "bye");
45+
TaskList.process_bye();
46+
exit_now = true;
47+
break;
48+
default:
49+
throw new InvalidKeywordException();
50+
}
51+
} catch (InvalidTaskFormatException e) {
52+
e.printMessage();
53+
} catch (InvalidNumberException e) {
54+
e.printMessage();
55+
} catch (InvalidKeywordException e) {
56+
e.printMessage();
57+
}
58+
}
59+
}
60+
61+
static void is_int(String s) throws InvalidNumberException {
62+
try {
63+
int x = Integer.parseInt(s);
64+
if (x > TaskList.getCount())
65+
throw new InvalidNumberException(Ui.InvalidNumberExceptionMessage());
66+
} catch (NumberFormatException e){
67+
throw new InvalidNumberException(Ui.InvalidNumberExceptionMessage());
68+
}
69+
}
70+
71+
72+
static void check_spl_length(String[] spl, int x, String task) throws InvalidTaskFormatException {
73+
if (spl.length != x)
74+
throw new InvalidTaskFormatException(Ui.InvalidTaskFormatExceptionMessage(task));
75+
}
76+
}

src/main/java/Storage.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.File;
2+
import java.io.FileNotFoundException;
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
import java.time.LocalDate;
6+
import java.util.ArrayList;
7+
import java.util.Scanner;
8+
9+
public class Storage {
10+
11+
static int upload_from_hard_drive(ArrayList<Task> storage) throws FileNotFoundException {
12+
int count = 0;
13+
File f = new File("./data/tasks.txt");
14+
Scanner s = new Scanner(f);
15+
while (s.hasNext()) {
16+
count++;
17+
String[] spl = s.nextLine().split("@@@", 4);
18+
Task to_add;
19+
switch(spl[0]) {
20+
case "T":
21+
to_add = new todo(spl[2]);
22+
break;
23+
case "D":
24+
to_add = new deadline(spl[2], LocalDate.parse(spl[3]));
25+
break;
26+
case "E":
27+
to_add = new event(spl[2], LocalDate.parse(spl[3]));
28+
break;
29+
default:
30+
to_add = new todo("error");
31+
}
32+
if (spl[1].equals("1")) {
33+
to_add.finished();
34+
}
35+
storage.add(to_add);
36+
}
37+
return count;
38+
}
39+
40+
static void upload_to_hard_drive(ArrayList<Task>
41+
storage) throws IOException {
42+
FileWriter fw = new FileWriter("./data/tasks.txt");
43+
String between = "@@@";
44+
for (Task t : storage) {
45+
String zero = t.get_initial();
46+
String one = t.get_done();
47+
String two = t.get_description();
48+
if (zero.equals("T")) {
49+
fw.write(zero + between + one + between + two + "\n");
50+
} else {
51+
LocalDate three = t.get_date();
52+
fw.write(zero + between + one + between + two + between + three + "\n");
53+
}
54+
}
55+
fw.close();
56+
}
57+
}

0 commit comments

Comments
 (0)