Advertisement
mark79

Articles_v2

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