Skip to content
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

[Ryan Chew] Duke Increments #355

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
65f72a8
Add support for Gradle workflow
j-lum Aug 6, 2019
0112efe
Add sample checkstyle configuration
j-lum Aug 12, 2019
7faa65a
Added Greet, Echo, Exit features
iltep64 Aug 16, 2019
46c55c6
Implement list add and show features.
iltep64 Aug 23, 2019
94b4414
Created Task class to represent tasks.
iltep64 Aug 23, 2019
4cec14a
Implemented marking a task as done.
iltep64 Aug 23, 2019
013cc37
Refactor IO interface into DukeIO instance, and Duke logic into objec…
iltep64 Aug 23, 2019
9303e1d
Implement deadline task class and addition logic
iltep64 Aug 23, 2019
5abe5d0
Refactor out and improve task-adding logic.
iltep64 Aug 23, 2019
11f4134
Create a uniform command interface, with parsing logic.
iltep64 Aug 23, 2019
3ec4d4e
Implement dialog block logic, so messages get aggregated into a singl…
iltep64 Aug 23, 2019
b79c5c0
Add header to list command output.
iltep64 Aug 23, 2019
3a0d632
Add Event task and make it available by the 'event' command.
iltep64 Aug 23, 2019
0139127
Add basic error handling support.
iltep64 Aug 24, 2019
16718fb
Added deletion functionality.
iltep64 Aug 24, 2019
8dd4b9b
Implement basic JSON decoder
iltep64 Aug 27, 2019
d7e9187
Add basic JSON writer
iltep64 Aug 27, 2019
501ae91
Add type registry for JSON serializiation
iltep64 Aug 28, 2019
9880921
Add convenience functions for JSON encoding/decoding.
iltep64 Aug 30, 2019
db72071
Refactor task types to enums
iltep64 Aug 30, 2019
6ca4441
Add JSON builder class for Tasks
iltep64 Sep 2, 2019
1cf63ce
Add listOf function, and produce ArrayList<> values instead of just L…
iltep64 Sep 2, 2019
f5ccf6e
Use JSON to load/store task list
iltep64 Sep 2, 2019
a0d17c1
Fix completed status not being loaded
iltep64 Sep 2, 2019
0b162a2
Add date parsing logic
iltep64 Sep 2, 2019
c842c6f
Have EventTask and DeadlineTask to use date logic.
iltep64 Sep 2, 2019
355f065
Merge branch 'branch-Level-7'
iltep64 Sep 2, 2019
93b6e1c
Merge branch 'branch-Level-8'
iltep64 Sep 2, 2019
dce444a
Add encoder lookup cache for Registry, and avoid printing debug messages
iltep64 Sep 2, 2019
45172d8
Move all classes into (sub)packages of org.duke
iltep64 Sep 2, 2019
9f28efa
Add task find command
iltep64 Sep 2, 2019
ed5ea49
Reformat code to satisfy standards
iltep64 Sep 3, 2019
8c16531
Add javadocs to public classes and methods.
iltep64 Sep 3, 2019
c907b2c
Merge branch 'branch-Level-9'
iltep64 Sep 3, 2019
c5eee9a
Merge branch 'codingstandard'
iltep64 Sep 3, 2019
af1b0d4
Merge branch 'javadocs'
iltep64 Sep 3, 2019
7e2227e
Merge branch 'gradle'
iltep64 Sep 3, 2019
e0b6066
Add JAR build plugin.
iltep64 Sep 3, 2019
63d9c81
Add tests for Command and DateParser, and automate via Gradle.
iltep64 Sep 3, 2019
3bfe760
Refactor out IO and command parsing.
iltep64 Sep 3, 2019
1e6e594
Implement a basic JavaFX UI.
iltep64 Sep 3, 2019
31fb5b4
Merge branch 'branch-Level-10'
iltep64 Sep 3, 2019
b04abda
Encapsulate Command fields.
iltep64 Sep 10, 2019
354adca
Beautify DukeFx UI, by adding colored message boxes for Duke and user.
iltep64 Sep 17, 2019
f653e2d
Extract command handlers into their own classes, and add a help command.
iltep64 Sep 17, 2019
7ea74be
Refactor storage out into own class, and improve CounterDecorator.
iltep64 Sep 17, 2019
00772ef
Refactored JavaFX UI to use FXML custom components.
iltep64 Sep 17, 2019
6d9eb22
Fix JavaFX startup classpath issues.
iltep64 Sep 17, 2019
1eebc7b
Add user guide/documentation.
iltep64 Sep 17, 2019
b508421
Set theme jekyll-theme-slate
iltep64 Sep 17, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/CounterDecorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.function.Function;

class CounterDecorator implements Function<String, String> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you create a class to format the string representation of a Task.

private int counter;
public CounterDecorator(int start) {
this.counter = start;
}
public String apply(String str) {
String ret = String.format("%d: %s", this.counter, str);
this.counter++;
return ret;
}
}
118 changes: 31 additions & 87 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
Expand All @@ -16,123 +13,70 @@ public class Duke {
"What can I do for you?"
};

private static final String lineRule =
"____________________________________________________________";
private DukeIO io;
private final List<Task> taskList;

private static void say(String... lines) {
say(Arrays.asList(lines).iterator());
}
private static void say(Iterator<String> lines) {
//Print start of reply line
System.out.println(lineRule);

//Print each given line
while(lines.hasNext()) {
System.out.println(lines.next());
}

//Print end of reply line, and extra empty line
System.out.println(lineRule);
System.out.println();
}

static class CounterDecorator implements Function<String, String> {
private int counter;
public CounterDecorator(int start) {
this.counter = start;
}
public String apply(String str) {
String ret = String.format("%d: %s", this.counter, str);
this.counter++;
return ret;
}
}

private static final Map<String, Predicate<String>> commandMap
= new HashMap<>();
private static Predicate<String> defaultHandler;

private static final List<Task> taskList = new ArrayList<>();

private static boolean handleBye(String input) {
say("Bye. Hope to see you again soon!");
private boolean handleBye(String input) {
this.io.say("Bye. Hope to see you again soon!");
return true;
}

private static boolean displayList(String input) {
say(Duke.taskList.stream()
private boolean displayList(String input) {
this.io.say(this.taskList.stream()
.map(Object::toString)
.map(new CounterDecorator(1))
.iterator());
return false;
}

private static boolean addTask(String input) {
Duke.taskList.add(new Task(input));
say("added: " + input);
private boolean addTask(String input) {
this.taskList.add(new Task(input));
this.io.say("added: " + input);
return false;
}

private static boolean markAsDone(String input) {
private boolean markAsDone(String input) {
int index;
try {
index = Integer.parseInt(input);
} catch(NumberFormatException e) {
say("Index provided was not an integer!");
this.io.say("Index provided was not an integer!");
return false;
}

if(index < 0 || index > Duke.taskList.size()) {
say("There's no task with that index!");
if(index < 0 || index > this.taskList.size()) {
this.io.say("There's no task with that index!");
return false;
}

Task selectedTask = Duke.taskList.get(index-1);
Task selectedTask = this.taskList.get(index-1);
selectedTask.markComplete();
say("Nice! I've marked this task as done:",
this.io.say("Nice! I've marked this task as done:",
" " + selectedTask);
return false;
}

public static void main(String[] args) {
private Duke() {
this.io = new DukeIO();
//Bind command handlers
Duke.commandMap.put("list", Duke::displayList);
Duke.commandMap.put("done", Duke::markAsDone);
Duke.commandMap.put("bye", Duke::handleBye);
Duke.defaultHandler = Duke::addTask;
this.io.bindCommand("list", this::displayList);
this.io.bindCommand("done", this::markAsDone);
this.io.bindCommand("bye", this::handleBye);
this.io.setUnknownCommandHandler(this::addTask);

//Initialize input.
Scanner s = new Scanner(System.in);
this.taskList = new ArrayList<>();
}

private void run() {
//Start off greeting the user.
say(initialGreeting);

//While there is still input from user
while(s.hasNextLine()) {
//Read single line of user input, and remove extra spaces
String userInput = s.nextLine().trim();
this.io.say(initialGreeting);

int spacePos = userInput.indexOf(' ');
String command, rest;
if(spacePos == -1) {
command = userInput;
rest = null;
} else {
command = userInput.substring(0, spacePos);
rest = userInput.substring(spacePos+1);
}

Predicate<String> cmdHandler = commandMap.get(command);
boolean shouldExit;
if(cmdHandler != null) {
shouldExit = cmdHandler.test(rest);
} else {
shouldExit = defaultHandler.test(userInput);
}
//Start listen loop.
this.io.listen();
}

if(shouldExit) {
break;
}
}
public static void main(String[] args) {
Duke duke = new Duke();
duke.run();
}
}
78 changes: 78 additions & 0 deletions src/main/java/DukeIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.function.Predicate;

public class DukeIO {

private static final String lineRule =
"____________________________________________________________";

private final Map<String, Predicate<String>> commandMap
= new HashMap<>();
private Predicate<String> defaultHandler = null;

private final Scanner parser;

public DukeIO() {
this.parser = new Scanner(System.in);
}

public void say(String... lines) {
say(Arrays.asList(lines).iterator());
}
public void say(Iterator<String> lines) {
//Print start of reply line
System.out.println(lineRule);

//Print each given line
while(lines.hasNext()) {
System.out.println(lines.next());
}

//Print end of reply line, and extra empty line
System.out.println(lineRule);
System.out.println();
}

public void bindCommand(String command, Predicate<String> handler) {
this.commandMap.put(command, handler);
}

public void setUnknownCommandHandler(Predicate<String> handler) {
this.defaultHandler = handler;
}

public void listen() {
//While there is still input from user
while(parser.hasNextLine()) {
//Read single line of user input, and remove extra spaces
String userInput = parser.nextLine().trim();

int spacePos = userInput.indexOf(' ');
String command, rest;
if(spacePos == -1) {
command = userInput;
rest = null;
} else {
command = userInput.substring(0, spacePos);
rest = userInput.substring(spacePos+1);
}

Predicate<String> cmdHandler = commandMap.get(command);
boolean shouldExit;
if(cmdHandler != null) {
shouldExit = cmdHandler.test(rest);
} else {
shouldExit = defaultHandler.test(userInput);
}

if(shouldExit) {
break;
}
}
}

}