Advertisement
Guest User

Untitled

a guest
May 23rd, 2020
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.lang.reflect.Array;
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import java.util.stream.Collectors;
  7.  
  8. public class das {
  9. public static void main(String[] args) {
  10. Scanner scan = new Scanner(System.in);
  11. String input = scan.nextLine();
  12.  
  13. LinkedHashMap<String, ArrayList<Double>> items = new LinkedHashMap<>();
  14.  
  15. while (!input.equals("buy")){
  16.  
  17. String[] tokens = input.split("\\s+");
  18. String name = tokens[0];
  19. double price = Double.parseDouble(tokens[1]);
  20. double count = Integer.parseInt(tokens[2]);
  21.  
  22. if (items.containsKey(name)){
  23. ArrayList<Double> ceni = items.get(name);
  24. double novaCena = ceni.get(1);
  25. ceni.remove(0);
  26. ceni.add(0, price);
  27. ceni.remove(1);
  28. ceni.add(1, novaCena + count);
  29. items.put(name, new ArrayList<>());
  30. items.put(name, ceni);
  31. }else {
  32. items.putIfAbsent(name, new ArrayList<>());
  33.  
  34. items.get(name).add(price);
  35. items.get(name).add(count);
  36. }
  37.  
  38. input = scan.nextLine();
  39.  
  40. }
  41.  
  42.  
  43. items
  44. .entrySet()
  45. .stream()
  46. .forEach(e -> System.out.printf("%s -> %.2f%n", e.getKey(), e.getValue()));
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement