Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
28ad2b8
Add Gradle support
May 24, 2020
ed6d4d2
Bump gradle and lib version
Eclipse-Dominator Aug 5, 2023
afe47af
Feature: greet and farewell
H1410101 Aug 24, 2023
4ccdfbe
Feature: echo until bye
H1410101 Aug 24, 2023
b2749c3
Feature: task types, task doneness, task list, and parser for slash a…
H1410101 Aug 24, 2023
696361d
Feature: textuitesting
H1410101 Aug 24, 2023
cfbfbac
Feature: handled various errors, implemented exceptions for missing a…
H1410101 Aug 24, 2023
1209ca0
Feature: delete
H1410101 Aug 24, 2023
8d970a2
Restructure project (transient)
H1410101 Sep 4, 2023
17a2cc2
Completely restructures
H1410101 Sep 6, 2023
3ef9980
Allow saving of todo list information through serialization
H1410101 Sep 6, 2023
ac0962c
Merge branch 'branch-Level-7'
H1410101 Sep 6, 2023
e2e6f0c
Add support for dates
H1410101 Sep 6, 2023
09eb837
Merge branch 'branch-Level-8'
H1410101 Sep 6, 2023
021f2e1
Rename for clarity and convention
H1410101 Sep 13, 2023
26f5425
Merge remote-tracking branch 'origin/add-gradle-support'
H1410101 Sep 13, 2023
5218357
Verify Gradle and JAR
H1410101 Sep 13, 2023
84540d8
Add JUnit tests and fix minor bugs
H1410101 Sep 13, 2023
347cb97
Add JavaDoc to half of the classes, especially Interfaces
H1410101 Sep 13, 2023
76f3604
Fix all coding standard except JavaDoc
H1410101 Sep 13, 2023
13fa8fb
Add find functionality
H1410101 Sep 13, 2023
493a117
Merge branch 'branch-Level-9'
H1410101 Sep 13, 2023
50839b0
Merge branch 'branch-A-JavaDoc'
H1410101 Sep 13, 2023
901f2c7
Merge branch 'branch-A-CodingStandard'
H1410101 Sep 13, 2023
bb8638f
Refactor for better OOP and style
H1410101 Sep 14, 2023
a40bfab
Refactored for OOP, fixed indexing bug
H1410101 Sep 15, 2023
d305a5a
Add working JavaFX prototype, expecting refactoring and reorganisation
H1410101 Sep 18, 2023
3fd5f4e
Add working JavaFX UI for chatbot
H1410101 Sep 19, 2023
3c32b3c
Merge branch 'branch-A-Varargs'
H1410101 Sep 19, 2023
66ff7aa
Merge branch 'branch-A-CheckStyle'
H1410101 Sep 19, 2023
f91fbe1
Add javafxplugin to build.gradle
H1410101 Sep 19, 2023
7b0e464
Remove irrelevant todos and clean up warnings
H1410101 Sep 19, 2023
6ceecdd
Add update/edit functionality
H1410101 Sep 19, 2023
77825cb
Fix style for checkstyle
H1410101 Sep 19, 2023
f10b024
Fix non-checkstyle code quality
H1410101 Sep 20, 2023
e351a0f
Merge pull request #2 from H1410101/branch-A-Assertions
H1410101 Sep 20, 2023
6e2d091
Merge branch 'master' into branch-A-CodeQuality
H1410101 Sep 20, 2023
0a08fda
Merge pull request #3
H1410101 Sep 20, 2023
7e1f6f6
Make header comments adhere to conventions
H1410101 Sep 20, 2023
030f583
Update GUI, implemented delayed application exit
H1410101 Sep 22, 2023
cb5ae6c
Add UI screenshot, add acknowledgement when task list is empty
H1410101 Sep 22, 2023
f2798ae
Update README.md
H1410101 Sep 22, 2023
9a5badd
Added acknowledgement when unnumbered lists are empty
H1410101 Sep 22, 2023
976c574
Merge remote-tracking branch 'origin/master'
H1410101 Sep 22, 2023
fa5cdf8
Polish UI, including rescaling images, changing padding, etc
H1410101 Sep 22, 2023
4893ea7
Format README.md
H1410101 Sep 22, 2023
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
Binary file added Tasks.txt
Binary file not shown.
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

102 changes: 102 additions & 0 deletions src/main/java/catbot/CatBot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package catbot;

import catbot.internal.CommandMap;
import catbot.internal.CommandPattern;
import catbot.internal.NamedParameterMap;
import catbot.io.CatbotConsoleIO;
import catbot.io.ErrorIndicatorIo;
import catbot.io.UserIo;
import catbot.task.Task;
import catbot.task.TaskList;

import java.util.Objects;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;

public class CatBot {

private static final CommandMap commands = new CommandMap();
static final UserIo io = new CatbotConsoleIO();
private static final TaskList taskList = new TaskList("Tasks.txt");
static {
addSupportedCommandsToCommandMap();
}

private static void addSupportedCommandsToCommandMap() {
CommandPattern<Integer> integerPattern = CatBotCommandPatterns.getIntegerPatternGenerator()
.generate(io::indicateInvalidInteger);
CommandPattern<NamedParameterMap> slashPattern = CatBotCommandPatterns.getSlashPatternGenerator()
.generate(CatBotCommandPatterns.NO_DEFAULT);

Copy link

Choose a reason for hiding this comment

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

I like how you name the variables in a greater explanatory sense

commands.setDefaultCommand(io::indicateInvalidCommand)
.addCommand("bye", args -> io.cleanup())
.addCommand("list", args -> io.printTaskList(taskList));

// User modifying existing tasks (through IntegerPattern, and Index)
BiConsumer<String, Consumer<Integer>> runIfValidIndexElseIndicateError =
(args, lambda) -> integerPattern.ifParsableElseDefault(args,
integer -> taskList.ifValidIndexElse(integer,
lambda,
invalidIndex -> io.indicateInvalidIndex(invalidIndex, taskList.getIndexBounds())
));

//noinspection SpellCheckingInspection
commands.addCommand("mark",
string -> runIfValidIndexElseIndicateError.accept(string,
validIndex -> {
taskList.markTask(validIndex-1);
io.printTaskModified(taskList, validIndex);
}
)
)
.addCommand("unmark",
string -> runIfValidIndexElseIndicateError.accept(string,
validIndex -> {
taskList.unmarkTask(validIndex-1);
io.printTaskModified(taskList, validIndex);
}
)
)
.addCommand("delete",
string -> runIfValidIndexElseIndicateError.accept(string,
validIndex -> io.printTaskDeleted(taskList.removeTask(validIndex-1))
)
);

// User creating new tasks (with SlashPattern)

BiConsumer<String, BiFunction<NamedParameterMap, BiConsumer<ErrorIndicatorIo.InvalidState, NamedParameterMap>, Optional<Task>>>

Choose a reason for hiding this comment

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

Perhaps u can split this line into 2? I think this line has 135 characters which exceeds the 120 characters limit stated in the coding standard.

Copy link
Author

Choose a reason for hiding this comment

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

Got it, and thanks for all the nudges for coding style.
I ended up running it through a stylechecker (finally), but I appreciate the pointers!

createTaskIfValidElseWarn = (args, bifunction) -> slashPattern.ifParsableElseDefault(args,
namedParameterMap -> bifunction.apply(
namedParameterMap,
io::indicateArgumentInvalid
).ifPresent(task -> {
taskList.addTask(task);
io.printTaskAdded(taskList);
})
);

commands.addCommand("todo",
args -> createTaskIfValidElseWarn.accept(args, Task.Todo::createIfValidElse)
)
.addCommand("event",
args -> createTaskIfValidElseWarn.accept(args, Task.Event::createIfValidElse)
)
.addCommand("deadline",
args -> createTaskIfValidElseWarn.accept(args, Task.Deadline::createIfValidElse)
)
;

}

Choose a reason for hiding this comment

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

Perhaps you could have added this to another class like CommandMap?

Copy link
Author

Choose a reason for hiding this comment

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

I actually spent quite a bit deliberating this, but I ended up not doing it (at least for now).

The part where I defend myself

My thought process was that I wanted to add bot-specific things in bot-specific classes; to change what commands are supported and what behaviours result from commands, my current iteration only requires that I change or even replace CatBot (and possibly CatBotConsoleIO) with minimal rewrite.
While there might be some merit in a CatBotCommands class that contains this method, that would leave CatBot as a small, anaemic class (I am leaning towards this being a bad thing, but I'm not too sure of myself here).
Ultimately, I had decided that it made more sense to me to have the commands be clear from the bot description, instead of its own separate class, especially since the class would otherwise be so small as to be trivial.

The part where I stop defending myself

I definitely see an alternative that takes your considerations into account. I could rename the current file to CatBotMain or CatBotEntrypoint to reflect its explicit purpose as a small and trivial class, put this initialization as a protected static method of CatBotCommandLoader, or have it be the default initialization of CatBotCommandMap.
I'd love to hear your thoughts on why this design might be preferable, though; or if you had a different design in mind, I'd like to chew on that as well.

Regardless, thanks for the feedback!


public static void main(String[] args) {
UserIo.CommandArgument commandArgument;
io.initialize();
do {
commandArgument = io.getNextCommand();
commands.run(commandArgument.getCommand(), commandArgument.getArgument());
} while (!Objects.equals(commandArgument.getCommand(), "bye"));
}
}
63 changes: 63 additions & 0 deletions src/main/java/catbot/CatBotCommandPatterns.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package catbot;

import catbot.internal.CommandPattern;
import catbot.internal.CommandPatternGenerator;
import catbot.internal.NamedParameterMap;

import java.util.function.Consumer;

public class CatBotCommandPatterns {

Choose a reason for hiding this comment

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

Maybe you can consider adding header comments/javadocs? For all your other classes as well

Copy link
Author

Choose a reason for hiding this comment

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

Yup! I'll get around to it. Eventually. :')


public static final Consumer<String> NO_DEFAULT = new Consumer<String>() {
@Override
public void accept(String s) {
assert false; //SHOULD NOT BE CALLED
}
};

//region Integer Pattern
public static CommandPatternGenerator<Integer> getIntegerPatternGenerator() {
return integerPatternGenerator;
}
private static final IntegerPatternGenerator integerPatternGenerator = new IntegerPatternGenerator();
private static class IntegerPatternGenerator implements CommandPatternGenerator<Integer> {

@Override
public CommandPattern<Integer> generate(Consumer<String> invalidInput) {
return new CommandPattern<Integer>() {
@Override
public void ifParsableElseDefault(String args, Consumer<Integer> consumer) {
try {
consumer.accept(Integer.parseInt(args));
} catch (NumberFormatException nfe) {
invalidInput.accept(args);
}
}
};
}
}
//endregion

//region Slash Arguments Pattern
private static final SlashArgumentPatternGenerator slashPatternGenerator = new SlashArgumentPatternGenerator();
Copy link

Choose a reason for hiding this comment

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

Perhaps having shorter variable names may increase the readability of the code?

Copy link
Author

Choose a reason for hiding this comment

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

Perhaps, but these are very infrequently used, so I thought it might have been better to make it very descriptive instead.
I'll think about it, but as of right now I'm still leaning towards my current convention, even if it results in excessively long variable names.

public static CommandPatternGenerator<NamedParameterMap> getSlashPatternGenerator() {
return slashPatternGenerator;
}

private static class SlashArgumentPatternGenerator implements CommandPatternGenerator<NamedParameterMap> {

@Override
public CommandPattern<NamedParameterMap> generate(Consumer<String> ignored) {
return new CommandPattern<NamedParameterMap>() {

private final Parser slashParser = Parser.by("/", true);
@Override
public void ifParsableElseDefault(String args, Consumer<NamedParameterMap> consumer) {
consumer.accept(slashParser.parse(args));
}
};
}
}
//endregion

}
80 changes: 80 additions & 0 deletions src/main/java/catbot/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package catbot;

import catbot.internal.NamedParameterMap;

import java.util.Arrays;

public class Parser {

private final String delimiter;
private final boolean emptyArgument;

//region Constructor

private Parser(String delimiter, boolean emptyArgument) {
this.delimiter = delimiter;
this.emptyArgument = emptyArgument;
}

public static Parser by(String delimiter) {
return by(delimiter, false);
}

public static Parser by(String delimiter, boolean emptyArgument) {
Copy link

Choose a reason for hiding this comment

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

Perhaps adding javadocs for public functions might help explain their functioning in greater detail?

Copy link
Author

Choose a reason for hiding this comment

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

Noted! I'll probably get back to it at some point.

if (delimiter == null || delimiter.isEmpty()) {
return new SingleParser();
} else {
return new Parser(delimiter, emptyArgument);
}
}
//endregion

public static class SingleParser extends Parser {

private SingleParser() {
super(null, false);
}

@Override
public NamedParameterMap parse(String s) {
NamedParameterMap cmdArgs = new NamedParameterMap();
parseCmdArg(s, cmdArgs);
return cmdArgs;
}
}

public NamedParameterMap parse(String s) {
NamedParameterMap cmdArgs = new NamedParameterMap();
String[] arr = s.split(delimiter);
parseCmdArg(arr[0], cmdArgs, this.emptyArgument);
Arrays.stream(arr).skip(1).forEach(segment -> parseCmdArg(segment, cmdArgs));
return cmdArgs;
}

//region Internal Helpers

/**
* Split string into one pair of command + argument based on the first whitespace.
* @param s string containing both command and argument, in that order
* @param map to store the mapping between command and argument
*/
private static void parseCmdArg(String s, NamedParameterMap map) {
String[] cmdArg = s.split("\\s", 2);

Choose a reason for hiding this comment

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

Perhaps you can write out the variable name of cmdArg to be in full so that it is clearer?

Copy link
Author

Choose a reason for hiding this comment

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

Good catch, will do. Thanks!

if (cmdArg.length == 2) {
map.addNamedParameter(cmdArg[0].trim(), cmdArg[1].trim());
} else {
map.addNamedParameter(cmdArg[0].trim(), "");
}
}

private static void parseCmdArg(String s, NamedParameterMap map, boolean emptyArgument) {
if (emptyArgument) {
map.addNamedParameter("", s.trim());
} else {
parseCmdArg(s, map);
}
}

Choose a reason for hiding this comment

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

I like that you still wrapped in curly brackets even though they are single statement!


//endregion

}
6 changes: 6 additions & 0 deletions src/main/java/catbot/internal/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package catbot.internal;

public interface Command {

public void run(String args);
}
24 changes: 24 additions & 0 deletions src/main/java/catbot/internal/CommandMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package catbot.internal;

import java.util.HashMap;

public class CommandMap {

private final HashMap<String, Command> commandMap = new HashMap<>();
private Command defaultCommand;
//endregion

public CommandMap addCommand(String invocation, Command lambda) {
commandMap.put(invocation, lambda);
return this;
}

public CommandMap setDefaultCommand(Command defaultCommand) {
this.defaultCommand = defaultCommand;
return this;
}

public void run(String s, String args) {
commandMap.getOrDefault(s, defaultCommand).run(args);
}
}
11 changes: 11 additions & 0 deletions src/main/java/catbot/internal/CommandPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package catbot.internal;

import java.util.function.Consumer;

public abstract class CommandPattern<T> {

protected Consumer<String> invalidCommandInput;

public abstract void ifParsableElseDefault(String args, Consumer<T> consumer);

}
7 changes: 7 additions & 0 deletions src/main/java/catbot/internal/CommandPatternGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package catbot.internal;

import java.util.function.Consumer;

public interface CommandPatternGenerator<T> {
public CommandPattern<T> generate(Consumer<String> invalidInput);
}
37 changes: 37 additions & 0 deletions src/main/java/catbot/internal/NamedParameterMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package catbot.internal;

import java.util.HashMap;
import java.util.Set;

public class NamedParameterMap {

private HashMap<String, String> parameters;

public NamedParameterMap() {
parameters = new HashMap<>();
}

public boolean containsKey(String key) {
return parameters.containsKey(key);
}

public void moveToNewKey(String oldKey, String newKey) {
if (!parameters.containsKey(newKey) && parameters.containsKey(oldKey)) {
parameters.put(newKey, parameters.get(oldKey));
parameters.remove(oldKey);
}
}

public Set<String> keySet() {
return parameters.keySet();
}

public String get(String key) {
return parameters.get(key);
}

public NamedParameterMap addNamedParameter(String parameterName, String parameterValue) {
parameters.put(parameterName, parameterValue);
return this;
}
}
Loading