Advertisement
petur_stoqnov

Java

Mar 26th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8. Scanner sc = new Scanner(System.in);
  9. //LinkedHashMap<String, Double> linkedHashMap = new LinkedHashMap<>();
  10. List<String> list = new ArrayList<>();
  11. double total = 0.00;
  12.  
  13. String command;
  14. while (!"Purchase".equals(command = sc.nextLine())) {
  15. Pattern regex = Pattern.compile(">>([A-Za-z\\s]+)<<(\\d+\\.?\\d+)!(\\d+)");
  16. //Pattern regex = Pattern.compile("([A-Za-z\\s]+)<<(\\d+(.\\d+)?)!(\\d+)");
  17. Matcher matcher = regex.matcher(command);
  18.  
  19. if (matcher.find()) {
  20. String furniture = matcher.group(1);
  21. double price = Double.parseDouble(matcher.group(2));
  22. int quantity = Integer.parseInt(matcher.group(3));
  23. // if(linkedHashMap.containsKey(furniture)){
  24. // linkedHashMap.put(furniture, linkedHashMap.get(furniture)+(price * quantity));
  25. // }
  26. // linkedHashMap.putIfAbsent(furniture, price * quantity);
  27. list.add(furniture);
  28. total += quantity * price;
  29. }
  30.  
  31. }
  32. //double totalSum;
  33. System.out.println("Bought furniture:");
  34. //linkedHashMap.forEach((key, value) -> System.out.printf("%s%n", key));
  35. // totalSum = linkedHashMap.values().stream().mapToDouble(e -> e).sum();
  36. // System.out.printf("Total money spend: %.2f", totalSum);
  37. list.stream().forEach(e-> System.out.printf("%s%n", e));
  38. System.out.printf("Total money spend: %.2f", total);
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement