Advertisement
desislava_topuzakova

06. A Miner Task

Jan 18th, 2022
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. package SetsAndMaps;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class AMinerTask_06 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         LinkedHashMap<String, Integer> resources = new LinkedHashMap<>();
  11.         while(true){
  12.             String resource = scanner.nextLine();
  13.             if(resource.equals("stop")){
  14.                 break;
  15.             }
  16.             int quantity = Integer.parseInt(scanner.nextLine());
  17.  
  18.             if(resources.containsKey(resource)){
  19.                 int currentCount = resources.get(resource);
  20.                 resources.put(resource, currentCount + quantity);
  21.             } else {
  22.                 resources.put(resource, quantity);
  23.             }
  24.  
  25.         }
  26.  
  27.         for (Map.Entry<String, Integer> entry : resources.entrySet()) {
  28.             System.out.printf("%s -> %d%n", entry.getKey(), entry.getValue());
  29.         }
  30.  
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement