-
Notifications
You must be signed in to change notification settings - Fork 47
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 #1
Open
OzairHasan
wants to merge
1
commit into
nus-cs2113-AY2122S2:master
Choose a base branch
from
OzairHasan:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Improve code #1
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,16 @@ | |
|
||
public class Contacts0 { | ||
|
||
public static final int MAX_RECORDS = 100; | ||
public static final String WORD_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]; | ||
showWelcomeMessage(); | ||
String[][] list = new String[MAX_RECORDS][3]; | ||
int count = 0; | ||
while (true) { | ||
System.out.print("|| " + "Enter command: "); | ||
System.out.print(WORD_SEPARATOR + "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_SEPARATOR) | ||
+ "%2$s", "add", String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tParameters: %1$s", "NAME " | ||
+ "p/" + "PHONE_NUMBER " | ||
+ "e/" + "EMAIL") + (System.lineSeparator() + "|| ") | ||
+ String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/[email protected]") + (System.lineSeparator() + "|| ")); | ||
+ "e/" + "EMAIL") + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/[email protected]") + (System.lineSeparator() + WORD_SEPARATOR)); | ||
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_SEPARATOR); | ||
} | ||
String listAsString = messageAccumulator.toString(); | ||
for (String m1 : new String[]{listAsString}) { | ||
System.out.println("|| " + m1); | ||
System.out.println(WORD_SEPARATOR + 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_SEPARATOR) | ||
+ String.format("\tParameters: %1$s", "NAME " | ||
+ "p/" + "PHONE_NUMBER " | ||
+ "e/" + "EMAIL") + (System.lineSeparator() + "|| ") | ||
+ String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/[email protected]") + (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_SEPARATOR) | ||
+ String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/[email protected]") + (System.lineSeparator() + WORD_SEPARATOR)) + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ (String.format("%1$s: %2$s", "list", "Displays all persons as a list with index numbers.") + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tExample: %1$s", "list") + (System.lineSeparator() + WORD_SEPARATOR)) + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ (String.format("%1$s: %2$s", "clear", "Clears all contacts.") + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tExample: %1$s", "clear") + (System.lineSeparator() + WORD_SEPARATOR)) + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ (String.format("%1$s: %2$s", "exit", "Exits the program.")+ (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tExample: %1$s", "exit")) + (System.lineSeparator() + WORD_SEPARATOR) + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ (String.format("%1$s: %2$s", "help", "Shows program usage instructions.")+ (System.lineSeparator() + WORD_SEPARATOR) | ||
+ 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_SEPARATOR + 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_SEPARATOR) | ||
+ "%2$s", commandType, (String.format("%1$s: %2$s", "add", "Adds a person to contacts.") + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tParameters: %1$s", "NAME " | ||
+ "p/" + "PHONE_NUMBER " | ||
+ "e/" + "EMAIL") + (System.lineSeparator() + "|| ") | ||
+ String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/[email protected]") + (System.lineSeparator() + "|| ")) + (System.lineSeparator() + "|| ") | ||
+ "e/" + "EMAIL") + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tExample: %1$s", "add" + " John Doe p/98765432 e/[email protected]") + (System.lineSeparator() + WORD_SEPARATOR)) + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ (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_SEPARATOR) | ||
+ String.format("\tParameters: %1$s", "KEYWORD [MORE_KEYWORDS]") + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tExample: %1$s", "find" + " alice bob charlie") + (System.lineSeparator() + WORD_SEPARATOR)) + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ (String.format("%1$s: %2$s", "list", "Displays all persons as a list with index numbers.") + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tExample: %1$s", "list") + (System.lineSeparator() + WORD_SEPARATOR)) + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ (String.format("%1$s: %2$s", "clear", "Clears all contacts.") + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ String.format("\tExample: %1$s", "clear") + (System.lineSeparator() + WORD_SEPARATOR)) + (System.lineSeparator() + WORD_SEPARATOR) | ||
+ (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_SEPARATOR) | ||
+ (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_SEPARATOR + 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("|| ==================================================="); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
|| =================================================== | ||
|| =================================================== | ||
|| Contacts - Version 0.0 | ||
|| Contacts - Version 0.1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good that you updated the test! |
||
|| Welcome to Contacts! | ||
|| =================================================== | ||
|| Enter command: || [Command entered:help] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job on extracting the constants