Advertisement
StoyanGrigorov

A Miners Task

Oct 29th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * Created by grigo on 25.10.2016 г..
  8.  */
  9. public class AMinerTask {
  10.     public static void main(String[] args) {
  11.         Scanner console = new Scanner(System.in);
  12.         boolean canContinue = true;
  13.         Map<String, Integer> mats = new LinkedHashMap<>();
  14.  
  15.         while (canContinue) {
  16.             String material = console.nextLine();
  17.  
  18.             int quantity = 0;
  19.             if (material.equals("stop")) {
  20.                 canContinue = false;
  21.                 PrintSortedResults(mats);
  22.                 break;
  23.             } else {
  24.                 quantity = Integer.parseInt(console.nextLine());
  25.             }
  26.  
  27.             if (mats.containsKey(material)) {
  28.                 mats.put(material, mats.get(material) + quantity);
  29.             } else {
  30.                 mats.put(material, quantity);
  31.             }
  32.  
  33.         }
  34.     }
  35.  
  36.     public static void PrintSortedResults(Map<String, Integer> mats) {
  37.         for (Map.Entry<String, Integer> entry : mats.entrySet()) {
  38.  
  39.             System.out.printf("%s -> %d\n", entry.getKey(), entry.getValue());
  40.         }
  41.  
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement