Skip to content

Commit 679ce6a

Browse files
committed
Resolve issue with file writing
1 parent bd32a19 commit 679ce6a

8 files changed

Lines changed: 54 additions & 69 deletions

File tree

META-INF/MANIFEST 2.MF

Lines changed: 0 additions & 3 deletions
This file was deleted.

META-INF/MANIFEST.MF

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
Manifest-Version: 1.0
22
Main-Class: Duke
3-

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ test {
4444
}
4545

4646
application {
47-
mainClassName = "seedu.duke.Duke"
47+
mainClassName = "Launcher"
4848
}
4949

5050
shadowJar {

src/main/java/Duke.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import java.io.File;
44

5-
import java.util.ArrayList;
6-
75
import javafx.application.Application;
8-
import javafx.application.Platform;
96
import javafx.scene.Scene;
107
import javafx.scene.control.Label;
118
import javafx.stage.Stage;
@@ -16,8 +13,6 @@
1613
import javafx.scene.layout.VBox;
1714
import javafx.scene.layout.Region;
1815
import javafx.scene.image.Image;
19-
import javafx.scene.image.ImageView;
20-
import javafx.fxml.FXML;
2116

2217
/**
2318
* Main class for Duke
@@ -46,31 +41,23 @@ public Duke() {
4641
this.file = new File("data/duke.txt");
4742

4843
try {
49-
if (storage.hasFile()) {
50-
this.tasks = new TaskList(storage.loadDataFromFile(this.ui));
51-
} else {
52-
throw new DukeException("file does not exist");
53-
}
54-
} catch (DukeException e) {
55-
this.ui.showFileLoadingError();
56-
this.tasks = new TaskList(new ArrayList<>());
44+
this.tasks = new TaskList(storage.loadDataFromFile(this.ui));
45+
} catch (Exception e) {
46+
ui.showFileLoadingError();
5747
}
5848
}
5949

50+
6051
public Duke(String filepath) {
6152
this.storage = new Storage(filepath);
6253
this.ui = new Ui();
6354
this.guiui = new GuiUi();
55+
this.file = new File(filepath);
6456

6557
try {
66-
if (storage.hasFile()) {
67-
this.tasks = new TaskList(storage.loadDataFromFile(this.ui));
68-
} else {
69-
throw new DukeException("file does not exist");
70-
}
71-
} catch (DukeException e) {
72-
this.ui.showFileLoadingError();
73-
this.tasks = new TaskList(new ArrayList<>());
58+
this.tasks = new TaskList(storage.loadDataFromFile(this.ui));
59+
} catch (Exception e) {
60+
ui.showFileLoadingError();
7461
}
7562
}
7663

src/main/java/MainWindow.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import javafx.application.Platform;
1010

11-
import java.util.concurrent.TimeUnit;
12-
1311
/**
1412
* Controller for MainWindow. Provides the layout for the other controls.
1513
*/

src/main/java/duke/Storage.java

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,13 @@
1717
*/
1818
public class Storage {
1919
protected File file;
20+
protected String filepath;
2021

2122
public Storage(String filepath) {
23+
this.filepath = filepath;
2224
this.file = new File(filepath);
2325
}
2426

25-
/**
26-
* checks if hard disk file directory exists
27-
*
28-
* @return whether the hard disk file exists
29-
*/
30-
public boolean hasFile() {
31-
String home = System.getProperty("user.home");
32-
33-
java.nio.file.Path path = java.nio.file.Paths.get(home, "Desktop", "CS2103", "Projects", "iP", "data",
34-
"duke.txt");
35-
return java.nio.file.Files.exists(path);
36-
}
37-
3827
/**
3928
* Writes task to hard disk file
4029
*
@@ -43,28 +32,24 @@ public boolean hasFile() {
4332
*/
4433
public void addTasksToFile(FileWriter writer, List<Task> ls) {
4534
try {
46-
if (writer != null) {
47-
for (Task task : ls) {
48-
// get details about task
49-
String item = task.getItem();
50-
String status = task.getStatus();
51-
String taskType = task.getSign();
52-
53-
// check task type
54-
if (task instanceof Todo) { // if task is to-do
55-
writer.write(taskType + " / " + status + " / " + item + "\n");
56-
} else if (task instanceof Deadline) { // if task is deadline
57-
Deadline actualTask = (Deadline) task;
58-
writer.write(taskType + " / " + status + " / " + item + " / " +
59-
actualTask.getDeadline() + "\n");
60-
} else { // if task is event
61-
Event actualTask = (Event) task;
62-
writer.write(taskType + " / " + status + " / " + item + " / " +
63-
actualTask.getTime() + "\n");
64-
}
35+
for (Task task : ls) {
36+
// get details about task
37+
String item = task.getItem();
38+
String status = task.getStatus();
39+
String taskType = task.getSign();
40+
41+
// check task type
42+
if (task instanceof Todo) { // if task is to-do
43+
writer.write(taskType + " / " + status + " / " + item + "\n");
44+
} else if (task instanceof Deadline) { // if task is deadline
45+
Deadline actualTask = (Deadline) task;
46+
writer.write(taskType + " / " + status + " / " + item + " / " +
47+
actualTask.getDeadline() + "\n");
48+
} else { // if task is event
49+
Event actualTask = (Event) task;
50+
writer.write(taskType + " / " + status + " / " + item + " / " +
51+
actualTask.getTime() + "\n");
6552
}
66-
} else {
67-
throw new IOException("invalid writer");
6853
}
6954
} catch (IOException e) {
7055
System.out.println("An error occurred: invalid writer");
@@ -82,10 +67,15 @@ public List<Task> loadDataFromFile(Ui ui) {
8267
List<Task> ls = new ArrayList<>();
8368

8469
try {
85-
if (this.file != null) {
70+
File directory = new File("data/");
71+
if (!directory.exists()) {
72+
directory.mkdir();
73+
}
74+
75+
if (this.file.exists()) {
8676
sc = new Scanner(this.file);
8777

88-
while(sc.hasNextLine()) {
78+
while (sc.hasNextLine()) {
8979
String line = sc.nextLine();
9080

9181
String[] inputArr = line.split(" / ", 3);
@@ -97,10 +87,14 @@ public List<Task> loadDataFromFile(Ui ui) {
9787
ls.add(newTask);
9888
}
9989
} else {
100-
throw new FileNotFoundException("File not found");
90+
try {
91+
this.file.createNewFile();
92+
} catch (IOException e) {
93+
ui.showCannotCreateFileError();
94+
}
10195
}
10296
} catch (FileNotFoundException e) {
103-
System.out.println("File not found!!");
97+
ui.showFileNotFoundError();
10498
}
10599

106100
return ls;
@@ -177,7 +171,7 @@ public Task createTask(String taskType, String status, String restOfTaskInfo, Ui
177171
* @throws IOException
178172
*/
179173
public void writeToFile(TaskList tasks) throws IOException {
180-
FileWriter writer = new FileWriter(file);
174+
FileWriter writer = new FileWriter(this.file);
181175
this.addTasksToFile(writer, tasks.getls());
182176
writer.close();
183177
}

src/main/java/duke/TaskList.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package duke;
22

3-
import java.time.format.DateTimeFormatter;
43
import java.util.List;
5-
import java.time.LocalDate;
64

75
/**
86
* Handles operations regarding user task list

src/main/java/duke/Ui.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,16 @@ public void showInvalidTaskTypeEditError() {
274274
System.out.println("Im so sorry but I only edit dates of deadlines or events !\n");
275275
System.out.println("??????????????????????????????????????????????????????????????\n");
276276
}
277+
278+
public void showFileNotFoundError() {
279+
System.out.println("??????????????????????????????????????????????????????????????\n");
280+
System.out.println("File not found ...\n");
281+
System.out.println("??????????????????????????????????????????????????????????????\n");
282+
}
283+
284+
public void showCannotCreateFileError() {
285+
System.out.println("??????????????????????????????????????????????????????????????\n");
286+
System.out.println("Cannot create file ...\n");
287+
System.out.println("??????????????????????????????????????????????????????????????\n");
288+
}
277289
}

0 commit comments

Comments
 (0)