Advertisement
vkarakolev

objectsEx4

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