diff --git a/src/seedu/addressbook/ui/Formatter.java b/src/seedu/addressbook/ui/Formatter.java new file mode 100644 index 000000000..357e54e87 --- /dev/null +++ b/src/seedu/addressbook/ui/Formatter.java @@ -0,0 +1,66 @@ +package seedu.addressbook.ui; + +import java.util.ArrayList; +import java.util.List; + +import seedu.addressbook.data.person.ReadOnlyPerson; + +public class Formatter { + /** + * A decorative prefix added to the beginning of lines printed by AddressBook + */ + static final String LINE_PREFIX = "|| "; + /** + * A platform independent line separator. + */ + static final String LS = System.lineSeparator(); + + /** Format of indexed list item */ + private static final String MESSAGE_INDEXED_LIST_ITEM = "\t%1$d. %2$s"; + + public Formatter() { + } + + String formatMessage(String m) { + return addLinePrefix(replaceNewline(m)); + } + + String replaceNewline(String m) { + return m.replace("\n", LS + LINE_PREFIX); + } + + String addLinePrefix(String message) { + return LINE_PREFIX + message; + } + + List getFormattedPersons(List persons) { + final List formattedPersons = new ArrayList(); + for (ReadOnlyPerson person : persons) { + formattedPersons.add(person.getAsTextHidePrivate()); + } + return formattedPersons; + } + + static StringBuilder getFormattedIndexedList(List listItems) { + final StringBuilder formatted = new StringBuilder(); + int displayIndex = 0 + TextUi.DISPLAYED_INDEX_OFFSET; + for (String listItem : listItems) { + formatted.append(getIndexedListItem(displayIndex, listItem)).append("\n"); + displayIndex++; + } + return formatted; + } + + /** + * Formats a string as a viewable indexed list item. + * + * @param visibleIndex visible index for this listing + */ + private static String getIndexedListItem(int visibleIndex, String listItem) { + return String.format(MESSAGE_INDEXED_LIST_ITEM, visibleIndex, listItem); + } + + String formatCommand(String fullInputLine) { + return "[Command entered:" + fullInputLine + "]"; + } +} \ No newline at end of file diff --git a/src/seedu/addressbook/ui/TextUi.java b/src/seedu/addressbook/ui/TextUi.java index d30371c70..7854b45a7 100644 --- a/src/seedu/addressbook/ui/TextUi.java +++ b/src/seedu/addressbook/ui/TextUi.java @@ -8,7 +8,6 @@ import java.io.InputStream; import java.io.PrintStream; -import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Scanner; @@ -21,18 +20,8 @@ */ public class TextUi { - /** A decorative prefix added to the beginning of lines printed by AddressBook */ - private static final String LINE_PREFIX = "|| "; - - /** A platform independent line separator. */ - private static final String LS = System.lineSeparator(); - private static final String DIVIDER = "==================================================="; - /** Format of indexed list item */ - private static final String MESSAGE_INDEXED_LIST_ITEM = "\t%1$d. %2$s"; - - /** Offset required to convert between 1-indexing and 0-indexing. */ public static final int DISPLAYED_INDEX_OFFSET = 1; @@ -41,6 +30,7 @@ public class TextUi { private final Scanner in; private final PrintStream out; + private final Formatter formatter = new Formatter(); public TextUi() { this(System.in, System.out); @@ -79,7 +69,7 @@ private boolean isCommentLine(String rawInputLine) { * @return command (full line) entered by the user */ public String getUserCommand() { - out.print(LINE_PREFIX + "Enter command: "); + out.print(formatter.addLinePrefix("Enter command: ")); String fullInputLine = in.nextLine(); // silently consume all ignored lines @@ -87,7 +77,7 @@ public String getUserCommand() { fullInputLine = in.nextLine(); } - showToUser("[Command entered:" + fullInputLine + "]"); + showToUser(formatter.formatCommand(fullInputLine)); return fullInputLine; } @@ -116,7 +106,7 @@ public void showInitFailedMessage() { /** Shows message(s) to the user */ public void showToUser(String... message) { for (String m : message) { - out.println(LINE_PREFIX + m.replace("\n", LS + LINE_PREFIX)); + out.println(formatter.formatMessage(m)); } } @@ -137,10 +127,7 @@ public void showResultToUser(CommandResult result) { * Private contact details are hidden. */ private void showPersonListView(List persons) { - final List formattedPersons = new ArrayList<>(); - for (ReadOnlyPerson person : persons) { - formattedPersons.add(person.getAsTextHidePrivate()); - } + final List formattedPersons = formatter.getFormattedPersons(persons); showToUserAsIndexedList(formattedPersons); } @@ -151,22 +138,7 @@ private void showToUserAsIndexedList(List list) { /** Formats a list of strings as a viewable indexed list. */ private static String getIndexedListForViewing(List listItems) { - final StringBuilder formatted = new StringBuilder(); - int displayIndex = 0 + DISPLAYED_INDEX_OFFSET; - for (String listItem : listItems) { - formatted.append(getIndexedListItem(displayIndex, listItem)).append("\n"); - displayIndex++; - } + final StringBuilder formatted = Formatter.getFormattedIndexedList(listItems); return formatted.toString(); } - - /** - * Formats a string as a viewable indexed list item. - * - * @param visibleIndex visible index for this listing - */ - private static String getIndexedListItem(int visibleIndex, String listItem) { - return String.format(MESSAGE_INDEXED_LIST_ITEM, visibleIndex, listItem); - } - }