Advertisement
petur_stoqnov

Java

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