Andreeva-Magdalena97

6.Product Shop

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