desislava_topuzakova

02. A Miner Task

Jul 20th, 2021
2,693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         Map<String, Integer> resourcesQuantity = new LinkedHashMap<>();
  7.         //ресурс -> сума от количество
  8.        String resource = scanner.nextLine(); //ресурс или "stop"
  9.        while(!resource.equals("stop")) {
  10.            //ресурс
  11.            int quantity = Integer.parseInt(scanner.nextLine());
  12.             //проверка имам ли такъв ресурс
  13.            //ако нямам такъв ресурс
  14.            if(!resourcesQuantity.containsKey(resource)) {
  15.                resourcesQuantity.put(resource, quantity);
  16.            }
  17.            //ако имам такъв ресурс
  18.            else {
  19.                int currentQuantity = resourcesQuantity.get(resource);
  20.                resourcesQuantity.put(resource, currentQuantity + quantity);
  21.            }
  22.            resource = scanner.nextLine();
  23.        }
  24.  
  25.        //resourcesQuantity.forEach((key, value) -> System.out.println(key + " -> " + value));
  26.         resourcesQuantity.entrySet().forEach(entry -> System.out.println(entry.getKey() + " -> " + entry.getValue()));
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment