Advertisement
Ligh7_of_H3av3n

06. Product Shop

May 20th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. package Lekciq;
  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.  
  13.         TreeMap<String, LinkedHashMap<String, Double>> shops = new TreeMap<>();
  14.  
  15.         String input;
  16.         while (!(input = scanner.nextLine()).equals("Revision")) {
  17.             String[] tokens = input.split(", ");
  18.             String shop = tokens[0];
  19.             String product = tokens[1];
  20.             double price = Double.parseDouble(tokens[2]);
  21.  
  22.             if (!shops.containsKey(shop)) {
  23.                 shops.put(shop, new LinkedHashMap<>());
  24.             }
  25.             shops.get(shop).put(product, price);
  26.         }
  27.  
  28.         for (Map.Entry<String, LinkedHashMap<String, Double>> entry : shops.entrySet()) {
  29.             String shop = entry.getKey();
  30.             LinkedHashMap<String, Double> products = entry.getValue();
  31.  
  32.             System.out.println(shop + "->");
  33.             for (Map.Entry<String, Double> productEntry : products.entrySet()) {
  34.                 String productName = productEntry.getKey();
  35.                 double productPrice = productEntry.getValue();
  36.                 System.out.printf("Product: %s, Price: %.1f\n", productName, productPrice);
  37.             }
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement