Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 name = "%([A-Z][a-z]+)%([^|$%.0-9]+)?<(\\w+)>([^|$%.0-9]+)?\\|(?<quantity>[0-9]+)\\|" +
- "([^|$%.0-9]+)?(([0-9]+)\\.?([0-9]+))*[$]";
- Pattern pattern = Pattern.compile(name);
- double total = 0.00;
- String input = sc.nextLine();
- while (!"end of shift".equals(input)) {
- Matcher isValid = pattern.matcher(input);
- String person = "", product = "";
- double price = 0.00;
- int quantity = 0;
- if (isValid.find()) {
- person = isValid.group(1);
- product = isValid.group(3);
- quantity = Integer.parseInt(isValid.group(5));
- price = Double.parseDouble(isValid.group(7));
- total += price * quantity;
- double finalAmount = quantity * price;
- System.out.printf("%s: %s - %.2f%n", person, product, finalAmount);
- }
- input = sc.nextLine();
- }
- System.out.printf("Total income: %.2f", total);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement