Skip to content

Commit 2061878

Browse files
committed
A-MoreOOP (added Tasklist.java, Storage.java, Parser.java and UI.java)
1 parent 75632ce commit 2061878

37 files changed

Lines changed: 537 additions & 151 deletions

src/duke/.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/duke/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/duke/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/duke/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/duke/.idea/workspace.xml

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/duke/Deadline.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
import java.time.format.DateTimeFormatter;
55
import java.time.format.DateTimeParseException;
66

7-
public class Deadline extends Task {
8-
LocalDate by;
7+
public class Deadline extends TimedTask {
98

109
Deadline() {
11-
10+
this.isDone = false;
1211
}
1312
static Deadline parseInput(String input) throws DukeIncompleteCommandException,
1413
DateTimeParseException {
@@ -27,7 +26,7 @@ static Deadline parseInput(String input) throws DukeIncompleteCommandException,
2726
}
2827
deadline.task = inputs[0].trim();
2928
deadline.isDone = false;
30-
deadline.by = LocalDate.parse(inputs[1].trim());
29+
deadline.date = LocalDate.parse(inputs[1].trim());
3130

3231
return deadline;
3332
}
@@ -42,15 +41,15 @@ static Deadline fileReader(String line) {
4241
String[] lines = line.substring(7).trim().split("by: ");
4342
deadline.task = lines[0].substring(0, lines[0].length() - 2).trim();
4443
String dateString = lines[1].substring(0, lines[1].length() - 1);
45-
deadline.by = LocalDate.parse(dateString.subSequence(0, dateString.length()));
44+
deadline.date = LocalDate.parse(dateString.subSequence(0, dateString.length()));
4645
return deadline;
4746
}
4847
@Override
4948
public String toString() {
50-
return String.format("DDLN%s (by: %s)" ,
51-
super.toString(), by);
49+
return String.format("DDLN%s (by: %s)" , super.toString(),
50+
date.format(DateTimeFormatter.ofPattern("MMM d yyyy")));
5251
}
53-
public String stringToSave() {
54-
return String.format("DDLN%s (at: %s)" , super.toString(), by);
52+
public String toFileString() {
53+
return String.format("DDLN%s (by: %s)" , super.toString(), date);
5554
}
5655
}

src/duke/Duke.java

Lines changed: 7 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2,132 +2,22 @@
22

33
import java.util.Scanner;
44
import java.util.ArrayList;
5-
import java.io.File;
6-
import java.io.FileWriter;
7-
import java.io.IOException;
85
import java.time.format.DateTimeParseException;
96

107
public class Duke {
118
public static void main(String[] args) {
12-
ArrayList<Task> list = loadFile();
13-
String logo = " ____ _ \n"
14-
+ "| _ \\ _ _| | _____ \n"
15-
+ "| | | | | | | |/ / _ \\\n"
16-
+ "| |_| | |_| | < __/\n"
17-
+ "|____/ \\__,_|_|\\_\\___|\n";
18-
System.out.println(logo);
19-
System.out.println("Hello! I'm Duke! \n" +
20-
"What would you like to do today? \n" +
21-
"***********************************");
22-
23-
Scanner sc = new Scanner(System.in);
24-
String input = sc.nextLine();
25-
while (!input.equals("bye")) {
26-
Task task;
27-
try {
28-
if (input.equals("list")) {
29-
displayList(list);
30-
System.out.println("\n");
31-
} else if (input.contains("done")) {
32-
String[] command = input.split(" ");
33-
task = list.get(Integer.parseInt(command[1]) - 1);
34-
System.out.println("Good job! I've marked this task as done:\n " +
35-
task.markDone() +
36-
"\n");
37-
} else if (input.contains("delete")) {
38-
String[] command = input.split(" ");
39-
int index = Integer.parseInt(command[1]) - 1;
40-
task = list.get(index);
41-
System.out.println("Alright, I've deleted this task:\n " +
42-
task);
43-
list.remove(index);
44-
saveFile(list);
45-
System.out.println("Now you have " + list.size() +
46-
" task(s) in the list. \n");
47-
} else if (input.contains("todo") ||
48-
input.contains("deadline") ||
49-
input.contains("event")) {
50-
task = Task.parseInput(input);
51-
list.add(task);
52-
saveFile(task.stringToSave());
53-
System.out.println("Alright! I've added this task: \n " +
54-
task + "\nNow you have " + list.size() +
55-
" task(s) in the list. \n");
56-
} else {
57-
throw new DukeInvalidCommandException();
58-
}
59-
} catch (IndexOutOfBoundsException e) {
60-
System.out.println("Oh no! This task does not exist. D:\n" );
61-
} catch (DateTimeParseException e) {
62-
System.out.println("Oh no! " +
63-
" Please key in the date in the format YYYY-MM-DD.\n");
64-
} catch (Exception e) {
65-
System.out.println(e.getMessage());
66-
}
67-
input = sc.nextLine();
68-
}
69-
70-
sc.close();
71-
System.out.println("Bye! Stay on task!");
72-
}
73-
74-
static void displayList(ArrayList<Task> list) {
75-
for(int i = 0; i < list.size(); i++) {
76-
System.out.printf("%d. %s%n",i + 1, list.get(i).toString());
77-
}
78-
}
79-
80-
static Task fileToTask(String input) {
81-
return Task.fileReader(input);
82-
}
83-
84-
static ArrayList<Task> loadFile() {
859
String path = "./data/duke.txt";
8610
String directory = "./data";
87-
File d = new File(directory);
88-
File f = new File(path);
89-
ArrayList<Task> list = new ArrayList<>();
90-
if (!d.exists()){
91-
d.mkdirs();
92-
}
93-
try {
94-
f.createNewFile();
95-
Scanner sc = new Scanner(f);
96-
while (sc.hasNext()) {
97-
try {
98-
String line = sc.nextLine();
99-
Task task = fileToTask(line);
100-
list.add(task);
101-
} catch (Exception e) {}
102-
}
103-
} catch (IOException e) {
104-
System.out.println(e.getMessage());
105-
}
106-
return list;
107-
}
10811

109-
static void saveFile(String input) {
110-
String path = "./data/duke.txt";
111-
try {
112-
FileWriter fw = new FileWriter(path, true);
113-
fw.write(input + System.lineSeparator());
114-
fw.close();
115-
} catch (IOException e) {
116-
}
117-
}
12+
Storage storage = new Storage(path, directory);
13+
TaskList taskList = new TaskList(storage);
14+
taskList.loadList();
15+
Parser parser = new Parser(taskList);
16+
Ui.printWelcome();
17+
parser.parseAll();
11818

119-
static void saveFile(ArrayList<Task> list) {
120-
String path = "./data/duke.txt";
121-
try {
122-
FileWriter fw = new FileWriter(path);
123-
for(Task t: list) {
124-
fw.write(t.toString() + System.lineSeparator());
125-
}
126-
fw.close();
127-
} catch (Exception e) {
128-
System.out.println(e.getMessage());
129-
}
13019
}
20+
13121
}
13222

13323

src/duke/DukeIncompleteCommandException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class DukeIncompleteCommandException extends Exception {
44
public String message;
55
DukeIncompleteCommandException() {
6-
this.message = "Oh no! Task cannot be empty. ):";
6+
this.message = "Oh no! Task cannot be empty. ):\n";
77
}
88
DukeIncompleteCommandException(String message) {
99
this.message = message;

src/duke/DukeInvalidCommandException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package duke;
22

33
public class DukeInvalidCommandException extends Exception {
4-
public String message = "Oh no! I don't know what that means. ):";
4+
public String message = "Oh no! I don't know what that means. ):\n";
55
DukeInvalidCommandException() {
66
}
77

src/duke/Event.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
import java.time.format.DateTimeFormatter;
55
import java.time.format.DateTimeParseException;
66

7-
public class Event extends Task {
8-
LocalDate at;
7+
public class Event extends TimedTask {
98

109
Event() {
11-
10+
this.isDone = false;
1211
}
1312

1413
static Event parseInput(String input) throws DukeIncompleteCommandException,
@@ -29,7 +28,7 @@ static Event parseInput(String input) throws DukeIncompleteCommandException,
2928

3029
event.task = inputs[0].trim();
3130
event.isDone = false;
32-
event.at = LocalDate.parse(inputs[1].trim());
31+
event.date = LocalDate.parse(inputs[1].trim());
3332
return event;
3433
}
3534
static Event fileReader(String line) {
@@ -42,16 +41,16 @@ static Event fileReader(String line) {
4241
String[] lines = line.substring(7).trim().split("at: ");
4342
event.task = lines[0].substring(0, lines[0].length() - 2).trim();
4443
String dateString = lines[1].substring(0, lines[1].length() - 1);
45-
event.at = LocalDate.parse(dateString.subSequence(0, dateString.length()));
44+
event.date = LocalDate.parse(dateString.subSequence(0, dateString.length()));
4645
return event;
4746
}
4847

4948
@Override
5049
public String toString() {
5150
return String.format("EVNT%s (at: %s)" , super.toString(),
52-
at.format(DateTimeFormatter.ofPattern("MMM d yyyy")));
51+
date.format(DateTimeFormatter.ofPattern("MMM d yyyy")));
5352
}
54-
public String stringToSave() {
55-
return String.format("EVNT%s (at: %s)" , super.toString(), at);
53+
public String toFileString() {
54+
return String.format("EVNT%s (at: %s)" , super.toString(), date);
5655
}
5756
}

0 commit comments

Comments
 (0)