-
Notifications
You must be signed in to change notification settings - Fork 479
[ChangruHenryQian] iP #544
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?
Changes from 16 commits
28ad2b8
ed6d4d2
5080e17
3d4a50f
fe29b0f
02481a9
91639a8
cab0141
28266b3
611f1ef
4197612
c0ea8a2
bf86dc1
992ec01
113e1d3
1f284c9
92e988c
5c4a3ba
d197fc2
e450bed
2a0dedd
8ac5391
dcc4829
98baa49
cb8b5d6
a011a9e
af45b36
b0af1ec
1d67ccf
b47f650
8e4c58e
c529dc9
b88d605
cfc25a9
2a192a3
9ff4bbc
76ab079
d1c94f8
3b3712a
b76decc
59658eb
e20422e
f1c1337
ed26f34
a146f5a
fe210a3
b761c70
3f8b8db
9e2ec38
925e859
5c6a409
950bfdf
b02a0ee
bf0487d
bac1dfd
3600c25
fd641b2
c32f9db
5e157e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package duke; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Locale; | ||
|
|
||
| public class Deadline extends Task { | ||
| private LocalDate by; | ||
| public Deadline(String description, LocalDate by) { | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
|
|
||
| public Deadline(String description, boolean isDone, LocalDate by) { | ||
| super(description, isDone); | ||
| this.by = by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D]" + super.toString() + " (by: " | ||
| + this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toTxt() { | ||
| return "D | " + super.toTxt() | ||
| + " | " + this.by.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package duke; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class Duke { | ||
|
|
||
| private Storage storage; | ||
| private TaskList tasks; | ||
| private Ui ui; | ||
|
|
||
| public Duke(String filePath) { | ||
| ui = new Ui(); | ||
| storage = new Storage(filePath); | ||
| try { | ||
| tasks = new TaskList(storage.load(), storage); | ||
| } catch (DukeException e) { | ||
| ui.printException(e); | ||
| tasks = new TaskList(storage); | ||
| } catch (IOException e) { | ||
| ui.printException(new DukeException("Unable to find storage file.")); | ||
| tasks = new TaskList(storage); | ||
| } | ||
| } | ||
|
|
||
| public void run() { | ||
| Ui.welcomeMessage(); | ||
| Scanner userInput = new Scanner(System.in); | ||
| while (true) { | ||
| String command = userInput.nextLine(); | ||
| Parser p = new Parser(command, this.storage, this.tasks); | ||
| p.parse(); | ||
| if (p.isEnd()) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| new Duke("data/tasks.txt").run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package duke; | ||
|
|
||
| public class DukeException extends RuntimeException{ | ||
| public DukeException(String message) { | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package duke; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.util.Locale; | ||
|
|
||
| public class Event extends Task{ | ||
| private LocalDate startTime; | ||
| private LocalDate endTime; | ||
| public Event(String description, LocalDate startTime, LocalDate endTime) { | ||
| super(description); | ||
| this.startTime = startTime; | ||
| this.endTime = endTime; | ||
| } | ||
|
|
||
| public Event(String description, boolean isDone, LocalDate startTime, LocalDate endTime) { | ||
| super(description, isDone); | ||
| this.startTime = startTime; | ||
| this.endTime = endTime; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E]" + super.toString() + " (from: " + startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) | ||
| + " to: " + endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toTxt() { | ||
| return "E | " + super.toTxt() + " | " + this.startTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)) | ||
| + " | " + this.endTime.format(DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package duke; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| public class Parser { | ||
| private String command; | ||
| private Storage storage; | ||
| private TaskList taskList; | ||
| private boolean isEnd = false; | ||
|
|
||
| public Parser(String command, Storage storage, TaskList taskList) { | ||
| this.command = command; | ||
| this.storage = storage; | ||
| this.taskList = taskList; | ||
| } | ||
|
|
||
| public void parse() { | ||
| try{ | ||
| if (command.startsWith("list")) { | ||
| this.taskList.listTasks(); | ||
| } else if (command.startsWith("mark")) { | ||
| this.taskList.markTask(Integer.valueOf(command.split(" ")[1]) - 1); | ||
| } else if (command.startsWith("todo")) { | ||
| if (command.split(" ", 2).length == 1) { | ||
| throw new DukeException(" OOPS!!! The description of a todo cannot be empty."); | ||
| } | ||
| ToDo newToDo = new ToDo(command.split(" ", 2)[1]); | ||
| this.taskList.addTask(newToDo); | ||
| } else if (command.startsWith("deadline")) { | ||
| LocalDate deadline = LocalDate.parse(command.split(" /by ", 2)[1]); | ||
| String name = command.split(" /by ", 2)[0].split(" ", 2)[1]; | ||
|
||
| Deadline newDeadline = new Deadline(name, deadline); | ||
| this.taskList.addTask(newDeadline); | ||
| } else if (command.startsWith("event")) { | ||
| LocalDate startTime = LocalDate.parse(command.split(" /from ", 2)[1] | ||
| .split(" /to ", 2)[0]); | ||
| LocalDate endTime = LocalDate.parse(command.split(" /to ", 2)[1]); | ||
| String name = command.split(" /from ", 2)[0].split(" ", 2)[1]; | ||
| Event newEvent = new Event(name, startTime, endTime); | ||
| this.taskList.addTask(newEvent); | ||
| } else if (command.startsWith("delete")) { | ||
| this.taskList.deleteTask(Integer.valueOf(command.split(" ")[1]) - 1); | ||
| } else if (command.startsWith("bye")) { | ||
| this.isEnd = true; | ||
| Ui.farewellMessage(); | ||
| } else { | ||
| throw new DukeException(" OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
| } | ||
| } catch (DukeException e) { | ||
| Ui.printException(e); | ||
| } | ||
| } | ||
|
|
||
| public boolean isEnd() { | ||
| return this.isEnd; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package duke; | ||
|
|
||
| import java.io.File; | ||
|
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. I like how you have organised your imports according to their functionality. It's clear and easy to read. Great job! |
||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
|
|
||
| import java.util.Locale; | ||
| import java.time.LocalDate; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Storage { | ||
| private final 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")) { | ||
| LocalDate d = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); | ||
| return new Deadline(description, isComplete, d); | ||
| } else { | ||
| LocalDate start = LocalDate.parse(input.split(" \\| ")[3], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); | ||
| LocalDate end = LocalDate.parse(input.split(" \\| ")[4], DateTimeFormatter.ofPattern("MMM dd yyyy", Locale.ENGLISH)); | ||
| return new Event(description, isComplete, start, end); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public void addOneLineToFile(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."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package duke; | ||
|
|
||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
|
||
| public Task(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public Task(String description, boolean isDone) { | ||
| this.description = description; | ||
| this.isDone = isDone; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| return (isDone ? "X" : " "); // mark done task with X | ||
| } | ||
|
|
||
| public void markAsDone() { | ||
| this.isDone = true; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[" + this.getStatusIcon() + "] " + this.description; | ||
| } | ||
|
|
||
| public String toTxt() { | ||
|
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. just a trivial issue but would spelling out the txt improve the readability of your code? |
||
| return (this.isDone ? "1" : "0") + " | " + this.description; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package duke; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class TaskList { | ||
| protected ArrayList<Task> tasks; | ||
| private Storage storage; | ||
|
|
||
| public TaskList(Storage storage) { | ||
| this.tasks = new ArrayList<>(); | ||
| this.storage = storage; | ||
| } | ||
|
|
||
| public TaskList(ArrayList<Task> tasks, Storage storage) { | ||
| this.tasks = tasks; | ||
| this.storage = storage; | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return tasks.isEmpty(); | ||
| } | ||
| public void listTasks() { | ||
| Ui.listTasks(this.tasks); | ||
| } | ||
|
|
||
| public void addTask(Task t) { | ||
| this.tasks.add(t); | ||
| Ui.addTask(t, this.tasks); | ||
| this.storage.addOneLineToFile(this.tasks); | ||
| } | ||
|
|
||
| public void deleteTask(int num) { | ||
| Task re = tasks.remove(num); | ||
| this.storage.rewriteFile(tasks); | ||
| Ui.deleteTask(re, tasks); | ||
| } | ||
|
|
||
| public void markTask(int num) { | ||
| Task t = tasks.get(num); | ||
| t.markAsDone(); | ||
| Ui.markTask(t); | ||
| this.storage.rewriteFile(tasks); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package duke; | ||
|
|
||
| public class ToDo extends Task { | ||
| public ToDo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| public ToDo(String description, boolean isDone) { | ||
| super(description, isDone); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[T]" + super.toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toTxt() { | ||
| return "T | " + super.toTxt(); | ||
| } | ||
| } |
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.
using switch statements may help reduce nesting. what do you think?