Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class ExObjClss_04_Articles_2_0{
- static class Article2{
- private String title;
- private String content;
- public String getTitle() {
- return title;
- }
- public String getContent() {
- return content;
- }
- public String getAuthor() {
- return author;
- }
- private String author;
- public Article2(String title, String content, String author) {
- this.title = title;
- this.content = content;
- this.author = author;
- }
- public String toString() {
- return String.format("%s - %s: %s", this.getTitle(), getContent(), this.getAuthor());
- }
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- List<Article2> articlesForSort = new ArrayList<>();
- while (n-- > 0) {
- String[] data = scanner.nextLine().split(", ");
- Article2 article = new Article2(data[0], data[1], data[2]);
- articlesForSort.add(article);
- }
- String sortBy = scanner.nextLine();
- switch (sortBy) {
- case "title":
- articlesForSort.stream().sorted((f, s) -> f.getTitle().compareTo(s.getTitle())).forEach(p -> System.out.println(p.toString()));
- break;
- case "content":
- articlesForSort.stream().sorted((f, s) -> f.getContent().compareTo(s.getContent())).forEach(p -> System.out.println(p.toString()));
- break;
- case "author":
- articlesForSort.stream().sorted((f, s) -> f.getAuthor().compareTo(s.getAuthor())).forEach(p -> System.out.println(p.toString()));
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement