valerielashvili

Registered Users_Nadya

Jul 21st, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.time.LocalDate;
  2. import java.time.format.DateTimeFormatter;
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5. import java.util.stream.Stream;
  6.  
  7. /*class DateRecord {
  8.     private int id;
  9.     private 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_2_Registered_Users_test {
  27.     public static void main(String[] args) {
  28.         Scanner scanner = new Scanner(System.in);
  29.         String inputLine = scanner.nextLine();
  30.  
  31.         LinkedHashMap<String, LocalDate> registeredUsers = new LinkedHashMap<>();
  32.         int id = 1;
  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, parsedDate);
  40.  
  41.             inputLine = scanner.nextLine();
  42.         }
  43.  
  44.         Stream<Map.Entry<String, LocalDate>> orderedData = /*new LinkedHashMap<>(*/registeredUsers.entrySet().stream()
  45.                 .sorted((a, b) -> {
  46.                     if (a.getValue().compareTo(b.getValue()) == 0) {
  47.                         return -1;
  48.                     }
  49.                     return a.getValue().compareTo(b.getValue());
  50.                 })
  51.                 .limit(5)
  52.                 .sorted((a, b) -> b.getValue().compareTo(a.getValue()));
  53.                 /*.collect(Collectors.toMap(Map.Entry<String, LocalDate>::getKey,
  54.                         Map.Entry<String, LocalDate>::getValue)));*/
  55.  
  56.         for (Map.Entry<String, LocalDate> orderedDataEntry : orderedData.collect(Collectors.toList())/*orderedData.entrySet()*/) {
  57.             //int idToPrint = orderedDataEntry.getValue().getId();
  58.             String name = orderedDataEntry.getKey();
  59.             String date = formatDate(orderedDataEntry.getValue());
  60.  
  61.             System.out.printf("%s - > %s\n", name, date);
  62.         }
  63.     }
  64.  
  65.     private static LocalDate parseDate(String dateInput) {
  66.         // Creating DateTimeFormatter to parse a date from a string using our custom pattern.
  67.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyy");
  68.         LocalDate date = LocalDate.parse(dateInput, formatter);
  69.  
  70.         return date;
  71.     }
  72.     private static String formatDate(LocalDate dateToFormat) {
  73.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyy");
  74.         String formattedDate = dateToFormat.format(formatter);
  75.  
  76.         return formattedDate;
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment