Skip to content

Commit

Permalink
fix hardcode file path issue with Jar
Browse files Browse the repository at this point in the history
  • Loading branch information
Masahiro21 committed Mar 5, 2023
1 parent fe90e8e commit fa1be06
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
File renamed without changes.
21 changes: 15 additions & 6 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package duke;
import duke.exceptions.*;

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -25,15 +26,17 @@ public class Duke {
*
* @param filePath the location of the storage file.
*/
public Duke(String filePath){
public Duke(String filePath) throws IOException, StorageFileException {
ui = new Ui();
storage = new Storage(filePath);
try {
taskList = new TaskList(storage.loadFile());
numOfTask = taskList.getNumOfTask();
} catch (FileNotFoundException e) {
File f = new File(filePath);
if(f.createNewFile()){
ui.showCreatingFileMessage();
storage.createFile(filePath);
taskList = new TaskList();
}else{
taskList = new TaskList(storage.loadFile());
numOfTask = taskList.getNumOfTask();
}
ui.showWelcomeMessage(taskList);
}
Expand Down Expand Up @@ -147,7 +150,13 @@ public void run(){
}
}
public static void main(String[] args){
new Duke(filePath).run();
try {
new Duke(filePath).run();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (StorageFileException e) {
throw new RuntimeException(e);
}
}
}

4 changes: 4 additions & 0 deletions src/main/java/duke/exceptions/StorageFileException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package duke.exceptions;

public class StorageFileException extends Exception{
}
10 changes: 4 additions & 6 deletions src/main/java/duke/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import duke.Event;
import duke.ToDo;
import duke.exceptions.EventException;
import duke.exceptions.StorageFileException;
import duke.exceptions.ToDoException;
import duke.Task;

Expand Down Expand Up @@ -42,13 +43,13 @@ public Storage(String userFilePath){
*
* @throws FileNotFoundException if the file cannot be located or does not exist.
*/
public ArrayList<Task> loadFile () throws FileNotFoundException {
public ArrayList<Task> loadFile () throws StorageFileException {
numOfTask = 0;
Scanner sc = null;
try {
sc = new Scanner(path);
} catch (IOException e) {
throw new RuntimeException(e);
return null;
}
while (sc.hasNext()) {
String task = sc.nextLine();
Expand Down Expand Up @@ -83,7 +84,6 @@ public ArrayList<Task> loadFile () throws FileNotFoundException {
}
default:
}
System.out.println(task);
}
return tasksList;
}
Expand All @@ -97,10 +97,8 @@ public ArrayList<Task> loadFile () throws FileNotFoundException {
public void createFile(String filePath){
try{
File f = new File(filePath);
if(f.createNewFile()){
if(f.createNewFile()) {
System.out.println("Your file has been created: " + f.getName());
} else {
System.out.println("Your file already exists");
}
} catch(IOException e) {
System.out.println("Error Occurred");
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/duke/tasklist/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public TaskList(ArrayList<Task> tasksList){
numOfTask = tasks.size();
}

public TaskList(){
tasks = new ArrayList<>();
numOfTask = tasks.size();
}

/**
* Prints out the tasks in the task list provided by the user.
*/
Expand Down

0 comments on commit fa1be06

Please sign in to comment.