Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package TextAndRegexExercise;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Furniture_09 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String regex = ">>([A-Za-z]*)<<(\\d+\\.?\\d+)!(\\d+)";
- //">>(?<name>[a-zA-Z]+)<<(?<price>\\d+\\.?\\d+)!(?<quantity>\\d+)"
- Pattern pattern = Pattern.compile(regex);
- Map<String, Double> boughtFurniture = new LinkedHashMap<>();
- String input = "";
- while (!"Purchase".equals(input = scanner.nextLine())) {
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()) {
- String furniture = matcher.group(1);
- double price = Double.parseDouble(matcher.group(2));
- int quantity = Integer.parseInt(matcher.group(3));
- boughtFurniture.put(furniture, price * quantity);
- }
- }
- System.out.println("Bought furniture: ");
- boughtFurniture.forEach((key, value) -> System.out.println(key));
- double sum = boughtFurniture.values().stream().mapToDouble(Double::doubleValue).sum();
- System.out.printf("Total money spend: %.2f",sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement