ilianrusev

Maps, Lambda and Stream API - Exercise - Parking

Mar 19th, 2019
78
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.     Scanner scanner = new Scanner(System.in);
  11.  
  12.         Map<String, String> data = new LinkedHashMap<>();
  13.  
  14.  
  15.  
  16.         int number = Integer.parseInt(scanner.nextLine());
  17.         System.out.println();
  18.  
  19.         for (int i = 0; i < number; i++) {
  20.             String[] commands = scanner.nextLine().split(" ");
  21.             String name = commands[1];
  22.  
  23.  
  24.             if (commands.length == 2){
  25.  
  26.  
  27.                 if (!data.containsKey(name)){
  28.                     System.out.printf("ERROR: user %s not found\n",name);
  29.                 }else {
  30.                     data.remove(name);
  31.                     System.out.printf("%s unregistered successfully\n",name);
  32.                 }
  33.             }else if (commands.length == 3){
  34.                 String licensePlate = commands[2];
  35.                 if (!data.containsKey(name)){
  36.                     data.put(name,licensePlate);
  37.                     System.out.printf("%s registered %s successfully\n",name,licensePlate);
  38.                 }else {
  39.                     System.out.printf("ERROR: already registered with plate number %s\n",licensePlate);
  40.                 }
  41.             }
  42.  
  43.         }
  44.  
  45.         data.entrySet().forEach(e -> {
  46.             System.out.printf("%s => %s\n",e.getKey(),e.getValue());
  47.         });
  48.     }
  49. }
RAW Paste Data Copied