Advertisement
Guest User

04. Articles 2.0

a guest
Dec 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.lang.reflect.Array;
  4. import java.util.ArrayList;
  5. import java.util.Comparator;
  6. import java.util.List;
  7. import java.util.Scanner;
  8.  
  9. public class Main {
  10.  
  11. public static class Article {
  12. private String getTitle () {
  13. return title;
  14.  
  15. }
  16.  
  17. private String getContent () {
  18. return content;
  19. }
  20.  
  21. private String getAuthor () {
  22. return author;
  23. }
  24. String title;
  25. String content;
  26. String author;
  27. public Article (String title, String content,String author ) {
  28. this.title=title;
  29. this.author= author;
  30. this.content=content;
  31. }
  32.  
  33.  
  34.  
  35.  
  36.  
  37. @Override
  38. public String toString() {
  39. return String.format ("%s - %s: %s%n", this.title, this.content, this.author);
  40. }
  41. }
  42.  
  43.  
  44.  
  45. public static void main(String[] args) {
  46. Scanner sc= new Scanner(System.in);
  47. List<Article> articles = new ArrayList<>();
  48. int numberCommannds = Integer.parseInt(sc.nextLine());
  49. for (int i = 0; i < numberCommannds ; i++) {
  50.  
  51. String[] tokens = sc.nextLine().split(", ");
  52. String title = tokens[0];
  53. String content = tokens[1];
  54. String author = tokens[2];
  55.  
  56. Article article = new Article(title, content, author);
  57. articles.add(article);
  58. }
  59. String command = sc.nextLine();
  60. if (command.equals("title")) {
  61. articles.sort(Comparator.comparing(Article::getTitle));
  62. System.out.println(articles.toString().replaceAll("[\\[\\],]", ""));
  63. } else if (command.equals("content")) {
  64. articles.sort(Comparator.comparing(Article::getContent));
  65. System.out.println(articles.toString().replaceAll("[\\[\\],]", ""));
  66.  
  67. } else if (command.equals("author")) {
  68. articles.sort(Comparator.comparing(Article::getAuthor));
  69. System.out.println(articles.toString().replaceAll("[\\[\\],]", ""));
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. }
  79.  
  80.  
  81.  
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement