Skip to content

Commit 335fbd2

Browse files
author
wayne987
committed
A-MoreOOP
1 parent 652ce4d commit 335fbd2

File tree

4 files changed

+123
-72
lines changed

4 files changed

+123
-72
lines changed

data/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
D | 0 | return book | 2019-12-20 18:00
2+
T | 0 | read book

src/main/java/Duke.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
1-
import java.io.IOException;
2-
import java.util.Scanner;
3-
import java.util.ArrayList;
4-
51
public class Duke {
62

7-
public static void main(String[] args) throws DukeException, IOException {
8-
9-
TaskList taskList = new TaskList();
10-
Parser parser = new Parser(taskList);
11-
Storage storage = new Storage(taskList,parser);
12-
13-
Scanner Sc = new Scanner(System.in);
14-
String line = "____________________________\n"
15-
+"____________________________\n";
16-
17-
String logo = "****** ****** ****** ******\n"
18-
+" * * * *\n"
19-
+" * ****** ****** ******\n"
20-
+"* * * * *\n"
21-
+"*** ****** * *\n";
22-
23-
System.out.println("My name is\n" + logo);
24-
System.out.println("What do you want?");
25-
System.out.println(line);
3+
private TaskList taskList;
4+
private Parser parser;
5+
private Storage storage;
6+
private Ui ui;
7+
8+
public Duke() {
9+
this.taskList = new TaskList();
10+
this.parser = new Parser(taskList);
11+
this.ui = new Ui();
12+
try{
13+
this.storage = new Storage(taskList,parser);
14+
} catch (DukeException e) {
15+
ui.showLoadingError();
16+
}
17+
}
2618

19+
public void run() {
20+
ui.printStarting();
2721
while(taskList.isUpdating) {
28-
String[] inputs = Sc.nextLine().trim().split(" ", 2);
29-
parser.ParseCommand(inputs);
30-
System.out.println(line);
22+
String[] fullCommand = ui.readCommand();
23+
parser.ParseCommand(fullCommand);
24+
ui.printLine();
3125
}
32-
33-
storage.saveFile();
3426
}
3527

36-
28+
public void stop() {
29+
try{
30+
storage.saveFile();
31+
}catch(DukeException e){
32+
ui.showSavingError();
33+
}
34+
}
35+
public static void main(String[] args) {
36+
Duke duke = new Duke();
37+
duke.run();
38+
duke.stop();
39+
}
3740

3841
}

src/main/java/Storage.java

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class Storage {
99
Parser parser;
1010
Path storageFilePath;
1111

12-
public Storage(TaskList taskList, Parser parser) throws IOException {
12+
public Storage(TaskList taskList, Parser parser) throws DukeException {
1313
this.taskList = taskList;
1414
this.parser = parser;
1515
storageFilePath = Paths.get(".", "data", "test.txt");
@@ -28,61 +28,63 @@ public Storage(TaskList taskList, Parser parser) throws IOException {
2828
LoadFile();
2929
}
3030

31-
public void LoadFile() throws IOException {
32-
33-
34-
BufferedReader bf = new BufferedReader(new FileReader(storageFilePath.toString()));
35-
String task = bf.readLine();
36-
String[] inputs;
37-
while(task != null){
38-
inputs = task.split(" \\| ",4);
39-
String taskType = inputs[0];
40-
Task newTask;
41-
// for(String s : inputs){
42-
// System.out.print(s);
43-
// }
44-
// System.out.println(inputs.length);
45-
try {
46-
switch (taskType) {
47-
case "T": {
48-
newTask = new Todo(inputs[2]);
49-
break;
50-
}
51-
52-
case "D": {
53-
newTask = Deadline.create(inputs[2], inputs[3]);
54-
break;
55-
}
56-
57-
case "E": {
58-
newTask = Event.create(inputs[2], inputs[3]);
59-
break;
31+
public void LoadFile() throws DukeException {
32+
try {
33+
BufferedReader bf = new BufferedReader(new FileReader(storageFilePath.toString()));
34+
String task = bf.readLine();
35+
String[] inputs;
36+
while (task != null) {
37+
inputs = task.split(" \\| ", 4);
38+
String taskType = inputs[0];
39+
Task newTask;
40+
try {
41+
switch (taskType) {
42+
case "T": {
43+
newTask = new Todo(inputs[2]);
44+
break;
45+
}
46+
47+
case "D": {
48+
newTask = Deadline.create(inputs[2], inputs[3]);
49+
break;
50+
}
51+
52+
case "E": {
53+
newTask = Event.create(inputs[2], inputs[3]);
54+
break;
55+
}
56+
57+
default: {
58+
throw new DukeException("smlj??????");
59+
}
6060
}
6161

62-
default: {
63-
throw new DukeException("smlj??????");
62+
if (inputs[1].equals("1")) {
63+
newTask.complete();
6464
}
65+
taskList.AddTask(newTask, false);
66+
task = bf.readLine();
67+
} catch (DukeException e) {
68+
System.out.println(e.getMessage());
6569
}
6670

67-
if(inputs[1].equals("1")){
68-
newTask.complete();
69-
}
70-
taskList.AddTask(newTask,false);
71-
task = bf.readLine();
72-
}catch(DukeException e){
73-
System.out.println(e.getMessage());
7471
}
75-
72+
}catch(IOException e){
73+
throw new DukeException("unable to load file");
7674
}
7775

7876
}
7977

80-
public void saveFile() throws IOException {
81-
FileWriter fw = new FileWriter(storageFilePath.toString());
78+
public void saveFile() throws DukeException{
79+
try {
80+
FileWriter fw = new FileWriter(storageFilePath.toString());
8281
for (int i = 0; i < taskList.taskList.size(); i++) {
8382
fw.write(taskList.taskList.get(i).safeFileFormat());
8483
}
85-
fw.close();
84+
fw.close();
85+
}catch(IOException e){
86+
throw new DukeException("unable to save file");
87+
}
8688
}
8789

8890
}

src/main/java/Ui.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Scanner;
2+
3+
public class Ui {
4+
5+
Scanner sc;
6+
String line = "____________________________\n"
7+
+"____________________________\n";
8+
9+
String logo = "****** ****** ****** ******\n"
10+
+" * * * *\n"
11+
+" * ****** ****** ******\n"
12+
+"* * * * *\n"
13+
+"*** ****** * *\n";
14+
15+
public Ui(){
16+
sc = new Scanner(System.in);
17+
}
18+
19+
public void showLoadingError(){
20+
System.out.println("******LOADING ERROR******");
21+
}
22+
23+
public void showSavingError(){
24+
System.out.println("******SAVING ERROR******");
25+
}
26+
27+
public void printLine(){
28+
System.out.println(line);
29+
}
30+
31+
public void printLogo(){
32+
System.out.println(logo);
33+
}
34+
35+
public void printStarting(){
36+
System.out.println("My name is\n");
37+
printLogo();
38+
System.out.println("What do you want?");
39+
printLine();
40+
}
41+
42+
public String[] readCommand(){
43+
return sc.nextLine().trim().split(" ", 2);
44+
}
45+
}

0 commit comments

Comments
 (0)