Didart

Articles 2.0

Nov 1st, 2021
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. import java.util.Scanner;
  5.  
  6. public class Articles2Point0 {
  7.  
  8.     public static class Article {
  9.         private String title;
  10.         private String content;
  11.         private String author;
  12.  
  13.         public Article (String title, String content, String author) {
  14.             this.title = title;
  15.             this.content = content;
  16.             this.author = author;
  17.         }
  18.  
  19.         public void edit (String newContent) {
  20.             this.content = newContent;
  21.         }
  22.  
  23.         public void changeAuthor (String author) {
  24.             this.author = author;
  25.         }
  26.  
  27.         public void rename (String newTitle) {
  28.             this.title = newTitle;
  29.         }
  30.  
  31.         @Override
  32.         public String toString() {
  33.             return this.title + " - " + this.content + ": " + this.author;
  34.         }
  35.  
  36.         public String getTitle() {
  37.             return title;
  38.         }
  39.  
  40.         public String getContent() {
  41.             return content;
  42.         }
  43.  
  44.         public String getAuthor() {
  45.             return author;
  46.         }
  47.     }
  48.     public static void main(String[] args) {
  49.         Scanner scanner = new Scanner(System.in);
  50.  
  51.         int n = Integer.parseInt(scanner.nextLine());
  52.         List<Article> articles = new ArrayList<>(n);
  53.         for (int i = 0; i < n; i++) {
  54.             String [] articleData = scanner.nextLine().split(", ");
  55.  
  56.             String title = articleData[0];
  57.             String content = articleData[1];
  58.             String author = articleData[2];
  59.  
  60.             Article article = new Article(title, content, author);
  61.  
  62.             articles.add(article);
  63.         }
  64.  
  65.         String type = scanner.nextLine();
  66.         switch(type){
  67.             case "title":
  68.                 articles.sort(Comparator.comparing(Article::getTitle));
  69.                 break;
  70.             case "content":
  71.                 articles.sort(Comparator.comparing(Article::getContent));
  72.                 break;
  73.             case "author":
  74.                 articles.sort(Comparator.comparing(Article::getAuthor));
  75.                 break;
  76.         }
  77.  
  78.         articles.forEach(System.out::println);
  79.     }
  80. }
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment