-
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
[Ming Gao Rickie Li] iP #469
base: master
Are you sure you want to change the base?
Changes from 16 commits
016ad8d
d81e3b0
87638eb
7f141e7
ad65ad1
2a2efbd
974d93a
b4b212f
8a2b75e
5927253
344c259
eddbc35
e376a76
f334294
1e24a6c
04d6408
0e270b5
bd59051
8ad2179
3369de3
c12c5a7
2e99d62
22c64b4
ed5c92c
88616f7
4155640
3a9ebac
3b2f505
472fac4
07688eb
caf4dfa
1a9c8fe
d75e934
91a4400
341b156
61841c2
cafd45e
5716959
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 @@ | ||
T | 0 | borrow book | ||
D | 0 | return book | Sunday | ||
T | 0 | borrow book | ||
T | 0 | return book |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package duke; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
public class Deadline extends Task { | ||
|
||
protected String by; | ||
protected LocalDate 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. If these variables are not used in other parts of the code base consider changing the accessor to private. 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. Consider declaring variables as final if the attributes are not expected to change. |
||
|
||
public Deadline(String description, String by) { | ||
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 need a descriptive header comments for the Deadline method? 🤔 |
||
super(description); | ||
this.by = by; | ||
String[] d = by.split("-"); | ||
if (d.length == 3) { | ||
date = LocalDate.parse(by); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (this.date != null) { | ||
return "[D]" + super.toString() + " (by: " + this.date.format(DateTimeFormatter.ofPattern("MMM d yyyy")) + ")"; | ||
} else { | ||
return "[D]" + super.toString() + " (by: " + by + ")"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package duke; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Duke { | ||
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 need a descriptive header comments for the Duke class? 🤔 |
||
|
||
private Storage storage; | ||
private Ui ui; | ||
private String filePath; | ||
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. Consider declaring as final. |
||
|
||
public Duke(String filePath) { | ||
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 need a descriptive header comments for the Duke method? 🤔 |
||
this.ui = new Ui(); | ||
this.filePath = filePath; | ||
this.storage = new Storage(filePath); | ||
} | ||
|
||
public void run() { | ||
String separator = " ____________________________________________________________"; | ||
System.out.println(this.ui.WelcomeMessage()); | ||
Scanner in = new Scanner(System.in); | ||
String input = in.nextLine(); | ||
while(!Parser.parse(input).equals("bye")) { | ||
Storage.save(this.ui.run(this.storage.load(), input), this.filePath); | ||
input = in.nextLine(); | ||
} | ||
System.out.println(separator); | ||
System.out.println(" Bye. Hope to see you again soon!"); | ||
System.out.println(separator); | ||
} | ||
|
||
public static void main(String[] args) { | ||
new Duke("C:\\Users\\Rickie\\Documents\\University\\Year 2\\Semester 1\\CS2103T\\week 2\\ip\\Data\\taskList.txt").run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package duke; | ||
public class DukeException extends RuntimeException{ | ||
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 need a space between |
||
|
||
public enum Type { | ||
EmptyDone { | ||
@Override | ||
public String getMessage() { | ||
return " ☹ OOPS!!! You haven't specified the task you have completed."; | ||
} | ||
}, | ||
EmptyDelete { | ||
@Override | ||
public String getMessage() { | ||
return " ☹ OOPS!!! You haven't specified the task you want to delete."; | ||
} | ||
}, | ||
InvalidTaskNumber { | ||
@Override | ||
public String getMessage() { | ||
return " ☹ OOPS!!! The task number you have specified is invalid."; | ||
} | ||
}, | ||
EmptyTodo { | ||
@Override | ||
public String getMessage() { | ||
return " ☹ OOPS!!! The description of a todo cannot be empty."; | ||
} | ||
}, | ||
EmptyEvent { | ||
@Override | ||
public String getMessage() { | ||
return " ☹ OOPS!!! The description of an event cannot be empty."; | ||
} | ||
}, | ||
EmptyDeadline { | ||
@Override | ||
public String getMessage() { | ||
return " ☹ OOPS!!! The description of a deadline cannot be empty."; | ||
} | ||
}, | ||
InvalidCommand { | ||
@Override | ||
public String getMessage() { | ||
return " ☹ OOPS!!! I'm sorry, but I don't know what that means :-("; | ||
} | ||
}; | ||
|
||
public abstract String getMessage(); | ||
} | ||
|
||
public DukeException(Type type) { | ||
super(type.getMessage()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package duke; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
public class Event extends Task { | ||
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 need a descriptive header comments for the Event class? 🤔 |
||
|
||
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. "at" is perhaps not descriptive enough |
||
protected LocalDate 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. Consider changing to private final. |
||
|
||
public Event(String description, String at) { | ||
super(description); | ||
this.at = at; | ||
String[] d = at.split("-"); | ||
if (d.length == 3) { | ||
date = LocalDate.parse(at); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (this.date != null) { | ||
return "[E]" + super.toString() + " (at: " + this.date.format(DateTimeFormatter.ofPattern("MMM d 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. Should this be wrapped to next line? 🤔 |
||
} else { | ||
return "[E]" + super.toString() + " (at: " + this.at + ")"; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package duke; | ||
public class Parser { | ||
|
||
public static String parse(String input) { | ||
String inputLower = input.toLowerCase(); | ||
if (input.replaceAll("\\s","").toLowerCase().equals("bye")) { | ||
return "bye"; | ||
} else if (inputLower.replaceAll("\\s", "").equals("list")) { | ||
return "list"; | ||
} else if (inputLower.length() > 3 && inputLower.substring(0, 4).equals("done")) { | ||
return "done"; | ||
} else if (inputLower.length() > 5 && inputLower.substring(0, 6).equals("delete")) { | ||
return "delete"; | ||
} else if (inputLower.length() >= 4 && inputLower.substring(0, 4).equals("todo")) { | ||
return "todo"; | ||
} else if (inputLower.length() >= 5 && inputLower.substring(0, 5).equals("event")) { | ||
return "event"; | ||
} else if (input.length() >= 8 && inputLower.substring(0, 8).equals("deadline")) { | ||
return "deadline"; | ||
} else { | ||
return "InvalidCommand"; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package duke; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Scanner; | ||
|
||
public class Storage { | ||
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 need a descriptive header comments for the Storage class? 🤔 |
||
|
||
protected String filePath; | ||
protected static File taskList; | ||
|
||
public Storage(String filePath) { | ||
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 need a descriptive header comments for the Storage method? 🤔 |
||
this.filePath = filePath; | ||
this.taskList = new File(filePath); | ||
} | ||
|
||
public static ArrayList<Task> load() { | ||
ArrayList<Task> list = new ArrayList<>(); | ||
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 name the |
||
try { | ||
if (!taskList.createNewFile()) { | ||
Scanner fileReader = new Scanner(taskList); | ||
while (fileReader.hasNextLine()) { | ||
String line = fileReader.nextLine(); | ||
String[] splitString = line.split(" \\| "); | ||
if (splitString[0].equals("T")) { | ||
Todo t = new Todo(line.substring(8)); | ||
if (line.charAt(4) == '1') { | ||
t.markAsDone(); | ||
} | ||
list.add(t); | ||
} else if (splitString[0].equals("D")) { | ||
Deadline d = new Deadline(splitString[2], splitString[3]); | ||
if (splitString[1].equals("1")) { | ||
d.markAsDone(); | ||
} | ||
list.add(d); | ||
} else { | ||
Event e = new Event(splitString[2], splitString[3]); | ||
if (splitString[1].equals("1")) { | ||
e.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. Could perhaps be SLAP-ed harder? |
||
} | ||
list.add(e); | ||
} | ||
} | ||
} | ||
} catch (IOException e) { | ||
System.out.println("An error occurred."); | ||
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. Consider specifying the type of error. eg. error loading tasks. |
||
e.printStackTrace(); | ||
} | ||
return list; | ||
} | ||
|
||
public static void save(ArrayList<Task> list, String filePath) { | ||
File taskList = new File(filePath); | ||
taskList.delete(); | ||
try { | ||
FileWriter writer = new FileWriter(filePath); | ||
for (int i = 0; i < list.size(); i++) { | ||
File tasks = new File(filePath); | ||
Task t = list.get(i); | ||
String isDone = t.isDone ? "1" : "0"; | ||
if (t instanceof Todo) { | ||
String line = "T | " + isDone + " | " + t.description; | ||
writer.write(line + "\n"); | ||
} else if (t instanceof Deadline) { | ||
Deadline d = (Deadline) t; | ||
String line = "D | " + isDone + " | " + t.description + " | " + d.by; | ||
writer.write(line + "\n"); | ||
} else if (t instanceof Event) { | ||
Event e = (Event) t; | ||
String line = "E | " + isDone + " | " + t.description + " | " + e.at; | ||
writer.write(line + "\n"); | ||
} | ||
} | ||
writer.close(); | ||
} catch (IOException e) { | ||
System.out.println("An error occurred."); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package duke; | ||
public class Task { | ||
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 need a descriptive header comments for the Task class? 🤔 |
||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
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 need a descriptive header comments for the Task method? 🤔 |
||
this.description = description; | ||
this.isDone = false; | ||
} | ||
|
||
public String getStatusIcon() { | ||
return (isDone ? "X" : " "); | ||
} | ||
|
||
public void markAsDone() { | ||
this.isDone = true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + this.getStatusIcon() + "] " + this.description; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package duke; | ||
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. Minor style issue here, but there should always be a new line after package statement |
||
public class Todo extends Task { | ||
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 need a descriptive header comments for the Todo class? 🤔 |
||
|
||
public Todo(String description) { | ||
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 need a descriptive header comments for the Todo method? 🤔 |
||
super(description); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[T]" + super.toString(); | ||
} | ||
} |
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 need a descriptive header comments for the Parser class? 🤔