-
Notifications
You must be signed in to change notification settings - Fork 461
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
[Lik Hern] iP #488
base: master
Are you sure you want to change the base?
[Lik Hern] iP #488
Changes from 11 commits
556af3f
f0c4e56
4f8f316
f66f01b
744d458
2da77c5
324e1b1
90aead3
c56dde6
89d2bfb
4cd6136
51cda61
a200367
634c9e6
399a384
f8ae081
e1538fb
4e9edd6
abc6e72
120db3d
a97d88e
ead284b
ba7937c
050cef3
d13c5bf
2bb32f4
cbb027c
e75560b
cccbaca
0921acb
859ae32
5e6b548
405f354
7658174
b49dcb9
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.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Deadline extends Task { | ||
protected LocalDate by; | ||
|
||
Deadline(String description, String by) { | ||
super(description); | ||
this.by = LocalDate.parse(by); | ||
} | ||
|
||
@Override | ||
public String fileFormat() { | ||
return String.format("deadline | %s | %s | %b", super.description, by, super.isDone); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("[D]%s (by: %s)", super.toString(), by.format(DateTimeFormatter.ofPattern("MMM dd yyyy"))); | ||
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. Line too long, can break line in the middle |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,123 @@ | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ListIterator; | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
public static void main(String[] args) throws IOException { | ||
List<Task> storage = new ArrayList<>(); | ||
File dataFile = new File("duke.txt"); | ||
if (!dataFile.createNewFile()) { | ||
Scanner fileReader = new Scanner(dataFile); | ||
while (fileReader.hasNextLine()) { | ||
fileReader.useDelimiter(" \\| "); | ||
UserCommand command = UserCommand.valueOf(fileReader.next().toUpperCase()); | ||
Task task; | ||
switch (command) { | ||
case TODO: | ||
String toDoDescription = fileReader.next(); | ||
task = new ToDo(toDoDescription); | ||
storage.add(task); | ||
break; | ||
case DEADLINE: | ||
String deadlineDescription = fileReader.next(); | ||
String deadlineBy = fileReader.next(); | ||
task = new Deadline(deadlineDescription, deadlineBy); | ||
storage.add(task); | ||
break; | ||
case EVENT: | ||
String eventDescription = fileReader.next(); | ||
String eventAt = fileReader.next(); | ||
task = new Event(eventDescription, eventAt); | ||
storage.add(task); | ||
break; | ||
default: | ||
throw new IllegalStateException("Unexpected value: " + command); | ||
} | ||
String isDone = fileReader.reset().skip(" \\| ").nextLine(); | ||
if (isDone.equals("true")) { | ||
task.markAsDoneWithoutMessage(); | ||
} | ||
} | ||
} | ||
System.out.println("Hello! I'm Duke\nWhat can I do for you?"); | ||
Scanner scanner = new Scanner(System.in); | ||
UserInput: while (scanner.hasNextLine()) { | ||
UserCommand command; | ||
try { | ||
command = UserCommand.valueOf(scanner.next().toUpperCase()); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println("OOPS!!! I'm sorry, but I don't know what that means:-("); | ||
scanner.nextLine(); | ||
continue; | ||
} | ||
switch (command) { | ||
case TODO: | ||
String toDoDescription = scanner.nextLine().strip(); | ||
if (toDoDescription.isBlank()) { | ||
System.out.println("OOPS!!! The description of a todo cannot be empty."); | ||
continue; | ||
} | ||
ToDo toDoTask = new ToDo(toDoDescription); | ||
storage.add(toDoTask); | ||
System.out.println("Got it. I've added this task:\n" + toDoTask | ||
+ "\nNow you have " + storage.size() + " tasks in your list."); | ||
break; | ||
case DEADLINE: | ||
scanner.useDelimiter("/by"); | ||
String deadlineDescription = scanner.next().strip(); | ||
scanner.reset().skip("/by"); | ||
String deadlineBy = scanner.nextLine().strip(); | ||
Deadline deadlineTask = new Deadline(deadlineDescription, deadlineBy); | ||
storage.add(deadlineTask); | ||
System.out.println("Got it. I've added this task:\n" + deadlineTask | ||
+ "\nNow you have " + storage.size() + " tasks in the list."); | ||
break; | ||
case EVENT: | ||
scanner.useDelimiter("/at"); | ||
String eventDescription = scanner.next().strip(); | ||
scanner.reset().skip("/at"); | ||
String eventAt = scanner.nextLine().strip(); | ||
Event eventTask = new Event(eventDescription, eventAt); | ||
storage.add(eventTask); | ||
System.out.println("Got it. I've added this task:\n" + eventTask | ||
+ "\nNow you have " + storage.size() + " tasks in the list."); | ||
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. Code seems to repeat the concept of finding some delimiter, perhaps this can be abstracted? |
||
case LIST: | ||
System.out.println("Here are the tasks in your list:"); | ||
ListIterator<Task> listIterator = storage.listIterator(); | ||
while (listIterator.hasNext()) { | ||
System.out.println(listIterator.nextIndex() + 1 + "." + listIterator.next()); | ||
} | ||
break; | ||
case MARK: | ||
int markIndex = scanner.nextInt(); | ||
storage.get(markIndex - 1).markAsDone(); | ||
break; | ||
case UNMARK: | ||
int unmarkIndex = scanner.nextInt(); | ||
storage.get(unmarkIndex - 1).markAsUndone(); | ||
break; | ||
case DELETE: | ||
int deleteIndex = scanner.nextInt(); | ||
Task deletedTask = storage.get(deleteIndex - 1); | ||
storage.remove(deletedTask); | ||
System.out.println("Noted. I've removed this task:\n" + deletedTask | ||
+ "\nNow you have " + storage.size() + " tasks in the list."); | ||
break; | ||
case BYE: | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
break UserInput; | ||
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. Interesting use of Java labels! |
||
} | ||
FileWriter fileWriter = new FileWriter(dataFile); | ||
for (Task task : storage) { | ||
fileWriter.write(task.fileFormat() + System.lineSeparator()); | ||
} | ||
fileWriter.close(); | ||
} | ||
scanner.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Event extends Task { | ||
protected String at; | ||
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 like the Deadline class, perhaps you can change the variable name from "at" to "eventTime"? |
||
|
||
Event(String description, String at) { | ||
super(description); | ||
this.at = at; | ||
} | ||
|
||
@Override | ||
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. Can write java docs |
||
public String fileFormat() { | ||
return String.format("event | %s | %s | %b", super.description, at, super.isDone); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("[E]%s (at: %s)", super.toString(), at); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
public abstract class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
protected Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
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. Coding standard suggests avoiding unnecessary use of |
||
} | ||
|
||
private String getStatusIcon() { | ||
return isDone ? "X" : " "; | ||
} | ||
|
||
public void 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. Can write java docs |
||
if (isDone) { | ||
System.out.println("This task is already marked as done!"); | ||
} else { | ||
this.isDone = true; | ||
System.out.println("Nice! I've marked this task as done:\n" + this); | ||
} | ||
} | ||
|
||
public void markAsDoneWithoutMessage() { | ||
this.isDone = true; | ||
} | ||
|
||
public void markAsUndone() { | ||
if (!isDone) { | ||
System.out.println("This task is already marked as not done yet!"); | ||
} else { | ||
this.isDone = false; | ||
System.out.println("OK, I've marked this task as not done yet:\n" + this); | ||
} | ||
} | ||
|
||
public abstract String fileFormat(); | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("[%s] %s", getStatusIcon(), description); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public class ToDo extends Task { | ||
ToDo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String fileFormat() { | ||
return String.format("todo | %s | %b", super.description, super.isDone); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("[T]%s", super.toString()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public enum UserCommand { | ||
TODO, DEADLINE, EVENT, LIST, MARK, UNMARK, DELETE, BYE | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
Hello from | ||
____ _ | ||
| _ \ _ _| | _____ | ||
| | | | | | | |/ / _ \ | ||
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
Hello! I'm Duke | ||
What can I do for you? | ||
Got it. I've added this task: | ||
[T][ ] read book | ||
Now you have 1 tasks in your list. | ||
Got it. I've added this task: | ||
[D][ ] return book (by: June 6th) | ||
Now you have 2 tasks in your list. | ||
Got it. I've added this task: | ||
[E][ ] project meeting (at: Aug 6th 2-4pm) | ||
Now you have 3 tasks in your list. | ||
Nice! I've marked this task as done: | ||
[T][X] read book | ||
This task is already marked as done! | ||
Nice! I've marked this task as done: | ||
[D][X] return book (by: June 6th) | ||
OK, I've marked this task as not done yet: | ||
[D][ ] return book (by: June 6th) | ||
This task is already marked as not done yet! | ||
Here are the tasks in your list: | ||
1.[T][X] read book | ||
2.[D][ ] return book (by: June 6th) | ||
3.[E][ ] project meeting (at: Aug 6th 2-4pm) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
todo read book | ||
deadline return book /by June 6th | ||
event project meeting /at Aug 6th 2-4pm | ||
mark 1 | ||
mark 1 | ||
mark 2 | ||
unmark 2 | ||
unmark 2 | ||
list |
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 the variable as "deadLineTime" instead of "by"?