Skip to content

Commit

Permalink
Handled invalid filepath
Browse files Browse the repository at this point in the history
  • Loading branch information
WinstonLeonard committed Sep 20, 2023
1 parent 72e7ee1 commit af7ebaa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions duke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[T][ ] CS2100 Homework
10 changes: 6 additions & 4 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package duke;


import java.io.IOException;

import javafx.application.Application;
import javafx.stage.Stage;

Expand All @@ -21,8 +24,9 @@ public Duke(String filePath) {
/**
* Old constructor
*/
public Duke() {
String filePath = "../ip/src/main/data/duke.txt";
public Duke() throws IOException {
//String filePath = "../ip/src/main/data/duke.txt";
String filePath = "./duke.txt";
tasks = new TaskList();
storage = new Storage(filePath, tasks);
storage.startStorage();
Expand All @@ -48,8 +52,6 @@ public void start(Stage stage) {

Window.addUserInput();

Window.welcomeMessage();

}

}
8 changes: 5 additions & 3 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public Storage(String filepath, TaskList tasks) {
/**
* Starts reading the storage
*/
public void startStorage() {
public void startStorage() throws IOException {
try {
reader = new BufferedReader(new FileReader(filepath));
Window.welcomeMessage();
String fileLine;

while ((fileLine = reader.readLine()) != null) {
Expand All @@ -44,8 +45,9 @@ public void startStorage() {
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("The file named duke.txt does not exist.");
writer = new BufferedWriter(new FileWriter("duke.txt"));
Window.sendMessage("The file duke.txt does not exist. I've just created that file for you. Please close "
+ "and reopen the program.");
} catch (EmptyDescriptionException e) {
//Already handled by EmptyDescriptionException
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/duke/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ public static void welcomeMessage() {
dialogContainer.getChildren().add(DialogBox.getDukeDialog(welcomeMessage, dukeImageView, welMessage));
}

/**
* Sends a custom message
*/
public static void sendMessage(String message) {
CircleClip clip = new CircleClip(40, 40, 40);
ImageView dukeImageView = new ImageView(duke);
clip.clip(dukeImageView);
Label welcomeMessage = new Label(message);
dialogContainer.getChildren().add(DialogBox.getDukeDialog(welcomeMessage, dukeImageView, message));
}
/**
* Sends error message
*/
Expand Down

0 comments on commit af7ebaa

Please sign in to comment.