Advertisement
Martina312

[НП] - Насловна страница

Aug 20th, 2020
1,764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.83 KB | None | 0 0
  1. import java.time.Duration;
  2. import java.util.*;
  3. import java.util.stream.Collectors;
  4.  
  5. class CategoryNotFoundException extends Exception{
  6.     public CategoryNotFoundException(String message) {
  7.         super(message);
  8.     }
  9. }
  10.  
  11. class Category{
  12.     private String name;
  13.  
  14.     public Category(String name) {
  15.         this.name = name;
  16.     }
  17.  
  18.     public String getName() {
  19.         return name;
  20.     }
  21.  
  22.     @Override
  23.     public boolean equals(Object o) {
  24.         if (this == o) return true;
  25.         if (o == null || getClass() != o.getClass()) return false;
  26.         Category category = (Category) o;
  27.         return name.equals(category.name);
  28.     }
  29. }
  30. abstract class NewsItem{
  31.     protected String title;
  32.     protected Date date;
  33.     protected Category category;
  34.  
  35.     public NewsItem(String title, Date date, Category category) {
  36.         this.title = title;
  37.         this.date = date;
  38.         this.category = category;
  39.     }
  40.  
  41.     public String getTitle() {
  42.         return title;
  43.     }
  44.  
  45.     public Date getDate() {
  46.         return date;
  47.     }
  48.  
  49.     public Category getCategory() {
  50.         return category;
  51.     }
  52.  
  53.     abstract String getTeaser();
  54. }
  55.  
  56. class TextNewsItem extends NewsItem{
  57.     private String text;
  58.  
  59.     public TextNewsItem(String title, Date date, Category category, String text) {
  60.         super(title, date, category);
  61.         this.text = text;
  62.     }
  63.  
  64.     public String getText() {
  65.         return text;
  66.     }
  67.  
  68.     @Override
  69.     String getTeaser() {
  70.         Date now = new Date();
  71.         int minutes = (int) ((now.getTime() - date.getTime()) / 60 / 1000);
  72.         return String.format("%s\n%d\n%s\n", title, minutes, text.length()<=80? text : text.substring(0,80));
  73.     }
  74.  
  75.     @Override
  76.     public String toString() {
  77.         return getTeaser();
  78.     }
  79. }
  80.  
  81. class MediaNewsItem extends NewsItem{
  82.     private String url;
  83.     private int views;
  84.  
  85.     public MediaNewsItem(String title, Date date, Category category, String url, int views) {
  86.         super(title, date, category);
  87.         this.url = url;
  88.         this.views = views;
  89.     }
  90.  
  91.     @Override
  92.     String getTeaser() {
  93.         Date now = new Date();
  94.         int minutes = (int) ((now.getTime() - date.getTime()) / 60 / 1000);
  95.         return String.format("%s\n%d\n%s\n%d\n", title, minutes, url, views);
  96.     }
  97.  
  98.     @Override
  99.     public String toString() {
  100.         return getTeaser();
  101.     }
  102. }
  103.  
  104. class FrontPage{
  105.     List<NewsItem> news;
  106.     Category [] categories;
  107.  
  108.     public FrontPage(Category [] categories) {
  109.         this.news = new ArrayList<>();
  110.         this.categories = categories;
  111.     }
  112.  
  113.     public void addNewsItem(NewsItem newsItem){
  114.         news.add(newsItem);
  115.     }
  116.  
  117.     List<NewsItem> listByCategory(Category category){
  118.         return news.stream().filter(newsItem -> newsItem.getCategory().equals(category)).collect(Collectors.toList());
  119.     }
  120.  
  121.     List<NewsItem> listByCategoryName(String category) throws CategoryNotFoundException {
  122.         if (Arrays.stream(categories).noneMatch(category1 -> category1.getName().equals(category)))
  123.             throw new CategoryNotFoundException("Category "+category+" was not found");
  124.  
  125.         List<NewsItem> result = new ArrayList<>();
  126.         Arrays.stream(categories).filter(category1 -> category1.getName().equals(category)).forEach(category1 -> result.addAll(listByCategory(category1)));
  127.         return result;
  128.     }
  129.  
  130.     @Override
  131.     public String toString() {
  132.         StringBuilder sb = new StringBuilder();
  133.         news.forEach(newsItem -> sb.append(newsItem.toString()));
  134.         return sb.toString();
  135.     }
  136. }
  137.  
  138. public class FrontPageTest {
  139.     public static void main(String[] args) {
  140.         // Reading
  141.         Scanner scanner = new Scanner(System.in);
  142.         String line = scanner.nextLine();
  143.         String[] parts = line.split(" ");
  144.         Category[] categories = new Category[parts.length];
  145.         for (int i = 0; i < categories.length; ++i) {
  146.             categories[i] = new Category(parts[i]);
  147.         }
  148.         int n = scanner.nextInt();
  149.         scanner.nextLine();
  150.         FrontPage frontPage = new FrontPage(categories);
  151.         Calendar cal = Calendar.getInstance();
  152.         for (int i = 0; i < n; ++i) {
  153.             String title = scanner.nextLine();
  154.             cal = Calendar.getInstance();
  155.             int min = scanner.nextInt();
  156.             cal.add(Calendar.MINUTE, -min);
  157.             Date date = cal.getTime();
  158.             scanner.nextLine();
  159.             String text = scanner.nextLine();
  160.             int categoryIndex = scanner.nextInt();
  161.             scanner.nextLine();
  162.             TextNewsItem tni = new TextNewsItem(title, date, categories[categoryIndex], text);
  163.             frontPage.addNewsItem(tni);
  164.         }
  165.  
  166.         n = scanner.nextInt();
  167.         scanner.nextLine();
  168.         for (int i = 0; i < n; ++i) {
  169.             String title = scanner.nextLine();
  170.             int min = scanner.nextInt();
  171.             cal = Calendar.getInstance();
  172.             cal.add(Calendar.MINUTE, -min);
  173.             scanner.nextLine();
  174.             Date date = cal.getTime();
  175.             String url = scanner.nextLine();
  176.             int views = scanner.nextInt();
  177.             scanner.nextLine();
  178.             int categoryIndex = scanner.nextInt();
  179.             scanner.nextLine();
  180.             MediaNewsItem mni = new MediaNewsItem(title, date, categories[categoryIndex], url, views);
  181.             frontPage.addNewsItem(mni);
  182.         }
  183.         // Execution
  184.         String category = scanner.nextLine();
  185.         System.out.println(frontPage);
  186.         for(Category c : categories) {
  187.             System.out.println(frontPage.listByCategory(c).size());
  188.         }
  189.         try {
  190.             System.out.println(frontPage.listByCategoryName(category).size());
  191.         } catch(CategoryNotFoundException e) {
  192.             System.out.println(e.getMessage());
  193.         }
  194.     }
  195. }
  196.  
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement