RGAlex

O3. SoftUni Bar Income / RegularExpressions - Exercise

Mar 19th, 2021 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. package RegularExpressions.Exercise;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class O3SoftUniBarIncome {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         Pattern pattern = Pattern.compile("%(?<customer>[A-Z][a-z]+)%[^$%|.]*<(?<product>\\w+)>[^$%|.]*\\|(?<quantity>\\d+)\\|[^$%|.]*?(?<price>\\d+\\.?\\d+)\\$");
  11.         String input = scanner.nextLine();
  12.         double sum = 0;
  13.         while (!"end of shift".equals(input)) {
  14.             Matcher matcher = pattern.matcher(input);
  15.             if (matcher.find()) {
  16.                 double totalPrice = Double.parseDouble(matcher.group("price")) * Integer.parseInt(matcher.group("quantity"));
  17.                 System.out.printf("%s: %s - %.2f%n", matcher.group("customer"), matcher.group("product"), totalPrice);
  18.                 sum += totalPrice;
  19.             }
  20.             input = scanner.nextLine();
  21.         }
  22.         System.out.printf("Total income: %.2f", sum);
  23.     }
  24. }
  25.  
Add Comment
Please, Sign In to add comment