-
Notifications
You must be signed in to change notification settings - Fork 270
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
[Ng Hong Liang] iP #277
base: master
Are you sure you want to change the base?
[Ng Hong Liang] iP #277
Changes from 13 commits
d669ce5
254eda2
7723b25
959058b
c80dda2
301de33
efc711e
78ac805
c61e079
8239eb6
dc1b29d
3fa0f96
237e571
e866177
6f2f008
5d00070
4dd3896
9d7baf3
afb83cb
3e3cd51
a519396
2b1a9ea
36c4d6d
63fee40
6792f7f
19ea522
230e6e4
a84882c
c18be9f
d606b75
33769db
115306b
2c63595
87d3f89
884d070
e81148b
09de543
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
# Binary files | ||
*.class | ||
*.jar | ||
*.exe | ||
|
||
# IDEA files | ||
/.idea/ | ||
/out/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
T | 1 | read book | ||
D | 0 | return book | June 6th | ||
E | 0 | project meeting | Aug 6th 2-4pm | ||
T | 1 | join sports club | ||
D | 0 | return book | Sunday | ||
E | 0 | project meeting | Mon 2-4pm |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
T | 1 | read book | ||
D | 0 | return book | June 6th | ||
E | 0 | project meeting | Aug 6th 2-4pm | ||
T | 1 | join sports club |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.ArrayList; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
/** | ||
* This class parse a varying format of dates into a LocalDate object. | ||
*/ | ||
|
||
public class DateParse { | ||
private LocalDate date; | ||
private boolean isFound = false; | ||
ArrayList<DateTimeFormatter> knownPatterns = new ArrayList<DateTimeFormatter>(); | ||
DateTimeFormatter format1 = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy/MM/dd"); | ||
DateTimeFormatter format3 = DateTimeFormatter.ofPattern("yyyy-MM-d"); | ||
DateTimeFormatter format4 = DateTimeFormatter.ofPattern("yyyy/MM/d"); | ||
DateTimeFormatter format5 = DateTimeFormatter.ofPattern("yyyy-M-d"); | ||
DateTimeFormatter format6 = DateTimeFormatter.ofPattern("yyyy/M/d"); | ||
DateTimeFormatter format7 = DateTimeFormatter.ofPattern("yyyy-M-dd"); | ||
DateTimeFormatter format8 = DateTimeFormatter.ofPattern("yyyy/M/dd"); | ||
DateTimeFormatter format9 = DateTimeFormatter.ofPattern("dd-MM-yyyy"); | ||
DateTimeFormatter format10 = DateTimeFormatter.ofPattern("dd/MM/yyyy"); | ||
DateTimeFormatter format11 = DateTimeFormatter.ofPattern("d-MM-yyyy"); | ||
DateTimeFormatter format12 = DateTimeFormatter.ofPattern("d/MM/yyyy"); | ||
DateTimeFormatter format13 = DateTimeFormatter.ofPattern("d-M-yyyy"); | ||
DateTimeFormatter format14 = DateTimeFormatter.ofPattern("d/M/yyyy"); | ||
DateTimeFormatter format15 = DateTimeFormatter.ofPattern("dd-M-yyyy"); | ||
DateTimeFormatter format16 = DateTimeFormatter.ofPattern("dd/M/yyyy"); | ||
|
||
public DateParse(String str) { | ||
knownPatterns.add(format1); | ||
knownPatterns.add(format2); | ||
knownPatterns.add(format3); | ||
knownPatterns.add(format4); | ||
knownPatterns.add(format5); | ||
knownPatterns.add(format6); | ||
knownPatterns.add(format7); | ||
knownPatterns.add(format8); | ||
knownPatterns.add(format9); | ||
knownPatterns.add(format10); | ||
knownPatterns.add(format11); | ||
knownPatterns.add(format12); | ||
knownPatterns.add(format13); | ||
knownPatterns.add(format14); | ||
knownPatterns.add(format15); | ||
knownPatterns.add(format16); | ||
|
||
int curr = 0; | ||
while((!isFound) && curr < 16) { | ||
try { | ||
date = LocalDate.parse(str, knownPatterns.get(curr)); | ||
isFound = true; | ||
} catch (DateTimeParseException e) { | ||
curr++; | ||
} | ||
} | ||
if ((!isFound) && curr == 16) { | ||
System.err.println("Format not found"); | ||
} | ||
this.helper(); | ||
} | ||
|
||
private LocalDate helper() { | ||
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 can use getDate here? 😄 |
||
return date; | ||
} | ||
|
||
public String toString() { | ||
return date.format(DateTimeFormatter.ofPattern("MMM dd yyyy")); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
public class Deadline extends Task { | ||
protected String dateInfo; | ||
protected DateParse date; | ||
protected TimeParse time; | ||
|
||
/** | ||
* Constructor for Deadline class | ||
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. Might be better to put a line after the JavaDoc descriptor and the params? |
||
* @param description Name of the Deadline | ||
* @param dateInfo Information for which this task is due | ||
*/ | ||
public Deadline(String description, String dateInfo) { | ||
super(description); | ||
String[] str = dateInfo.split(" ", 2); | ||
this.date = new DateParse(str[0]); | ||
this.time = new TimeParse(str[1]); | ||
this.dateInfo = this.date.toString() + " " + this.time.toString(); | ||
} | ||
|
||
public String whatType() { | ||
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 consider naming the method with verbs such as 'getType()'? |
||
return "D"; | ||
} | ||
|
||
/** | ||
* toString method specific for Deadline class, | ||
* inherits toString() fromTask class while adding additional information | ||
* Like the type of task, [D], and date information | ||
*/ | ||
@Override | ||
public String toString() { | ||
return "[D]" + super.toString() + " (by: " + this.dateInfo + ")"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,104 @@ | ||
import java.io.FileNotFoundException; | ||
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 consider putting your Duke.java in a package? |
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
/** | ||
* main functions like a to-do list | ||
*/ | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
Scanner scanner = new Scanner(System.in); | ||
String input = ""; | ||
Boolean end = 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. Perhaps use a name that sounds more like a boolean? What about something like 'isEnd' or 'isExited'? |
||
ArrayList<Task> store = new ArrayList(100); | ||
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 consider using a plural noun as the name of the ArrayList? |
||
|
||
try { | ||
new ReadFile(store); | ||
} catch (FileNotFoundException e) { | ||
System.err.println(e); | ||
System.exit(0); | ||
} | ||
|
||
int curr = store.size(); | ||
System.out.println("Hello! I'm Duke\nWhat can I do for you?"); | ||
|
||
// While loop ends when user inputs bye | ||
while(!end) { | ||
input = scanner.nextLine(); | ||
String[] splitInput = input.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 consider using a plural noun as a variable name here? |
||
try { | ||
new DukeException().checker(splitInput, curr); | ||
} catch (DukeException e) { | ||
System.err.println(e); | ||
continue; | ||
} | ||
// If user inputs bye, terminate the program | ||
if (input.equals("bye")) { | ||
end = true; | ||
} | ||
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. Good use of in-line comments, they adhere to the coding standard's requirement as well 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. Agreed! I like how you use in-line comments to help in code readability and understanding the code :) |
||
// If user inputs list, run through the to-do list, array store[], and list out all the to-dos | ||
else if (input.equals("list")) { | ||
for (int n = 0; n < curr; n++) { | ||
int temp = n + 1; | ||
System.out.println(temp + "." + store.get(n).toString()); | ||
} | ||
} | ||
// If user inputs keyword mark, mark the task corresponding to the index number as done | ||
else if (splitInput[0].equals("mark")) { | ||
Integer num = Integer.parseInt(splitInput[1]) - 1; | ||
store.get(num).setMark(); | ||
System.out.println("Nice! I've marked this task as done:\n " + store.get(num).toString()); | ||
} | ||
// If user inputs keyword unmark, mark the task corresponding to the index number as not done | ||
else if (splitInput[0].equals("unmark")){ | ||
Integer num = Integer.parseInt(splitInput[1]) - 1; | ||
store.get(num).setUnmark(); | ||
System.out.println("OK, I've marked this task as not done yet:\n " + store.get(num).toString()); | ||
} | ||
// If user inputs keyword to-do, add this task as a Todo class | ||
else if (splitInput[0].equals("todo")) { | ||
store.add(new Todo(splitInput[1])); | ||
int temp = curr + 1; | ||
System.out.println("Got it. I've added this task:\n " + store.get(curr).toString() | ||
+ "\nNow you have " + temp + " tasks in the list."); | ||
curr++; | ||
} | ||
// If user inputs keyword deadline, add this task as a Deadline class | ||
else if (splitInput[0].equals("deadline")) { | ||
String[] splitInput2 = splitInput[1].split(" /by ", 2); | ||
store.add(new Deadline(splitInput2[0], splitInput2[1])); | ||
int temp = curr + 1; | ||
System.out.println("Got it. I've added this task:\n " + store.get(curr).toString() | ||
+ "\nNow you have " + temp + " tasks in the list."); | ||
curr++; | ||
} | ||
// If user inputs keyword event, add this task as an Event class | ||
else if (splitInput[0].equals("event")) { | ||
String[] splitInput2 = splitInput[1].split(" /at ", 2); | ||
store.add(new Event(splitInput2[0], splitInput2[1])); | ||
int temp = curr + 1; | ||
System.out.println("Got it. I've added this task:\n " + store.get(curr).toString() | ||
+ "\nNow you have " + temp + " tasks in the list."); | ||
curr++; | ||
} | ||
// If user inputs keyword delete, delete the task corresponding to the index given | ||
else if (splitInput[0].equals("delete")) { | ||
Integer num = Integer.parseInt(splitInput[1]) - 1; | ||
curr--; | ||
System.out.println("Noted. I've removed this task:\n " + store.get(num).toString() | ||
+ "\nNow you have " + curr + " tasks in the list."); | ||
store.remove(num); | ||
} | ||
// Unused | ||
else {} | ||
} | ||
System.out.println("Bye. Hope to see you again soon!"); | ||
try { | ||
new WriteFile(store); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* DukeException class is a custom exception class to handle | ||
* problems that might occur in Duke.java | ||
*/ | ||
public class DukeException extends Exception { | ||
|
||
public DukeException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
|
||
public DukeException() {} | ||
|
||
/** | ||
* checker method is used to run checks for all the different types of request | ||
* and check if it invalids any of them. Returns a description or advice on what | ||
* is missing or how to resolve it. | ||
* @param arr This String array stores the user's input, used to check for validity | ||
* @param curr Holds the capacity for the stored list, used to check if request is out of bounds | ||
* @throws DukeException | ||
*/ | ||
public void checker(String[] arr, int curr) throws DukeException { | ||
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 can use a more descriptive name for the method 😄 |
||
String request = arr[0]; | ||
int len = arr.length; | ||
if (request.equals("delete")) { | ||
if (len == 1) { | ||
throw new DukeException(" ☹ OOPS!!! Please tell me the task's" + | ||
" index number so that I can delete it from the list."); | ||
} else { | ||
Integer index = Integer.parseInt(arr[1]); | ||
if (index < 1 || index > curr) { | ||
throw new DukeException(" ☹ OOPS!!! The task you want to delete is out of bounds," + | ||
" please double check the index number"); | ||
} | ||
} | ||
} else if (request.equals("mark")) { | ||
if (len == 1) { | ||
throw new DukeException(" ☹ OOPS!!! Please tell me the task's" + | ||
" index number so that I can mark it as done."); | ||
} else { | ||
Integer index = Integer.parseInt(arr[1]); | ||
if (index < 1 || index > curr) { | ||
throw new DukeException(" ☹ OOPS!!! The task you want to mark as done is out of bounds," + | ||
" please double check the index number"); | ||
} | ||
} | ||
} else if (request.equals("unmark")) { | ||
if (len == 1) { | ||
throw new DukeException("☹ OOPS!!! Please tell me the task's\" +\n" + | ||
" \" index number so that I can mark it as not done."); | ||
} else { | ||
Integer index = Integer.parseInt(arr[1]); | ||
if (index < 1 || index > curr) { | ||
throw new DukeException(" ☹ OOPS!!! The task you want to mark as not done is out of bounds," + | ||
" please double check the index number"); | ||
} | ||
} | ||
} else if (request.equals("todo")) { | ||
if (len == 1) { | ||
throw new DukeException(" ☹ OOPS!!! The description of a todo cannot be empty."); | ||
} | ||
} else if (request.equals("deadline")) { | ||
if (len == 1) { | ||
throw new DukeException(" ☹ OOPS!!! The description of a deadline cannot be empty."); | ||
} else { | ||
String[] components = arr[1].split("/by", 2); | ||
if (components[0].equals("") || components[0].equals(" ")) { | ||
throw new DukeException(" ☹ OOPS!!! The description of a deadline cannot be empty."); | ||
} else if (components.length == 1 || components[1].equals("") || components[1].equals(" ")) { | ||
throw new DukeException(" ☹ OOPS!!! Please input a due date for this task"); | ||
} else {} | ||
} | ||
} else if (request.equals("event")) { | ||
if (len == 1) { | ||
throw new DukeException(" ☹ OOPS!!! The description of a event cannot be empty."); | ||
} else { | ||
String[] components = arr[1].split("/at", 2); | ||
if (components[0].equals("") || components[0].equals(" ")) { | ||
throw new DukeException(" ☹ OOPS!!! The description of a event cannot be empty."); | ||
} else if (components.length == 1 || components[1].equals("") || components[1].equals(" ")) { | ||
throw new DukeException(" ☹ OOPS!!! Please input a due date for this task"); | ||
} else {} | ||
} | ||
} else if (request.equals("bye") || request.equals("list")) {} | ||
else { | ||
throw new DukeException(" ☹ OOPS!!! I'm sorry, but I don't know what that means :-(\n " + | ||
" Please add a valid request"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
public class Event extends Task { | ||
protected String dateInfo; | ||
protected DateParse date; | ||
protected TimeParse time; | ||
|
||
/** | ||
* Constructor for Event class | ||
* @param description Name of the Event | ||
* @param dateInfo Information for which this task is due | ||
*/ | ||
public Event(String description, String dateInfo) { | ||
super(description); | ||
String[] str = dateInfo.split(" ", 2); | ||
this.date = new DateParse(str[0]); | ||
this.time = new TimeParse(str[1]); | ||
this.dateInfo = this.date.toString() + " " + this.time.toString(); | ||
} | ||
|
||
public String whatType() { | ||
return "E"; | ||
} | ||
|
||
/** | ||
* toString method specific for Event class, | ||
* inherits toString() fromTask class while adding additional information | ||
* Like the type of task, [E], and date information | ||
*/ | ||
@Override | ||
public String toString() { | ||
return "[E]" + super.toString() + " (at: " + this.dateInfo + ")"; | ||
} | ||
} |
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 consider separating similar ones into blocks?