Advertisement
CR7CR7

tretaBar

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