desislava_topuzakova

03. SoftUni Bar Income

Jul 31st, 2021
982
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class SoftuniBarIncome_03 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String input = scanner.nextLine();
  9.         String regex = "%(?<customerName>[A-Z][a-z]*)%[^|$%.]*<(?<product>\\w+)>[^|$%.]*\\|(?<count>[0-9]+)\\|[^|$%.]*?(?<price>[0-9]+\\.*[0-9]*)\\$";
  10.         Pattern pattern = Pattern.compile(regex);
  11.         double income = 0; //общия приход от всички продукти
  12.         while(!input.equals("end of shift")) {
  13.             //%George%<Croissant>|2|10.3$
  14.             Matcher matcher = pattern.matcher(input);
  15.             if(matcher.find()) {
  16.                 String customerName = matcher.group("customerName");
  17.                 String product = matcher.group("product");
  18.                 int count = Integer.parseInt(matcher.group("count"));
  19.                 double price = Double.parseDouble(matcher.group("price"));
  20.                 double totalPrice = count * price; //приход за текущия продукт
  21.                 System.out.printf("%s: %s - %.2f%n", customerName, product, totalPrice);
  22.                 income += totalPrice;
  23.             }
  24.             input = scanner.nextLine();
  25.         }
  26.  
  27.         System.out.printf("Total income: %.2f", income);
  28.     }
  29. }
  30.  
RAW Paste Data