Advertisement
Guest User

treta

a guest
Mar 29th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class regex {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.        double income = 0.0;
  10.  
  11.        String input = scanner.nextLine();
  12.  
  13.        while (!"end of shift".equals(input)) {
  14.  
  15.            Pattern pattern = Pattern.compile("%(?<name>[A-Z][a-z]{2,})%[^|$%.]*<(?<product>\\w+)>[^|$%.]*\\|(?<count>[0-9]+)\\|[^|$%.]*?(?<price>\\d+\\.?\\d*)\\$");
  16.  
  17.            Matcher matcher = pattern.matcher(input);
  18.  
  19.            if (matcher.find()) {
  20.                String name = matcher.group("name");
  21.                String product = matcher.group("product");
  22.                int count = Integer.parseInt(matcher.group("count"));
  23.                double price = Double.parseDouble(matcher.group("price"));
  24.  
  25.                System.out.printf("%s: %s - %.2f%n", name, product, price * count);
  26.                income = income + price * count;
  27.            }
  28.  
  29.  
  30.  
  31.            input = scanner.nextLine();
  32.        }
  33.  
  34.         System.out.printf("Total income: %.2f", income);
  35.  
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement