Advertisement
drago68

Untitled

Oct 26th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class Articles20 {
  9.  
  10. public static class Article {
  11. private String getTitle() {
  12. return title;
  13. }
  14.  
  15. private String getContent() {
  16. return content;
  17. }
  18.  
  19. public String getAuthor() {
  20. return author;
  21. }
  22.  
  23. String title;
  24. String content;
  25. String author;
  26.  
  27. private Article(String title, String content, String author) {
  28. this.title = title;
  29. this.content = content;
  30. this.author = author;
  31. }
  32.  
  33. @Override
  34. public String toString() {
  35. return String.format("%s – %s: %s%n", this.title, this.content, this.author);
  36. }
  37. }
  38.  
  39. public static void main(String[] args) {
  40. Scanner scanner = new Scanner(System.in);
  41.  
  42. List<Article> articles = new ArrayList<>();
  43. int n = Integer.parseInt(scanner.nextLine());
  44. for (int i = 0; i < n; i++) {
  45. String[] tokens = scanner.nextLine().split(", ");
  46. String title = tokens[0];
  47. String content = tokens[1];
  48. String author = tokens[2];
  49.  
  50. Article article = new Article(title, content, author);
  51. articles.add(article);
  52. }
  53. String command = scanner.nextLine();
  54. if (command.equals("title")) {
  55. articles.sort(Comparator.comparing(Article::getTitle));
  56. System.out.println(articles.toString().replaceAll("[\\[\\],]", ""));
  57. } else if (command.equals("content")) {
  58. articles.sort(Comparator.comparing(Article::getContent));
  59. System.out.println(articles.toString().replaceAll("[\\[\\],]", ""));
  60. } else if (command.equals("author")) {
  61. articles.sort(Comparator.comparing(Article::getAuthor));
  62. System.out.print(articles.toString().replaceAll("[\\[\\],]", ""));
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement