Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package RegularExpressions.Exercise;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class O3SoftUniBarIncome {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Pattern pattern = Pattern.compile("%(?<customer>[A-Z][a-z]+)%[^$%|.]*<(?<product>\\w+)>[^$%|.]*\\|(?<quantity>\\d+)\\|[^$%|.]*?(?<price>\\d+\\.?\\d+)\\$");
- String input = scanner.nextLine();
- double sum = 0;
- while (!"end of shift".equals(input)) {
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()) {
- double totalPrice = Double.parseDouble(matcher.group("price")) * Integer.parseInt(matcher.group("quantity"));
- System.out.printf("%s: %s - %.2f%n", matcher.group("customer"), matcher.group("product"), totalPrice);
- sum += totalPrice;
- }
- input = scanner.nextLine();
- }
- System.out.printf("Total income: %.2f", sum);
- }
- }
Add Comment
Please, Sign In to add comment