public void addMessage(Message message) { data.add(message); Message last = data.get(data.size() - 1); if (last.getText().length() > MESSAGE_LENGTH) { System.out.println(RED + "WARNING: long message (more than 140 symbols)." + END); } System.out.println("Successfully added: " + last.getFormattedMessage()); log("ADD " + last.getId() + " " + last.getAuthor() + " " + last.getText()); saveMessagesToJsonFile(MESSAGES_EXTERNAL_STORAGE); }
private void searchByKeyword(String keyword) throws Exception { System.out.println("BY KEYWORD: " + keyword); int counter = 0; for (Message i : data) { if (i.getText().contains(keyword)) { System.out.println(i.getFormattedMessage()); counter++; } } if (counter == 0) { System.out.println("nothing found"); log("SEARCH by keyword: " + keyword + ", nothing found"); } else { log("SEARCH by keyword: " + keyword + ", found: " + counter); } }
private void searchByRegex(String regex) throws Exception { System.out.println("BY REGULAR EXPRESSION: " + regex); int counter = 0; for (Message i : data) { if (Pattern.matches(regex, i.getText())) { System.out.println(i.getFormattedMessage()); counter++; } } if (counter == 0) { System.out.println("nothing found"); log("SEARCH by regex: " + regex + ", nothing found"); } else { log("SEARCH by regex: " + regex + ", found: " + counter); } }
private void searchByAuthor(String author) throws Exception { System.out.println("BY AUTHOR: " + author); int counter = 0; for (Message i : data) { if (i.getAuthor().equals(author)) { System.out.println(i.getFormattedMessage()); counter++; } } if (counter == 0) { System.out.println("nothing found"); log("SEARCH by author: " + author + ", nothing found"); } else { log("SEARCH by author: " + author + ", found: " + counter); } }
public void showMessages(boolean isFormatted) { try { Collections.sort(data); if (isFormatted) { System.out.println("FORMATTED LIST OF MESSAGES:"); log("QUERY formatted list"); for (Message i : data) { System.out.println(i.getFormattedMessage()); } } else { System.out.println("FULL LIST OF MESSAGES:"); log("QUERY full list"); for (Message i : data) { System.out.println(i.toString()); } } } catch (Exception e) { System.out.println(RED + "Failed on your query. Try another one." + END); log("QUERY failed"); } }