Advertisement
IrinaIgnatova

RegEx - SoftUni Bar Income

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