Advertisement
desislava_topuzakova

03. SoftUni Bar Income

Nov 20th, 2022
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package regex;
  2.  
  3. import java.math.MathContext;
  4. import java.util.Scanner;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class BarIncome_02 {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.         String input = scanner.nextLine();
  12.  
  13.         String regex = "%(?<customerName>[A-Z][a-z]*)%[^|$%.]*<(?<product>\\w+)>[^|$%.]*\\|(?<count>[0-9]+)\\|[^|$%.]*?(?<price>[0-9]+\\.?[0-9]*)\\$";
  14.         Pattern pattern = Pattern.compile(regex); //шаблон
  15.  
  16.         double incomes = 0; //общия приход от всички продукти
  17.  
  18.         while(!input.equals("end of shift")) {
  19.             //input = "%George%<Croissant>|2|10.3$"
  20.             Matcher matcher = pattern.matcher(input);
  21.             //matcher = "%(?<customerName>George)%<(?<product>Croissant)>|(?<count>2)|(?<price>10.3)$"
  22.             if(matcher.find()) {
  23.                 String customerName = matcher.group("customerName"); //"George"
  24.                 String product = matcher.group("product"); //"Croissant"
  25.                 int count = Integer.parseInt(matcher.group("count")); // "2" -> parseInt -> 2
  26.                 double price = Double.parseDouble(matcher.group("price")); // "10.3" -> parseDouble -> 10.3
  27.                 double totalPrice = count * price; //приход за единичния продукт
  28.                 incomes += totalPrice;
  29.                 System.out.printf("%s: %s - %.2f%n", customerName, product, totalPrice);
  30.             }
  31.             input = scanner.nextLine();
  32.         }
  33.  
  34.         System.out.printf("Total income: %.2f", incomes);
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement