Didart

A Miner Task

Nov 7th, 2021
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class AMinerTask {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         Map<String, Integer> quantity = new LinkedHashMap<>();
  10.  
  11.         String resource = scanner.nextLine();
  12.         while (!resource.equals("stop")) {
  13.             int value = Integer.parseInt(scanner.nextLine());
  14.  
  15.             if (!quantity.containsKey(resource)) {
  16.                 quantity.put(resource, value);
  17.             } else {
  18.                 int currentQuantity = quantity.get(resource);
  19.                 quantity.put(resource, currentQuantity + value);
  20.             }
  21.             resource = scanner.nextLine();
  22.         }
  23.         quantity.forEach((key, value) -> System.out.println
  24.                 (key + " -> " + value));
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment