-
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 11 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,200 @@ | ||
| import java.awt.*; | ||
| import java.util.List; | ||
| 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 Duke { | ||
| private static List<Task> tasks = new ArrayList<>(); | ||
|
|
||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| System.out.println(logo); | ||
|
|
||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| try { | ||
| readFile(); | ||
| } catch (FileNotFoundException e) { | ||
| System.out.println("File not found"); | ||
| } | ||
| greet(); | ||
| while (scanner.hasNextLine()) { | ||
| String command = scanner.nextLine(); | ||
| try { | ||
| handleCommand(command); | ||
| } catch (DukeException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void greet() { | ||
| System.out.println("Hello! I'm Duke\n What can I do for you?"); | ||
| } | ||
|
|
||
| private static void exit() { | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| } | ||
|
|
||
| private static void echo(String command) { | ||
| System.out.println("" + command); | ||
| } | ||
|
|
||
| private static void add(Task task) { | ||
| tasks.add(task); | ||
| System.out.println("Got it. I've added this task:\n" + task.toString() | ||
| + "\n Now you have " + tasks.size() + " tasks in the list."); | ||
| } | ||
|
|
||
| private static void done(Task task) { | ||
| task.setDone(); | ||
| System.out.println("Nice! I've marked this task as done:\n" + task.toString()); | ||
| } | ||
|
|
||
| private static void delete(int index) { | ||
| System.out.println("Noted. I've removed this task:\n" + tasks.get(index-1).toString()); | ||
| tasks.remove(index-1); | ||
| System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
| } | ||
|
|
||
| private static void printList() { | ||
| System.out.println("Here are the tasks in your list:"); | ||
| for (int i = 0; i < tasks.size(); i++) { | ||
| if (tasks.get(i) != null) { | ||
| System.out.println((i + 1) + ". " + tasks.get(i).toString()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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) | ||
| */ | ||
|
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. As per iP task on code quality, should the JavaDoc contain descriptions on the input parameter? |
||
| private static void handleCommand(String command) throws DukeException { | ||
| if (command.equals("bye")) { | ||
| exit(); | ||
| } else if (command.equals("list")) { | ||
| printList(); | ||
| } else if (command.split(" ")[0].equals("done")) { | ||
| int index = Integer.parseInt(command.split(" ")[1]); | ||
| done(tasks.get(index - 1)); | ||
| try { | ||
| writeToFile(); | ||
| } catch (IOException e) { | ||
| System.out.println("Oops! " + e.getMessage()); | ||
| } | ||
| } else if (command.split(" ")[0].equals("delete")) { | ||
| delete(Integer.parseInt(command.split(" ")[1])); | ||
| try { | ||
| writeToFile(); | ||
| } catch (IOException e) { | ||
| System.out.println("Oops! " + e.getMessage()); | ||
| } | ||
| } else if (command.split(" ")[0].equals("todo")) { | ||
| String taskcommand = command.replace("todo", ""); | ||
| if (!taskcommand.equals("")) { | ||
| add(new ToDo(taskcommand)); | ||
| } else { | ||
| throw new DukeException("EmptyToDo"); | ||
| } | ||
| try { | ||
| writeToFile(); | ||
| } catch (IOException e) { | ||
| System.out.println("Oops! " + e.getMessage()); | ||
| } | ||
| } 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"); | ||
| add(new Deadline(taskcommand, LocalDateTime.parse(time, formatter))); | ||
| } else { | ||
| throw new DukeException("EmptyDeadline"); | ||
| } | ||
| try { | ||
| writeToFile(); | ||
| } catch (IOException e) { | ||
| System.out.println("Oops! " + e.getMessage()); | ||
| } | ||
| } 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"); | ||
| add(new Deadline(taskcommand, LocalDateTime.parse(time, formatter))); | ||
| } else { | ||
| throw new DukeException("EmptyEvent"); | ||
| } | ||
| try { | ||
| writeToFile(); | ||
| } catch (IOException e) { | ||
| System.out.println("Oops! " + e.getMessage()); | ||
| } | ||
| } else { | ||
| throw new DukeException("invalid"); | ||
| } | ||
| } | ||
|
|
||
| private static void readFile() throws FileNotFoundException { | ||
|
||
| File file = new File("C:\\Users\\e0316059\\Desktop\\cs2103 ip\\src\\main\\java\\data\\duke.txt"); | ||
| Scanner sc = new Scanner(file); | ||
| while (sc.hasNextLine()) { | ||
| String[] data = sc.nextLine().split(" \\| "); | ||
| if (data[0].equals("T")) { | ||
| ToDo todo = new ToDo(data[2]); | ||
| 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); | ||
| 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); | ||
| add(event); | ||
| if (data[1].equals("1")) { | ||
| event.setDone(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void writeToFile() throws IOException { | ||
| FileWriter fileWriter = new FileWriter("C:\\Users\\e0316059\\Desktop\\cs2103 ip\\src\\main\\java\\data\\duke.txt"); | ||
| 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(); | ||
| fileWriter.write(text + System.lineSeparator()); | ||
| } else if (task instanceof Event) { | ||
| String text = "E | " + (task.getStatus() ? "1" : "0") + " | " + task.getCommand() | ||
| + " | " + ((Event) task).getTime(); | ||
| fileWriter.write(text + System.lineSeparator()); | ||
| } | ||
| } | ||
| fileWriter.close(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| public class DukeException extends Exception { | ||
| private String type; | ||
|
|
||
| public DukeException (String type) { | ||
| this.type = type; | ||
| } | ||
|
|
||
| public String getMessage() { | ||
| if (type.equals("EmptyToDo")) { | ||
| return "OOPS!!! The description of a todo cannot be empty."; | ||
| } | ||
| if (type.equals("EmptyDeadline")) { | ||
| return "OOPS!!! The description of a deadline cannot be empty."; | ||
| } | ||
| if (type.equals("EmptyEvent")) { | ||
| return "OOPS!!! The description of a event cannot be empty."; | ||
| } | ||
| return "OOPS!!! I'm sorry, but I don't know what that means :-("; | ||
| } | ||
| } |
| 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,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,11 @@ | ||
| public class ToDo extends Task { | ||
|
|
||
| public ToDo(String command){ | ||
| super(command); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(){ | ||
| return "[T]" + super.toString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| T | 0 | return book | ||
| D | 0 | meeting | 2020-08-31T12:00 | ||
| D | 0 | homework | 2020-08-31T23:59 | ||
| T | 0 | quiz |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,17 @@ | ||
| Hello from | ||
| ____ _ | ||
| | _ \ _ _| | _____ | ||
| | | | | | | | |/ / _ \ | ||
| | |_| | |_| | < __/ | ||
| |____/ \__,_|_|\_\___| | ||
|
|
||
| Hello! I'm Duke | ||
| What can I do for you? | ||
| Got it. I've added this task: | ||
| [T][✘] borrow book | ||
| Now you have 1 tasks in the list. | ||
| Got it. I've added this task: | ||
| [D][✘] return book (by: Sunday) | ||
| Now you have 2 tasks in the list. | ||
| Got it. I've added this task: | ||
| [E][✘] meeting (at: 1200) | ||
| Now you have 3 tasks in the list. | ||
| Bye. Hope to see you again soon! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| todo borrow book | ||
| deadline return book /by Sunday | ||
| event meeting /at 1200 | ||
| bye |
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.