-
Notifications
You must be signed in to change notification settings - Fork 197
[irfandeen] iP #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[irfandeen] iP #178
Changes from 25 commits
876151e
e614df3
bf9e4bd
290ffb1
90a643a
827efec
7fec201
b578e77
a2e877e
9c6ad3e
46c910b
39abee7
1918418
cbafecb
6578450
5ca2ee5
03306fb
bba9299
e601a9f
43d21ea
45037e0
34ff431
0aec5e3
43b40c8
e0c8602
543da72
31afe02
9e75e32
da8b95b
fe47074
e6338cd
826535c
bff51c2
0f2aa02
300372a
a1a16ed
d13d5bd
a106526
642d36b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| 0,T,hi | ||
| 1,T,bye | ||
| 1,E,seminar,4pm,6pm | ||
| 0,D,push commits,today 4pm |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # Duke User Guide | ||
| # WallE User Guide | ||
|
|
||
| // Update the title above to match the actual product name | ||
|
|
||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Manifest-Version: 1.0 | ||
| Main-Class: walle.WallE | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package walle; | ||
|
|
||
| public class Deadline extends Task { | ||
| protected String dueDate; | ||
|
|
||
| public Deadline(String description, String dueDate) { | ||
| super(description); | ||
| this.dueDate = dueDate; | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return "[D]"; | ||
| } | ||
|
|
||
| public String getTypeIcon() { | ||
| return "D"; | ||
| } | ||
|
|
||
| public String getDueDate() { | ||
| return dueDate; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return super.toString() + " (by: " + dueDate + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package walle; | ||
|
|
||
| public class Event extends Task { | ||
| protected String startDate; | ||
| protected String endDate; | ||
|
|
||
| public Event(String description, String startDate, String endDate) { | ||
| super(description); | ||
| this.startDate = startDate; | ||
| this.endDate = endDate; | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return "[E]"; | ||
| } | ||
|
|
||
| public String getTypeIcon() { | ||
| return "E"; | ||
| } | ||
|
|
||
| public String getStartDate() { | ||
| return startDate; | ||
| } | ||
|
|
||
| public String getEndDate() { | ||
| return endDate; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return super.toString() + " (from: " + startDate + ", to: " + endDate + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package walle; | ||
|
|
||
| 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 FileParser { | ||
| public static int readFileContents(String filePath, ArrayList<Task> tasks) throws FileNotFoundException { | ||
| File f = new File(filePath); | ||
| Scanner s = new Scanner(f); | ||
| int listSize = 0; | ||
| while (s.hasNext()) { | ||
| String[] taskString = s.nextLine().split(","); | ||
| boolean isDone = taskString[0].equals("1") ? true : false; | ||
| String taskType = taskString[1]; | ||
| String taskDescription = taskString[2]; | ||
|
|
||
| if (taskType.equals("T")) { | ||
| tasks.add(new Todo(taskDescription)); | ||
| } else if (taskType.equals("D")) { | ||
| String dueDate = taskString[3]; | ||
| tasks.add(new Deadline(taskDescription, dueDate)); | ||
| } else if (taskType.equals("E")) { | ||
| String fromDate = taskString[3]; | ||
| String toDate = taskString[4]; | ||
| tasks.add(new Event(taskDescription, fromDate, toDate)); | ||
| } | ||
|
|
||
| if (isDone) { | ||
| tasks.get(listSize).markAsDone(); | ||
| } | ||
|
||
|
|
||
| listSize++; | ||
| } | ||
| s.close(); | ||
| return listSize; | ||
| } | ||
|
|
||
| private static void createFile(String filePath) { | ||
| try { | ||
| File f = new File(filePath); | ||
| if (f.createNewFile()) { | ||
| System.out.println("File created: " + f.getName()); | ||
| FileWriter writer = new FileWriter(f); | ||
| writer.close(); | ||
| } else { | ||
| System.out.println("File already exists."); | ||
| } | ||
| } catch (IOException e) { | ||
| System.out.println("An error occurred while creating the file."); | ||
| } | ||
| } | ||
|
|
||
| public FileParser(String filePath) { | ||
| File f = new File(filePath); | ||
| if (!f.exists()) { | ||
| createFile(filePath); | ||
| } | ||
| } | ||
|
|
||
| public void saveToFile(String filePath, ArrayList<Task> tasks, int listSize) { | ||
| File f = new File(filePath); | ||
| if (!f.exists()) { | ||
| createFile(filePath); | ||
| } | ||
|
|
||
| try { | ||
| FileWriter writer = new FileWriter(f); | ||
| for (int i = 0; i < listSize; i++) { | ||
| Task t = tasks.get(i); | ||
| String status = t.isDone() ? "1" : "0"; | ||
| writer.write(status + "," + t.getTypeIcon() + "," + t.getDescription()); | ||
| if (t.getTypeIcon() == "T") { | ||
| writer.write("\n"); | ||
| } else if (t.getTypeIcon() == "D") { | ||
| Deadline d = (Deadline) t; | ||
| writer.write("," + d.getDueDate() + "\n"); | ||
| } else if (t.getTypeIcon() == "E") { | ||
| Event e = (Event) t; | ||
| writer.write("," + e.getStartDate() + "," + e.getEndDate() + "\n"); | ||
| } | ||
| } | ||
| writer.close(); | ||
|
Comment on lines
+70
to
+
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is around 3 levels of indentation, consider refactoring the internal levels into another method. |
||
| } catch (Exception e) { | ||
| System.out.println("An error occurred while saving the file."); | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package walle; | ||
|
|
||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
|
||
| public Task(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| return (isDone ? "[X]" : "[ ]"); | ||
| } | ||
|
|
||
| public void markAsDone() { | ||
| isDone = true; | ||
| } | ||
|
|
||
| public void unmarkAsDone() { | ||
| isDone = false; | ||
| } | ||
|
|
||
| public String getType() { | ||
| return "[ ]"; | ||
| } | ||
|
|
||
| public String getTypeIcon() { | ||
| return " "; | ||
| } | ||
|
|
||
| public boolean isDone() { | ||
| return isDone; | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return getType() + getStatusIcon() + " " + description; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package walle; | ||
|
|
||
| public class Todo extends Task { | ||
| public Todo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| @Override | ||
| public String getType() { | ||
| return "[T]"; | ||
| } | ||
|
|
||
| public String getTypeIcon() { | ||
| return "T"; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using more intuitive naming,
fandsdo not explain what the variables entail.