Niksi

НП Календар на настани

Jun 19th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1. import java.text.DateFormat;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Comparator;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Scanner;
  9. import java.util.Set;
  10. import java.util.TreeSet;
  11.  
  12. public class EventCalendarTest {
  13.     public static void main(String[] args) throws ParseException {
  14.         Scanner scanner = new Scanner(System.in);
  15.         int n = scanner.nextInt();
  16.         scanner.nextLine();
  17.         int year = scanner.nextInt();
  18.         scanner.nextLine();
  19.         EventCalendar eventCalendar = new EventCalendar(year);
  20.         DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm");
  21.         for (int i = 0; i < n; ++i) {
  22.             String line = scanner.nextLine();
  23.             String[] parts = line.split(";");
  24.             String name = parts[0];
  25.             String location = parts[1];
  26.             Date date = df.parse(parts[2]);
  27.             try {
  28.                 eventCalendar.addEvent(name, location, date);
  29.             } catch (WrongDateException e) {
  30.                 System.out.println(e.getMessage());
  31.             }
  32.         }
  33.         Date date = df.parse(scanner.nextLine());
  34.         eventCalendar.listEvents(date);
  35.         eventCalendar.listByMonth();
  36.     }
  37. }
  38.  
  39. // vashiot kod ovde
  40.  
  41. class Event implements Comparable<Event>{
  42.     String name;
  43.     String location;
  44.     Date date;
  45.     public Event(String name, String location, Date date) {
  46.         super();
  47.         this.name = name;
  48.         this.location = location;
  49.         this.date = date;
  50.     }
  51.     public String getName() {
  52.         return name;
  53.     }
  54.     public void setName(String name) {
  55.         this.name = name;
  56.     }
  57.     public String getLocation() {
  58.         return location;
  59.     }
  60.     public void setLocation(String location) {
  61.         this.location = location;
  62.     }
  63.     public Date getDate() {
  64.         return date;
  65.     }
  66.     public void setDate(Date date) {
  67.         this.date = date;
  68.     }
  69.     @Override
  70.     public String toString() {
  71.         DateFormat df = new SimpleDateFormat("dd MMM, YYY HH:mm");
  72.         String dateFormatted = df.format(date);
  73.         return String.format("%s at %s, %s", dateFormatted,location,name);
  74.     }
  75.     @Override
  76.     public int compareTo(Event o) {
  77.         return Comparator.comparing(Event::getDate)
  78.                 .thenComparing(Event::getName).compare(this, o);
  79.     }
  80.    
  81. }
  82.  
  83. class EventCalendar{
  84.     Map<String,TreeSet<Event>> map;
  85.     Map<Integer,Integer> mesecno;
  86.     int year;
  87.    
  88.     public EventCalendar(int year) {
  89.         super();
  90.         this.year = year;
  91.         map=new HashMap<>();
  92.         mesecno=new HashMap<>();
  93.     }
  94.    
  95.     public void addEvent(String name, String location, Date date)  throws WrongDateException{
  96.         if(date.getYear()!=year-1900) {
  97.             throw new WrongDateException(date);
  98.         }
  99.        
  100.         int mesec=date.getMonth();
  101.        
  102.         mesecno.computeIfPresent(mesec, (key,value) -> ++value);
  103.        
  104.         mesecno.computeIfAbsent(mesec, (key)->{
  105.             return 1;
  106.         });
  107.        
  108.         String data=date.getMonth()+" "+date.getDate();
  109.        
  110.         map.computeIfPresent(data, (key,value)->{
  111.             value.add(new Event(name,location,date));
  112.             return value;
  113.         });
  114.        
  115.         map.computeIfAbsent(data, (key)->{
  116.             TreeSet<Event> set=new TreeSet<>();
  117.             set.add(new Event(name,location,date));
  118.             return set;
  119.         });
  120.        
  121.        
  122.     }
  123.    
  124.     public void listEvents(Date date) {
  125.         String data=date.getMonth()+" "+date.getDate();
  126.         if(map.get(data)==null) {
  127.             System.out.println("No events on this day!");
  128.             return;
  129.         }
  130.        
  131.        
  132.         map.get(data).stream().forEach(System.out::println);
  133.     }
  134.    
  135.    
  136.     public void listByMonth() {
  137.         for(int i=0;i<12;i++) {
  138.             if(mesecno.get(i)!=null) {
  139.             System.out.printf("%d : %d\n", i+1,mesecno.get(i));
  140.             }else {
  141.                 System.out.printf("%d : %d\n", i+1,0);
  142.             }
  143.         }
  144.        
  145.        
  146.     }
  147.    
  148.    
  149.    
  150. }
  151.  
  152. class WrongDateException extends Exception{
  153.     public WrongDateException(Date date) {
  154.         super(String.format("Wrong date: %s", date.toString().replaceAll("GMT", "UTC")));
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment