Filip_Markoski

[NP] Насловна Страница

Nov 12th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.19 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class CategoryNotFoundException extends Exception {
  4.     private String category;
  5.  
  6.     public CategoryNotFoundException(String category) {
  7.         this.category = category;
  8.     }
  9.  
  10.     @Override
  11.     public String getMessage() {
  12.         return String.format("Category %s was not found", category);
  13.     }
  14. }
  15.  
  16.  
  17. class Category {
  18.     private String name;
  19.  
  20.     public Category(String name) {
  21.         this.name = name;
  22.     }
  23.  
  24.     @Override
  25.     public boolean equals(Object o) {
  26.         if (this == o) return true;
  27.         if (o == null || getClass() != o.getClass()) return false;
  28.  
  29.         Category category = (Category) o;
  30.  
  31.         return name != null ? name.equals(category.name) : category.name == null;
  32.     }
  33.  
  34.     @Override
  35.     public int hashCode() {
  36.         return name != null ? name.hashCode() : 0;
  37.     }
  38. }
  39.  
  40. abstract class NewsItem {
  41.     protected String title;
  42.     protected Date date;
  43.     protected Category category;
  44.  
  45.     public NewsItem(String title, Date date, Category category) {
  46.         this.title = title;
  47.         this.date = date;
  48.         this.category = category;
  49.     }
  50.  
  51.     public String getType() {
  52.         return getClass().toString();
  53.     }
  54.  
  55.     public String getTitle() {
  56.         return title;
  57.     }
  58.  
  59.     public void setTitle(String title) {
  60.         this.title = title;
  61.     }
  62.  
  63.     public Date getDate() {
  64.         return date;
  65.     }
  66.  
  67.     public void setDate(Date date) {
  68.         this.date = date;
  69.     }
  70.  
  71.     public void setCategory(Category category) {
  72.         this.category = category;
  73.     }
  74.  
  75.     public Category getCategory() {
  76.         return category;
  77.     }
  78.  
  79.     public String getTeaser() {
  80.         StringBuffer sb = new StringBuffer();
  81.  
  82.         /* Append title */
  83.         sb.append(title);
  84.         sb.append(System.lineSeparator());
  85.  
  86.         /* Append minutes */
  87.         Date now = new Date();
  88.         long minutes = (now.getTime() - getDate().getTime()) / 60000;
  89.         sb.append(minutes);
  90.         sb.append(System.lineSeparator());
  91.  
  92.         return sb.toString();
  93.     }
  94. }
  95.  
  96. class TextNewsItem extends NewsItem {
  97.     private StringBuffer text;
  98.  
  99.     public TextNewsItem(String title, Date date, Category category, String text) {
  100.         super(title, date, category);
  101.         this.text = new StringBuffer(text);
  102.     }
  103.  
  104.     public String getType() {
  105.         return getClass().toString();
  106.     }
  107.  
  108.     public String getTeaser() {
  109.         StringBuffer sb = new StringBuffer();
  110.  
  111.         /* Append title and minutes */
  112.         sb.append(super.getTeaser());
  113.  
  114.         /* Append teaser */
  115.         if (text.length() > 80) {
  116.             sb.append(text.subSequence(0, 80).toString());
  117.         } else {
  118.             sb.append(text.toString());
  119.         }
  120.         sb.append(System.lineSeparator());
  121.         return sb.toString();
  122.     }
  123. }
  124.  
  125. class MediaNewsItem extends NewsItem {
  126.     private String url;
  127.     private int views;
  128.  
  129.     public MediaNewsItem(String title, Date date, Category category, String url, int views) {
  130.         super(title, date, category);
  131.         this.url = url;
  132.         this.views = views;
  133.     }
  134.  
  135.     public String getType() {
  136.         return getClass().toString();
  137.     }
  138.  
  139.     @Override
  140.     public String getTeaser() {
  141.         StringBuffer sb = new StringBuffer();
  142.  
  143.         /* Append title and minutes */
  144.         sb.append(super.getTeaser());
  145.  
  146.         /* Append url */
  147.         sb.append(url);
  148.         sb.append(System.lineSeparator());
  149.  
  150.         /* Append visits */
  151.         sb.append(views);
  152.         sb.append(System.lineSeparator());
  153.  
  154.         return sb.toString();
  155.     }
  156. }
  157.  
  158. class FrontPage {
  159.     ArrayList<NewsItem> items;
  160.     Category categories[];
  161.  
  162.     public FrontPage(Category categories[]) {
  163.         this.items = new ArrayList<>();
  164.         this.categories = Arrays.copyOf(categories, categories.length);
  165.     }
  166.  
  167.     public void addNewsItem(NewsItem newsItem) {
  168.         items.add(newsItem);
  169.     }
  170.  
  171.     public ArrayList<NewsItem> listByCategory(Category category) {
  172.         ArrayList<NewsItem> select = new ArrayList<>();
  173.         for (NewsItem item : items) {
  174.             if (item.getCategory().equals(category)) {
  175.                 select.add(item);
  176.             }
  177.         }
  178.         return select;
  179.     }
  180.  
  181.     public ArrayList<NewsItem> listByCategoryName(String category) throws CategoryNotFoundException {
  182.         Category temp = new Category(category);
  183.         boolean exists = false;
  184.         for (Category cat : categories) {
  185.             if (cat.equals(temp)) {
  186.                 exists = true;
  187.             }
  188.         }
  189.         if (!exists) {
  190.             throw new CategoryNotFoundException(category);
  191.         }
  192.  
  193.         /* Given that it exists */
  194.         return listByCategory(temp);
  195.     }
  196.  
  197.     @Override
  198.     public String toString() {
  199.         StringBuffer sb = new StringBuffer();
  200.         for (NewsItem item : items) {
  201.             sb.append(item.getTeaser());
  202.         }
  203.         return sb.toString();
  204.     }
  205. }
  206.  
  207. public class FrontPageTest {
  208.     public static void main(String[] args) {
  209.         // Reading
  210.         Scanner scanner = new Scanner(System.in);
  211.         String line = scanner.nextLine();
  212.         String[] parts = line.split(" ");
  213.         Category[] categories = new Category[parts.length];
  214.         for (int i = 0; i < categories.length; ++i) {
  215.             categories[i] = new Category(parts[i]);
  216.         }
  217.         int n = scanner.nextInt();
  218.         scanner.nextLine();
  219.         FrontPage frontPage = new FrontPage(categories);
  220.         Calendar cal = Calendar.getInstance();
  221.         for (int i = 0; i < n; ++i) {
  222.             String title = scanner.nextLine();
  223.             cal = Calendar.getInstance();
  224.             int min = scanner.nextInt();
  225.             cal.add(Calendar.MINUTE, -min);
  226.             Date date = cal.getTime();
  227.             scanner.nextLine();
  228.             String text = scanner.nextLine();
  229.             int categoryIndex = scanner.nextInt();
  230.             scanner.nextLine();
  231.             TextNewsItem tni = new TextNewsItem(title, date, categories[categoryIndex], text);
  232.             frontPage.addNewsItem(tni);
  233.         }
  234.  
  235.         n = scanner.nextInt();
  236.         scanner.nextLine();
  237.         for (int i = 0; i < n; ++i) {
  238.             String title = scanner.nextLine();
  239.             int min = scanner.nextInt();
  240.             cal = Calendar.getInstance();
  241.             cal.add(Calendar.MINUTE, -min);
  242.             scanner.nextLine();
  243.             Date date = cal.getTime();
  244.             String url = scanner.nextLine();
  245.             int views = scanner.nextInt();
  246.             scanner.nextLine();
  247.             int categoryIndex = scanner.nextInt();
  248.             scanner.nextLine();
  249.             MediaNewsItem mni = new MediaNewsItem(title, date, categories[categoryIndex], url, views);
  250.             frontPage.addNewsItem(mni);
  251.         }
  252.         // Execution
  253.         String category = scanner.nextLine();
  254.         System.out.println(frontPage);
  255.         for (Category c : categories) {
  256.             System.out.println(frontPage.listByCategory(c).size());
  257.         }
  258.         try {
  259.             System.out.println(frontPage.listByCategoryName(category).size());
  260.         } catch (CategoryNotFoundException e) {
  261.             System.out.println(e.getMessage());
  262.         }
  263.     }
  264. }
Add Comment
Please, Sign In to add comment