-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
QianChangru
committed
Sep 5, 2023
1 parent
611f1ef
commit 4197612
Showing
11 changed files
with
225 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
T | 1 | hihi | ||
T | 0 | hihihi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public class Parser { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Storage { | ||
private String filePath; | ||
|
||
public Storage(String filePath) { | ||
this.filePath = filePath; | ||
} | ||
|
||
public ArrayList<Task> load() throws IOException { | ||
File dir = new File("./data"); | ||
if (!dir.exists()) { | ||
dir.mkdir(); | ||
} | ||
File f = new File(filePath); | ||
f.createNewFile(); | ||
Scanner s = new Scanner(f); | ||
ArrayList<Task> tasks = new ArrayList<>(); | ||
while (s.hasNext()) { | ||
tasks.add(addTask(s.nextLine())); | ||
} | ||
return tasks; | ||
} | ||
|
||
private Task addTask(String input) { | ||
String taskType = input.split(" \\| ")[0]; | ||
boolean isComplete = input.split(" \\| ")[1].equals("1"); | ||
String description = input.split(" \\| ")[2]; | ||
if (taskType.equals("T")) { | ||
return new ToDo(description, isComplete); | ||
} else if (taskType.equals("D")) { | ||
return new Deadline(description, isComplete, input.split(" \\| ")[3]); | ||
} else { | ||
return new Event(description, isComplete, input.split(" \\| ")[3], input.split(" \\| ")[4]); | ||
} | ||
} | ||
|
||
|
||
public void addToFile(ArrayList<Task> list) throws DukeException { | ||
try { | ||
FileWriter fw = new FileWriter(filePath, true); | ||
if (list.size() == 1) { | ||
fw.write(list.get(0).toTxt()); | ||
} else { | ||
fw.write("\n" + list.get(list.size() - 1).toTxt()); | ||
} | ||
fw.close(); | ||
} catch (IOException e) { | ||
throw new DukeException("Unable to write to file."); | ||
} | ||
} | ||
|
||
public void rewriteFile(ArrayList<Task> list) throws DukeException { | ||
try { | ||
FileWriter fw = new FileWriter(filePath); | ||
for (int i = 0; i < list.size(); i++) { | ||
if (i == list.size() - 1) { | ||
fw.write(list.get(i).toTxt()); | ||
} else { | ||
fw.write(list.get(i).toTxt() + "\n"); | ||
} | ||
} | ||
fw.close(); | ||
} catch (IOException e) { | ||
throw new DukeException("Unable to write to file."); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import java.util.ArrayList; | ||
|
||
public class TaskList { | ||
protected ArrayList<Task> tasks; | ||
|
||
public TaskList() { | ||
this.tasks = new ArrayList<>(); | ||
} | ||
|
||
public TaskList(ArrayList<Task> tasks) { | ||
this.tasks = tasks; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return tasks.isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
public class Ui { | ||
} |