Advertisement
Martina312

Untitled

Aug 23rd, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. import java.text.DateFormat;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.time.LocalDate;
  5. import java.util.*;
  6. import java.util.stream.IntStream;
  7.  
  8. class WrongDateException extends Exception {
  9. public WrongDateException(String message) {
  10. super(message);
  11. }
  12. }
  13.  
  14. public class EventCalendarTest {
  15. public static void main(String[] args) throws ParseException {
  16. Scanner scanner = new Scanner(System.in);
  17. int n = scanner.nextInt();
  18. scanner.nextLine();
  19. int year = scanner.nextInt();
  20. scanner.nextLine();
  21. EventCalendar eventCalendar = new EventCalendar(year);
  22. DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm");
  23. for (int i = 0; i < n; ++i) {
  24. String line = scanner.nextLine();
  25. String[] parts = line.split(";");
  26. String name = parts[0];
  27. String location = parts[1];
  28. Date date = df.parse(parts[2]);
  29. try {
  30. eventCalendar.addEvent(name, location, date);
  31. } catch (WrongDateException e) {
  32. System.out.println(e.getMessage());
  33. }
  34. }
  35. Date date = df.parse(scanner.nextLine());
  36. eventCalendar.listEvents(date);
  37. eventCalendar.listByMonth();
  38. }
  39. }
  40.  
  41. // vashiot kod ovde
  42. class EventCalendar {
  43. class Event implements Comparable<Event> {
  44. String name;
  45. String location;
  46. Date date;
  47.  
  48. public Event(String name, String location, Date date) {
  49. this.name = name;
  50. this.location = location;
  51. this.date = date;
  52. }
  53.  
  54. public Date getDate() {
  55. return date;
  56. }
  57.  
  58. @Override
  59. public int compareTo(Event event) {
  60. Comparator<Event> date = Comparator.comparing(Event::getDate);
  61. Comparator<Event> name = date.thenComparing(Event::getName);
  62.  
  63. return name.compare(this, event);
  64. }
  65.  
  66. public String getName() {
  67. return name;
  68.  
  69. }
  70.  
  71. @Override
  72. public String toString() {
  73. SimpleDateFormat df = new SimpleDateFormat("dd MMM, YYY HH:mm");
  74. String d = df.format(date);
  75. return String.format("%s at %s, %s", d, location, name);
  76. }
  77. }
  78. int year;
  79. HashMap<Integer, TreeSet<Event>> events;
  80. int[] eventsInMonth;
  81.  
  82. public EventCalendar(int year) {
  83. this.year = year;
  84. events = new HashMap<>();
  85. eventsInMonth = new int[12];
  86. }
  87.  
  88. public void addEvent(String name, String location, Date date) throws WrongDateException {
  89. Calendar cal = Calendar.getInstance();
  90. cal.setTime(date);
  91.  
  92. if (cal.get(Calendar.YEAR) != year)
  93. throw new WrongDateException("Wrong date: " + date.toString()
  94. .replaceAll("GMT", "UTC"));
  95.  
  96. eventsInMonth[cal.get(Calendar.MONTH)]++;
  97.  
  98. events.putIfAbsent(cal.get(Calendar.DAY_OF_YEAR), new TreeSet<>());
  99. events.get(cal.get(Calendar.DAY_OF_YEAR)).add(new Event(name, location, date));
  100. }
  101.  
  102. public void listEvents(Date date) {
  103. Calendar cal = Calendar.getInstance();
  104. cal.setTime(date);
  105. if (events.containsKey(cal.get(Calendar.DAY_OF_YEAR)))
  106. events.get(cal.get(Calendar.DAY_OF_YEAR))
  107. .forEach(System.out::println);
  108. else
  109. System.out.println("No events on this day!");
  110. }
  111.  
  112. public void listByMonth() {
  113. for (int i = 0; i < 12; i++) {
  114. System.out.println(i + 1 + " : " + eventsInMonth[i]);
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement