Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Comparator;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.TreeSet;
- public class EventCalendarTest {
- public static void main(String[] args) throws ParseException {
- Scanner scanner = new Scanner(System.in);
- int n = scanner.nextInt();
- scanner.nextLine();
- int year = scanner.nextInt();
- scanner.nextLine();
- EventCalendar eventCalendar = new EventCalendar(year);
- DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm");
- for (int i = 0; i < n; ++i) {
- String line = scanner.nextLine();
- String[] parts = line.split(";");
- String name = parts[0];
- String location = parts[1];
- Date date = df.parse(parts[2]);
- try {
- eventCalendar.addEvent(name, location, date);
- } catch (WrongDateException e) {
- System.out.println(e.getMessage());
- }
- }
- Date date = df.parse(scanner.nextLine());
- eventCalendar.listEvents(date);
- eventCalendar.listByMonth();
- }
- }
- // vashiot kod ovde
- class Event implements Comparable<Event>{
- String name;
- String location;
- Date date;
- public Event(String name, String location, Date date) {
- super();
- this.name = name;
- this.location = location;
- this.date = date;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getLocation() {
- return location;
- }
- public void setLocation(String location) {
- this.location = location;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- @Override
- public String toString() {
- DateFormat df = new SimpleDateFormat("dd MMM, YYY HH:mm");
- String dateFormatted = df.format(date);
- return String.format("%s at %s, %s", dateFormatted,location,name);
- }
- @Override
- public int compareTo(Event o) {
- return Comparator.comparing(Event::getDate)
- .thenComparing(Event::getName).compare(this, o);
- }
- }
- class EventCalendar{
- Map<String,TreeSet<Event>> map;
- Map<Integer,Integer> mesecno;
- int year;
- public EventCalendar(int year) {
- super();
- this.year = year;
- map=new HashMap<>();
- mesecno=new HashMap<>();
- }
- public void addEvent(String name, String location, Date date) throws WrongDateException{
- if(date.getYear()!=year-1900) {
- throw new WrongDateException(date);
- }
- int mesec=date.getMonth();
- mesecno.computeIfPresent(mesec, (key,value) -> ++value);
- mesecno.computeIfAbsent(mesec, (key)->{
- return 1;
- });
- String data=date.getMonth()+" "+date.getDate();
- map.computeIfPresent(data, (key,value)->{
- value.add(new Event(name,location,date));
- return value;
- });
- map.computeIfAbsent(data, (key)->{
- TreeSet<Event> set=new TreeSet<>();
- set.add(new Event(name,location,date));
- return set;
- });
- }
- public void listEvents(Date date) {
- String data=date.getMonth()+" "+date.getDate();
- if(map.get(data)==null) {
- System.out.println("No events on this day!");
- return;
- }
- map.get(data).stream().forEach(System.out::println);
- }
- public void listByMonth() {
- for(int i=0;i<12;i++) {
- if(mesecno.get(i)!=null) {
- System.out.printf("%d : %d\n", i+1,mesecno.get(i));
- }else {
- System.out.printf("%d : %d\n", i+1,0);
- }
- }
- }
- }
- class WrongDateException extends Exception{
- public WrongDateException(Date date) {
- super(String.format("Wrong date: %s", date.toString().replaceAll("GMT", "UTC")));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment