-
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 17 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,6 @@ | ||
todo laundry | ||
deadline return book /by 02/12/2019 1800 | ||
event return b ook /at 09/12/2012 1900 | ||
done 2 | ||
done 2 | ||
done 1 |
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,124 @@ | ||
package duke; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
|
||
private static final String LOCAL_FILE = "data/duke.txt"; | ||
|
||
private Storage storage; | ||
private TaskList tasks; | ||
private Ui ui; | ||
|
||
public Duke(String filePath) { | ||
this.ui = new Ui(); | ||
this.storage = new Storage(filePath); | ||
try { | ||
this.tasks = storage.load(); | ||
} catch (DukeException e) { | ||
ui.showError(e); | ||
} | ||
} | ||
|
||
public void run() { | ||
this.ui.welcome(); | ||
|
||
Scanner sc = new Scanner(System.in); | ||
Parser parser = new Parser(sc.nextLine()); | ||
|
||
|
||
while (!parser.isBye()) { | ||
try { | ||
if (parser.isList()) { | ||
ui.list(this.tasks); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} else if (parser.isDone()) { | ||
try { | ||
this.tasks.done(parser.secondPartInInt()); | ||
this.storage.save(parser.getCommand()); | ||
ui.done(this.tasks.getMostRecent()); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} catch (DukeException e) { | ||
ui.showError(e); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} | ||
} else if (parser.isToDo()) { | ||
ToDo task; | ||
try { | ||
task = new ToDo(parser.secondPart()); | ||
this.tasks.add(task); | ||
this.storage.save(parser.getCommand()); | ||
ui.addTask(this.tasks.getMostRecent(), this.tasks); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} catch (DukeException e) { | ||
ui.showError(e); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} | ||
} else if (parser.isDeadline()) { | ||
Deadline task; | ||
try { | ||
task = new Deadline(parser.deadline()[0], parser.deadline()[1]); | ||
this.tasks.add(task); | ||
this.storage.save(parser.getCommand()); | ||
ui.addTask(this.tasks.getMostRecent(), this.tasks); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} catch (DukeException e) { | ||
ui.showError(e); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} | ||
} else if (parser.isEvent()) { | ||
Event task; | ||
try { | ||
task = new Event(parser.event()[0], parser.event()[1]); | ||
this.tasks.add(task); | ||
this.storage.save(parser.getCommand()); | ||
ui.addTask(this.tasks.getMostRecent(), this.tasks); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} catch (DukeException e) { | ||
ui.showError(e); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} | ||
} else if (parser.isDelete()) { | ||
try { | ||
this.tasks.delete(parser.secondPartInInt()); | ||
ui.deleteTask(this.tasks.getMostRecent(), this.tasks); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} catch (DukeException e) { | ||
ui.showError(e); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} | ||
} else { | ||
throw new DukeException("I do not know what you want to do!"); | ||
} | ||
} catch (DukeException e) { | ||
ui.showError(e); | ||
|
||
parser = new Parser(sc.nextLine()); | ||
} | ||
} | ||
|
||
ui.bye(); | ||
sc.close(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
new Duke(LOCAL_FILE).run(); | ||
|
||
} | ||
} | ||
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,104 @@ | ||
package duke; | ||
|
||
public class Parser { | ||
private String command; | ||
private String[] twoPart; | ||
private String[] time; | ||
|
||
public Parser(String command) { | ||
this.command = command; | ||
this.twoPart = this.command.split(" ", 2); | ||
} | ||
|
||
public String getCommand() { | ||
return this.command; | ||
} | ||
|
||
public String firstPart() { | ||
return this.twoPart[0]; | ||
} | ||
|
||
public int secondPartInInt() 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. Method names should be verbs. So this would be better named as 'getSecondPartInInt'. |
||
try { | ||
return Integer.valueOf(this.twoPart[1]); | ||
} catch (ArrayIndexOutOfBoundsException e) { | ||
throw new DukeException("which specific task from the list? Give a number!"); | ||
} | ||
} | ||
|
||
public String secondPart() throws DukeException { | ||
try { | ||
return this.twoPart[1]; | ||
} catch (ArrayIndexOutOfBoundsException error) { | ||
throw new DukeException("do remember to add your description!"); | ||
} | ||
} | ||
|
||
public String[] deadline() { | ||
return this.twoPart[1].split(" /by ", 2); | ||
} | ||
|
||
public String[] event() { | ||
return this.twoPart[1].split(" /at ", 2); | ||
} | ||
|
||
public String[] parse() { | ||
return this.twoPart; | ||
} | ||
|
||
public boolean isBye() { | ||
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. I guess u can shorten this into one-liner by just returning 'this.command.equals("bye"')'. Helps improve readability somewhat but I guess I'm just nitpicking here. |
||
if (this.command.equals("bye")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean isList() { | ||
if (this.command.equals("list")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean isDone() { | ||
if (this.twoPart[0].equals("done")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean isToDo() { | ||
if (this.twoPart[0].equals("todo")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean isDeadline() { | ||
if (this.twoPart[0].equals("deadline")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean isEvent() { | ||
if (this.twoPart[0].equals("event")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean isDelete() { | ||
if (this.twoPart[0].equals("delete")) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package duke; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
|
||
public class Storage { | ||
private File storage; | ||
private String fileName; | ||
|
||
public Storage(String fileName) { | ||
this.fileName = fileName; | ||
this.storage = new File(fileName); | ||
} | ||
|
||
public TaskList load() throws DukeException { | ||
try { | ||
this.storage.createNewFile(); | ||
} catch (IOException error) { | ||
throw new DukeException("ensure you have created a folder named 'data' within the main project directory!"); | ||
} | ||
try { | ||
Scanner fileScanner = new Scanner(this.storage); | ||
TaskList tasklist = new TaskList(); | ||
|
||
while (fileScanner.hasNext()) { | ||
String fileData = fileScanner.nextLine(); | ||
Parser parser = new Parser(fileData); | ||
|
||
if (parser.isDone()) { | ||
tasklist.done(parser.secondPartInInt()); | ||
} else if (parser.isToDo()) { | ||
ToDo task = new ToDo(parser.secondPart()); | ||
tasklist.add(task); | ||
} else if (parser.isDeadline()) { | ||
Deadline task = new Deadline(parser.deadline()[0], parser.deadline()[1]); | ||
tasklist.add(task); | ||
} else if (parser.isEvent()) { | ||
Event task = new Event(parser.event()[0], parser.event()[1]); | ||
tasklist.add(task); | ||
} else if (parser.isDelete()) { | ||
tasklist.delete(parser.secondPartInInt()); | ||
} | ||
} | ||
|
||
return tasklist; | ||
} catch (FileNotFoundException e) { | ||
throw new DukeException("ensure you have created a folder named 'data' within the main project directory!"); | ||
} | ||
|
||
//load contents into TaskList | ||
} | ||
|
||
private void appendToFile(String textToAppend) throws IOException { | ||
FileWriter fw = new FileWriter(this.fileName, true); // create a FileWriter in append mode | ||
fw.write(textToAppend); | ||
fw.close(); | ||
} | ||
|
||
public void save(String history) throws DukeException{ | ||
try { | ||
appendToFile(history + System.lineSeparator()); | ||
} catch (IOException e) { | ||
throw new DukeException(e.getMessage()); | ||
} | ||
} | ||
} |
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