Advertisement
Edzhevit

Article

Nov 12th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. package ObjectsAndClassesExercise;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Comparator;
  8. import java.util.List;
  9.  
  10. public class Articles2 {
  11.     private String title;
  12.     private String content;
  13.     private String author;
  14.  
  15.     public Articles2(String title, String content, String author) {
  16.         this.title = title;
  17.         this.content = content;
  18.         this.author = author;
  19.     }
  20.  
  21.     public String getTitle() {
  22.         return title;
  23.     }
  24.  
  25.     public String getContent() {
  26.         return content;
  27.     }
  28.  
  29.     public String getAuthor() {
  30.         return author;
  31.     }
  32.  
  33.     @Override
  34.     public String toString() {
  35.         return String.format("%s - %s: %s",this.title,this.content,this.author);
  36.     }
  37.  
  38.     public static void main(String[] args) throws IOException {
  39.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  40.  
  41.         List<Articles2> articles2List = new ArrayList<>();
  42.  
  43.         int n = Integer.parseInt(reader.readLine());
  44.  
  45.         while (n-- > 0){
  46.             String[] tokens = reader.readLine().split(", ");
  47.             String title = tokens[0];
  48.             String content = tokens[1];
  49.             String author = tokens[2];
  50.  
  51.             Articles2 articles2 = new Articles2(title,content,author);
  52.             articles2List.add(articles2);
  53.             if (n == 0){
  54.                 if (title.equals("title")){
  55.                     articles2List.stream().sorted((a,a1) -> a.getTitle().compareTo(a1.getTitle()))
  56.                             .forEach(p -> System.out.println(p.toString()));
  57.                 } else if (title.equals("content")){
  58.                     articles2List.stream().sorted(Comparator.comparing(Articles2::getContent))
  59.                             .forEach(p -> System.out.println(p.toString()));
  60.                 } else {
  61.                     articles2List.stream().sorted(Comparator.comparing(Articles2::getAuthor))
  62.                             .forEach(p -> System.out.println(p.toString()));
  63.                 }
  64.             }
  65.  
  66.         }
  67.  
  68.  
  69.  
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement