Advertisement
Lyubohd

SoftUni Bar One Big ReGex

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