nikeza

Product Shop /nested Map in Map/

Sep 10th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class temp_Advanced {
  5.     public static void main(String[] args) {
  6.          Scanner scanner = new Scanner(System.in);
  7.  
  8.         String input = scanner.nextLine();
  9.  
  10.         Map<String, Map<String, Double>> shops = new TreeMap<>();
  11.  
  12.         while (!input.equals("Revision")) {
  13.             String[] tokens = input.split(", ");
  14.             String shopName = tokens[0];
  15.             String product = tokens[1];
  16.             double price = Double.parseDouble(tokens[2]);
  17.  
  18.             Map<String, Double> priceList = shops.get(shopName);
  19.             if (!shops.containsKey(shopName)) {
  20.                 priceList = new LinkedHashMap<>();
  21.                 priceList.put(product, price);
  22.                 shops.put(shopName, priceList);
  23.             } else {
  24.                 shops.get(shopName).put(product, price);
  25.             }
  26.  
  27.             input = scanner.nextLine();
  28.         }
  29.  
  30.         for (Map.Entry<String, Map<String, Double>> entry : shops.entrySet()) {
  31.             System.out.println(entry.getKey() + "->");
  32.             for (Map.Entry<String, Double> a : entry.getValue().entrySet()) {
  33.                 System.out.printf("Product: %s, Price: %.1f%n", a.getKey(), a.getValue());
  34.             }
  35.  
  36.         }
  37.  
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment