valerielashvili

Registered Users

Jul 20th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.LinkedHashMap;
  4. import java.util.Scanner;
  5. import java.util.stream.Collectors;
  6.  
  7. class DateRecord {
  8.     public int id;
  9.     public LocalDate date;
  10.  
  11.     // Declaring a class constructor to create DateRecord object for our HashMap(Dictionary).
  12.     // "This" keyword is used to reference to the class variables "id" and "date",
  13.     // otherwise they will be hidden(shadowed) by the constructors local variables with the same name.
  14.     public DateRecord(int id, LocalDate date) {
  15.         this.id = id;
  16.         this.date = date;
  17.     }
  18.     public int getId() {
  19.         return this.id;
  20.     }
  21.     public LocalDate getDate() {
  22.         return this.date;
  23.     }
  24. }
  25.  
  26. public class p01_Registered_Users {
  27.     public static void main(String[] args) {
  28.         Scanner scanner = new Scanner(System.in);
  29.         String inputLine = scanner.nextLine();
  30.  
  31.         LinkedHashMap<String, DateRecord> registeredUsers = new LinkedHashMap<>();
  32.         int id = 0;
  33.  
  34.         while (!"end".equals(inputLine)) {
  35.             String[] tokens = inputLine.split(" -> ");
  36.             String name = tokens[0];
  37.             LocalDate parsedDate = parseDate(tokens[1]);
  38.  
  39.             registeredUsers.put(name, new DateRecord(id++, parsedDate));
  40.  
  41.             inputLine = scanner.nextLine();
  42.         }
  43.  
  44.         LinkedHashMap<String, DateRecord> orderedData = registeredUsers.entrySet().stream()
  45.                 .sorted((d1, d2) -> d1.getValue().getDate().compareTo(d2.getValue().getDate()))
  46.                 .sorted((id1, id2) -> {return id2.getValue().getId() - id1.getValue().getId();})
  47.                 .limit(5).collect(Collectors.toMap(e -> e.)
  48.     }
  49.  
  50.     private static LocalDate parseDate(String dateInput) {
  51.         // Creating DateTimeFormatter to parse a date from a string using our custom pattern.
  52.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyy");
  53.         LocalDate date = LocalDate.parse(dateInput, formatter);
  54.  
  55.         return date;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment