Advertisement
shniaga

Untitled

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