-
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 11 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,33 @@ | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.text.ParseException; | ||
|
||
public class Deadline extends Task { | ||
private String name; | ||
private boolean done; | ||
private Date by; | ||
|
||
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy kkmm"); | ||
|
||
public Deadline(String name, String by) { | ||
super(name); | ||
try { | ||
this.by = format.parse(by); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public String getBy() { | ||
return format.format(by); | ||
} | ||
|
||
public void mark() { | ||
super.mark(); | ||
} | ||
|
||
@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,47 @@ | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
public class Duke { | ||
|
||
private Storage storage; | ||
private TaskList tasks; | ||
private Ui ui; | ||
|
||
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(); | ||
} | ||
} | ||
|
||
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,33 @@ | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.text.ParseException; | ||
|
||
public class Event extends Task { | ||
private String name; | ||
private boolean done; | ||
private Date at; | ||
|
||
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy kkmm"); | ||
|
||
public Event(String name, String at) { | ||
super(name); | ||
try { | ||
this.at = format.parse(at); | ||
} catch (ParseException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public String getAt() { | ||
return format.format(at); | ||
} | ||
|
||
public void mark() { | ||
super.mark(); | ||
} | ||
|
||
@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,12 @@ | ||
public class Parser { | ||
private String[] commands; | ||
|
||
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 |
||
|
||
public String[] parse(String command) { | ||
commands = command.split(" "); | ||
return commands; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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; | ||
|
||
public class Storage { | ||
private String filePath; | ||
private FileReader reader; | ||
private BufferedReader bufferedReader; | ||
private ArrayList<Task> tasks; | ||
|
||
public Storage(String filePath) throws FileNotFoundException { | ||
this.filePath = filePath; | ||
this.reader = new FileReader(this.filePath); | ||
this.bufferedReader = new BufferedReader(reader); | ||
this.tasks = new ArrayList<>(); | ||
} | ||
|
||
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.mark(); | ||
} | ||
|
||
tasks.add(task); | ||
line = bufferedReader.readLine(); | ||
} | ||
|
||
return tasks; | ||
} | ||
|
||
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.getMark(), item.getName())); | ||
} else if (item instanceof Deadline) { | ||
bufferedWriter.write(String.format("D , %s , %s , %s", item.getMark(), item.getName(), ((Deadline) item).getBy())); | ||
} else { | ||
bufferedWriter.write(String.format("D , %s , %s , %s", item.getMark(), item.getName(), ((Event) item).getAt())); | ||
} | ||
bufferedWriter.newLine(); | ||
} | ||
bufferedWriter.close(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
public abstract class Task { | ||
private String name; | ||
private boolean 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. Done can be renamed isDone 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. Name and done should be protected instead, so that classes that extends it can use it |
||
|
||
public Task(String name) { | ||
this.name = name; | ||
this.done = false; | ||
} | ||
|
||
public String getMark() { | ||
if (done) { | ||
return "y"; | ||
} else { | ||
return "n"; | ||
} | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void mark() { | ||
done = true; | ||
} | ||
|
||
@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() |
||
String mark; | ||
if (done) { | ||
mark = "y"; | ||
} else { | ||
mark = "n"; | ||
} | ||
|
||
return String.format("[%s] %s", mark, name); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import java.util.ArrayList; | ||
|
||
public class TaskList { | ||
private ArrayList<Task> tasks; | ||
|
||
public TaskList(ArrayList<Task> tasks) { | ||
this.tasks = tasks; | ||
} | ||
|
||
public TaskList() { | ||
this.tasks = new ArrayList<>(); | ||
} | ||
|
||
public void add(Task task) { | ||
tasks.add(task); | ||
} | ||
|
||
public void delete(int index) { | ||
tasks.remove(index); | ||
} | ||
|
||
public void done(int index) { | ||
tasks.get(index).mark(); | ||
} | ||
|
||
public ArrayList<Task> getTasks() { | ||
return tasks; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
public class Todo extends Task { | ||
private String name; | ||
private boolean 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. Should not recreate a new private variable in Todo. Instead use Task's protected isDone and name |
||
|
||
public Todo(String name) { | ||
super(name); | ||
} | ||
|
||
public void mark() { | ||
super.mark(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("[T]%s", super.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. I like the use of String.format() here |
||
} | ||
} |
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.