Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions Contacts.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
245 changes: 144 additions & 101 deletions src/main/java/Contacts0.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

public class Contacts0 {

public static final int MAX_ENTITIES = 100;
public static final String SEPARATOR = "===================================================";

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];
printWelcomeScreen();
String[][] list = new String[MAX_ENTITIES][3];
programmeManager(SCANNER, list);
}

private static void programmeManager(Scanner SCANNER, String[][] list) {
int count = 0;
while (true) {
System.out.print("|| " + "Enter command: ");
Expand All @@ -27,68 +30,16 @@ public static void main(String[] args) {
final String commandArgs = commandTypeAndParams[1];
switch (commandType) {
case "add":
String[] decodeResult = null;
final String matchAnyPersonDataPrefix = "p/" + '|' + "e/";
final String[] splitArgs = commandArgs.trim().split(matchAnyPersonDataPrefix);
if (splitArgs.length == 3 // 3 arguments
&& !splitArgs[0].isEmpty() // non-empty arguments
&& !splitArgs[1].isEmpty()
&& !splitArgs[2].isEmpty()) {
final String[] person1 = new String[3];
final int indexOfPhonePrefix = commandArgs.indexOf("p/");
final int indexOfEmailPrefix = commandArgs.indexOf("e/");// name is leading substring up to first data prefix symbol
int indexOfFirstPrefix = Math.min(indexOfEmailPrefix, indexOfPhonePrefix);
person1[0] = commandArgs.substring(0, indexOfFirstPrefix).trim();
String result;
final int indexOfPhonePrefix1 = commandArgs.indexOf("p/");
final int indexOfEmailPrefix1 = commandArgs.indexOf("e/");
if (indexOfPhonePrefix1 > indexOfEmailPrefix1) {
result = commandArgs.substring(indexOfPhonePrefix1, commandArgs.length()).trim().replace("p/", "");
} else {
result = commandArgs.substring(indexOfPhonePrefix1, indexOfEmailPrefix1).trim().replace("p/", "");
}
person1[1] = result;
String result1;
final int indexOfPhonePrefix2 = commandArgs.indexOf("p/");
final int indexOfEmailPrefix2 = commandArgs.indexOf("e/");// email is last arg, target is from prefix to end of string
if (indexOfEmailPrefix2 > indexOfPhonePrefix2) {
result1 = commandArgs.substring(indexOfEmailPrefix2, commandArgs.length()).trim().replace("e/", "");
} else {
result1 = commandArgs.substring(indexOfEmailPrefix2, indexOfPhonePrefix2).trim().replace("e/", "");
}
person1[2] = result1;
decodeResult = !person1[0].isEmpty()
&& !person1[1].isEmpty()
&& !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() + "|| ")
+ 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() + "|| "));
break;
}
String[] decodeResult = addResult(commandArgs);
feedback = handleNullResult(decodeResult);
if (feedback == null) break;
list[count] = decodeResult;
count++;
feedback = String.format("New person added: %1$s, Phone: %2$s, Email: %3$s",
decodeResult[0], decodeResult[1], decodeResult[2]);
break;
case "list":
String[][] toBeDisplayed = list;
final StringBuilder messageAccumulator = new StringBuilder();
for (int i = 0; i < count; i++) {
final String[] person = toBeDisplayed[i];
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("|| ");
}
String listAsString = messageAccumulator.toString();
for (String m1 : new String[]{listAsString}) {
System.out.println("|| " + m1);
}
listContact(list, count);
feedback = String.format("%1$d persons found!", count);
break;
case "clear":
Expand All @@ -97,47 +48,12 @@ public static void main(String[] args) {
feedback = "Contacts have been cleared!";
break;
case "help":
feedback = (String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + "|| ")
+ 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() + "|| ")
+ String.format("\tExample: %1$s", "help"));
feedback = formatHelp();
break;
case "exit":
for (String m1 : new String[]{"Exiting Contacts... Good bye!",
"===================================================",
"==================================================="}) {
System.out.println("|| " + 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() + "|| ")
+ 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", "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() + "|| ")
+ (String.format("%1$s: %2$s", "exit", "Exits the program.")
+ String.format("\tExample: %1$s", "exit")) + (System.lineSeparator() + "|| ")
+ (String.format("%1$s: %2$s", "help", "Shows program usage instructions.")
+ String.format("\tExample: %1$s", "help")));
exit();
default:
feedback = formatDefaultFeedback(commandType);
break;
}
for (String m : new String[]{feedback, "==================================================="}) {
Expand All @@ -146,4 +62,131 @@ public static void main(String[] args) {
}
}

private static String handleNullResult(String[] decodeResult) {
String feedback = "";
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() + "|| ")
+ 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() + "|| "));
return null;
}
return feedback;
}

private static String[] addResult(String commandArgs) {
String[] decodeResult = null;
final String matchAnyPersonDataPrefix = "p/" + '|' + "e/";
final String[] splitArgs = commandArgs.trim().split(matchAnyPersonDataPrefix);
if (splitArgs.length == 3 // 3 arguments
&& !splitArgs[0].isEmpty() // non-empty arguments
&& !splitArgs[1].isEmpty()
&& !splitArgs[2].isEmpty()) {
final String[] person1 = new String[3];
final int indexOfPhonePrefix = commandArgs.indexOf("p/");
final int indexOfEmailPrefix = commandArgs.indexOf("e/");// name is leading substring up to first data prefix symbol
int indexOfFirstPrefix = Math.min(indexOfEmailPrefix, indexOfPhonePrefix);
person1[0] = commandArgs.substring(0, indexOfFirstPrefix).trim();
String result;
final int indexOfPhonePrefix1 = commandArgs.indexOf("p/");
final int indexOfEmailPrefix1 = commandArgs.indexOf("e/");
if (indexOfPhonePrefix1 > indexOfEmailPrefix1) {
result = commandArgs.substring(indexOfPhonePrefix1, commandArgs.length()).trim().replace("p/", "");
} else {
result = commandArgs.substring(indexOfPhonePrefix1, indexOfEmailPrefix1).trim().replace("p/", "");
}
person1[1] = result;
String result1;
final int indexOfPhonePrefix2 = commandArgs.indexOf("p/");
final int indexOfEmailPrefix2 = commandArgs.indexOf("e/");// email is last arg, target is from prefix to end of string
if (indexOfEmailPrefix2 > indexOfPhonePrefix2) {
result1 = commandArgs.substring(indexOfEmailPrefix2, commandArgs.length()).trim().replace("e/", "");
} else {
result1 = commandArgs.substring(indexOfEmailPrefix2, indexOfPhonePrefix2).trim().replace("e/", "");
}
person1[2] = result1;
decodeResult = !person1[0].isEmpty()
&& !person1[1].isEmpty()
&& !person1[2].isEmpty() && person1[2].contains("@") ? person1 : null;
}
return decodeResult;
}

private static void listContact(String[][] list, int count) {
String[][] toBeDisplayed = list;
final StringBuilder messageAccumulator = new StringBuilder();
for (int i = 0; i < count; i++) {
final String[] person = toBeDisplayed[i];
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("|| ");
}
String listAsString = messageAccumulator.toString();
for (String m1 : new String[]{listAsString}) {
System.out.println("|| " + m1);
}
}

private static String formatHelp() {
String feedback;
feedback = (String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + "|| ")
+ 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() + "|| ")
+ String.format("\tExample: %1$s", "help"));
return feedback;
}

private static void exit() {
for (String m1 : new String[]{"Exiting Contacts... Good bye!",
SEPARATOR,
SEPARATOR}) {
System.out.println("|| " + m1);
}
System.exit(0);
// Fallthrough
}

private static String formatDefaultFeedback(String commandType) {
String feedback;
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() + "|| ")
+ 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", "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() + "|| ")
+ (String.format("%1$s: %2$s", "exit", "Exits the program.")
+ String.format("\tExample: %1$s", "exit")) + (System.lineSeparator() + "|| ")
+ (String.format("%1$s: %2$s", "help", "Shows program usage instructions.")
+ String.format("\tExample: %1$s", "help")));
return feedback;
}

private static void printWelcomeScreen() {
System.out.println("|| ===================================================");
System.out.println("|| ===================================================");
System.out.println("|| Contacts - Version 0.0");
System.out.println("|| Welcome to Contacts!");
System.out.println("|| ===================================================");
}

}
2 changes: 1 addition & 1 deletion src/main/java/Contacts1.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ private static boolean isValidPhone(String phone) {
return !phone.isEmpty();
//TODO: implement a better validation
}

// test
/**
* Returns true if the given string is a legal person email
*
Expand Down