Advertisement
IrinaIgnatova

Articles 2.0

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