diff --git a/src/main/java/Contacts0.java b/src/main/java/Contacts0.java index 212842f..37e8801 100644 --- a/src/main/java/Contacts0.java +++ b/src/main/java/Contacts0.java @@ -3,17 +3,16 @@ public class Contacts0 { + public static final int MAX_RECORDS = 100; + public static final String WORD_SEPERATOR = "|| "; + public static void main(String[] args) { final Scanner SCANNER = new Scanner(System.in); - System.out.println("|| ==================================================="); - System.out.println("|| ==================================================="); - System.out.println("|| Contacts - Version 0.0"); - System.out.println("|| Welcome to Contacts!"); - System.out.println("|| ==================================================="); - String[][] list = new String[100][3]; + showWelcomeMessage(); + String[][] list = new String[MAX_RECORDS][3]; int count = 0; while (true) { - System.out.print("|| " + "Enter command: "); + System.out.print(WORD_SEPERATOR + "Enter command: "); String inputLine = SCANNER.nextLine(); while (inputLine.trim().isEmpty() || inputLine.trim().charAt(0) == '#') { inputLine = SCANNER.nextLine(); @@ -62,12 +61,12 @@ public static void main(String[] args) { && !person1[2].isEmpty() && person1[2].contains("@") ? person1 : null; } if (decodeResult == null) { - feedback = String.format("Invalid command format: %1$s " + (System.lineSeparator() + "|| ") - + "%2$s", "add", String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + "|| ") + feedback = String.format("Invalid command format: %1$s " + (System.lineSeparator() + WORD_SEPERATOR) + + "%2$s", "add", String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + WORD_SEPERATOR) + String.format("\tParameters: %1$s", "NAME " + "p/" + "PHONE_NUMBER " - + "e/" + "EMAIL") + (System.lineSeparator() + "|| ") - + String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/johnd@gmail.com") + (System.lineSeparator() + "|| ")); + + "e/" + "EMAIL") + (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/johnd@gmail.com") + (System.lineSeparator() + WORD_SEPERATOR)); break; } list[count] = decodeResult; @@ -83,67 +82,75 @@ public static void main(String[] args) { final int displayIndex = i + 1; messageAccumulator.append('\t').append(String.format("%1$d. ", displayIndex)) .append(String.format("%1$s Phone Number: %2$s Email: %3$s", person[0], person[1], person[2])) - .append(System.lineSeparator()).append("|| "); + .append(System.lineSeparator()).append(WORD_SEPERATOR); } String listAsString = messageAccumulator.toString(); for (String m1 : new String[]{listAsString}) { - System.out.println("|| " + m1); + System.out.println(WORD_SEPERATOR + m1); } feedback = String.format("%1$d persons found!", count); break; case "clear": - list = new String[100][3]; + list = new String[MAX_RECORDS][3]; count = 0; feedback = "Contacts have been cleared!"; break; case "help": - feedback = (String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + "|| ") + feedback = (String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + WORD_SEPERATOR) + String.format("\tParameters: %1$s", "NAME " + "p/" + "PHONE_NUMBER " - + "e/" + "EMAIL") + (System.lineSeparator() + "|| ") - + String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/johnd@gmail.com") + (System.lineSeparator() + "|| ")) + (System.lineSeparator() + "|| ") - + (String.format("%1$s: %2$s", "list", "Displays all persons as a list with index numbers.") + (System.lineSeparator() + "|| ") - + String.format("\tExample: %1$s", "list") + (System.lineSeparator() + "|| ")) + (System.lineSeparator() + "|| ") - + (String.format("%1$s: %2$s", "clear", "Clears all contacts.") + (System.lineSeparator() + "|| ") - + String.format("\tExample: %1$s", "clear") + (System.lineSeparator() + "|| ")) + (System.lineSeparator() + "|| ") - + (String.format("%1$s: %2$s", "exit", "Exits the program.")+ (System.lineSeparator() + "|| ") - + String.format("\tExample: %1$s", "exit")) + (System.lineSeparator() + "|| ") + (System.lineSeparator() + "|| ") - + (String.format("%1$s: %2$s", "help", "Shows program usage instructions.")+ (System.lineSeparator() + "|| ") + + "e/" + "EMAIL") + (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/johnd@gmail.com") + (System.lineSeparator() + WORD_SEPERATOR)) + (System.lineSeparator() + WORD_SEPERATOR) + + (String.format("%1$s: %2$s", "list", "Displays all persons as a list with index numbers.") + (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tExample: %1$s", "list") + (System.lineSeparator() + WORD_SEPERATOR)) + (System.lineSeparator() + WORD_SEPERATOR) + + (String.format("%1$s: %2$s", "clear", "Clears all contacts.") + (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tExample: %1$s", "clear") + (System.lineSeparator() + WORD_SEPERATOR)) + (System.lineSeparator() + WORD_SEPERATOR) + + (String.format("%1$s: %2$s", "exit", "Exits the program.")+ (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tExample: %1$s", "exit")) + (System.lineSeparator() + WORD_SEPERATOR) + (System.lineSeparator() + WORD_SEPERATOR) + + (String.format("%1$s: %2$s", "help", "Shows program usage instructions.")+ (System.lineSeparator() + WORD_SEPERATOR) + String.format("\tExample: %1$s", "help")); break; case "exit": for (String m1 : new String[]{"Exiting Contacts... Good bye!", "===================================================", "==================================================="}) { - System.out.println("|| " + m1); + System.out.println(WORD_SEPERATOR + m1); } System.exit(0); // Fallthrough default: - feedback = String.format("Invalid command format: %1$s " + (System.lineSeparator() + "|| ") - + "%2$s", commandType, (String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + "|| ") + feedback = String.format("Invalid command format: %1$s " + (System.lineSeparator() + WORD_SEPERATOR) + + "%2$s", commandType, (String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + WORD_SEPERATOR) + String.format("\tParameters: %1$s", "NAME " + "p/" + "PHONE_NUMBER " - + "e/" + "EMAIL") + (System.lineSeparator() + "|| ") - + String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/johnd@gmail.com") + (System.lineSeparator() + "|| ")) + (System.lineSeparator() + "|| ") + + "e/" + "EMAIL") + (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/johnd@gmail.com") + (System.lineSeparator() + WORD_SEPERATOR)) + (System.lineSeparator() + WORD_SEPERATOR) + (String.format("%1$s: %2$s", "find", "Finds all persons whose names contain any of the specified " - + "keywords (case-sensitive) and displays them as a list with index numbers.") + (System.lineSeparator() + "|| ") - + String.format("\tParameters: %1$s", "KEYWORD [MORE_KEYWORDS]") + (System.lineSeparator() + "|| ") - + String.format("\tExample: %1$s", "find" + " alice bob charlie") + (System.lineSeparator() + "|| ")) + (System.lineSeparator() + "|| ") - + (String.format("%1$s: %2$s", "list", "Displays all persons as a list with index numbers.") + (System.lineSeparator() + "|| ") - + String.format("\tExample: %1$s", "list") + (System.lineSeparator() + "|| ")) + (System.lineSeparator() + "|| ") - + (String.format("%1$s: %2$s", "clear", "Clears all contacts.") + (System.lineSeparator() + "|| ") - + String.format("\tExample: %1$s", "clear") + (System.lineSeparator() + "|| ")) + (System.lineSeparator() + "|| ") + + "keywords (case-sensitive) and displays them as a list with index numbers.") + (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tParameters: %1$s", "KEYWORD [MORE_KEYWORDS]") + (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tExample: %1$s", "find" + " alice bob charlie") + (System.lineSeparator() + WORD_SEPERATOR)) + (System.lineSeparator() + WORD_SEPERATOR) + + (String.format("%1$s: %2$s", "list", "Displays all persons as a list with index numbers.") + (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tExample: %1$s", "list") + (System.lineSeparator() + WORD_SEPERATOR)) + (System.lineSeparator() + WORD_SEPERATOR) + + (String.format("%1$s: %2$s", "clear", "Clears all contacts.") + (System.lineSeparator() + WORD_SEPERATOR) + + String.format("\tExample: %1$s", "clear") + (System.lineSeparator() + WORD_SEPERATOR)) + (System.lineSeparator() + WORD_SEPERATOR) + (String.format("%1$s: %2$s", "exit", "Exits the program.") - + String.format("\tExample: %1$s", "exit")) + (System.lineSeparator() + "|| ") + + String.format("\tExample: %1$s", "exit")) + (System.lineSeparator() + WORD_SEPERATOR) + (String.format("%1$s: %2$s", "help", "Shows program usage instructions.") + String.format("\tExample: %1$s", "help"))); break; } for (String m : new String[]{feedback, "==================================================="}) { - System.out.println("|| " + m); + System.out.println(WORD_SEPERATOR + m); } } } + private static void showWelcomeMessage() { + System.out.println("|| ==================================================="); + System.out.println("|| ==================================================="); + System.out.println("|| Contacts - Version 0.1"); + System.out.println("|| Welcome to Contacts!"); + System.out.println("|| ==================================================="); + } + } diff --git a/src/main/java/Contacts1.java b/src/main/java/Contacts1.java index 4f2d805..1cebf1a 100644 --- a/src/main/java/Contacts1.java +++ b/src/main/java/Contacts1.java @@ -51,8 +51,8 @@ public class Contacts1 { private static final String COMMAND_ADD_WORD = "add"; private static final String COMMAND_ADD_DESC = "Adds a person to contacts."; private static final String COMMAND_ADD_PARAMETERS = "NAME " - + PERSON_DATA_PREFIX_PHONE + "PHONE_NUMBER " - + PERSON_DATA_PREFIX_EMAIL + "EMAIL"; + + PERSON_DATA_PREFIX_PHONE + "PHONE_NUMBER " + + PERSON_DATA_PREFIX_EMAIL + "EMAIL"; private static final String COMMAND_ADD_EXAMPLE = COMMAND_ADD_WORD + " John Doe p/98765432 e/johnd@gmail.com"; private static final String COMMAND_LIST_WORD = "list"; @@ -87,7 +87,7 @@ public class Contacts1 { * The number of data elements for a single person. */ private static final int PERSON_DATA_COUNT = 3; - + /** * Maximum number of persons that can be held. */ @@ -192,8 +192,8 @@ private static void exitProgram() { /** * Executes the command as specified by the {@code userInputString} * - * @param userInputString raw input from user - * @return feedback about how the command was executed + * @param userInputString raw input from user + * @return feedback about how the command was executed */ private static String executeCommand(String userInputString) { final String[] commandTypeAndParams = splitCommandWordAndArgs(userInputString); @@ -219,11 +219,11 @@ private static String executeCommand(String userInputString) { /** * Splits raw user input into command word and command arguments string * - * @return size 2 array; first element is the command type and second element is the arguments string + * @return size 2 array; first element is the command type and second element is the arguments string */ private static String[] splitCommandWordAndArgs(String rawUserInput) { final String[] split = rawUserInput.trim().split("\\s+", 2); - return split.length == 2 ? split : new String[] { split[0] , "" }; // else case: no parameters + return split.length == 2 ? split : new String[]{split[0], ""}; // else case: no parameters } /** @@ -349,7 +349,6 @@ private static void showToUser(String... message) { /** * Shows the list of persons to the user. * The list will be indexed, starting from 1. - * */ private static void showToUser(String[][] persons) { String listAsString = getDisplayString(persons); @@ -365,8 +364,8 @@ private static String getDisplayString(String[][] persons) { final String[] person = persons[i]; final int displayIndex = i + 1; messageAccumulator.append('\t') - .append(getIndexedPersonListElementMessage(displayIndex, person)) - .append(LS); + .append(getIndexedPersonListElementMessage(displayIndex, person)) + .append(LS); } return messageAccumulator.toString(); } @@ -375,7 +374,7 @@ private static String getDisplayString(String[][] persons) { * Constructs a prettified listing element message to represent a person and their data. * * @param visibleIndex visible index for this listing - * @param person to show + * @param person to show * @return formatted listing message with index */ private static String getIndexedPersonListElementMessage(int visibleIndex, String[] person) { @@ -456,7 +455,7 @@ private static String getEmailFromPerson(String[] person) { /** * Creates a person from the given data. * - * @param name of person + * @param name of person * @param phone without data prefix * @param email without data prefix * @return constructed person @@ -474,7 +473,7 @@ private static String[] makePersonFromData(String name, String phone, String ema * * @param encoded string to be decoded * @return if cannot decode: empty Optional - * else: Optional containing decoded person + * else: Optional containing decoded person */ private static String[] decodePersonFromString(String encoded) { // check that we can extract the parts of a person from the encoded string @@ -535,7 +534,7 @@ private static String extractPhoneFromPersonString(String encoded) { return removePrefixSign(encoded.substring(indexOfPhonePrefix, encoded.length()).trim(), PERSON_DATA_PREFIX_PHONE); - // phone is middle arg, target is from own prefix to next prefix + // phone is middle arg, target is from own prefix to next prefix } else { return removePrefixSign( encoded.substring(indexOfPhonePrefix, indexOfEmailPrefix).trim(), @@ -558,7 +557,7 @@ private static String extractEmailFromPersonString(String encoded) { return removePrefixSign(encoded.substring(indexOfEmailPrefix, encoded.length()).trim(), PERSON_DATA_PREFIX_EMAIL); - // email is middle arg, target is from own prefix to next prefix + // email is middle arg, target is from own prefix to next prefix } else { return removePrefixSign( encoded.substring(indexOfEmailPrefix, indexOfPhonePrefix).trim(), @@ -615,7 +614,9 @@ private static boolean isValidEmail(String email) { * =============================================== */ - /** Returns usage info for all commands */ + /** + * Returns usage info for all commands + */ private static String getUsageInfoForAllCommands() { return getUsageInfoForAddCommand() + LS + getUsageInfoForViewCommand() + LS @@ -624,32 +625,42 @@ private static String getUsageInfoForAllCommands() { + getUsageInfoForHelpCommand(); } - /** Returns the string for showing 'add' command usage instruction */ + /** + * Returns the string for showing 'add' command usage instruction + */ private static String getUsageInfoForAddCommand() { return String.format(MESSAGE_COMMAND_HELP, COMMAND_ADD_WORD, COMMAND_ADD_DESC) + LS + String.format(MESSAGE_COMMAND_HELP_PARAMETERS, COMMAND_ADD_PARAMETERS) + LS + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_ADD_EXAMPLE) + LS; } - /** Returns string for showing 'clear' command usage instruction */ + /** + * Returns string for showing 'clear' command usage instruction + */ private static String getUsageInfoForClearCommand() { return String.format(MESSAGE_COMMAND_HELP, COMMAND_CLEAR_WORD, COMMAND_CLEAR_DESC) + LS + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_CLEAR_EXAMPLE) + LS; } - /** Returns the string for showing 'view' command usage instruction */ + /** + * Returns the string for showing 'view' command usage instruction + */ private static String getUsageInfoForViewCommand() { return String.format(MESSAGE_COMMAND_HELP, COMMAND_LIST_WORD, COMMAND_LIST_DESC) + LS + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_LIST_EXAMPLE) + LS; } - /** Returns string for showing 'help' command usage instruction */ + /** + * Returns string for showing 'help' command usage instruction + */ private static String getUsageInfoForHelpCommand() { return String.format(MESSAGE_COMMAND_HELP, COMMAND_HELP_WORD, COMMAND_HELP_DESC) + LS + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_HELP_EXAMPLE); } - /** Returns the string for showing 'exit' command usage instruction */ + /** + * Returns the string for showing 'exit' command usage instruction + */ private static String getUsageInfoForExitCommand() { return String.format(MESSAGE_COMMAND_HELP, COMMAND_EXIT_WORD, COMMAND_EXIT_DESC) + LS + String.format(MESSAGE_COMMAND_HELP_EXAMPLE, COMMAND_EXIT_EXAMPLE) + LS; @@ -665,9 +676,9 @@ private static String getUsageInfoForExitCommand() { /** * Removes sign(p/, d/, etc) from parameter string * - * @param s Parameter as a string - * @param sign Parameter sign to be removed - * @return string without the sign + * @param s Parameter as a string + * @param sign Parameter sign to be removed + * @return string without the sign */ private static String removePrefixSign(String s, String sign) { return s.replace(sign, ""); diff --git a/text-ui-test/EXPECTED.txt b/text-ui-test/EXPECTED.txt index 047bf26..32a1d30 100644 --- a/text-ui-test/EXPECTED.txt +++ b/text-ui-test/EXPECTED.txt @@ -1,6 +1,6 @@ || =================================================== || =================================================== -|| Contacts - Version 0.0 +|| Contacts - Version 0.1 || Welcome to Contacts! || =================================================== || Enter command: || [Command entered:help]