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

Add static CommandDictionary method to get standardized command usage messages #283

Merged
merged 9 commits into from
Nov 1, 2019
Binary file modified docs/images/Ui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 47 additions & 20 deletions src/main/java/eggventory/logic/commands/CommandDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,34 @@
public class CommandDictionary {

// ArrayList of Pairs of format (commands, arguments)
private ArrayList<Pair<String, String>> commandDict = new ArrayList<>();
private static ArrayList<Pair<String, String>> commandDict = new ArrayList<>();

/**
* Contains all possible valid commands that can be used by the user. Can add new commands here
* and they will be automatically used by the predictive search.
* and they will be automatically used by the predictive search. This is already initialized by
* InputPredictor, and thus does not need to be initialized once again.
*/
public CommandDictionary() {
// Add Commands
commandDict.add(new Pair<>("add stock", "<Stock Type> <Stock Code> <Quantity> <Description>"));
commandDict.add(new Pair<>("add stocktype", "<Stock Type>"));
commandDict.add(new Pair<>("add person", "<Matric No.> <Name>"));
commandDict.add(new Pair<>("add template", "<Template Name>"));
commandDict.add(new Pair<>("add stock", "<StockType> <StockCode> <Quantity> <Description>"));
commandDict.add(new Pair<>("add stocktype", "<StockType>"));
commandDict.add(new Pair<>("add person", "<MatricNo> <Name>"));
commandDict.add(new Pair<>("add template", "<TemplateName>"));

// Delete Commands
commandDict.add(new Pair<>("delete stock", "<Stock Code>"));
commandDict.add(new Pair<>("delete stocktype", "<Stock Type>"));
commandDict.add(new Pair<>("delete person", "<Matric No.>"));
commandDict.add(new Pair<>("delete template", "<Template Name>"));
commandDict.add(new Pair<>("delete stock", "<StockCode>"));
commandDict.add(new Pair<>("delete stocktype", "<StockType>"));
commandDict.add(new Pair<>("delete person", "<MatricNo>"));
commandDict.add(new Pair<>("delete template", "<TemplateName>"));

// Edit Commands
commandDict.add(new Pair<>("edit stock", "<Stock Code> <Property> <New Value>"));
commandDict.add(new Pair<>("edit person", "<Property> <New Value>"));
commandDict.add(new Pair<>("edit stock", "<StockCode> <Property> <NewValue>"));
commandDict.add(new Pair<>("edit person", "<Property> <NewValue>"));

// List Commands
commandDict.add(new Pair<>("list stock", null));
commandDict.add(new Pair<>("list stocktype", "all"));
commandDict.add(new Pair<>("list stocktype", "<Stock Type>"));
commandDict.add(new Pair<>("list stocktype", "<StockType>"));
commandDict.add(new Pair<>("list loan", null));
commandDict.add(new Pair<>("list template", null));
commandDict.add(new Pair<>("list lost", null));
Expand All @@ -44,13 +45,13 @@ public CommandDictionary() {
commandDict.add(new Pair<>("find stocktype", "<Query>"));

// Loan Commands
commandDict.add(new Pair<>("loan add", "<Matric No.> <Stock Code> <Quantity>"));
commandDict.add(new Pair<>("loan add", "<MatricNo> <StockCode> <Quantity>"));
commandDict.add(new Pair<>("loan add", "<Template Name>"));
commandDict.add(new Pair<>("loan returned", "<Matric No.> <Stock Code> <Quantity>"));
commandDict.add(new Pair<>("loan returned", "<Matric No.> <Template Name>"));
commandDict.add(new Pair<>("loan returned", "<MatricNo> <StockCode> <Quantity>"));
commandDict.add(new Pair<>("loan returned", "<MatricNo> <TemplateName>"));

// Lost Commands
commandDict.add(new Pair<>("lost", "<Stock Code> <Quantity>"));
commandDict.add(new Pair<>("lost", "<StockCode> <Quantity>"));
Copy link

Choose a reason for hiding this comment

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

standardised yes v good


// Bye Commands
commandDict.add(new Pair<>("bye", null));
Expand All @@ -64,15 +65,14 @@ public CommandDictionary() {
* Returns exactly one
* Returns an empty ArrayList if no matches have been found.
*/
public ArrayList<String> searchDictCommands(String query) {
public static ArrayList<String> searchDictCommands(String query) {
ArrayList<String> curSearch = new ArrayList<>();

// Iterating through arguments of the same command
for (int i = 0; i < commandDict.size(); i++) {
// Checking if current Dictionary key matches what
// the user has entered so far.
if (commandDict.get(i).getKey().startsWith(query)) {
//System.out.println("Detected");
curSearch.add(commandDict.get(i).getKey());
}
}
Expand All @@ -89,7 +89,7 @@ public ArrayList<String> searchDictCommands(String query) {
* Returns an empty ArrayList if no matches have been found.
* Returns
*/
public ArrayList<String> searchDictArguments(String command) {
public static ArrayList<String> searchDictArguments(String command) {
ArrayList<String> curSearch = new ArrayList<>();

for (int i = 0; i < commandDict.size(); i++) {
Expand All @@ -101,4 +101,31 @@ public ArrayList<String> searchDictArguments(String command) {
// Returns an empty ArrayList if no match has been found.
return curSearch;
}

/**
* Gets String with all usages of a particular Command. Used to print messages that
* handler errors in user input by showing correct format of commands to be entered.
* @param query Type of command to list use cases for, for example, 'list'.
* @return Formatted String to be printed by GUI containing proper use cases of command.
*/
public static String getCommandUsage(String query) {
StringBuilder outputString = new StringBuilder();
outputString.append(String.format("\nAppropriate usage of '%s' is as follows:\n", query));

for (int i = 0; i < commandDict.size(); i++) {

if (commandDict.get(i).getKey().startsWith(query)) {
if (commandDict.get(i).getValue() == null) {
outputString.append(String.format("- '%s'\n", commandDict.get(i).getKey()));
} else {
outputString.append(String.format("- '%s %s'\n", commandDict.get(i).getKey(),
commandDict.get(i).getValue()));
}

}
}

return outputString.toString();
}
}
//@@author
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/**
* Command objects for deleting stocks.
* Requires the index (as listed by the system) of the stock. //TODO: Change this to the stock code.
* Requires the StockCode of the Stock.
*/
public class DeleteStockCommand extends Command {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ListStockTypeCommand(CommandType type, String query) {


@Override
public String execute(StockList list, Ui ui, Storage storage) throws BadInputException {
public String execute(StockList list, Ui ui, Storage storage) {
String output = "";

if (query.equals("all")) { // list stocktype all command
Expand All @@ -33,8 +33,7 @@ public String execute(StockList list, Ui ui, Storage storage) throws BadInputExc
output = listString;

if (listString.equals("")) {
ui.print("Invalid command: No such stocktype. list stock / list stocktype all / "
+ "list stocktype <Stock Type>");
ui.print("Invalid command: No such stocktype exists!");
} else {
ui.print(output);
// Drawing data on stocks under specific stocktype in GUI table.
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/eggventory/logic/parsers/ParseAdd.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eggventory.logic.parsers;

import eggventory.logic.commands.Command;
import eggventory.logic.commands.CommandDictionary;
import eggventory.logic.commands.add.AddLoanCommand;
import eggventory.logic.commands.add.AddPersonCommand;
import eggventory.logic.commands.add.AddStockCommand;
Expand Down Expand Up @@ -90,32 +91,28 @@ public Command parse(String inputString) throws InsufficientInfoException, BadIn
switch (addInput[0]) {
case "stock":
if (!Parser.isCommandComplete(inputString, 4)) {
throw new InsufficientInfoException("Please enter stock information after the 'add' command in"
+ " this format:\nadd stock <StockType> <StockCode> <Quantity> <Description>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("add stock"));
}
addCommand = processAddStock(addInput[1]);
break;

case "stocktype":
if (!Parser.isCommandComplete(inputString, 1)) {
throw new InsufficientInfoException("Please enter stock information after the 'add' command in"
+ " this format:\nadd stocktype <StockType>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("add stocktype"));
}
addCommand = processAddStockType(addInput[1]);
break;

case "loan":
if (!Parser.isCommandComplete(inputString, 3)) {
throw new InsufficientInfoException("Please enter loan information after the 'add' command in"
+ " this format:\nadd loan <StockCode> <MatricNo> <Quantity>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("add loan"));
}
addCommand = processAddLoan(addInput[1]);
break;

case "person":
if (!Parser.isCommandComplete(inputString, 2)) {
throw new InsufficientInfoException("Please enter person information after the 'add' command in"
+ "this format:\nadd person <MatricNo> <Name>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("add person"));
}

addCommand = processAddPerson(addInput[1]);
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/eggventory/logic/parsers/ParseDelete.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eggventory.logic.parsers;

import eggventory.logic.commands.Command;
import eggventory.logic.commands.CommandDictionary;
import eggventory.logic.commands.delete.DeleteLoanCommand;
import eggventory.logic.commands.delete.DeleteStockCommand;
import eggventory.logic.commands.delete.DeleteStockTypeCommand;
Expand Down Expand Up @@ -36,25 +37,22 @@ public Command parse(String inputString) throws InsufficientInfoException, BadIn

case "stock":
if (!Parser.isCommandComplete(inputString, 1)) {
throw new InsufficientInfoException("Please enter information after the 'delete' command in"
+ " this format:\ndelete stock <StockCode>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("delete stock"));
}
deleteInput[1] = deleteInput[1].strip();
deleteCommand = new DeleteStockCommand(CommandType.DELETE, deleteInput[1]);
break;

case "stocktype":
if (!Parser.isCommandComplete(inputString, 1)) {
throw new InsufficientInfoException("Please enter information after the 'delete' command in"
+ " this format:\ndelete stocktype <StockType>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("delete stocktype"));
}
deleteCommand = new DeleteStockTypeCommand(CommandType.DELETE, deleteInput[1]);
break;

case "loan":
if (!Parser.isCommandComplete(inputString, 2)) {
throw new InsufficientInfoException("Please enter information after the 'delete' command in"
+ " this format:\ndelete loan <StockCode> <MatricNo>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("delete loan"));
}
deleteCommand = processDeleteLoan(deleteInput[1]);
break;
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/eggventory/logic/parsers/ParseEdit.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eggventory.logic.parsers;

import eggventory.logic.commands.Command;
import eggventory.logic.commands.CommandDictionary;
import eggventory.logic.commands.edit.EditStockCommand;
import eggventory.logic.commands.edit.EditStockTypeCommand;
import eggventory.commons.enums.CommandType;
Expand Down Expand Up @@ -64,16 +65,14 @@ public Command parse(String inputString) throws InsufficientInfoException, BadIn
switch (addInput[0]) {
case "stock":
if (!Parser.isCommandComplete(inputString, 3)) {
throw new InsufficientInfoException("Please enter the edit information after the 'edit' command in"
+ "this format:\nedit stock <stockCode> <property> <newValue>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("edit stock"));
}
editCommand = processEditStock(addInput[1]);
break;

case "stocktype":
if (!Parser.isCommandComplete(inputString, 2)) {
throw new InsufficientInfoException("Please enter the edit information after the 'edit' command in"
+ "this format:\nedit stocktype <StockType> <New StockType Name>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("edit stocktype"));
}
editCommand = processEditStockType(addInput[1]);
break;
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/eggventory/logic/parsers/ParseList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import eggventory.commons.exceptions.InsufficientInfoException;
import eggventory.logic.commands.Command;
import eggventory.logic.commands.CommandDictionary;
import eggventory.logic.commands.list.ListPersonCommand;
import eggventory.logic.commands.list.ListStockCommand;
import eggventory.logic.commands.list.ListStockTypeCommand;
Expand All @@ -11,29 +12,26 @@
//@@author yanprosobo
public class ParseList {

private final String listErrorMessageGeneric = "Usage of list: 'list stock', 'list stocktype all', "
+ "'list stocktype <Stock Type>', or 'list person'";

private Command processListStock(String input) throws BadInputException {
String[] inputArr = input.split(" ");
if (inputArr.length > 1) { // Checking if anything extraneous is after stock
throw new BadInputException(listErrorMessageGeneric);
throw new BadInputException(CommandDictionary.getCommandUsage("list stock"));
}
return new ListStockCommand(CommandType.LIST);
}

private Command processListStockType(String input) throws BadInputException {
String[] inputArr = input.split(" ");
if (inputArr.length > 1) { // Checking for extra arguments
throw new BadInputException(listErrorMessageGeneric);
throw new BadInputException(CommandDictionary.getCommandUsage("list stocktype"));
}
return new ListStockTypeCommand(CommandType.LIST, inputArr[0]);
}

private Command processListPerson(String input) throws BadInputException {
String[] inputArr = input.split(" +");
if (inputArr.length > 1) {
throw new BadInputException(listErrorMessageGeneric);
throw new BadInputException(CommandDictionary.getCommandUsage("list person"));
}

return new ListPersonCommand(CommandType.LIST);
Expand Down Expand Up @@ -61,8 +59,7 @@ public Command parse(String inputString) throws BadInputException, InsufficientI

case "stocktype":
if (!Parser.isCommandComplete(inputString, 1)) {
throw new InsufficientInfoException("Please enter stock information after the 'list' command in"
+ " this format:\nlist stocktype <StockType> OR list stocktype all");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("list stocktype"));
}
listCommand = processListStockType(inputArr[1]);
break;
Expand All @@ -72,11 +69,10 @@ public Command parse(String inputString) throws BadInputException, InsufficientI
break;

default:
throw new BadInputException("Usage of list: 'list stock', 'list stocktype all' or "
+ "'list stocktype <Stock Type>'");
throw new BadInputException(CommandDictionary.getCommandUsage("list"));
}

return listCommand;
}
//@@author
}
//@@author
14 changes: 6 additions & 8 deletions src/main/java/eggventory/logic/parsers/Parser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eggventory.logic.parsers;

import eggventory.logic.commands.Command;
import eggventory.logic.commands.CommandDictionary;
import eggventory.logic.commands.FindCommand;
import eggventory.logic.HelpCommand;
import eggventory.logic.commands.ByeCommand;
Expand Down Expand Up @@ -87,9 +88,7 @@ private Command handleListInput(String listInput) throws BadInputException,
//Commands which are single words.
case "list":
if (inputArr.length == 1) {
// TODO: Interface this with HELP feature or CommandDictionary.
throw new InsufficientInfoException("Usage of list: 'list stock', 'list stocktype all' or "
+ "'list stocktype <Stock Type>'");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("list"));
} else {
command = listParser.parse(inputArr[1]);
}
Expand All @@ -100,8 +99,7 @@ private Command handleListInput(String listInput) throws BadInputException,

case "delete":
if (inputArr.length == 1) {
throw new InsufficientInfoException("Usage of delete: delete stock <StockCode>, delete stocktype "
+ "<StockType, or delete loan <StockCode> <MatricNo>");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("delete"));
}
inputArr[1] = inputArr[1].strip(); //Removes whitespace after the stockCode so that it can parse correctly.
command = deleteParser.parse(inputArr[1]);
Expand All @@ -111,15 +109,15 @@ private Command handleListInput(String listInput) throws BadInputException,
case "add": {
if (inputArr.length == 1) { //User command only said "add" and nothing else.
//TODO: Instead of BadInputException, we should be returning a helpCommand.
throw new InsufficientInfoException("'" + inputArr[0] + "' requires 1 or more arguments.");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("add"));
} else {
command = addParser.parse(inputArr[1]);
}
break;
}
case "find": {
if (inputArr.length == 1) {
throw new InsufficientInfoException("'" + inputArr[0] + "' requires 1 or more arguments.");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("find"));
}

String description = inputArr[1].trim(); //Might need to catch empty string exceptions?
Expand All @@ -132,7 +130,7 @@ private Command handleListInput(String listInput) throws BadInputException,
}
case "edit": {
if (inputArr.length == 1) {
throw new InsufficientInfoException("'" + inputArr[0] + "' requires 1 or more arguments.");
throw new InsufficientInfoException(CommandDictionary.getCommandUsage("edit"));
} else {
command = editParser.parse(inputArr[1]);
}
Expand Down