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