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

Improve code #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
205 changes: 205 additions & 0 deletions data visualization.ipynb

Large diffs are not rendered by default.

91 changes: 53 additions & 38 deletions src/main/java/Contacts0.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

public class Contacts0 {

//replace magic number
public static final int MAX_RECORDS = 100;
public static final String CONSOLE_WALL = "|| ";

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];
showWelcomeScreen();
String[][] list = new String[MAX_RECORDS][3];
int count = 0;
while (true) {
System.out.print("|| " + "Enter command: ");
System.out.print(CONSOLE_WALL + "Enter command: ");
String inputLine = SCANNER.nextLine();
while (inputLine.trim().isEmpty() || inputLine.trim().charAt(0) == '#') {
inputLine = SCANNER.nextLine();
Expand Down Expand Up @@ -62,12 +62,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() + CONSOLE_WALL)
+ "%2$s", "add", String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + CONSOLE_WALL)
+ 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() + CONSOLE_WALL)
+ String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/johnd@gmail.com") + (System.lineSeparator() + CONSOLE_WALL));
break;
}
list[count] = decodeResult;
Expand All @@ -83,67 +83,82 @@ 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(CONSOLE_WALL);
}
String listAsString = messageAccumulator.toString();
for (String m1 : new String[]{listAsString}) {
System.out.println("|| " + m1);
System.out.println(CONSOLE_WALL + 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() + CONSOLE_WALL)
+ 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() + CONSOLE_WALL)
+ String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/johnd@gmail.com") + (System.lineSeparator() + CONSOLE_WALL)) + (System.lineSeparator() + CONSOLE_WALL)
+ (String.format("%1$s: %2$s", "list", "Displays all persons as a list with index numbers.") + (System.lineSeparator() + CONSOLE_WALL)
+ String.format("\tExample: %1$s", "list") + (System.lineSeparator() + CONSOLE_WALL)) + (System.lineSeparator() + CONSOLE_WALL)
+ (String.format("%1$s: %2$s", "clear", "Clears all contacts.") + (System.lineSeparator() + CONSOLE_WALL)
+ String.format("\tExample: %1$s", "clear") + (System.lineSeparator() + CONSOLE_WALL)) + (System.lineSeparator() + CONSOLE_WALL)
+ (String.format("%1$s: %2$s", "exit", "Exits the program.")+ (System.lineSeparator() + CONSOLE_WALL)
+ String.format("\tExample: %1$s", "exit")) + (System.lineSeparator() + CONSOLE_WALL) + (System.lineSeparator() + CONSOLE_WALL)
+ (String.format("%1$s: %2$s", "help", "Shows program usage instructions.")+ (System.lineSeparator() + CONSOLE_WALL)
+ 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(CONSOLE_WALL + 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() + CONSOLE_WALL)
+ "%2$s", commandType, (String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + CONSOLE_WALL)
+ 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() + CONSOLE_WALL)
+ String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/johnd@gmail.com") + (System.lineSeparator() + CONSOLE_WALL)) + (System.lineSeparator() + CONSOLE_WALL)
+ (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() + CONSOLE_WALL)
+ String.format("\tParameters: %1$s", "KEYWORD [MORE_KEYWORDS]") + (System.lineSeparator() + CONSOLE_WALL)
+ String.format("\tExample: %1$s", "find" + " alice bob charlie") + (System.lineSeparator() + CONSOLE_WALL)) + (System.lineSeparator() + CONSOLE_WALL)
+ (String.format("%1$s: %2$s", "list", "Displays all persons as a list with index numbers.") + (System.lineSeparator() + CONSOLE_WALL)
+ String.format("\tExample: %1$s", "list") + (System.lineSeparator() + CONSOLE_WALL)) + (System.lineSeparator() + CONSOLE_WALL)
+ (String.format("%1$s: %2$s", "clear", "Clears all contacts.") + (System.lineSeparator() + CONSOLE_WALL)
+ String.format("\tExample: %1$s", "clear") + (System.lineSeparator() + CONSOLE_WALL)) + (System.lineSeparator() + CONSOLE_WALL)
+ (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() + CONSOLE_WALL)
+ (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(CONSOLE_WALL + m);
}
}
}

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

}


//how to improve code quality
// extract method
// replace magic number
// rename variable