Advertisement
beinsaduno

04.Orders

Mar 19th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Demo {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. Map<String, Map<Double, Integer>> map = new LinkedHashMap<>();
  8.  
  9. String command = scanner.nextLine();
  10.  
  11. while (!"buy".equals(command)) {
  12. String[] array = command.trim().split("\\s+");
  13. String product = array[0];
  14. double price = Double.parseDouble(array[1]);
  15. int quantity = Integer.parseInt(array[2]);
  16.  
  17. if (map.containsKey(product)) {
  18. map.get(product).put(price, map.get(product).get(price) + quantity);
  19. } else {
  20. map.putIfAbsent(product, new LinkedHashMap<>());
  21. map.get(product).put(price, quantity);
  22. }
  23.  
  24. command = scanner.nextLine();
  25. }
  26. map
  27. .forEach((k, v) -> {
  28. System.out.printf("%s -> ", k);
  29. v
  30. .forEach((key, value) -> System.out.printf("%.2f%n", key * value));
  31. });
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement