-
Notifications
You must be signed in to change notification settings - Fork 462
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
[Sien Soon] iP #505
base: master
Are you sure you want to change the base?
[Sien Soon] iP #505
Changes from 10 commits
556af3f
dbb7d2f
3dcd64e
f8486c1
ff25932
f9492c8
5551f11
690ced2
65661df
daf1faa
68c04ca
efd5205
e0cdb1b
2a7633a
e81658d
99aabf4
7717770
793b714
6203c9c
7a38405
e0cf62a
32d820c
1ec5154
dc7219e
fcdd02a
102f161
020a8e3
d5f52df
79972fa
2a96419
b50dfe3
a8d3ebc
b3caa65
41554b3
c6c399c
57ec27f
331eb34
e916bf3
d9e999c
a05cbe5
04ce912
eca8928
26c05ac
1080320
f1bb5d6
753caa7
5641335
132a977
2090ffe
27aa548
7388ab3
d38b68b
62b7e1d
3ee3d3d
9f170d3
79dde49
b04f6d8
c6ef386
8cab955
02c42d4
a937afe
f966e87
24eabe0
ac4a600
8813ac4
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,24 @@ | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Deadline extends Task { | ||
|
||
protected LocalDateTime by; | ||
|
||
public Deadline(String description, String by) { | ||
super(description); | ||
this.by = LocalDateTime.parse(by); //Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30. | ||
} | ||
|
||
@Override | ||
public String saveString() { | ||
return "D | " + (this.isDone ? "1 | " : "0 | ") + this.description + " | " + this.by; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + | ||
" (by: " + this.by.format(DateTimeFormatter.ofPattern("MMM d yyyy hh:mm:ss")) + ")"; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,10 +1,235 @@ | ||||||
import java.util.Scanner; | ||||||
import java.util.ArrayList; | ||||||
import java.io.File; | ||||||
import java.io.FileNotFoundException; | ||||||
import java.io.FileWriter; | ||||||
import java.io.IOException; | ||||||
import java.util.List; | ||||||
|
||||||
public class Duke { | ||||||
|
||||||
private final String horizontalLine = "-------------------------"; | ||||||
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. You may want the name of your constant variables like this one to match the coding standard. Perhaps something like:
Suggested change
|
||||||
private ArrayList<Task> taskList; | ||||||
private Scanner sc; | ||||||
private static File saveFile = new File("duke.txt"); | ||||||
|
||||||
public Duke() { | ||||||
this.taskList = new ArrayList<>(); | ||||||
} | ||||||
public static void main(String[] args) { | ||||||
String logo = " ____ _ \n" | ||||||
+ "| _ \\ _ _| | _____ \n" | ||||||
+ "| | | | | | | |/ / _ \\\n" | ||||||
+ "| |_| | |_| | < __/\n" | ||||||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||||||
System.out.println("Hello from\n" + logo); | ||||||
Duke duke = new Duke(); | ||||||
duke.greeting(); | ||||||
duke.run(); | ||||||
duke.goodBye(); | ||||||
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. Maybe you could name the methods as verbs like greet() and exit() instead? |
||||||
} | ||||||
//Solution below adapted from https://github.com/24Donovan24/ip | ||||||
|
||||||
private void run() { | ||||||
if (saveFile.exists()) { | ||||||
load(); | ||||||
} else { | ||||||
initialise(); | ||||||
} | ||||||
} | ||||||
|
||||||
public void load() { | ||||||
try { | ||||||
sc = new Scanner(this.saveFile); | ||||||
while (sc.hasNextLine()) { | ||||||
String taskInFile = sc.nextLine(); | ||||||
String[] taskInArray = taskInFile.split(" \\| "); //The \\ is really equivalent to a single \ (the first \ is required as a Java escape sequence in string literals). | ||||||
// It is then a special character in regular expressions which means "use the next character literally, don't interpret its special meaning" | ||||||
String taskType = taskInArray[0]; | ||||||
switch (taskType) { | ||||||
case "T": { | ||||||
Task task = new ToDo(taskInArray[2]); | ||||||
taskList.add(task); | ||||||
if (taskInArray[1].equals("1")) { | ||||||
task.markAsDone(); | ||||||
} | ||||||
break; | ||||||
} | ||||||
|
||||||
case "D": { | ||||||
Task task = new Deadline(taskInArray[2], taskInArray[3]); | ||||||
taskList.add(task); | ||||||
if (taskInArray[1].equals("1")) { | ||||||
task.markAsDone(); | ||||||
} | ||||||
break; | ||||||
} | ||||||
|
||||||
case "E": { | ||||||
Task task = new Event(taskInArray[2], taskInArray[3]); | ||||||
taskList.add(task); | ||||||
if (taskInArray[1].equals("1")) { | ||||||
task.markAsDone(); | ||||||
} | ||||||
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. Since in each case you are marking the task as done when loading them, maybe u can abstract the process out? |
||||||
break; | ||||||
} | ||||||
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. The indentation for each case statement should be aligned to the switch |
||||||
|
||||||
} | ||||||
} | ||||||
initialise(); | ||||||
} catch (FileNotFoundException exception) { | ||||||
System.out.println("File not found!"); | ||||||
} | ||||||
} | ||||||
|
||||||
|
||||||
private void initialise() { | ||||||
sc = new Scanner(System.in); | ||||||
String command = sc.next(); | ||||||
|
||||||
while (!command.equals("bye")) { | ||||||
try { | ||||||
|
||||||
switch (command) { | ||||||
|
||||||
case "list": { | ||||||
showList(); | ||||||
break; | ||||||
} | ||||||
|
||||||
case "mark": { | ||||||
int index = sc.nextInt() - 1; | ||||||
markTask(index); | ||||||
save(taskList); | ||||||
break; | ||||||
} | ||||||
|
||||||
case "unmark": { | ||||||
int index = sc.nextInt() - 1; | ||||||
unMarkTask(index); | ||||||
save(taskList); | ||||||
break; | ||||||
} | ||||||
|
||||||
case "todo": { | ||||||
String description = sc.nextLine(); | ||||||
setToDo(description); | ||||||
save(taskList); | ||||||
break; | ||||||
} | ||||||
|
||||||
case "deadline": { | ||||||
String input = sc.nextLine(); | ||||||
String description = input.substring(0, input.indexOf("/") - 1); | ||||||
String by = input.substring(input.indexOf("/") + 4); | ||||||
setDeadLine(description, by); | ||||||
save(taskList); | ||||||
break; | ||||||
} | ||||||
|
||||||
case "event": { | ||||||
String input = sc.nextLine(); | ||||||
String description = input.substring(0, input.indexOf("/") - 1); | ||||||
String at = input.substring(input.indexOf("/") + 4); | ||||||
setEvent(description, at); | ||||||
save(taskList); | ||||||
break; | ||||||
} | ||||||
|
||||||
case "delete": { | ||||||
int index = sc.nextInt() - 1; | ||||||
deleteTask(index); | ||||||
save(taskList); | ||||||
break; | ||||||
} | ||||||
|
||||||
default: | ||||||
sc.nextLine(); | ||||||
throw new DukeException("I'm sorry, but I don't know what that means."); | ||||||
} | ||||||
} catch (DukeException exception) { | ||||||
System.out.println(exception); | ||||||
} | ||||||
command = sc.next(); | ||||||
} | ||||||
|
||||||
} | ||||||
|
||||||
private void greeting() { | ||||||
System.out.println("To all Subjects of Ymir. My name is Eren Yeager.\n" + "How can I help you?" + "\n" + horizontalLine); | ||||||
} | ||||||
|
||||||
private void goodBye() { | ||||||
System.out.println(horizontalLine + "\n" + "Keep moving forward until you finish all your tasks. Goodbye." + "\n" + horizontalLine); | ||||||
} | ||||||
|
||||||
private void showList() { | ||||||
System.out.println(horizontalLine + "\n" + "Here are the tasks in your list:"); | ||||||
for (int i = 0; i < taskList.size(); i++) { | ||||||
System.out.println((i + 1) + ". " + taskList.get(i)); | ||||||
} | ||||||
System.out.println(horizontalLine); | ||||||
} | ||||||
|
||||||
private void markTask(int index) { | ||||||
Task completedTask = taskList.get(index); | ||||||
completedTask.markAsDone(); | ||||||
System.out.println(horizontalLine); | ||||||
System.out.println("Nice! I've marked this task as done:" + "\n" + completedTask); | ||||||
System.out.println(horizontalLine); | ||||||
} | ||||||
|
||||||
private void unMarkTask(int index) { | ||||||
Task unfinishedTask = taskList.get(index); | ||||||
unfinishedTask.markAsNotDone(); | ||||||
System.out.println(horizontalLine); | ||||||
System.out.println("OK, I've marked this task as not done yet:" + "\n" + unfinishedTask); | ||||||
System.out.println(horizontalLine); | ||||||
} | ||||||
|
||||||
private void setToDo(String description) throws DukeException { | ||||||
if (description.isEmpty()) { | ||||||
throw new DukeException("The description of todo cannot be empty"); | ||||||
} | ||||||
Task toDo = new ToDo(description); | ||||||
taskList.add(toDo); | ||||||
System.out.println(horizontalLine); | ||||||
System.out.println("Got it. I've added this task:" + "\n" + toDo + "\n" + "Now you have " + taskList.size() | ||||||
+ " tasks in your list."); | ||||||
System.out.println(horizontalLine); | ||||||
} | ||||||
|
||||||
private void setDeadLine(String description, String by) { | ||||||
Task deadLine = new Deadline(description, by); | ||||||
taskList.add(deadLine); | ||||||
System.out.println(horizontalLine); | ||||||
System.out.println("Got it. I've added this task:" + "\n" + deadLine + "\n" + "Now you have " + taskList.size() | ||||||
+ " tasks in your list."); | ||||||
System.out.println(horizontalLine); | ||||||
} | ||||||
|
||||||
private void setEvent(String description, String at) { | ||||||
Task event = new Event(description, at); | ||||||
taskList.add(event); | ||||||
System.out.println(horizontalLine); | ||||||
System.out.println("Got it. I've added this task:" + "\n" + event + "\n" + "Now you have " + taskList.size() | ||||||
+ " tasks in your list."); | ||||||
System.out.println(horizontalLine); | ||||||
} | ||||||
|
||||||
private void deleteTask(int index) { | ||||||
Task toBeDeleted = taskList.get(index); | ||||||
taskList.remove(index); | ||||||
System.out.println(horizontalLine); | ||||||
System.out.println("Noted. I've removed this task:" + "\n" + toBeDeleted + "\n" + "Now you have " + taskList.size() | ||||||
+ " tasks in your list."); | ||||||
System.out.println(horizontalLine); | ||||||
} | ||||||
|
||||||
public void save(List<Task> list) { | ||||||
try { | ||||||
FileWriter writer = new FileWriter(saveFile.getPath()); | ||||||
for (Task task : list) { | ||||||
writer.write(task.saveString() + System.lineSeparator()); | ||||||
} | ||||||
writer.close(); | ||||||
} catch (IOException exception) { | ||||||
System.out.println("I cannot save your tasks in the file."); | ||||||
} | ||||||
} | ||||||
|
||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
public class DukeException extends Exception { | ||
|
||
public DukeException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String horizontalLine = "-------------------------"; | ||
return horizontalLine + "\n" + super.toString() + "\n" + horizontalLine; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
public class Event extends Task { | ||
|
||
protected String at; | ||
|
||
public Event(String description, String at) { | ||
super(description); | ||
this.at = at; | ||
} | ||
|
||
@Override | ||
public String saveString() { | ||
return "E | " + (this.isDone ? "1 | " : "0 | ") + this.description + " | " + this.at; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (at: " + this.at + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
public abstract 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() { | ||
this.isDone = true; | ||
} | ||
|
||
public void markAsNotDone() { | ||
this.isDone = false; | ||
} | ||
|
||
abstract String saveString(); | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + getStatusIcon() + "] " + this.description; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
public class ToDo extends Task { | ||
|
||
public ToDo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String saveString() { | ||
return "T | " + (this.isDone ? "1 | " : "0 | ") + this.description; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,33 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
To all Subjects of Ymir. My name is Eren Yeager. | ||
How can I help you? | ||
------------------------- | ||
------------------------- | ||
DukeException: I'm sorry, but I don't know what that means. | ||
------------------------- | ||
------------------------- | ||
Got it. I've added this task: | ||
[T][ ] borrow book | ||
Now you have 1 tasks in your list. | ||
------------------------- | ||
------------------------- | ||
Got it. I've added this task: | ||
[D][ ] return book (by: Monday) | ||
Now you have 2 tasks in your list. | ||
------------------------- | ||
------------------------- | ||
Here are the tasks in your list: | ||
1. [T][ ] borrow book | ||
2. [D][ ] return book (by: Monday) | ||
------------------------- | ||
------------------------- | ||
Nice! I've marked this task as done: | ||
[D][X] return book (by: Monday) | ||
------------------------- | ||
------------------------- | ||
Here are the tasks in your list: | ||
1. [T][ ] borrow book | ||
2. [D][X] return book (by: Monday) | ||
------------------------- | ||
------------------------- | ||
Keep moving forward until you finish all your tasks. Goodbye. | ||
------------------------- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
eat lunch | ||
todo borrow book | ||
deadline return book /by Monday | ||
list | ||
mark 2 | ||
list | ||
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.
Maybe you could name it as "deadlineTime" instead of "by"?