emodev

sodtuni bar

Dec 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.List;
  2. import java.util.Scanner;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class SoftUniBarIncome {
  7.     public static void main(String[] args) {
  8.         Scanner scan = new Scanner(System.in);
  9.  
  10.         String line = scan.nextLine();
  11.         Double sum = 0d;
  12.  
  13. //customer, product, count and a price
  14.  
  15.         while (!line.equals("end of shift")) {
  16.             String regex = "%(?<customer>[A-Z][a-z]+)\\%[^|$%.]*\\<(?<product>\\w+)\\>[^|$%.]*\\|(?<count>\\d+)\\|[^|$%.]*?(?<price>\\d+([.]\\d+)?)\\$";
  17.             Pattern pattern = Pattern.compile(regex);
  18.             Matcher matcher = pattern.matcher(line);
  19.  
  20.             if (matcher.find()) {
  21.                 String customer = matcher.group("customer");
  22.                 String product = matcher.group("product");
  23.                 Double count = Double.parseDouble(matcher.group("count"));
  24.                 Double price = Double.parseDouble(matcher.group("price"));
  25.                 Double allPrice = count * price;
  26.                 sum += allPrice;
  27.  
  28.                 System.out.println(String.format("%s: %s - %.2f", customer, product, allPrice));
  29.  
  30.             }
  31.             line = scan.nextLine();
  32.         }
  33.         System.out.println(String.format("Total income: %.2f", sum));
  34.     }
  35. }
Add Comment
Please, Sign In to add comment