Didart

SoftUni Bar Income

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