Advertisement
AngelKejov

SoftUniBarIncome

Jul 30th, 2021
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Exercises;
  2.  
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class SoftUniBarIncome {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.         String input = sc.nextLine();
  12.  
  13.         String regex = "^%(?<name>[A-Z][a-z]*)%[^|$%.]*<(?<product>\\w+)>[^|$%.]*\\" +
  14.                 "|(?<count>[0-9]+)\\|[^|$%.]*?(?<price>[0-9]+\\.*[0-9]*)\\$$";
  15.         Pattern pattern = Pattern.compile(regex);
  16.         double sum = 0;
  17.         double total = 0;
  18.         while (!input.equals("end of shift")) {
  19.             Matcher matcher = pattern.matcher(input);
  20.             while (matcher.find()) {
  21.                 int quantity = Integer.parseInt(matcher.group(3));
  22.                 double price = Double.parseDouble(matcher.group(4));
  23.                 sum = quantity * price;
  24.                 System.out.printf("%s: %s - %.2f%n", matcher.group("name"),
  25.                         matcher.group("product"), sum);
  26.             }
  27.             total += sum;
  28.             input = sc.nextLine();
  29.         }
  30.  
  31.         System.out.printf("Total income: %.2f", total);
  32.     }
  33. }
  34.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement