Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. package ObjectsAndClassesExcercise;
  2.  
  3.         import java.util.ArrayList;
  4.         import java.util.List;
  5.         import java.util.Scanner;
  6.  
  7. public class ArticlesTwo {
  8.     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 String getTitle() {
  20.             return title;
  21.         }
  22.  
  23.         public String getContent() {
  24.             return content;
  25.         }
  26.  
  27.         public String getAuthor() {
  28.             return author;
  29.         }
  30.  
  31.         @Override
  32.         public String toString() {
  33.             return String.format("%s - %s: %s", this.title, this.content, this.author);
  34.         }
  35.     }
  36.     public static void main(String[] args) {
  37.         Scanner scanner = new Scanner(System.in);
  38.  
  39.         int n = Integer.parseInt(scanner.nextLine());
  40.         List<Article> articles = new ArrayList<>();
  41.  
  42.         while (n-- > 0) {
  43.             String[] input = scanner.nextLine().split(", ");
  44.             String title = input[0];
  45.             String content = input[1];
  46.             String author = input[2];
  47.  
  48.             Article article = new Article(title, content, author);
  49.             articles.add(article);
  50.         }
  51.  
  52.         String criteria = scanner.nextLine();
  53.  
  54.         if (criteria.equalsIgnoreCase("title")){
  55.             articles.sort((f,s) -> f.getTitle().compareTo(s.getTitle()));
  56.         } else if (criteria.equalsIgnoreCase("content")) {
  57.             articles.sort((f,s) -> f.getContent().compareTo(s.getContent()));
  58.         } else {
  59.             articles.sort((f,s) -> f.getAuthor().compareTo(s.getAuthor()));
  60.         }
  61.         for (Article article : articles) {
  62.             System.out.println(article.toString());
  63.  
  64.         }
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement