Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ObjectsAndClassesExercise;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- public class Articles2 {
- private String title;
- private String content;
- private String author;
- public Articles2(String title, String content, String author) {
- this.title = title;
- this.content = content;
- this.author = author;
- }
- public String getTitle() {
- return title;
- }
- public String getContent() {
- return content;
- }
- public String getAuthor() {
- return author;
- }
- @Override
- public String toString() {
- return String.format("%s - %s: %s",this.title,this.content,this.author);
- }
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- List<Articles2> articles2List = new ArrayList<>();
- int n = Integer.parseInt(reader.readLine());
- while (n-- > 0){
- String[] tokens = reader.readLine().split(", ");
- String title = tokens[0];
- String content = tokens[1];
- String author = tokens[2];
- Articles2 articles2 = new Articles2(title,content,author);
- articles2List.add(articles2);
- if (n == 0){
- if (title.equals("title")){
- articles2List.stream().sorted((a,a1) -> a.getTitle().compareTo(a1.getTitle()))
- .forEach(p -> System.out.println(p.toString()));
- } else if (title.equals("content")){
- articles2List.stream().sorted(Comparator.comparing(Articles2::getContent))
- .forEach(p -> System.out.println(p.toString()));
- } else {
- articles2List.stream().sorted(Comparator.comparing(Articles2::getAuthor))
- .forEach(p -> System.out.println(p.toString()));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement