-
Notifications
You must be signed in to change notification settings - Fork 454
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
[Cheah Yan (Xie Yan)] iP #486
base: master
Are you sure you want to change the base?
Changes from 14 commits
d839859
11af5b5
0caf30f
b228ba5
07274fb
0b02f80
c11d4f6
6f0ba24
24bb9e5
3feae08
881bfc1
b489ae1
588650b
cb928b3
4c03146
ffb8151
eacfa44
77cb385
e48671c
259f0dd
b4671e3
c06a62d
82a5af5
de2dab3
93d8803
e7834d4
d94a1ea
571b9f1
aa2e2b2
9c5ec82
e5bdf9f
860fa28
4315658
f162e12
5bcc063
f0ba15b
fef4323
7bfa996
6d9bcae
0e0a9b8
3aa26c4
b0c8db7
4f13f5f
de42740
d6aa8d4
dd466c1
53f0877
36101c6
ff4e22b
b654417
f6ce0f9
6c5ee40
192813f
4a4d536
6cc0fc7
e89f694
afbc471
e6b2ed4
0bd4ee1
76b4453
08f7db2
394c1c8
3f2132e
24bfe32
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,4 @@ | ||
todo laundry | ||
deadline return book /by 02/12/2019 1800 | ||
event return b ook /at 09/12/2012 1900 | ||
done 2 |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package duke; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Deadline extends Task { | ||
|
||
protected LocalDateTime date; | ||
|
||
public Deadline(String description, String date) { | ||
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. Perhaps a method could have a header comment to inform users about what the method does. Same suggestion for the other methods both in this file and other .java files |
||
super(description); | ||
|
||
DateTimeFormatter scanned = DateTimeFormatter.ofPattern("dd/MM/yyyy HHmm"); | ||
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. Perhaps a more meaningful name for this variable would be datePattern or dateFormat. |
||
this.date = LocalDateTime.parse(date, scanned); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd MMM yyyy hh:mma"); | ||
return "[D]" + super.toString() + " (by: " + this.date.format(dateFormat) + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
package duke; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.sql.SQLOutput; | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
|
||
protected static final String LOCAL_FILE = "data/duke.txt"; | ||
|
||
public static void appendToFile(String filePath, String textToAppend) throws IOException { | ||
FileWriter fw = new FileWriter(filePath, true); // create a FileWriter in append mode | ||
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. leaving comments to help readers enables a better understanding of your code and might even help to hasten the onboarding process. Good job! |
||
fw.write(textToAppend); | ||
fw.close(); | ||
} | ||
|
||
|
||
public static void main(String[] args) { | ||
|
||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
System.out.println("What can I do for you?"); | ||
|
||
Scanner sc = new Scanner(System.in); | ||
String a = sc.nextLine(); | ||
String[] b = a.split(" ", 2); | ||
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. Perhaps it would be good if you could avoid using single letters to name variables unless they are iterator variables. I noticed this issue in other parts too. |
||
|
||
ArrayList<Task> history = new ArrayList<>(); | ||
|
||
File importedFile = new File(LOCAL_FILE); | ||
|
||
try { | ||
importedFile.createNewFile(); | ||
} catch (IOException error) { | ||
System.out.println("Ensure you have created a folder named 'data' within the main project directory!"); | ||
} | ||
|
||
try { | ||
Scanner fileScanner = new Scanner(importedFile); | ||
while (fileScanner.hasNext()) { | ||
String fileData = fileScanner.nextLine(); | ||
String[] details = fileData.split(" ", 2); | ||
|
||
if (details[0].equals("done")) { | ||
int taskIndex = Integer.valueOf(details[1]); | ||
Task completedTask = history.get(taskIndex - 1); | ||
completedTask.Done(); | ||
|
||
} else if (details[0].equals("todo")) { | ||
ToDo task = new ToDo(details[1]); | ||
history.add(task); | ||
|
||
} else if (details[0].equals("deadline")) { | ||
String[] c = details[1].split(" /by ", 2); | ||
Deadline task = new Deadline(c[0], c[1]); | ||
history.add(task); | ||
|
||
} else if (details[0].equals("event")) { | ||
String[] c = details[1].split(" /at ", 2); | ||
Event task = new Event(c[0], c[1]); | ||
history.add(task); | ||
|
||
} else if (details[0].equals("delete")) { | ||
int taskIndex = Integer.valueOf(details[1]); | ||
Task removed = history.get(taskIndex - 1); | ||
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. Perhaps a more meaningful name would be removedTask to indicate that this is the task that had been removed. |
||
|
||
history.remove(taskIndex - 1); | ||
} | ||
} | ||
} catch (FileNotFoundException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
|
||
while (!a.equals("bye")) { | ||
try { | ||
if (a.equals("list")) { | ||
System.out.println("Here are the tasks in your list:"); | ||
|
||
int length = history.size(); | ||
for (int i = 0; i < length; i++) { | ||
System.out.println(String.valueOf(i + 1) + ". " + history.get(i)); | ||
} | ||
a = sc.nextLine(); | ||
b = a.split(" ", 2); | ||
|
||
} else if (b[0].equals("done")) { | ||
try { | ||
int taskIndex = Integer.valueOf(b[1]); | ||
Task completedTask = history.get(taskIndex - 1); | ||
completedTask.Done(); | ||
System.out.println("Nice! I have marked this task as done!"); | ||
System.out.println(completedTask); | ||
|
||
try { | ||
appendToFile(LOCAL_FILE, a + System.lineSeparator()); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
a = sc.nextLine(); | ||
b = a.split(" ", 2); | ||
} catch (ArrayIndexOutOfBoundsException error) { | ||
System.out.println(":(( sorry bud but which specific task is done?"); | ||
|
||
a = sc.nextLine(); | ||
b = a.split(" ", 2); | ||
} | ||
} else if (b[0].equals("todo")) { | ||
try { | ||
ToDo task = new ToDo(b[1]); | ||
history.add(task); | ||
int length = history.size(); | ||
|
||
System.out.println("Added task:"); | ||
System.out.println(task); | ||
System.out.println("You have " + length + " tasks in the list"); | ||
|
||
try { | ||
appendToFile(LOCAL_FILE, a + System.lineSeparator()); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
a = sc.nextLine(); | ||
b = a.split(" ", 2); | ||
} catch (ArrayIndexOutOfBoundsException error) { | ||
System.out.println(":(( sorry bud but the description of your todo cannot be empty!"); | ||
|
||
a = sc.nextLine(); | ||
b = a.split(" ", 2); | ||
} | ||
} else if (b[0].equals("deadline")) { | ||
String[] c = b[1].split(" /by ", 2); | ||
Deadline task = new Deadline(c[0], c[1]); | ||
history.add(task); | ||
int length = history.size(); | ||
|
||
System.out.println("Added task:"); | ||
System.out.println(task); | ||
System.out.println("You have " + String.valueOf(length) + " tasks in the list"); | ||
|
||
try { | ||
appendToFile(LOCAL_FILE, a + System.lineSeparator()); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
a = sc.nextLine(); | ||
b = a.split(" ", 2); | ||
|
||
} else if (b[0].equals("event")) { | ||
String[] c = b[1].split(" /at ", 2); | ||
Event task = new Event(c[0], c[1]); | ||
history.add(task); | ||
int length = history.size(); | ||
|
||
System.out.println("Added task:"); | ||
System.out.println(task); | ||
System.out.println("You have " + String.valueOf(length) + " tasks in the list"); | ||
|
||
try { | ||
appendToFile(LOCAL_FILE, a + System.lineSeparator()); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
a = sc.nextLine(); | ||
b = a.split(" ", 2); | ||
} else if (b[0].equals("delete")) { | ||
int taskIndex = Integer.valueOf(b[1]); | ||
Task removed = history.get(taskIndex - 1); | ||
|
||
history.remove(taskIndex - 1); | ||
int length = history.size(); | ||
|
||
System.out.println("Ok! I have removed this task:"); | ||
System.out.println(removed); | ||
System.out.println("Now you have " + String.valueOf(length) + " tasks in the list."); | ||
|
||
try { | ||
appendToFile(LOCAL_FILE, a + System.lineSeparator()); | ||
} catch (IOException e) { | ||
System.out.println("Something went wrong: " + e.getMessage()); | ||
} | ||
|
||
a = sc.nextLine(); | ||
b = a.split(" ", 2); | ||
} else { | ||
throw new DukeException("I do not know what you want to do!"); | ||
} | ||
} catch (DukeException error) { | ||
System.out.println(error); | ||
|
||
a = sc.nextLine(); | ||
b = a.split(" ", 2); | ||
} | ||
} | ||
System.out.println("Bye! Hope to see you again soon!"); | ||
sc.close(); | ||
|
||
} | ||
} | ||
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. Perhaps you could apply more OOP by extracting out closely related code as classes, rather than letting the Main method handle everything. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package duke; | ||
|
||
public class DukeException extends Exception { | ||
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. Great work using inheritance! LGTM! |
||
|
||
protected String errorMessage; | ||
|
||
public DukeException(String errorMessage) { | ||
this.errorMessage = errorMessage; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ":(( sorry bud but " + this.errorMessage; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package duke; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class Event extends Task { | ||
|
||
protected LocalDateTime date; | ||
|
||
|
||
public Event(String description, String date) { | ||
super(description); | ||
|
||
DateTimeFormatter scanned = DateTimeFormatter.ofPattern("dd/MM/yyyy HHmm"); | ||
this.date = LocalDateTime.parse(date, scanned); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd MMM yyyy hh:mma"); | ||
return "[E]" + super.toString() + " (at: " + this.date.format(dateFormat) + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package duke; | ||
|
||
public class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String convertToFile() { | ||
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. Great use of naming conventions! 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. Perhaps a more suitable name is getDescription. I feel that naming it convertToFile can be misleading since this method does not handle any conversion. |
||
return this.description; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); | ||
} | ||
|
||
public void Done() { | ||
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. Method names should be in camel case. |
||
this.isDone = true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + getStatusIcon() + "] " + this.description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package duke; | ||
|
||
public class ToDo extends Task { | ||
|
||
public ToDo(String description) { | ||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Hello from | |
| |_| | |_| | < __/ | ||
|____/ \__,_|_|\_\___| | ||
|
||
What can I do for you? |
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.
Perhaps a class should have a header comment to inform users about what the class does. Same suggestion for the other .java files