Didart

Product Shop

Jan 19th, 2023 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. package SetsAndMaps;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6. import java.util.TreeMap;
  7.  
  8. public class ProductShop {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         String input;
  13.         Map<String, LinkedHashMap<String, Double>> stores = new TreeMap<>();
  14.  
  15.         while (!"Revision".equals(input = scanner.nextLine())) {
  16.             String[] gross = input.split(", ");
  17.             String store = gross[0];
  18.             String product = gross[1];
  19.             double price = Double.parseDouble(gross[2]);
  20.  
  21.             stores.putIfAbsent(store, new LinkedHashMap<>());
  22.             stores.get(store).putIfAbsent(product, price);
  23.         }
  24.         for (String shopName : stores.keySet()) {
  25.             System.out.println(shopName + "->");
  26.             LinkedHashMap<String, Double> products = stores.get(shopName);
  27.  
  28.             for (String proName : products.keySet()) {
  29.                 System.out.printf("Product: %s, Price: %.1f%n", proName, products.get(proName));
  30.             }
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment