desislava_topuzakova

04. Articles 2.0

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