Skip to content

Commit b677242

Browse files
committed
Refactor code to include packages and more OOP
1 parent c782b58 commit b677242

23 files changed

+510
-284
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Duke project template
1+
# duke.Duke project template
22

33
This is a project template for a greenfield Java project. It's named after the Java mascot _Duke_. Given below are instructions on how to use it.
44

@@ -13,7 +13,7 @@ Prerequisites: JDK 11, update Intellij to the most recent version.
1313
1. If there are any further prompts, accept the defaults.
1414
1. Configure the project to use **JDK 11** (not other versions) as explained in [here](https://www.jetbrains.com/help/idea/sdk.html#set-up-jdk).<br>
1515
In the same dialog, set the **Project language level** field to the `SDK default` option.
16-
3. After that, locate the `src/main/java/Duke.java` file, right-click it, and choose `Run Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
16+
3. After that, locate the `src/main/java/duke.Duke.java` file, right-click it, and choose `Run duke.Duke.main()` (if the code editor is showing compile errors, try restarting the IDE). If the setup is correct, you should see something like the below as the output:
1717
```
1818
Hello from
1919
____ _

src/main/java/Duke.java

-255
This file was deleted.

src/main/java/Main.java

-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +0,0 @@
1-
import java.util.Scanner;
2-
3-
public class Main {
4-
public static void main(String[] args) {
5-
Duke duke = new Duke();
6-
duke.loadFile();
7-
duke.greet();
8-
boolean loop = true;
9-
Scanner scanner = new Scanner(System.in); // Create a Scanner object
10-
while(loop) {
11-
String input = scanner.nextLine();
12-
loop = duke.process(input);
13-
}
14-
15-
}
16-
}

src/main/java/duke/Duke.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package duke;
2+
3+
import duke.command.Command;
4+
import java.util.Scanner;
5+
6+
public class Duke {
7+
8+
private Storage storage;
9+
private TaskList tasks;
10+
private Ui ui;
11+
12+
public Duke(String filePath) {
13+
ui = new Ui();
14+
storage = new Storage(filePath);
15+
try {
16+
tasks = new TaskList(storage.load(), storage);
17+
} catch (DukeException e) {
18+
ui.showLoadingError();
19+
tasks = new TaskList(storage);
20+
}
21+
}
22+
23+
public void run() {
24+
ui.greet();
25+
boolean isExit = false;
26+
Scanner scanner = new Scanner(System.in); // Create a Scanner object
27+
while (!isExit) {
28+
String input = scanner.nextLine();
29+
try {
30+
Command c = Parser.parse(input);
31+
c.execute(tasks, ui, storage);
32+
isExit = c.isExit();
33+
} catch (DukeException e) {
34+
ui.printError(e.getMessage());
35+
}
36+
}
37+
}
38+
39+
public static void main(String[] args) {
40+
new Duke("data/tasks.txt").run();
41+
}
42+
43+
}

src/main/java/duke/DukeException.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package duke;
2+
3+
public class DukeException extends Exception {
4+
public DukeException(String errorMessage) {
5+
super(errorMessage);
6+
}
7+
}

0 commit comments

Comments
 (0)