Advertisement
desislava_topuzakova

03. SoftuniBarIncome

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