Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MapsAndSets;
- import java.util.*;
- public class ProductShop {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- String input = s.nextLine();
- Map<String, Map<String, Double>> map = new TreeMap<>();
- while (!input.equals("Revision")){
- String[]elements = input.split(", ");
- String shop = elements[0];
- String product = elements[1];
- double price = Double.parseDouble(elements[2]);
- if (!map.containsKey(shop)){
- map.put(shop, new LinkedHashMap<String, Double>());
- map.get(shop).put(product, price);
- }else {
- map.get(shop).put(product, price);
- }
- input = s.nextLine();
- }
- map.entrySet().stream().sorted((a,b) -> a.getKey().compareTo(b.getKey()))
- .forEach(a ->{
- System.out.println(a.getKey() + "->");
- a.getValue().entrySet().stream().forEach(e -> {
- System.out.printf("Product: %s, Price: %.1f%n" ,e.getKey(), e.getValue());
- });
- });
- }
- }
Add Comment
Please, Sign In to add comment