emodev

SoftUni Bar Income

Dec 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package RegularExpressions.Exercises;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class SoftUniBarIncome_04 {
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner sc = new Scanner(System.in);
  12.  
  13.         String line = sc.nextLine();
  14.         double income = 0.0;
  15.        
  16.         Pattern validateLine = Pattern.compile
  17.         ("([^\\|\\$%.]+)?%" +
  18.         "(?<name>[A-Z][a-z]+)%([^\\|\\$%.]+)?<" +
  19.         "(?<product>\\w+)>([^\\|\\$%.]+)?\\|" +
  20.         "(?<quantity>\\d+)\\|([^0-9\\|\\$%.]+)?" +
  21.         "(?<price>\\d+\\.?\\d+)\\$([^\\|\\$%.]+)?");
  22.  
  23.         while (!line.equals("end of shift")) {
  24.             Matcher matcher = validateLine.matcher(line);
  25.  
  26.             if (matcher.find()) {
  27.                 String name = matcher.group("name");
  28.                 String product = matcher.group("product");
  29.                 int quantity = Integer.parseInt(matcher.group("quantity"));
  30.                 double price = Double.parseDouble(matcher.group("price"));
  31.  
  32.                 income += quantity * price;
  33.                 System.out.printf("%s: %s - %.2f%n",name, product, quantity * price);
  34.             }
  35.  
  36.             line = sc.nextLine();
  37.         }
  38.  
  39.         System.out.printf("Total income: %.2f", income);
  40.     }
  41. }
Add Comment
Please, Sign In to add comment