Advertisement
Lyubohd

SoftUni Bar

Jul 24th, 2020
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 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.         String nameReGex = "%(?<name>[A-Z][a-z]+)%";
  12.         String productReGex = "<(?<product>\\w+)>";
  13.         String countRegex = "\\|(?<count>\\d+)\\|";
  14.         String priceRegex = "(?<price>\\d+\\.?\\d+)\\$";
  15.         Pattern pattern = Pattern.compile(nameReGex);
  16.         Pattern pattern1 = Pattern.compile(productReGex);
  17.         Pattern pattern2 = Pattern.compile(countRegex);
  18.         Pattern pattern3 = Pattern.compile(priceRegex);
  19.  
  20.         String input = scan.nextLine();
  21.         double income = 0.0;
  22.         while (!"end of shift".equals(input)) {
  23.             Matcher matcher = pattern.matcher(input);
  24.             Matcher matcher1 = pattern1.matcher(input);
  25.             Matcher matcher2 = pattern2.matcher(input);
  26.             Matcher matcher3 = pattern3.matcher(input);
  27.             if (matcher.find() && matcher1.find() && matcher2.find() && matcher3.find()) {
  28.                 String name = matcher.group("name");
  29.                 String product = matcher1.group("product");
  30.                 int count = Integer.parseInt(matcher2.group("count"));
  31.                 double price = Double.parseDouble(matcher3.group("price"));
  32.  
  33.                 double totalPrice = count * price;
  34.                 income += totalPrice;
  35.                 System.out.println(String.format("%s: %s - %.2f", name, product, totalPrice));
  36.             }
  37.  
  38.             input = scan.nextLine();
  39.         }
  40.         System.out.println(String.format("Total income: %.2f", income));
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement