-
Notifications
You must be signed in to change notification settings - Fork 435
[Qu Mingsi] iP #468
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?
[Qu Mingsi] iP #468
Changes from 14 commits
3b19ba1
9f821c0
f62a7f3
6b59ff6
c1a2ea9
182a931
cba08aa
31df1b6
8cc05e0
518cfd9
b813133
cfa7a2b
8eba1f4
6f69ae0
ada2f9c
376be01
b7ea569
a512baa
d9f7624
e0f008f
ec7e0d3
f8b1a63
b531cfc
9975db1
4092f83
0730d55
060dc2a
bc38e89
16b68c6
384a6b9
713f827
94a4c3d
d515ee4
475d7e2
de7ce07
da2dde7
1c80fa5
3c22118
236630d
1744f64
674b1eb
be78175
07e7a7a
32b0be5
038d9e5
a83a8bc
f9df5c3
58335be
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,21 @@ | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| public class Deadline extends Task{ | ||
| private LocalDateTime time; | ||
|
|
||
| public Deadline(String command, LocalDateTime time) { | ||
| super(command); | ||
| this.time = time; | ||
| } | ||
|
|
||
| public LocalDateTime getTime() { | ||
| return this.time; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D]" + super.toString() + "(by: " + | ||
| time.format(DateTimeFormatter.ofPattern("MMM d yyyy HH:mm")) + ")"; | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,28 @@ | ||
| import java.io.FileNotFoundException; | ||
|
|
||
| public class Duke { | ||
| private Storage storage; | ||
| private TaskList tasks; | ||
| private Ui ui; | ||
| private Parser parser; | ||
|
|
||
| public Duke(String filePath) { | ||
| this.ui = new Ui(); | ||
| this.storage = new Storage(filePath); | ||
| try { | ||
| this.tasks = new TaskList(storage.load()); | ||
| } catch (FileNotFoundException e) { | ||
| ui.showLoadingError(); | ||
| this.tasks = new TaskList(); | ||
| } | ||
| } | ||
|
|
||
| public void run() { | ||
| this.parser = new Parser(ui, tasks, storage); | ||
| parser.run(); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| new Duke("C:\\Users\\e0316059\\Desktop\\cs2103 ip\\src\\main\\java\\data\\duke.txt").run(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public class DukeException extends Exception { | ||
| private String type; | ||
|
|
||
| public DukeException (String type) { | ||
| this.type = type; | ||
| } | ||
|
|
||
| public String getType() { | ||
| return this.type; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| public class Event extends Task { | ||
| private LocalDateTime time; | ||
|
|
||
| public Event(String command, LocalDateTime time) { | ||
| super(command); | ||
| this.time = time; | ||
| } | ||
|
|
||
| public LocalDateTime getTime() { | ||
| return this.time; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E]" + super.toString() + "(at: " + | ||
| time.format(DateTimeFormatter.ofPattern("MMM d yyyy HH:mm")) + ")"; | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import java.util.Scanner; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| public class Parser { | ||
| private static Ui ui; | ||
| private static TaskList tasks; | ||
| private static Storage storage; | ||
|
|
||
| public Parser(Ui ui, TaskList tasks, Storage storage) { | ||
| this.ui = ui; | ||
| this.tasks = tasks; | ||
| this.storage = storage; | ||
| } | ||
|
|
||
| public static void run() { | ||
| Scanner scanner = new Scanner(System.in); | ||
| ui.printLogo(); | ||
| ui.greet(); | ||
| while (scanner.hasNextLine()) { | ||
| String command = scanner.nextLine(); | ||
| try { | ||
| handleCommand(command); | ||
| } catch (DukeException e) { | ||
| ui.showCommandError(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * this method handles two types of error: invalid input (does not contain todo/deadline/event) | ||
|
||
| * and empty task (if the input starts with todo/deadline/event and the content is not empty, | ||
| * it is assumed that the input has correct structure) | ||
| */ | ||
| public static void handleCommand(String command) throws DukeException { | ||
| if (command.equals("bye")) { | ||
| ui.exit(); | ||
| } else if (command.equals("list")) { | ||
| ui.printList(tasks.getTasks()); | ||
| } else if (command.split(" ")[0].equals("done")) { | ||
| int index = Integer.parseInt(command.split(" ")[1]); | ||
| tasks.done(index - 1); | ||
| storage.record(tasks.getTasks()); | ||
| } else if (command.split(" ")[0].equals("delete")) { | ||
| tasks.delete(Integer.parseInt(command.split(" ")[1])); | ||
| storage.record(tasks.getTasks()); | ||
| } else if (command.split(" ")[0].equals("todo")) { | ||
| String taskcommand = command.replace("todo", ""); | ||
| if (!taskcommand.equals("")) { | ||
| tasks.add(new ToDo(taskcommand)); | ||
| } else { | ||
| throw new DukeException("EmptyToDo"); | ||
| } | ||
| storage.record(tasks.getTasks()); | ||
| } else if (command.split(" ")[0].equals("deadline")) { | ||
| String taskcommand = command.split("/")[0].replace("deadline", ""); | ||
| if (!taskcommand.equals("")) { | ||
| String time = command.split("/")[1].replace("by ", ""); | ||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); | ||
| tasks.add(new Deadline(taskcommand, LocalDateTime.parse(time, formatter))); | ||
| } else { | ||
| throw new DukeException("EmptyDeadline"); | ||
| } | ||
| storage.record(tasks.getTasks()); | ||
| } else if (command.split(" ")[0].equals("event")) { | ||
| String taskcommand = command.split("/")[0].replace("event", ""); | ||
| if (!taskcommand.equals("")) { | ||
| String time = command.split("/")[1].replace("at ", ""); | ||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); | ||
| tasks.add(new Deadline(taskcommand, LocalDateTime.parse(time, formatter))); | ||
| } else { | ||
| throw new DukeException("EmptyEvent"); | ||
| } | ||
| storage.record(tasks.getTasks()); | ||
| } else { | ||
| throw new DukeException("invalid"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import java.util.Scanner; | ||
| import java.util.ArrayList; | ||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
|
|
||
| public class Storage { | ||
| private static String filePath; | ||
| private static Ui ui; | ||
|
|
||
| public Storage(String filePath) { | ||
| this.filePath = filePath; | ||
| this.ui = new Ui(); | ||
| } | ||
|
|
||
| public static ArrayList<Task> load() throws FileNotFoundException { | ||
| File file = new File(filePath); | ||
| Scanner sc = new Scanner(file); | ||
| ArrayList<Task> tasks = new ArrayList<Task>(); | ||
| while (sc.hasNextLine()) { | ||
| String[] data = sc.nextLine().split(" \\| "); | ||
| if (data[0].equals("T")) { | ||
| ToDo todo = new ToDo(data[2]); | ||
| tasks.add(todo); | ||
| if (data[1].equals("1")) { | ||
| todo.setDone(); | ||
| } | ||
| } else if (data[0].equals("D")) { | ||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); | ||
| LocalDateTime time = LocalDateTime.parse(data[3], formatter); | ||
| Deadline deadline = new Deadline(data[2], time); | ||
| tasks.add(deadline); | ||
| if (data[1].equals("1")) { | ||
| deadline.setDone(); | ||
| } | ||
| } else if (data[0].equals("E")) { | ||
| DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm"); | ||
| LocalDateTime time = LocalDateTime.parse(data[3], formatter); | ||
| Event event = new Event(data[2], time); | ||
| tasks.add(event); | ||
| if (data[1].equals("1")) { | ||
| event.setDone(); | ||
| } | ||
| } | ||
| } | ||
| return tasks; | ||
| } | ||
|
|
||
| public static void writeToFile(ArrayList<Task> tasks) throws IOException { | ||
| FileWriter fileWriter = new FileWriter(filePath); | ||
| for (int i = 0; i < tasks.size(); i++) { | ||
| Task task = tasks.get(i); | ||
| if (task instanceof ToDo) { | ||
| String text = "T | " + (task.getStatus() ? "1" : "0") + " | " + task.getCommand(); | ||
| fileWriter.write(text + System.lineSeparator()); | ||
| } else if (task instanceof Deadline) { | ||
| String text = "D | " + (task.getStatus() ? "1" : "0") + " | " + task.getCommand() | ||
| + " | " + ((Deadline) task).getTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm")); | ||
| fileWriter.write(text + System.lineSeparator()); | ||
| } else if (task instanceof Event) { | ||
| String text = "E | " + (task.getStatus() ? "1" : "0") + " | " + task.getCommand() | ||
| + " | " + ((Event) task).getTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm")); | ||
| fileWriter.write(text + System.lineSeparator()); | ||
| } | ||
| } | ||
| fileWriter.close(); | ||
| } | ||
|
|
||
| public static void record(ArrayList<Task> tasks) { | ||
| try { | ||
| writeToFile(tasks); | ||
| } catch (IOException e) { | ||
| ui.showIOException(e); | ||
|
||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| public class Task { | ||
| protected String command; | ||
| protected boolean isDone; | ||
|
|
||
| public Task(String command){ | ||
| this.command = command; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public String getCommand() { | ||
| return this.command; | ||
| } | ||
|
|
||
| public boolean getStatus() { | ||
| return this.isDone; | ||
| } | ||
|
|
||
| public void setDone() { | ||
| this.isDone = true; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (isDone) { | ||
| return "[\u2713]" + this.command; | ||
| } else { | ||
| return "[\u2718]" + this.command; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import java.util.ArrayList; | ||
|
|
||
| public class TaskList { | ||
| private static ArrayList<Task> tasks; | ||
| private static Ui ui; | ||
|
|
||
| public TaskList(ArrayList<Task> tasks) { | ||
| this.tasks = tasks; | ||
| this.ui = new Ui(); | ||
| } | ||
|
|
||
| public TaskList() { | ||
| this.tasks = new ArrayList<Task>(); | ||
| this.ui = new Ui(); | ||
| } | ||
|
|
||
| public ArrayList<Task> getTasks() { | ||
| return this.tasks; | ||
| } | ||
|
|
||
| public static void add(Task task) { | ||
| tasks.add(task); | ||
| ui.add(task, tasks); | ||
| } | ||
|
|
||
| public static void done(int n) { | ||
| tasks.get(n-1).setDone(); | ||
| ui.done(n, tasks); | ||
| } | ||
|
|
||
| public static void delete(int n) { | ||
| ui.delete(n, tasks); | ||
| tasks.remove(n-1); | ||
| ui.count(tasks); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public class ToDo extends Task { | ||
|
|
||
| public ToDo(String command){ | ||
| super(command); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(){ | ||
| return "[T]" + super.toString(); | ||
| } | ||
| } |
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.
According to the module's coding standards, should a javadoc comment exist above the method? The same comment for all other public methods.