-
Notifications
You must be signed in to change notification settings - Fork 315
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
[Chan Jing Rong] Duke Increments #346
base: master
Are you sure you want to change the base?
Changes from 16 commits
65f72a8
0112efe
cfd6da7
6e6ace1
a3ca5a4
7b60e81
3672239
f7d21a1
2eeda50
e711463
cd13da7
bac973c
96fc35c
e5e16e2
66a6aa4
acbe16b
dbf905d
534fdfd
be2b14f
d7c47a0
daa3220
7a98b43
d469352
14d6de2
0c42333
1598a4d
f4ff23a
d4cfa86
a6cb27b
778e691
3eeb423
61d55fb
bf468af
2ce87e7
656740f
ca6585d
10548b3
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,74 @@ | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* Encapsulates a deadline to be stored in Duke. | ||
*/ | ||
public class Deadline extends Task { | ||
private String name; | ||
private boolean done; | ||
private Date by; | ||
private SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy kkmm"); | ||
|
||
/** | ||
* Creates a Deadline object tagged with name, whether it is completed, and task deadline. | ||
* | ||
* @param name Name of event. | ||
* @param by Deadline by when task is due. | ||
*/ | ||
public Deadline(String name, String by) { | ||
super(name); | ||
try { | ||
this.by = format.parse(by); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns name of deadline. | ||
* | ||
* @return Name of deadline. | ||
*/ | ||
public String getName() { | ||
return super.getName(); | ||
} | ||
|
||
/** | ||
* Returns "y" or "n" based on completion of deadline. | ||
* | ||
* @return String "y" or "n". | ||
*/ | ||
public String isDone() { | ||
return super.isDone(); | ||
} | ||
|
||
/** | ||
* Returns deadline of task. | ||
* | ||
* @return Deadline of task in String format. | ||
*/ | ||
public String getBy() { | ||
return format.format(by); | ||
} | ||
|
||
/** | ||
* Marks deadline as done. | ||
*/ | ||
public void setDone() { | ||
super.setDone(); | ||
} | ||
|
||
/** | ||
* Overrides the original toString method. | ||
* Returns description of deadline as `[D][isDone] name (by: date)`. | ||
* | ||
* @return Formatted description of deadline. | ||
*/ | ||
@Override | ||
public String toString() { | ||
return String.format("[D]%s (by: %s)", super.toString(), format.format(by)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,57 @@ | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Encapsulates a Duke object for the execution of the Duke program. | ||
*/ | ||
public class Duke { | ||
|
||
private Storage storage; | ||
private TaskList tasks; | ||
private Ui ui; | ||
|
||
/** | ||
* Creates a Duke object tagged with ui, storage and list of tasks. | ||
* | ||
* @param filePath path of file to be used as storage. | ||
*/ | ||
public Duke(String filePath) { | ||
ui = new Ui(); | ||
try { | ||
storage = new Storage(filePath); | ||
} catch (FileNotFoundException e) { | ||
ui.showError("filepath"); | ||
return; | ||
} | ||
try { | ||
tasks = new TaskList(storage.load()); | ||
} catch (IOException e) { | ||
ui.showError("load"); | ||
tasks = new TaskList(); | ||
} | ||
} | ||
|
||
/** | ||
* Runs the Duke program. | ||
*/ | ||
public void run() { | ||
ui.showWelcome(); | ||
boolean isExit = false; | ||
while (!isExit) { | ||
ui.execute(tasks); | ||
isExit = ui.isExit(); | ||
} | ||
|
||
try { | ||
storage.store(); | ||
} catch (IOException e) { | ||
ui.showError("store"); | ||
} | ||
|
||
ui.exit(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
new Duke("/Users/jingrong/duke/duke.txt").run(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* Encapsulates an event to be stored in Duke. | ||
*/ | ||
public class Event extends Task { | ||
private String name; | ||
private boolean done; | ||
private Date at; | ||
private SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy kkmm"); | ||
|
||
/** | ||
* Creates an Event object tagged with name, whether it is completed, and event time. | ||
* | ||
* @param name Name of event. | ||
* @param at Date of event. | ||
*/ | ||
public Event(String name, String at) { | ||
super(name); | ||
try { | ||
this.at = format.parse(at); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public String getName() { | ||
return super.getName(); | ||
} | ||
|
||
/** | ||
* Returns "y" or "n" based on completion of event. | ||
* | ||
* @return String "y" or "n". | ||
*/ | ||
public String isDone() { | ||
return super.isDone(); | ||
} | ||
|
||
/** | ||
* Returns date of event. | ||
* | ||
* @return Date of event in String format. | ||
*/ | ||
public String getAt() { | ||
return format.format(at); | ||
} | ||
|
||
/** | ||
* Marks event as done. | ||
*/ | ||
public void setDone() { | ||
super.setDone(); | ||
} | ||
|
||
/** | ||
* Overrides the original toString method. | ||
* Returns description of event as `[E][isDone] name (at: date)`. | ||
* | ||
* @return Formatted description of event. | ||
*/ | ||
@Override | ||
public String toString() { | ||
return String.format("[E]%s (at: %s)", super.toString(), format.format(at)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Encapsulates a parser used to understand user commands. | ||
*/ | ||
public class Parser { | ||
private String[] commands; | ||
|
||
/** | ||
* Creates a Parser object. | ||
*/ | ||
public Parser() { | ||
} | ||
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. You can choose not to type out the constructor method |
||
|
||
/** | ||
* Returns user commands parsed as a String array. | ||
* | ||
* @param command Command from user input. | ||
* @return String array of split commands. | ||
*/ | ||
public String[] parse(String command) { | ||
commands = command.split(" "); | ||
return commands; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Encapsulates a Storage object to load and save tasks from and to file. | ||
*/ | ||
public class Storage { | ||
private String filePath; | ||
private FileReader reader; | ||
private BufferedReader bufferedReader; | ||
private ArrayList<Task> tasks; | ||
|
||
/** | ||
* Creates a Storage object tagged with path of file, file reader, buffered reader, and list of tasks. | ||
* | ||
* @param filePath path of file for loading and saving of tasks. | ||
* @throws FileNotFoundException If file is not found at specified location. | ||
*/ | ||
public Storage(String filePath) throws FileNotFoundException { | ||
this.filePath = filePath; | ||
this.reader = new FileReader(this.filePath); | ||
this.bufferedReader = new BufferedReader(reader); | ||
this.tasks = new ArrayList<>(); | ||
} | ||
|
||
/** | ||
* Returns ArrayList of tasks loaded from file. | ||
* | ||
* @return ArrayList of tasks. | ||
* @throws IOException If tasks failed to be retrieved. | ||
*/ | ||
public ArrayList<Task> load() throws IOException { | ||
String line = bufferedReader.readLine(); | ||
while (! (line == null)) { | ||
String[] words = line.split(" , "); | ||
Task task; | ||
if (words[0].equals("T")) { | ||
task = new Todo(words[2]); | ||
} else if (words[0].equals("D")) { | ||
task = new Deadline(words[2], words[3]); | ||
} else { | ||
task = new Event(words[2], words[3]); | ||
} | ||
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 using switch if your if-else statements are long |
||
|
||
if (words[1].equals("y")) { | ||
task.setDone(); | ||
} | ||
|
||
tasks.add(task); | ||
line = bufferedReader.readLine(); | ||
} | ||
|
||
return tasks; | ||
} | ||
|
||
/** | ||
* Stores tasks in file. | ||
* | ||
* @throws IOException If tasks failed to be stored. | ||
*/ | ||
public void store() throws IOException { | ||
FileWriter writer = new FileWriter(this.filePath, false); | ||
BufferedWriter bufferedWriter = new BufferedWriter(writer); | ||
|
||
for (Task item : tasks) { | ||
if (item instanceof Todo) { | ||
bufferedWriter.write(String.format("T , %s , %s", item.isDone(), item.getName())); | ||
} else if (item instanceof Deadline) { | ||
bufferedWriter.write(String.format("D , %s , %s , %s", item.isDone(), item.getName(), ((Deadline) item) | ||
.getBy())); | ||
} else { | ||
bufferedWriter.write(String.format("D , %s , %s , %s", item.isDone(), item.getName(), ((Event) item) | ||
.getAt())); | ||
} | ||
bufferedWriter.newLine(); | ||
} | ||
bufferedWriter.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Encapsulates a task to be stored in Duke. | ||
*/ | ||
public abstract class Task { | ||
private String name; | ||
private boolean isDone; | ||
|
||
/** | ||
* Creates a Task object tagged with name and whether it is completed. | ||
* | ||
* @param name Name of task. | ||
*/ | ||
public Task(String name) { | ||
this.name = name; | ||
this.isDone = false; | ||
} | ||
|
||
/** | ||
* Returns "y" or "n" based on completion of task. | ||
* | ||
* @return String "y" or "n". | ||
*/ | ||
public String isDone() { | ||
if (isDone) { | ||
return "y"; | ||
} else { | ||
return "n"; | ||
} | ||
} | ||
|
||
/** | ||
* Returns name of task. | ||
* | ||
* @return Name of task. | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* Marks task as done. | ||
*/ | ||
public void setDone() { | ||
isDone = true; | ||
} | ||
|
||
/** | ||
* Overrides the original toString method. | ||
* Returns description of task as `[isDone] name`. | ||
* | ||
* @return Formatted description of task. | ||
*/ | ||
@Override | ||
public String toString() { | ||
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 work in abstracting common parts of code and using it in subclasses with super() |
||
return String.format("[%s] %s", isDone(), name); | ||
} | ||
} |
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.
Should change done to isDone() to follow variable naming.