Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.Duration;
- import java.util.*;
- import java.util.stream.Collectors;
- class CategoryNotFoundException extends Exception{
- public CategoryNotFoundException(String message) {
- super(message);
- }
- }
- class Category{
- private String name;
- public Category(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Category category = (Category) o;
- return name.equals(category.name);
- }
- }
- abstract class NewsItem{
- protected String title;
- protected Date date;
- protected Category category;
- public NewsItem(String title, Date date, Category category) {
- this.title = title;
- this.date = date;
- this.category = category;
- }
- public String getTitle() {
- return title;
- }
- public Date getDate() {
- return date;
- }
- public Category getCategory() {
- return category;
- }
- abstract String getTeaser();
- }
- class TextNewsItem extends NewsItem{
- private String text;
- public TextNewsItem(String title, Date date, Category category, String text) {
- super(title, date, category);
- this.text = text;
- }
- public String getText() {
- return text;
- }
- @Override
- String getTeaser() {
- Date now = new Date();
- int minutes = (int) ((now.getTime() - date.getTime()) / 60 / 1000);
- return String.format("%s\n%d\n%s\n", title, minutes, text.length()<=80? text : text.substring(0,80));
- }
- @Override
- public String toString() {
- return getTeaser();
- }
- }
- class MediaNewsItem extends NewsItem{
- private String url;
- private int views;
- public MediaNewsItem(String title, Date date, Category category, String url, int views) {
- super(title, date, category);
- this.url = url;
- this.views = views;
- }
- @Override
- String getTeaser() {
- Date now = new Date();
- int minutes = (int) ((now.getTime() - date.getTime()) / 60 / 1000);
- return String.format("%s\n%d\n%s\n%d\n", title, minutes, url, views);
- }
- @Override
- public String toString() {
- return getTeaser();
- }
- }
- class FrontPage{
- List<NewsItem> news;
- Category [] categories;
- public FrontPage(Category [] categories) {
- this.news = new ArrayList<>();
- this.categories = categories;
- }
- public void addNewsItem(NewsItem newsItem){
- news.add(newsItem);
- }
- List<NewsItem> listByCategory(Category category){
- return news.stream().filter(newsItem -> newsItem.getCategory().equals(category)).collect(Collectors.toList());
- }
- List<NewsItem> listByCategoryName(String category) throws CategoryNotFoundException {
- if (Arrays.stream(categories).noneMatch(category1 -> category1.getName().equals(category)))
- throw new CategoryNotFoundException("Category "+category+" was not found");
- List<NewsItem> result = new ArrayList<>();
- Arrays.stream(categories).filter(category1 -> category1.getName().equals(category)).forEach(category1 -> result.addAll(listByCategory(category1)));
- return result;
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- news.forEach(newsItem -> sb.append(newsItem.toString()));
- return sb.toString();
- }
- }
- public class FrontPageTest {
- public static void main(String[] args) {
- // Reading
- Scanner scanner = new Scanner(System.in);
- String line = scanner.nextLine();
- String[] parts = line.split(" ");
- Category[] categories = new Category[parts.length];
- for (int i = 0; i < categories.length; ++i) {
- categories[i] = new Category(parts[i]);
- }
- int n = scanner.nextInt();
- scanner.nextLine();
- FrontPage frontPage = new FrontPage(categories);
- Calendar cal = Calendar.getInstance();
- for (int i = 0; i < n; ++i) {
- String title = scanner.nextLine();
- cal = Calendar.getInstance();
- int min = scanner.nextInt();
- cal.add(Calendar.MINUTE, -min);
- Date date = cal.getTime();
- scanner.nextLine();
- String text = scanner.nextLine();
- int categoryIndex = scanner.nextInt();
- scanner.nextLine();
- TextNewsItem tni = new TextNewsItem(title, date, categories[categoryIndex], text);
- frontPage.addNewsItem(tni);
- }
- n = scanner.nextInt();
- scanner.nextLine();
- for (int i = 0; i < n; ++i) {
- String title = scanner.nextLine();
- int min = scanner.nextInt();
- cal = Calendar.getInstance();
- cal.add(Calendar.MINUTE, -min);
- scanner.nextLine();
- Date date = cal.getTime();
- String url = scanner.nextLine();
- int views = scanner.nextInt();
- scanner.nextLine();
- int categoryIndex = scanner.nextInt();
- scanner.nextLine();
- MediaNewsItem mni = new MediaNewsItem(title, date, categories[categoryIndex], url, views);
- frontPage.addNewsItem(mni);
- }
- // Execution
- String category = scanner.nextLine();
- System.out.println(frontPage);
- for(Category c : categories) {
- System.out.println(frontPage.listByCategory(c).size());
- }
- try {
- System.out.println(frontPage.listByCategoryName(category).size());
- } catch(CategoryNotFoundException e) {
- System.out.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement